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.
| Author |
Topic |
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-04 : 10:14:28
|
I have the following sql statement that I need a little help with. Select mail_date, COUNT (CASE WHEN Product_Code LIKE '%12563%' OR Product_Code LIKE '%12567%' THEN Product_Code END) AS 'IBM_5_bundle'FROM historywhere Mail_date between '201020' and '20100301'and State in ('In Process', 'Order Fulfilled')and Prom_Code in ('AB101', 'AB102', 'AB103')and FileName like '%FA%'group by mail_dateSelect mail_date, COUNT (CASE WHEN Product_Code LIKE '%72147%' OR Product_Code LIKE '%72148%' THEN Product_Code END) AS a, COUNT (CASE WHEN Product_Code LIKE '%7215%' OR Product_Code LIKE '%2150%' THEN Product_Code END) AS b COUNT (CASE WHEN Product_Code LIKE '%7219%' OR Product_Code LIKE '%7291%' THEN Product_Code END) AS 'c', COUNT (CASE WHEN Product_Code LIKE '%709%'OR Product_Code LIKE '%717%' OR Product_Code LIKE '%7361%' OR Product_Code LIKE '%7176%' OR Product_Code LIKE '%3715%' OR Product_Code LIKE '%7715%' THEN Product_Code END) AS F, COUNT (CASE WHEN Product_Code LIKE '%7512%' OR Product_Code LIKE '%71247%' OR Product_Code LIKE '%75412%' THEN Product_Code END) AS G, COUNT (CASE WHEN Product_Code LIKE '%71231%' OR Product_Code LIKE '%45612%' THEN Product_Code END) AS H, COUNT (CASE WHEN Product_Code LIKE '%1233%' OR Product_Code LIKE '%6541%' THEN Product_Code END) AS IFROM historyWHERE Mail_date between '2010201' and '20100301'AND State IN ('In Process', 'Order Fulfilled')AND PROM_CODE IN ('AB101', 'AB102', 'AB103')group by mail_dateselect mail_date, planB =(select mail_date, count(*)from history a, offer.dbo.offertype bwhere a. offertypeID = b. offertypeIDand a.Mail_date between '2010201' and '20100301'and a.State in ('In Process', 'Order Fulfilled')and a.Priceplan <> ''and a.Prom_Code in ('AB101', 'AB102', 'AB103')and a.priceplan in('4243324','234234234','234234')and b.price_plan not in ('4243324','234234234','234234'))+(select count(*)from history a, offer.dbo.offertype bwhere a. offertypeID = b. offertypeIDand a.Mail_date between '2010201' and '20100301'--and calldate >= '20100220'and a.State in ('In Process', 'Order Fulfilled')and a.Priceplan <> ''and a.Prom_Code in ('AB101', 'AB102', 'AB103')and (a.priceplan <> b.price_plan)and a.priceplan in ('234234','234231','68967')and b.price_plan not in ('234234','234231','68967'))group by mail_dateSelect mail_date, sum((1+len(NULLIF(LTRIM(Accessory_Code),''))-len(replace(NULLIF(LTRIM(AccessoryCodeList),''),' ','')))) as Kfrom historywhere State in ('In Process', 'Order Fulfilled')and Mail_date between '2010201' and '20100301'--and calldate >= '20100220'and Prom_Code IN ('AB101', 'AB102', 'AB103')and AccessoryCode > ' 'group by mail_dateI could choose mail date as the group by for all those queries except the one with the nested select "select mail_date, planB =" The error I get i"s, "does not match with a table name or alias name used in the query.Also I have the results output looking something like this:mail_date IBM_5_bundle20100201 120100202 020100203 320100204 120100205 020100208 320100209 220100210 1mail_date a b c20100201 2 0 020100202 3 0 020100203 3 0 020100204 4 0 020100205 2 0 020100208 6 0 020100209 4 0 020100210 3 0 020100211 0 0 0is there a way to have it look like this so that I can copy it into excel and pivot the data easily?mail_date Type Count20100201 a 220100202 a 320100203 a 320100204 a 420100205 a 220100208 a 620100209 a 420100210 a 320100211 a 020100201 b 020100202 b 020100203 b 020100204 b 020100205 b 020100208 b 020100209 b 020100210 b 020100211 b 020100201 c 020100202 c 020100203 c 020100204 c 020100205 c 020100208 c 020100209 c 020100210 c 020100211 c 020100201 IBM_5_bundle 120100202 IBM_5_bundle 020100203 IBM_5_bundle 320100204 IBM_5_bundle 120100205 IBM_5_bundle 020100208 IBM_5_bundle 320100209 IBM_5_bundle 220100210 IBM_5_bundle 120100211 IBM_5_bundle 0 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-03-04 : 11:12:05
|
| I don't know why you'd want to unpivot the data in SQL and then pivot it back in excel, but this will do the trickSELECT mail_date ,[Type],[Count]FROM (select mail_date,a ,b,c from @table) pUNPIVOT ([Count] FOR [Type] in (a,b,c) )as unpvt JimEveryday I learn something that somebody else already knew |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-04 : 11:14:09
|
quote: Originally posted by jimf I don't know why you'd want to unpivot the data in SQL and then pivot it back in excel, but this will do the trickSELECT mail_date ,[Type],[Count]FROM (select mail_date,a ,b,c from @table) pUNPIVOT ([Count] FOR [Type] in (a,b,c) )as unpvt JimEveryday I learn something that somebody else already knew
Not if version is below 2005 or compatibility level is below 90 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-04 : 12:06:29
|
quote: Originally posted by jimf I don't know why you'd want to unpivot the data in SQL and then pivot it back in excel, but this will do the trickSELECT mail_date ,[Type],[Count]FROM (select mail_date,a ,b,c from @table) pUNPIVOT ([Count] FOR [Type] in (a,b,c) )as unpvt JimEveryday I learn something that somebody else already knew
I see what you're saying. I'm kinda doign redundant work. How can I get this part to work? I can't select mail_date and group byselect mail_date, planB =(select mail_date, count(*)from history a, offer.dbo.offertype bwhere a. offertypeID = b. offertypeIDand a.Mail_date between '2010201' and '20100301'and a.State in ('In Process', 'Order Fulfilled')and a.Priceplan <> ''and a.Prom_Code in ('AB101', 'AB102', 'AB103')and a.priceplan in('4243324','234234234','234234')and b.price_plan not in ('4243324','234234234','234234'))+(select count(*)from history a, offer.dbo.offertype bwhere a. offertypeID = b. offertypeIDand a.Mail_date between '2010201' and '20100301'--and calldate >= '20100220'and a.State in ('In Process', 'Order Fulfilled')and a.Priceplan <> ''and a.Prom_Code in ('AB101', 'AB102', 'AB103')and (a.priceplan <> b.price_plan)and a.priceplan in ('234234','234231','68967')and b.price_plan not in ('234234','234231','68967'))group by mail_date |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-04 : 12:11:08
|
| whats the purpose of extra mail_date in first query? i dont think it will work unless you group by mail_date------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-04 : 12:15:52
|
quote: Originally posted by visakh16 whats the purpose of extra mail_date in first query? i dont think it will work unless you group by mail_date------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
I want it to look like thismaildate planb 02/01/2010 502/02/2010 6instead ofplanb56 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-04 : 12:20:31
|
| and whats the purpose of addition?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-04 : 12:29:14
|
quote: Originally posted by visakh16 and whats the purpose of addition?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
So i can copy and paste the dates with the count into excel |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-04 : 12:32:43
|
| ok. no need of that. how should your final output be like?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-04 : 12:42:03
|
quote: Originally posted by visakh16 ok. no need of that. how should your final output be like?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
it should be like this:maildate planb02/01/2010 502/02/2010 602/03/2010 602/04/2010 6dates, then planb |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-04 : 13:34:16
|
| then you require only first part rite? why should you add up count thingy?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|