国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

實現(xiàn)按部門月卡余額總額分組統(tǒng)計的SQL查詢代碼

2024-07-21 02:10:38
字體:
供稿:網(wǎng)友

陳優(yōu)章的專欄

(原創(chuàng),到現(xiàn)在為至最為復雜的sql查詢代碼)實現(xiàn)按部門月卡余額總額分組統(tǒng)計的sql查詢代碼(在ms sql server中調(diào)試通過)

select dp.dpname1 as 部門, cust_dp_sumoddfre.sum_oddfare as 當月卡總余額
from (select t_department.dpcode1, sum(custid_sumoddfare_group.sum_oddfare)
              as sum_oddfare
        from (select l2.customerid, sum(r1.oddfare) as sum_oddfare
                from (select customerid, max(opcount) as max_opcount
                        from (select customerid, opcount, rtrim(cast(year(opdt)
                                      as char)) + '-' + rtrim(cast(month(opdt) as char))
                                      + '-' + rtrim(day(0)) as dt
                                from t_consumerec
                                union
                                select customerid, opcount, rtrim(cast(year(cashdt)
                                      as char)) + '-' + rtrim(cast(month(cashdt) as char))
                                      + '-' + rtrim(day(0)) as dt
                                from t_cashrec) l1
                        where (dt <= '2005-6-1')/*輸入查詢月份,可用參數(shù)傳遞*/
                        group by customerid) l2 inner join
                          (select customerid, opcount, oddfare
                         from t_consumerec
                         union
                         select customerid, opcount, oddfare
                         from t_cashrec) r1 on l2.customerid = r1.customerid and
                      r1.opcount = l2.max_opcount
                group by l2.customerid) custid_sumoddfare_group inner join
              t_customers on
              custid_sumoddfare_group.customerid = t_customers.customerid inner join
              t_department on substring(t_customers.account, 1, 2)
              = t_department.dpcode1 and substring(t_customers.account, 3, 2)
              = t_department.dpcode2 and substring(t_customers.account, 5, 3)
              = t_department.dpcode3
        group by dpcode1) cust_dp_sumoddfre inner join
          (select distinct dpcode1, dpname1
         from t_department) dp on dp.dpcode1 = cust_dp_sumoddfre.dpcode1

附:查詢用到的基本表形成腳本:

create table [dbo].[t_cashrec] ( --出納明細賬本
 [statid] [tinyint] not null ,
 [cashid] [smallint] not null ,
 [port] [tinyint] not null ,
 [term] [tinyint] not null ,
 [cashdt] [datetime] not null ,--存取款時間
 [collectdt] [datetime] not null ,
 [customerid] [int] not null ,
 [opcount] [int] not null ,--某卡的操作次數(shù),只累加
 [infare] [money] not null ,
 [outfare] [money] not null ,
 [sumfare] [money] not null ,
 [oddfare] [money] not null ,--此次操作后該卡的余額
 [mngfare] [money] not null ,
 [hz] [tinyint] not null ,
 [cursum] [smallmoney] null ,
 [curcount] [smallint] null ,
 [cardsn] [tinyint] null
) on [primary]
go

create table [dbo].[t_consumerec] ( --消費明細賬本
 [statid] [tinyint] not null ,
 [port] [tinyint] not null ,
 [term] [tinyint] not null ,
 [customerid] [int] not null ,
 [opcount] [int] not null , --某卡的操作次數(shù),只累加
 [opdt] [datetime] not null ,--消費時間
 [collectdt] [datetime] not null ,
 [mealid] [tinyint] not null ,
 [sumfare] [smallmoney] not null ,
 [oddfare] [smallmoney] not null ,--此次操作后該卡的余額
 [mngfare] [smallmoney] not null ,
 [opfare] [smallmoney] not null ,
 [hz] [tinyint] not null ,
 [menuid] [smallint] null ,
 [menunum] [tinyint] null ,
 [oddfarepre] [smallmoney] null ,
 [recno] [smallint] null ,
 [cardsn] [tinyint] not null ,
 [cardver] [tinyint] null
) on [primary]
go

create table [dbo].[t_customers] ( --客戶賬本
 [customerid] [int] not null , --客戶號,主鍵
 [statcode] [varchar] (3) collate chinese_prc_ci_as not null ,
 [account] [varchar] (7) collate chinese_prc_ci_as not null ,--單位代號
 [name] [varchar] (12) collate chinese_prc_ci_as not null ,
 [cardno] [int] not null ,
 [cardsn] [tinyint] null ,
 [cardtype] [tinyint] not null ,
 [status] [tinyint] not null ,
 [opendt] [datetime] not null ,
 [cashid] [smallint] not null ,
 [sumfare] [smallmoney] not null ,
 [consumefare] [smallmoney] not null ,
 [oddfare] [smallmoney] not null ,
 [opcount] [int] not null ,
 [cursubsidyfare] [smallmoney] not null ,
 [subsidydt] [datetime] not null ,
 [subsidyout] [char] (1) collate chinese_prc_ci_as not null ,
 [alias] [varchar] (10) collate chinese_prc_ci_as null ,
 [outid] [varchar] (20) collate chinese_prc_ci_as null ,
 [updateid] [tinyint] not null ,
 [pwd] [char] (4) collate chinese_prc_ci_as null ,
 [quchargfare] [smallmoney] null ,
 [hastaken] [tinyint] null ,
 [dragoncardno] [char] (19) collate chinese_prc_ci_as null ,
 [applycharg] [smallmoney] null ,
 [chargper] [smallmoney] null ,
 [mingzu] [varchar] (20) collate chinese_prc_ci_as null ,
 [sex] [char] (2) collate chinese_prc_ci_as null ,
 [memo] [varchar] (100) collate chinese_prc_ci_as null ,
 [weipeidw] [varchar] (10) collate chinese_prc_ci_as null ,
 [cardconsumetype] [tinyint] null ,
 [leaveschooldt] [datetime] null ,
 [usevaliddt] [tinyint] not null ,
 [nousedate] [datetime] not null
) on [primary]
go

create table [dbo].[t_department] ( --單位帳本,三級單位制,樹型結(jié)構(gòu)
 [dpcode1] [char] (2) collate chinese_prc_ci_as not null ,
 [dpcode2] [char] (2) collate chinese_prc_ci_as null ,
 [dpcode3] [char] (3) collate chinese_prc_ci_as null ,
 [dpname1] [varchar] (30) collate chinese_prc_ci_as null ,
 [dpname2] [varchar] (30) collate chinese_prc_ci_as null ,
 [dpname3] [varchar] (30) collate chinese_prc_ci_as null ,
 [n_sr] [int] not null ,
 [batnum] [smallint] null
) on [primary]
go

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 固镇县| 灌阳县| 师宗县| 灵寿县| 措勤县| 洱源县| 枝江市| 罗城| 济源市| 高台县| 定远县| 安泽县| 宜兴市| 邓州市| 安龙县| 阳信县| 分宜县| 交口县| 高州市| 通榆县| 奉化市| 若羌县| 濮阳市| 屯留县| 兖州市| 从江县| 肇州县| 广汉市| 白银市| 五莲县| 龙游县| 博湖县| 华阴市| 神木县| 获嘉县| 威信县| 隆尧县| 塔河县| 洮南市| 疏附县| 波密县|