1

我真的被一个 sql select 语句所困扰。

这是我从下面的 sql 语句中得到的输出/结果:

在此处输入图像描述

我需要什么:我需要通过 msisdn 将列assignedVouchersNumber 和 usedVouchersNumber 放在一行中。因此,例如,如果您可以看到“msisdn”723709656,那么现在有两行.. 一个是assignedVouchersNumber = 1,第二个是assignedVouchersNumber = 1。

但是我需要将它放在一行中,assignedVouchersNumber = 2。你现在哪里出了问题?

SELECT eu.msisdn,
   eu.id as userId,

   sum(case ev.voucherstate when '1' then 1 else 0 end) as assignedVouchersNumber,
   sum(case ev.voucherstate when '2' then 1 else 0 end) as usedVouchersNumber,

   ev.extra_offer_id,
   ev.create_time,
   ev.use_time,
   ev.id as voucherId,
   ev.voucherstate
FROM   extra_users eu
   JOIN (SELECT sn.msisdn AS telcislo,
                stn.numberid
         FROM   stats_number sn
                JOIN stats_target_number AS stn
                  ON ( sn.numberid = stn.numberid )
         WHERE  stn.targetid = 1) xy
     ON eu.msisdn = xy.telcislo
   JOIN extra_vouchers AS ev
     ON ( eu.id = ev.extra_user_id )
WHERE  ev.create_time BETWEEN '2012-07-23 00:00:00' AND '2013-08-23 23:59:59'
   AND ev.use_time <= '2013-08-23 23:59:59'
   AND ev.use_time >= '2012-07-23 00:00:00'
   AND ev.voucherstate IN ( 1, 2 )
   AND Ifnull(ev.extra_offer_id IN( 2335, 3195, 30538 ), 1)
   GROUP BY eu.msisdn, ev.extra_offer_id, ev.voucherState
ORDER  BY eu.msisdn ASC  
4

2 回答 2

1

对于相同的 msisdn 和 VouchersNumber,您有两个不同的 extra_offer_id。这就是为什么你得到两行。

于 2013-08-28T09:25:40.733 回答
1

我明白了...不应该按 ev.voucherState 分组

GROUP BY eu.msisdn, ev.extra_offer_id, ev.voucherState

之后我删除了 ev.voucherState 它现在正在工作。

于 2013-08-28T10:12:30.347 回答