SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to pass dynamic values in my query??
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 11/29/2012 :  01:45:13  Show Profile  Reply with Quote
select DimConditionDefinitionId
,SUM (CASE WHEN DimConditionDefinitionId between 1 and 4
THEN totalusers
ELSE 0
END) /246624.0 AS PopulationCount from pop_fact

GROUP by DimConditionDefinitionId
Here i am getting solution for condition 1,2,3,4 it is fine but here i need to get if i pass condition 1 and condition 2 or else condition 3 or condition 4 in jumbling way ???

i have written case condition but it is showing result set as condition 1 and condition 2.So when i give both conditions it should count both and give total count






P.V.P.MOhan

ahmeds08
Aged Yak Warrior

India
667 Posts

Posted - 11/29/2012 :  01:57:23  Show Profile  Send ahmeds08 a Yahoo! Message  Reply with Quote
you can do it by using IF statement.
write 3 IF statements.
first one for condition 1,second for condition 2 and third for condition 1 and 2.
Go to Top of Page

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 11/29/2012 :  02:29:55  Show Profile  Reply with Quote
No it won't works it is multiplying single condition i need to get sum total of cond 1 and cond2 else cond2 and cond3 it is not about the case statement

P.V.P.MOhan
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22765 Posts

Posted - 11/29/2012 :  02:41:42  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Post some sample data with expected output

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 11/29/2012 :  03:18:29  Show Profile  Reply with Quote
dynamically i will pass condition 1 or 2 or 3 or 4 if i pass 2 parameters at one go it should show the count of 2 conditions if i pass 3 conditions at one go it should show count of 3 cond divided by 246624.0

P.V.P.MOhan
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17658 Posts

Posted - 11/29/2012 :  03:28:40  Show Profile  Reply with Quote

select DimConditionDefinitionId
      ,SUM (CASE WHEN DimConditionDefinitionId = @condition
                 THEN totalusers
                 ELSE 0
                 END) /246624.0 AS PopulationCount 
from  pop_fact
GROUP by DimConditionDefinitionId



KH
Time is always against us

Go to Top of Page

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 11/29/2012 :  03:37:35  Show Profile  Reply with Quote
this for single condition it works fine but when i pass 2 con ditions at one time then how i need to write the query

P.V.P.MOhan
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17658 Posts

Posted - 11/29/2012 :  03:41:58  Show Profile  Reply with Quote
quote:
Originally posted by mohan123

this for single condition it works fine but when i pass 2 con ditions at one time then how i need to write the query

P.V.P.MOhan



what do you mean by that ? can you illustrate with an example ?


KH
Time is always against us

Go to Top of Page

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 11/29/2012 :  03:55:10  Show Profile  Reply with Quote
Declare @condition int

set @condition = @condition1 and @condition2 like this now it showing result of single condition count if i pass condition 1 and 2 combining count should come

select DimConditionDefinitionId
,SUM (CASE WHEN DimConditionDefinitionId = @condition
THEN totalusers
ELSE 0
END) /246624.0 AS PopulationCount
from pop_fact
GROUP by DimConditionDefinitionId

P.V.P.MOhan
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17658 Posts

Posted - 11/29/2012 :  04:22:24  Show Profile  Reply with Quote
sounds like you need something like this
http://www.sommarskog.se/arrays-in-sql.html


KH
Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 1.17 seconds. Powered By: Snitz Forums 2000