| Author |
Topic |
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-08 : 09:50:08
|
I need help with a case when it comes to joined tables. Everything else in my Case Statement comes from 1 table but how can I incorporate it with a joined table?I have this sql:select Blanket = (select count(*)from History a, Plans.dbo.PlanRecords bwhere a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'and a.Process in ('On Hand', 'Ready')and a.Priceplan <> ''and a.PromoCode in ('AB555', 'AB556', 'AB557')and a.priceplan in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234', '83241', '83236', '83237', '83238'))+(select count(*)from History a, Plans.dbo.PlanRecords bwhere a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'and a.Process in ('On Hand', 'Ready')and a.Priceplan <> ''and a.PromoCode in ('AB555', 'AB556', 'AB557')and (a.priceplan <> b.price_plan)and a.priceplan in ('73442', '73443', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73442', '73443', '83234','83241', '83236', '83237', '83238'))The Promocode, and Process for the other case are the same and the maildate is the same too, and it all comes from the History table.Also I am having trouble casing this as well.I tried to turn this: Select sum((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCoderyCodeList),''),' ','')))) as 'ACC'from historywhere Process in ('On Hand', 'Ready')and maildate= '20100301'and PromoCode in ('AB555', 'AB556', 'AB557')and AccCode> ' 'Into sum (case when AccCode> ' ' and ((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ','')))) THEN 1 end) as[ACC] but get an error at the Then part. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-08 : 09:56:39
|
is this what you're looking at?select Blanket = count(case when a.priceplan in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234', '83241', '83236', '83237', '83238') then 1 else null end) +count(case when a.priceplan <> b.price_planand a.priceplan in ('73442', '73443', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73442', '73443', '83234','83241', '83236', '83237', '83238') then 1 else null end)from History a, Plans.dbo.PlanRecords bwhere a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'and a.Process in ('On Hand', 'Ready')and a.Priceplan <> ''and a.PromoCode in ('AB555', 'AB556', 'AB557')------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-08 : 09:57:46
|
quote: Originally posted by visakh16 is this what you're looking at?select Blanket = count(case when a.priceplan in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234', '83241', '83236', '83237', '83238') then 1 else null end) +count(case when a.priceplan <> b.price_planand a.priceplan in ('73442', '73443', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73442', '73443', '83234','83241', '83236', '83237', '83238') then 1 else null end)from History a, Plans.dbo.PlanRecords bwhere a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'and a.Process in ('On Hand', 'Ready')and a.Priceplan <> ''and a.PromoCode in ('AB555', 'AB556', 'AB557')------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Yes it is, thank you visakh. Do you know how I can case the second one? Wit the sum.Also do I really need this part?a.maildate= '20100301'Because my other case have the same maildate as well, but without the join like this: COUNT (CASE WHEN FCLIKE '%12132231%' OR FC LIKE '%123123%' THEN FC END) AS 'ASDAD'COUNT (CASE WHEN FCLIKE '%1233%' OR FC LIKE '%123555%' THEN FC END) AS 'asdgvc'from history where maildate = '20100301' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-08 : 10:01:28
|
| whats the purpose of below condition?and ((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ',''))))------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-08 : 10:08:12
|
quote: Originally posted by visakh16 whats the purpose of below condition?and ((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ',''))))------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
The AccCode field has multiple strings so it counts the number of words and then it multiples it by the count. So if the string is Samesung Motorola Nokia and if it appears 4 times then the sum would be 12 since its 4 times 3 words. And it ignores the blanks. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-08 : 10:15:39
|
quote: Originally posted by fengfeng
quote: Originally posted by visakh16 whats the purpose of below condition?and ((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ',''))))------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
The AccCode field has multiple strings so it counts the number of words and then it multiples it by the count. So if the string is Samesung Motorola Nokia and if it appears 4 times then the sum would be 12 since its 4 times 3 words. And it ignores the blanks.
what happens when you add this?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-08 : 10:21:59
|
quote: Originally posted by visakh16
quote: Originally posted by fengfeng
quote: Originally posted by visakh16 whats the purpose of below condition?and ((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ',''))))------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
The AccCode field has multiple strings so it counts the number of words and then it multiples it by the count. So if the string is Samesung Motorola Nokia and if it appears 4 times then the sum would be 12 since its 4 times 3 words. And it ignores the blanks.
what happens when you add this?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
When I add sum (case when AccCode> ' ' and ((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ','')))) THEN 1 end) as[ACC] I get an error at THENAlso I get an error here:from History a, Plans.dbo.PlanRecords bwhere a.PlanRecordID = b.PlanRecordID I think it's because my other case comes from the History table, so my code is going to look like...COUNT (CASE WHEN FCLIKE '%12132231%'OR FC LIKE '%123123%'THEN FCEND) AS 'ASDAD'COUNT (CASE WHEN FCLIKE '%1233%'OR FC LIKE '%123555%'THEN FCEND) AS 'asdgvc'count(case when a.priceplan <> b.price_planand a.priceplan in ('73442', '73443', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73442', '73443', '83234','83241', '83236', '83237', '83238') then 1 else null end)from History a, Plans.dbo.PlanRecords bwhere a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'from historywhere Process in ('On Hand', 'Ready')and maildate= '20100301'and PromoCode in ('AB555', 'AB556', 'AB557')so you see there are 2 from statements and maildate even though they are coming from the same table and same maildate? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-08 : 10:32:39
|
| no need of two from. you need just single from. if you dont want second table in one of count take left join with it and nulls will get ignored inside sum() or count() so in effect you aggregate only values from first table------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-08 : 10:41:11
|
quote: Originally posted by visakh16 no need of two from. you need just single from. if you dont want second table in one of count take left join with it and nulls will get ignored inside sum() or count() so in effect you aggregate only values from first table------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
I'm sorry but I have no idea what that means |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-08 : 10:43:18
|
| [code]....from History aleft join Plans.dbo.PlanRecords bon a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'...[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-08 : 10:52:23
|
quote: Originally posted by visakh16
....from History aleft join Plans.dbo.PlanRecords bon a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'... ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
thank you for all your help. I am still getting an error at the THEN part of the Sum AccCode select |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-08 : 10:59:08
|
| [code]select Blanket = count(case when a.priceplan in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234', '83241', '83236', '83237', '83238') then 1 else null end) +count(case when a.priceplan <> b.price_planand a.priceplan in ('73442', '73443', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73442', '73443', '83234','83241', '83236', '83237', '83238') then 1 else null end),sum (case when AccCode> ' 'and ((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ','')))) THEN 1 end) from History aleft join Plans.dbo.PlanRecords bon a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'and a.Process in ('On Hand', 'Ready')and a.Priceplan <> ''and a.PromoCode in ('AB555', 'AB556', 'AB557')[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-08 : 11:21:34
|
quote: Originally posted by visakh16
select Blanket = count(case when a.priceplan in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234', '83241', '83236', '83237', '83238') then 1 else null end) +count(case when a.priceplan <> b.price_planand a.priceplan in ('73442', '73443', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73442', '73443', '83234','83241', '83236', '83237', '83238') then 1 else null end),sum (case when AccCode> ' 'and ((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ','')))) THEN 1 end) from History aleft join Plans.dbo.PlanRecords bon a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'and a.Process in ('On Hand', 'Ready')and a.Priceplan <> ''and a.PromoCode in ('AB555', 'AB556', 'AB557')------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Getting error at THEN 1 end) by the SUM Case select. I added Then 1 end) as [sumofList]Getting error for THEN |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-08 : 11:23:43
|
| what should this be compared to?((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ',''))))------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-08 : 11:30:31
|
quote: Originally posted by visakh16 what should this be compared to?((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ',''))))------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
this is the original[code]Select sum((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ','')))) as 'SumofList'from historywhere Process in ('On Hand', 'Ready')and exportdate = '20100301'and PromoCode in ('AB555', 'AB556', 'AB557')and AccCode> ' ' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-08 : 11:32:59
|
| [code]then shouldnt equivalent beselect Blanket = count(case when a.priceplan in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234', '83241', '83236', '83237', '83238') then 1 else null end) +count(case when a.priceplan <> b.price_planand a.priceplan in ('73442', '73443', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73442', '73443', '83234','83241', '83236', '83237', '83238') then 1 else null end),sum (case when AccCode> ' 'then ((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ','')))) end) from History aleft join Plans.dbo.PlanRecords bon a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'and a.Process in ('On Hand', 'Ready')and a.Priceplan <> ''and a.PromoCode in ('AB555', 'AB556', 'AB557')[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-08 : 11:42:57
|
quote: Originally posted by visakh16
then shouldnt equivalent beselect Blanket = count(case when a.priceplan in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234', '83241', '83236', '83237', '83238') then 1 else null end) +count(case when a.priceplan <> b.price_planand a.priceplan in ('73442', '73443', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73442', '73443', '83234','83241', '83236', '83237', '83238') then 1 else null end),sum (case when AccCode> ' 'then ((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ','')))) end) from History aleft join Plans.dbo.PlanRecords bon a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'and a.Process in ('On Hand', 'Ready')and a.Priceplan <> ''and a.PromoCode in ('AB555', 'AB556', 'AB557')------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
i have something that looks like this now:select maildate, COUNT (CASE WHEN FCODE LIKE '%72147%' OR FCODE LIKE '%72148%' THEN FCODE END) AS [FCODE_COUNT],select Blanket = count(case when a.priceplan in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234', '83241', '83236', '83237', '83238') then 1 else null end) +count(case when a.priceplan <> b.price_planand a.priceplan in ('73442', '73443', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73442', '73443', '83234','83241', '83236', '83237', '83238') then 1 else null end),sum (case when AccCode> ' 'then ((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ','')))) end) from History aleft join Plans.dbo.PlanRecords bon a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'and a.Process in ('On Hand', 'Ready')and a.Priceplan <> ''and a.PromoCode in ('AB555', 'AB556', 'AB557')Now i get an error "Invalid column name 'FCODE'Do I have to put something like a.FCODE and a.maildate? If i do that, i get an error saying "The column prefix 'a' does not match with a table name or alias name used in the query." |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-08 : 11:46:04
|
select maildate, COUNT (CASE WHEN FCODE LIKE '%72147%' OR FCODE LIKE '%72148%' THEN FCODE END) AS [FCODE_COUNT],Blanket = count(case when a.priceplan in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234', '83241', '83236', '83237', '83238') then 1 else null end) +count(case when a.priceplan <> b.price_planand a.priceplan in ('73442', '73443', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73442', '73443', '83234','83241', '83236', '83237', '83238') then 1 else null end),sum (case when AccCode> ' 'then ((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ','')))) end) from History aleft join Plans.dbo.PlanRecords bon a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'and a.Process in ('On Hand', 'Ready')and a.Priceplan <> ''and a.PromoCode in ('AB555', 'AB556', 'AB557')------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-08 : 11:52:36
|
quote: Originally posted by visakh16
select maildate, COUNT (CASE WHEN FCODE LIKE '%72147%' OR FCODE LIKE '%72148%' THEN FCODE END) AS [FCODE_COUNT],Blanket = count(case when a.priceplan in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73957','73958','73959','73960','73961','73962','74558','74566','74569','74577','74591','74592','74593','74594','74595','74596','74608','74609','74610','74611','74612','74613','75310','75311','75312', '83234', '83241', '83236', '83237', '83238') then 1 else null end) +count(case when a.priceplan <> b.price_planand a.priceplan in ('73442', '73443', '83234','83241', '83236', '83237', '83238')and b.price_plan not in ('73442', '73443', '83234','83241', '83236', '83237', '83238') then 1 else null end),sum (case when AccCode> ' 'then ((1+len(NULLIF(LTRIM(AccCode),''))-len(replace(NULLIF(LTRIM(AccCode),''),' ','')))) end) from History aleft join Plans.dbo.PlanRecords bon a.PlanRecordID = b.PlanRecordID and a.maildate= '20100301'and a.Process in ('On Hand', 'Ready')and a.Priceplan <> ''and a.PromoCode in ('AB555', 'AB556', 'AB557')------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Sorry but I'm still getting the invalid column name error. I have it as exactly what you have |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-08 : 11:56:29
|
| invalid column? which column? at least post error message in full------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fengfeng
Yak Posting Veteran
64 Posts |
Posted - 2010-03-08 : 12:03:34
|
quote: Originally posted by visakh16 invalid column? which column? at least post error message in full------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
I get "Invalid column name 'FCODE'." |
 |
|
|
Next Page
|
|
|