Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi I have a table which contains number of stock groups along with their stock category as shown below:Stock Group Stock_CategoryBE 1505BE 1506BE 1515BE 773BE 792BE 811BE 847BE 848BE 911CAB 7600DIU 1034DIU 1086DIU 12DIU 25DIU 28DIU 1086AINS 3601INS 4011LPC 9700MO TC130SEE SERCOTSB 1033BUK 2620UK 2663UK 2673UK 2966UK 2971Each of the stock groups belongs to a higher stock group family:BER contains BE/INS/UKOG contains CAB/LPC/MO/SEEOD contains DFI/DIUI am trying to do a count in the table to match each record to the relevant higher stock group family. I have used the follwoing CASE statement to do so:SELECT CASE WHEN [Stock Group] IN ('BE','INS','UK') THEN 'BER' WHEN [Stock Group] IN ('CAB','LPC','MO','SEE') THEN 'OG' WHEN [Stock Group] IN ('DFI,DIU') THEN 'OD'END AS 'MainStockGroup',COUNT(*) FROM StockGroupsGROUP BY [Stock Group]I get the following results: (Please note the number figures are not accurate)MainStockGroupBER 81OD 1OG 10BER 2OD 1OG 1OD 1The problem I am having here is that the higher stock group family is being used is being used twice with different counts.Does anyone know how to combine the counts so the output is similar to? BER 83OG 11OD 3Thanking you in advance!!!
rcr69er
Constraint Violating Yak Guru
327 Posts
Posted - 2008-05-10 : 10:18:35
HiSorry my imported data appeared wrong. The stock category is the 2 or 3 letter code.
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-05-10 : 12:27:57
Change like this and try:-
SELECT t.MainStockGroup,COUNT(*)FROM(SELECT CASE WHEN [Stock Group] IN ('BE','INS','UK') THEN 'BER'WHEN [Stock Group] IN ('CAB','LPC','MO','SEE') THEN 'OG'WHEN [Stock Group] IN ('DFI,DIU') THEN 'OD'END AS 'MainStockGroup',*FROM StockGroups)tGROUP BY t.MainStockGroup