【问题标题】:Grouping query and subquery items in SQL在 SQL 中对查询和子查询项进行分组
【发布时间】:2015-11-09 20:18:14
【问题描述】:

我正在尝试对查询进行分组,以便每个帐号只显示一次结果。下面是我正在使用的sql。

`select top 10 c.svc, sum(c.tx_amt) as "Total Payments", d.fyear_name, d.fmonth_name, c.acctnbr_int,
(select iif(datediff("d",c.svc_dte,c.pst_dte)<=6 and c.trans_cd in ('0092001','0092051','0093201','0093204','0093212','0093601','0093652','0093653','0093654','0093655','0093701','0096316','0096351','0096366',
'0096405','0096407','0096408','0096409','0096422','0096452','0096455','0096456','0096459','0096473','0096474','0091551'),sum(c.tx_amt),0)) as "POS Payments"
from rptg.vwall_chg_tx_plus c
join rptg.vwdim_date d on c.pst_dte = d.day_date
where c.Pst_Dte > '09/30/2015' and c.Pst_Dte < '11/1/2015'
and c.Svc = '002'
and c.pt_acct_ctg_nme <> 'inpatient'
and c.trans_cd in ('0093101','0093102','0093151','0093152','0093251','0096302','0096303','0096304','0096305','0096306','0096307','0096308','0096309','0096311','0096312',
'0096313','0096314','0096322','0096343','0096352','0096353','0096354','0096355','0096356','0096357','0096358','0096359','0096361','0096362','0096363','0096364','0096367',
'0096372','0096375','0096380','0096381','0096382','0096383','0096392','0096398','0096399','0096426','0096454','0096458','0096477','0096478','0098353','0098354','0098400',
'0098401','0092001','0092051','0093201','0093204','0093212','0093601','0093652','0093653','0093654','0093655','0093701','0096316','0096351','0096366','0096405','0096407',
'0096408','0096409','0096422','0096452','0096455','0096456','0096459','0096473','0096474','0091551','0093254','0093272','0093751','0096395','0096406','0096410','0096412',
'0096413','0096451','0096457','0096472','0099201')
and c.AcctNbr_int in ('3215385349')`

结果是: svc 总付款 fyear_name fmonth_name acctnbr_int POS 付款 002 -150.00 FY 2016 FY16 M4 3215385349 -150.00 002 -100.00 FY 2016 FY16 M4 3215385349 -100.00

但结果应该是: Svc POS Pmts Total Pmts FYear_name fmonth_name AcctNbr_int Insrnc_Pyr_Cde 002 ($250.00) ($250.00) FY 2016 FY16 M4 3215385349 自费

当我尝试对项目和 sum(c.tx_amt) 进行分组时,我收到有关 group by 子句中不允许的聚合或子查询的错误消息。

谢谢

【问题讨论】:

    标签: sql group-by subquery


    【解决方案1】:

    我能够弄清楚。向子查询添加 sum 为我提供了所需的结果。

    (select sum(iif(datediff("d",c.svc_dte,c.pst_dte)

    【讨论】:

      猜你喜欢
      • 2010-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多