简体   繁体   中英

how to use sum function when there are multiple rows

MSISDN      Name    y/N     top     cycle   ActivationDate  Bottom  Provisioned  renewdate
78          W1      N       NA       24         3-Apr-15    th4         512     10-Apr-15
78          W1      N       NA       24         3-Apr-15    th5         256     17-Apr-15
78          W1      N       NA       24         3-Apr-15    th2         512     5-Apr-15
79          w2      Y       33       29         4-Apr-15    th5         1024    8-Apr-15
79          w2      Y       33       29         4-Apr-15    th4         2048    8-Apr-15

Expected output

MSISDN      Name    y/N     top     cycle   ActivationDate  Bottom  Provisioned  renewdate
78          W1      N       NA       24         3-Apr-15    null 1280-2048      null
79          w2      Y       33       29         4-Apr-15    th5         1024            8-Apr-15
79          w2      Y       33       29         4-Apr-15    th4         2048            8-Apr-15

when i write a sql i am able to query the sql for NA case grouping based on the name. can we write a sql to meet the both conditions.when "NA" it has to sum up all the data provisioned

You can use a UNION to do this:

SELECT MSISDN, Name, y/N, top, cycle, activationDate, NULL, Sum(PRovisioned), Null
FROM <table> 
WHERE top = 'NA'
GROUP BY  MSISDN, Name, y/N, top, cycle, activationDate

UNION ALL

SELECT MSISDN,Name,y/N,top,cycle,ActivationDate,Bottom,Provisioned,renewdate
FROM <table>
WHERE top <> 'NA'

That should get you close. I have no idea how you are getting 1280-2048 for your 'Provisioned' in the GROUP BY query though. You'll have to explain the logic there for additional help should you need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM