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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to pass dynamic values in my query??

Author  Topic 

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2012-11-29 : 01:45:13
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

737 Posts

Posted - 2012-11-29 : 01:57:23
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

252 Posts

Posted - 2012-11-29 : 02:29:55
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

22864 Posts

Posted - 2012-11-29 : 02:41:42
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

252 Posts

Posted - 2012-11-29 : 03:18:29
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)

17689 Posts

Posted - 2012-11-29 : 03:28:40

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



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2012-11-29 : 03:37:35
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)

17689 Posts

Posted - 2012-11-29 : 03:41:58
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
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2012-11-29 : 03:55:10
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)

17689 Posts

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


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -