| Author |
Topic |
|
osupratt
Posting Yak Master
238 Posts |
Posted - 2008-04-16 : 13:53:38
|
| I have the following query. I have two problems with this. For each Region I should only have a CustomerNumber listed once with their SOExtChargeAmount summed. I have some showing up more than once.The second problem is that after my UNION ALL I need to sum 'AllOthers' as one row of data. I can't figure this out. Any help would be absolutely awesome. Thanks.SELECT Region, Location, WarehouseCode, CustomerNumber, CustomerName, MonthLessEleven, MonthLessTen, MonthLessNine, MonthLessEight, MonthLessSeven,MonthLessSix, MonthLessFive, MonthLessFour, MonthLessThree, MonthLessTwo, MonthLessOne, CurrentMonth, CurrentYearTotal, LastYearYTD,LastYearTotal, CustRankFROM(SELECT case when substring(gla.Account,5,3) = '936' then 'North Region' when substring(gla.Account,5,3) = '908' then 'East Region' ELSE 'Unknown'END AS Region,case when substring(gla.Account,5,3)='900' then 'ALE' when substring(gla.Account,5,3)='902' then 'ATO' when substring(gla.Account,5,3)='904' then 'BOW' when substring(gla.Account,5,3)='906' then 'BRY' when substring(gla.Account,5,3)='908' then 'BPT' when substring(gla.Account,5,3)='910' then 'BYD' when substring(gla.Account,5,3)='912' then 'BUF' when substring(gla.Account,5,3)='914' then 'CLE' when substring(gla.Account,5,3)='916' then 'GRN' when substring(gla.Account,5,3)='920' then 'DXN' when substring(gla.Account,5,3)='924' then 'CTH' when substring(gla.Account,5,3)='926' then 'ELC' when substring(gla.Account,5,3)='928' then 'FTL' when substring(gla.Account,5,3)='930' then 'FTW' when substring(gla.Account,5,3)='932' then 'I35' when substring(gla.Account,5,3) IN ('936','000') then 'GAI' when substring(gla.Account,5,3)='939' then 'STW' when substring(gla.Account,5,3)='940' then 'GRE' when substring(gla.Account,5,3)='942' then 'HEN' when substring(gla.Account,5,3)='944' then 'FTS' when substring(gla.Account,5,3)='948' then 'JAC' when substring(gla.Account,5,3)='952' then 'JEN' when substring(gla.Account,5,3)='956' then 'KIL' when substring(gla.Account,5,3)='957' then 'MCA' when substring(gla.Account,5,3)='958' then 'MIN' when substring(gla.Account,5,3)='960' then 'NOC' when substring(gla.Account,5,3)='962' then 'ODE' when substring(gla.Account,5,3)='964' then 'BTP' when substring(gla.Account,5,3)='966' then 'RA' when substring(gla.Account,5,3)='968' then 'RIF' when substring(gla.Account,5,3)='970' then 'SWD' when substring(gla.Account,5,3)='971' then '3PS' when substring(gla.Account,5,3)='972' then 'ROC' when substring(gla.Account,5,3)='976' then 'SJO' when substring(gla.Account,5,3)='978' then 'SMB' when substring(gla.Account,5,3)='980' then 'STO' when substring(gla.Account,5,3)='982' then 'TOL' when substring(gla.Account,5,3)='984' then 'VEL' when substring(gla.Account,5,3)='985' then 'CFP' when substring(gla.Account,5,3)='986' then 'CLM' when substring(gla.Account,5,3)='988' then 'WHI' when substring(gla.Account,5,3)='992' then 'WRA' when substring(gla.Account,5,3)='995' then 'ADM' ELSE 'Unknown'END AS Location,ihh.WarehouseCode,ihh.CustomerNumber, cm.CustomerName,SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 11 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessEleven,SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 10 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessTen, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 9 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessNine, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 8 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessEight, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 7 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessSeven, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 6 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessSix, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 5 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessFive, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 4 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessFour, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 3 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessThree, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 2 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessTwo, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 1 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessOne, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 0 THEN ihd.SOExtChargeAmount ELSE 0 END) AS CurrentMonth, SUM(CASE WHEN DATEDIFF([YEAR], ihh.SOTransDate, getdate()) = 0 THEN ihd.SOExtChargeAmount ELSE 0 END) AS CurrentYearTotal,SUM(CASE WHEN DATEADD([year], - 1, getdate()) > ihh.SOTransDate AND DATEDIFF([YEAR], ihh.SOTransDate, getdate()) = 1 THEN ihd.SOExtChargeAmount ELSE 0 END) AS LastYearYTD,SUM(CASE WHEN DATEDIFF([YEAR], ihh.SOTransDate, getdate()) = 1 THEN ihd.SOExtChargeAmount ELSE 0 END) LastYearTotal,ROW_NUMBER() OVER(PARTITION BY case when substring(gla.Account,5,3) = '936' then 'North Region' when substring(gla.Account,5,3) = '908' then 'East Region' ELSE ' 'ENDORDER BY case when substring(gla.Account,5,3) = '936' then 'North Region' when substring(gla.Account,5,3) = '908' then 'East Region' ELSE ' 'END, SUM(CASE WHEN DATEDIFF([YEAR], ihh.SOTransDate, getdate()) = 0 THEN ihd.SOExtChargeAmount ELSE 0 END) desc) AS CustRankFROM MAS_BIF_AR1_CustomerMaster AS cm INNER JOIN MAS_BIF_ARN_InvHistoryHeader AS ihh ON cm.CustomerNumber = ihh.CustomerNumber INNER JOIN MAS_BIF_ARO_InvHistoryDetail AS ihd ON ihh.InvoiceNumber = ihd.InvoiceNumber INNER JOIN MAS_BIF_GL_Account AS gla ON ihd.SOGLSalesAcct = gla.AccountKeyGROUP BY ihh.CustomerNumber, cm.CustomerName, gla.Account, ihh.WarehouseCode) XWHERE CustRank < 20union allSELECT Region, Location, WarehouseCode, 'AllOthers', CustomerName, MonthLessEleven, MonthLessTen, MonthLessNine, MonthLessEight, MonthLessSeven,MonthLessSix, MonthLessFive, MonthLessFour, MonthLessThree, MonthLessTwo, MonthLessOne, CurrentMonth, CurrentYearTotal, LastYearYTD,LastYearTotal, 20FROM(SELECT case when substring(gla.Account,5,3) = '936' then 'North Region' when substring(gla.Account,5,3) = '908' then 'East Region' ELSE 'Unknown'END AS Region,case when substring(gla.Account,5,3)='900' then 'ALE' when substring(gla.Account,5,3)='902' then 'ATO' when substring(gla.Account,5,3)='904' then 'BOW' when substring(gla.Account,5,3)='906' then 'BRY' when substring(gla.Account,5,3)='908' then 'BPT' when substring(gla.Account,5,3)='910' then 'BYD' when substring(gla.Account,5,3)='912' then 'BUF' when substring(gla.Account,5,3)='914' then 'CLE' when substring(gla.Account,5,3)='916' then 'GRN' when substring(gla.Account,5,3)='920' then 'DXN' when substring(gla.Account,5,3)='924' then 'CTH' when substring(gla.Account,5,3)='926' then 'ELC' when substring(gla.Account,5,3)='928' then 'FTL' when substring(gla.Account,5,3)='930' then 'FTW' when substring(gla.Account,5,3)='932' then 'I35' when substring(gla.Account,5,3) IN ('936','000') then 'GAI' when substring(gla.Account,5,3)='939' then 'STW' when substring(gla.Account,5,3)='940' then 'GRE' when substring(gla.Account,5,3)='942' then 'HEN' when substring(gla.Account,5,3)='944' then 'FTS' when substring(gla.Account,5,3)='948' then 'JAC' when substring(gla.Account,5,3)='952' then 'JEN' when substring(gla.Account,5,3)='956' then 'KIL' when substring(gla.Account,5,3)='957' then 'MCA' when substring(gla.Account,5,3)='958' then 'MIN' when substring(gla.Account,5,3)='960' then 'NOC' when substring(gla.Account,5,3)='962' then 'ODE' when substring(gla.Account,5,3)='964' then 'BTP' when substring(gla.Account,5,3)='966' then 'RA' when substring(gla.Account,5,3)='968' then 'RIF' when substring(gla.Account,5,3)='970' then 'SWD' when substring(gla.Account,5,3)='971' then '3PS' when substring(gla.Account,5,3)='972' then 'ROC' when substring(gla.Account,5,3)='976' then 'SJO' when substring(gla.Account,5,3)='978' then 'SMB' when substring(gla.Account,5,3)='980' then 'STO' when substring(gla.Account,5,3)='982' then 'TOL' when substring(gla.Account,5,3)='984' then 'VEL' when substring(gla.Account,5,3)='985' then 'CFP' when substring(gla.Account,5,3)='986' then 'CLM' when substring(gla.Account,5,3)='988' then 'WHI' when substring(gla.Account,5,3)='992' then 'WRA' when substring(gla.Account,5,3)='995' then 'ADM' ELSE 'Unknown'END AS Location,ihh.WarehouseCode,ihh.CustomerNumber, cm.CustomerName, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 11 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessEleven,SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 10 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessTen, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 9 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessNine, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 8 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessEight, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 7 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessSeven, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 6 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessSix, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 5 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessFive, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 4 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessFour, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 3 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessThree, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 2 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessTwo, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 1 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessOne, SUM(CASE WHEN DATEDIFF([MONTH], ihh.SOTransDate, getdate()) = 0 THEN ihd.SOExtChargeAmount ELSE 0 END) AS CurrentMonth, SUM(CASE WHEN DATEDIFF([YEAR], ihh.SOTransDate, getdate()) = 0 THEN ihd.SOExtChargeAmount ELSE 0 END) AS CurrentYearTotal,SUM(CASE WHEN DATEADD([year], - 1, getdate()) > ihh.SOTransDate AND DATEDIFF([YEAR], ihh.SOTransDate, getdate()) = 1 THEN ihd.SOExtChargeAmount ELSE 0 END) AS LastYearYTD,SUM(CASE WHEN DATEDIFF([YEAR], ihh.SOTransDate, getdate()) = 1 THEN ihd.SOExtChargeAmount ELSE 0 END) LastYearTotal,ROW_NUMBER() OVER(PARTITION BY case when substring(gla.Account,5,3) = '936' then 'North Region' when substring(gla.Account,5,3) = '908' then 'East Region' ELSE ' 'ENDORDER BY case when substring(gla.Account,5,3) = '936' then 'North Region' when substring(gla.Account,5,3) = '908' then 'East Region' ELSE ' 'END, SUM(CASE WHEN DATEDIFF([YEAR], ihh.SOTransDate, getdate()) = 0 THEN ihd.SOExtChargeAmount ELSE 0 END) desc) AS CustRankFROM MAS_BIF_AR1_CustomerMaster AS cm INNER JOIN MAS_BIF_ARN_InvHistoryHeader AS ihh ON cm.CustomerNumber = ihh.CustomerNumber INNER JOIN MAS_BIF_ARO_InvHistoryDetail AS ihd ON ihh.InvoiceNumber = ihd.InvoiceNumber INNER JOIN MAS_BIF_GL_Account AS gla ON ihd.SOGLSalesAcct = gla.AccountKeyGROUP BY ihh.CustomerNumber, cm.CustomerName, gla.Account, ihh.WarehouseCode) XWHERE CustRank > 19 |
|
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2008-04-16 : 14:02:19
|
| is this in SQL 2005? if so you have to look into CTE, nice feature to break things up modularly. can you please in PDL (program design language= plain Enlish) state what you are looking for? will be glad to help. It is a huge query and it is very hard to see what you are trying to do. |
 |
