报表导出sql

这篇博客记录了一段使用毕生所学编写的SQL查询,主要用于报表导出。通过递归技术在Postgres数据库中实现了复杂的查询操作,具有较高的学习价值。
摘要由CSDN通过智能技术生成

用了毕生所学,写出来的sql,记录一下,留作纪念

SELECT
	(
				select string_agg(unit_name, '-->') as unit_chain from ( with recursive cte (id,unit_name) as (
        select *, 1 as depath from t_unit where id = (select unit_id from t_role where id = t3.role_id) union all
        select b.*,depath+1 from t_unit b inner join cte c on b.id=c.parent_id and b.deleted = 0 and c.deleted = 0)
        select id,unit_name from cte WHERE cte.deleted = 0 ORDER BY depath desc) as t2 
	
	) as 组织机构,
	t1.id as 房源id,
  t6.real_name as 收房客户经理,
	t2.building_name as 小区名称,
	t1.building_no || '-' || t1.unit_no || '-' || room_no as 门牌号,
	t3.rent_per_month as 收房月租金,
	to_char(to_timestamp(t8.final_rent_day/1000),'yyyy-MM-dd') as 起租日,
	to_char(to_timestamp(t4.start_date/1000),'yyyy-MM-dd') as 首次租赁开始时间,
	t4.price as 首次出房租金,
	CASE t4.pay_method
		WHEN 1 THEN '月付'
		WHEN 2 THEN '季付'
		WHEN 3 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值