我需要将“总计”列加上“帐户”和“月”,我希望每个帐户有一行 - 月份组合
SELECT
'NPI' as Company, SYACTFIL_SQL.acc`enter code here`t_desc as Description ,
left(GLTRXFIL_SQL.mn_no,5) + '-' + left(GLTRXFIL_SQL.sb_no,4) as Account,
right(left(GLTRXFIL_SQL.trx_dt,6),2) as Month, SUM(GLTRXFIL_SQL.trx_amt) as Total
FROM Data.dbo.GLTRXFIL_SQL GLTRXFIL_SQL,Data.dbo.SYACTFIL_SQL SYACTFIL_SQL
WHERE GLTRXFIL_SQL.mn_no = SYACTFIL_SQL.mn_no AND
GLTRXFIL_SQL.sb_no`enter code here` = SYACTFIL_SQL.sb_no AND
(GLTRXFIL_SQL.trx_dt>=20130101) AND (GLTRXFIL_SQL.trx_dt<=20130630) AND
SYACTFIL_SQL.fin_stmt_tp = 'P'
Group by SYACTFIL_SQL.acct_desc, GLTRXFIL_SQL.trx_dt, GLTRXFIL_SQL.mn_no,GLTRXFIL_SQL.sb_no
Order by GLTRXFIL_SQL.mn_no,GLTRXFIL_SQL.trx_dt;
答案 0 :(得分:0)
假设每个帐户有1个帐户描述,并且没有看到表格结构更好地了解,这应该有效:
SELECT 'NPI' as Company
, b.acct_desc as Description
, left(a.mn_no,5) + '-' + left(a.sb_no,4) as Account
, right(left(a.trx_dt,6),2) as MONTH
, SUM(a.trx_amt) as Total
FROM Data.dbo.GLTRXFIL_SQL GLTRXFIL_SQL a
JOIN Data.dbo.SYACTFIL_SQL SYACTFIL_SQL b
ON a.mn_no = b.mn_no
AND a.sb_no = b.sb_no
WHERE a.trx_dt>=20130101
AND a.trx_dt<=20130630
AND b.fin_stmt_tp = 'P'
GROUP BY b.acct_desc
, left(a.mn_no,5) + '-' + left(a.sb_no,4)
, right(left(a.trx_dt,6),2)
ORDER BY left(a.mn_no,5) + '-' + left(a.sb_no,4)
, right(left(a.trx_dt,6),2)