|
|
osupratt
Posting Yak Master
238 Posts |
Posted - 2008-04-16 : 18:29:46
|
| This is a Cutomer Sales report. It is grouped or broken out by Region. The report shows top 19 Customers (with most Sum Amount) and shows 'AllOther' customers per Region.My report shows some Customers more than once when it should not (only once per region).Also I want to sum 'AllOthers' to show as one row (the 20th Customer of a Region).I hope this makes sense. |
 |
|
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2008-04-16 : 18:46:53
|
| ok that is better...are you trying to do this in SQL 2005? |
 |
|
|
osupratt
Posting Yak Master
238 Posts |
Posted - 2008-04-16 : 18:56:51
|
| yes |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-17 : 02:43:08
|
[code]SELECT Region, Location, WarehouseCode, CASE WHEN CustRank <= 19 THEN CustomerNumber ELSE 'AllOthers' END AS CustomerNumber CustomerName, MonthLessEleven, MonthLessTen, MonthLessNine, MonthLessEight, MonthLessSeven, MonthLessSix, MonthLessFive, MonthLessFour, MonthLessThree, MonthLessTwo, MonthLessOne, CurrentMonth, CurrentYearTotal, LastYearYTD, LastYearTotal, CASE WHEN CustRank <= 19 THEN CustRank ELSE 20 END AS CustRankFROM ( SELECT gla.Region, gla.Location, ihh.WarehouseCode, ihh.CustomerNumber, cm.CustomerName, SUM(CASE WHEN DATEDIFF(MONTH, ihh.SOTransDate, GETDATE()) = 11 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessEleven, SUM(CASE WHEN DATEDIFF(MONTH, ihh.SOTransDate, GETDATE()) = 10 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessTen, SUM(CASE WHEN DATEDIFF(MONTH, ihh.SOTransDate, GETDATE()) = 9 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessNine, SUM(CASE WHEN DATEDIFF(MONTH, ihh.SOTransDate, GETDATE()) = 8 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessEight, SUM(CASE WHEN DATEDIFF(MONTH, ihh.SOTransDate, GETDATE()) = 7 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessSeven, SUM(CASE WHEN DATEDIFF(MONTH, ihh.SOTransDate, GETDATE()) = 6 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessSix, SUM(CASE WHEN DATEDIFF(MONTH, ihh.SOTransDate, GETDATE()) = 5 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessFive, SUM(CASE WHEN DATEDIFF(MONTH, ihh.SOTransDate, GETDATE()) = 4 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessFour, SUM(CASE WHEN DATEDIFF(MONTH, ihh.SOTransDate, GETDATE()) = 3 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessThree, SUM(CASE WHEN DATEDIFF(MONTH, ihh.SOTransDate, GETDATE()) = 2 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessTwo, SUM(CASE WHEN DATEDIFF(MONTH, ihh.SOTransDate, GETDATE()) = 1 THEN ihd.SOExtChargeAmount ELSE 0 END) AS MonthLessOne, SUM(CASE WHEN DATEDIFF(MONTH, ihh.SOTransDate, GETDATE()) = 0 THEN ihd.SOExtChargeAmount ELSE 0 END) AS CurrentMonth, SUM(CASE WHEN DATEDIFF(YEAR, ihh.SOTransDate, GETDATE()) = 0 THEN ihd.SOExtChargeAmount ELSE 0 END) AS CurrentYearTotal, SUM(CASE WHEN DATEADD(YEAR, - 1, GETDATE()) > ihh.SOTransDate AND DATEDIFF(YEAR, ihh.SOTransDate, GETDATE()) = 1 THEN ihd.SOExtChargeAmount ELSE 0 END) AS LastYearYTD, SUM(CASE WHEN DATEDIFF(YEAR, ihh.SOTransDate, GETDATE()) = 1 THEN ihd.SOExtChargeAmount ELSE 0 END) LastYearTotal, ROW_NUMBER() OVER (PARTITION BY gla.Region ORDER BY gla.Region, SUM(CASE WHEN DATEDIFF(YEAR, ihh.SOTransDate, GETDATE()) = 0 THEN ihd.SOExtChargeAmount ELSE 0 END) DESC) AS CustRank FROM MAS_BIF_AR1_CustomerMaster AS cm INNER JOIN MAS_BIF_ARN_InvHistoryHeader AS ihh ON ihh.CustomerNumber = cm.CustomerNumber INNER JOIN MAS_BIF_ARO_InvHistoryDetail AS ihd ON ihd.InvoiceNumber = ihh.InvoiceNumber INNER JOIN ( SELECT AccountKey, Account, CASE SUBSTRING(Account, 5, 3) WHEN '936' THEN 'North Region' WHEN '908' THEN 'East Region' ELSE 'Unknown' END AS Region, CASE SUBSTRING(Account, 5, 3) WHEN '900' THEN 'ALE' WHEN '902' THEN 'ATO' WHEN '904' THEN 'BOW' WHEN '906' THEN 'BRY' WHEN '908' THEN 'BPT' WHEN '910' THEN 'BYD' WHEN '912' THEN 'BUF' WHEN '914' THEN 'CLE' WHEN '916' THEN 'GRN' WHEN '920' THEN 'DXN' WHEN '924' THEN 'CTH' WHEN '926' THEN 'ELC' WHEN '928' THEN 'FTL' WHEN '930' THEN 'FTW' WHEN '932' THEN 'I35' WHEN '936' THEN 'GAI' WHEN '000' THEN 'GAI' WHEN '939' THEN 'STW' WHEN '940' THEN 'GRE' WHEN '942' THEN 'HEN' WHEN '944' THEN 'FTS' WHEN '948' THEN 'JAC' WHEN '952' THEN 'JEN' WHEN '956' THEN 'KIL' WHEN '957' THEN 'MCA' WHEN '958' THEN 'MIN' WHEN '960' THEN 'NOC' WHEN '962' THEN 'ODE' WHEN '964' THEN 'BTP' WHEN '966' THEN 'RA' WHEN '968' THEN 'RIF' WHEN '970' THEN 'SWD' WHEN '971' THEN '3PS' WHEN '972' THEN 'ROC' WHEN '976' THEN 'SJO' WHEN '978' THEN 'SMB' WHEN '980' THEN 'STO' WHEN '982' THEN 'TOL' WHEN '984' THEN 'VEL' WHEN '985' THEN 'CFP' WHEN '986' THEN 'CLM' WHEN '988' THEN 'WHI' WHEN '992' THEN 'WRA' WHEN '995' THEN 'ADM' ELSE 'Unknown' END AS Location FROM MAS_BIF_GL_Account ) AS gla ON gla.AccountKey = ihd.SOGLSalesAcct GROUP BY ihh.CustomerNumber, cm.CustomerName, gla.Account, ihh.WarehouseCode ) AS x[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
osupratt
Posting Yak Master
238 Posts |
Posted - 2008-04-17 : 10:19:52
|
| Thanks Peso, but this does exactly the same thing as what I posted. The problem is that the Customers are NOT combining as one row per Region like they should be. There $ totals should be summed on one row and then that row should get ranked accordingly.Also I would like ALL the rows that are ranked 20 with the column name 'AllOthers' to be summed together to show as only one row.So I would have Region 'North' for example with Customer 1-19 with their totals descending then on row 20 the AllOthers will show with their totals summed as one row.Thanks for your help. I appreciate it. |
 |
|
|
osupratt
Posting Yak Master
238 Posts |
Posted - 2008-04-17 : 12:22:18
|
| OK. I've got one of the issues figured out.I still need help with combining all of the 'AllOthers' (>19 in rank) on one row. |
 |
|
|
osupratt
Posting Yak Master
238 Posts |
Posted - 2008-04-17 : 13:11:23
|
| OK. I've got one of the issues figured out.I still need help with combining all of the 'AllOthers' (>19 in rank) on one row. |
 |
|
|
|
|
|