SQL - 子查询多个值

时间:2017-09-25 09:39:46

标签: sql subquery

尝试使用子查询时出现以下错误。

  

子查询返回的值超过1。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

在完整的代码中,我将做更多的子查询并更改datediff(day, open_dt, trn.tran_dt) = '0' to 30, 60 and 90

如果有更简单的方法,我很高兴或不使用子查询。

SELECT 
    chn.acct_identifier,
    acq_channel, 
    COUNT(ExpRowId) [total transactions],
    (SELECT COUNT(trn.ExpRowId) 
     FROM hrg_prod.client.acquisition_table chn
     LEFT JOIN hrg_prod.prd.[transaction] trn ON CHN.acct_identifier = trn.applid 
                                              AND trn.trans_cd = 101 
                                              AND trn.auth_cd IS NOT NULL 
                                              AND trn.merch_num IS NOT NULL
     WHERE open_dt > '01Jul17'
       AND DATEDIFF(DAY, open_dt, trn.tran_dt) = '0' 
     GROUP BY 
         acct_identifier, Acq_Channel, chn.open_dt) [0 days],
    (select sum(trn.amt_tran) FROM hrg_prod.client.acquisition_table chn
left JOIN hrg_prod.prd.[transaction] trn
                 ON CHN.acct_identifier = trn.applid 
                   AND trn.trans_cd = 101 
                   AND trn.auth_cd IS NOT NULL 
                   AND trn.merch_num IS NOT NULL
where open_dt > '01Jul17'
and datediff(day, open_dt, trn.tran_dt) = '0' 
group by acct_identifier, Acq_Channel, chn.open_dt) [0 days tran],

chn.open_dt,
min(tran_dt) [first trans_dt],
datediff(day, open_dt, min(tran_dt)) [date difference]
FROM hrg_prod.client.acquisition_table chn
left JOIN hrg_prod.prd.[transaction] AS trn 
                ON CHN.acct_identifier = trn.applid 
                   AND trn.trans_cd = 101 
                   AND trn.auth_cd IS NOT NULL 
                   AND trn.merch_num IS NOT NULL
 where open_dt > '01Jul17'
 group by acct_identifier, Acq_Channel, chn.open_dt

如果我只使用一个acct_identifier,我会得到以下结果

SELECT chn.acct_identifier,
acq_channel,
count(ExpRowId) [total transactions],
chn.open_dt,
min(tran_dt) [first trans_dt],
FROM   hrg_prod.client.acquisition_table chn
   left JOIN hrg_prod.prd.[transaction] AS trn 
                ON CHN.acct_identifier = trn.applid 
                   AND trn.trans_cd = 101 
                   AND trn.auth_cd IS NOT NULL 
                   AND trn.merch_num IS NOT NULL
where chn.acct_identifier = '6675378'
group by acct_identifier, Acq_Channel, chn.open_dt

acct_identifier | acq_channes | transactions | open_dt    | first tran_dt 
6675378         | Online      | 16           | 2017-08-04 | 2017-08-04

我想要做的是有更多的交易栏,我把过滤器放在datediff(day,open_dt,trn.tran_dt)&lt; ='?'

1 个答案:

答案 0 :(得分:0)

我很确定你不需要子查询,只需要条件聚合。你的问题有点难以理解,但我认为这就是你想要的:

select chn.acct_identifier, acq_channel, 
       count(ExpRowId) as [total transactions],
       sum(case when datediff(day, open_dt, trn.tran_dt) = 0 and
                       trn.applid is not null
                  then 1 else 0
             end) as [0 days],
       sum(case when datediff(day, open_dt, trn.tran_dt) = 0 and
                       trn.applid is not null
                  then trn.amt_tran else 0
             end) as [0 days],
            chn.open_dt,
            min(tran_dt) [first trans_dt],
            datediff(day, open_dt, min(tran_dt)) as [date difference]
from hrg_prod.client.acquisition_table chn left join
     hrg_prod.prd.[transaction] AS trn 
     on CHN.acct_identifier = trn.applid and
        trn.trans_cd = 101 and
        trn.auth_cd IS NOT NULL and
        trn.merch_num IS NOT NULL
where open_dt > '01Jul17'
group by acct_identifier, Acq_Channel, chn.open_dt;