| Author |
Topic |
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-06-05 : 23:48:25
|
| Please help me in getting the output..Table:Tid Test1 Test2 Test3 ----------- ------ ------ ------ 1 45 45 902 60 60 603 15 20 254 80 80 805 35 45 356 36 38 40Rules:I want to display the rows with a category based on certain conditions..If in a row two columns have same values then i make category1.If in a row three columns have same values then it should be category2.If in a row three columns have different values then it should be Category3.Expected output:I want the ouput:Tid Test1 Test2 Test3 Desc----------- ------ ------ ------ -----1 45 45 90 Category12 60 60 60 Category23 15 20 25 Category34 80 80 80 Category25 35 45 35 Category16 36 38 40 Category3Thanks for your help in advance! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-06 : 00:37:43
|
use a case when to do itselect Tid, Test1, Test2, Test3, desc = case when Test1 = Test2 or Test1 = Test3 or Test2 = Test3 then 'Categoy1' when Test1 = Test2 and Test2 = Test3 then 'Category2' when Test1 <> Test2 and Test2 <> Test3 then 'Category3' endfrom yourtable KH |
 |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2007-06-06 : 01:09:39
|
| Select Test1 , Test2 , Test3 , Case when (Test1 = Test2 and Test2 = Test3 ) Then 'Category2' when ((Test1 = Test2 or Test1 = Test3) and Test2 <> Test3 ) Then 'Category1' When (Test1 <> Test2 and Test2 <> Test3 and Test1 <> Test3 )Then 'Category3' End as 'CateGory'From @Temp |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2007-06-06 : 01:48:29
|
| Declare @Temp Table (Tid int, Test1 int, Test2 int, Test3 Int)Insert into @TempSelect 1,45,45,90 union allselect 2,60,60,60 union allselect 3,15,20,25 union allselect 4,80,80,80 union allSelect 5,35,45,35 union allSelect 6,36,38,40--Select * From @TempSelect Test1 , Test2 , Test3 , Case when (Test1 = Test2 and Test2 = Test3 ) Then 'Category2' when ((Test1 = Test2 or Test1 = Test3) and Test2 <> Test3 ) Then 'Category1' When (Test1 <> Test2 and Test2 <> Test3 and Test1 <> Test3 )Then 'Category3' End as 'CateGory'From @Temp |
 |
|
|
sqlfresher2k7
Aged Yak Warrior
623 Posts |
Posted - 2007-06-06 : 07:52:39
|
| Thanks for your reply..One more condition i need to apply sum (test1,test2,test3) should not be greater than 180.If it greater then 180 then it should be under Category 4..Please help....... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-06 : 08:11:54
|
just add another like of when test1 + test + ... to it. KH |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-06-06 : 08:34:23
|
| like...... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-06 : 08:35:36
|
like thiswhen test1 + test2 + test3 < 180 then 'Category4' KH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-06 : 10:12:46
|
quote: Originally posted by khtan like thiswhen test1 + test2 + test3 < 180 then 'Category4' KH
You should handle NULL as well MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-06 : 10:17:48
|
Not sure if this worksSelect Tid,count(Test1) as counting from ( Select Tid, Test1 from table union all Select Tid, Test2 from table union all Select Tid, Test3 from table) Tgroup by Tid Now based on counting you can categorizeMadhivananFailing to plan is Planning to fail |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
|