| Author |
Topic |
|
Naveen Merupula
Starting Member
6 Posts |
Posted - 2011-04-26 : 03:38:51
|
| Hi, I want to retrieve the name of the Column based on the values in the corresponding rows.For example,CustID , Custname1 , custname21 XYZ ABC2 xxx XYZNow I wantthe Column name which is having XYZ in the rows of CustID 1 and 2That is I want the output as BelowCustID Columnname 1 Custname1 2 Custname2Be happy.. :) |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-04-26 : 03:57:07
|
[code]select CustID, ColumnName = case when Custname1 = 'XYZ' then 'Columnname1' when Custname2 = 'XYZ' then 'Columnname2' endfrom YourTable[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2011-04-26 : 05:05:11
|
Little correction as per OPquote: Originally posted by khtan
select CustID, ColumnName = case when Custname1 = 'XYZ' then 'Custname1' when Custname2 = 'XYZ' then 'Custname2' endfrom YourTable KH[spoiler]Time is always against us[/spoiler]
Vaibhav TIf I cant go back, I want to go fast... |
 |
|
|
Naveen Merupula
Starting Member
6 Posts |
Posted - 2011-04-26 : 05:19:44
|
| Thanks....will try it.Be happy.. :) |
 |
|
|
Naveen Merupula
Starting Member
6 Posts |
Posted - 2011-04-26 : 05:27:54
|
| hiIf my table is like this:CustID , Custname1 , custname21 XYZ ABC2 xxx XYZ3 XXX XXXAfter executing the query....i am getting the output like thisCustID Columnname1 Custname12 Custname23 NullBut I dont want Null value record in the output...can u suggest me the solutin for thisBe happy.. :) |
 |
|
|
raghuveer125
Constraint Violating Yak Guru
285 Posts |
Posted - 2011-04-26 : 05:56:56
|
| Just put this condition after Katan quereywhere Columnname is not nullLike thisselect CustID, ColumnName = case when Custname1 = 'XYZ' then 'Custname1' when Custname2 = 'XYZ' then 'Custname2' endfrom YourTablewhere Columnname is not nullRaghu' S |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-04-26 : 06:00:44
|
the WHERE clause should beWHERE Custname1 = 'XYZ'or Custname2 = 'XYZ' KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-04-26 : 06:01:45
|
quote: Originally posted by raghuveer125 Just put this condition after Katan quereywhere Columnname is not nullLike thisselect CustID, ColumnName = case when Custname1 = 'XYZ' then 'Custname1' when Custname2 = 'XYZ' then 'Custname2' endfrom YourTablewhere Columnname is not nullRaghu' S
You can't reference a column alias in the WHERE clause like that.However, using a derived table or CTE, it will be possibleselect *from( select CustID, ColumnName = case when Custname1 = 'XYZ' then 'Custname1' when Custname2 = 'XYZ' then 'Custname2' end from YourTable) twhere ColumnName is not null KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Naveen Merupula
Starting Member
6 Posts |
Posted - 2011-04-26 : 06:18:25
|
| Ya I got it....:) ThanksBe happy.. :) |
 |
|
|
Naveen Merupula
Starting Member
6 Posts |
Posted - 2011-04-26 : 08:25:50
|
| Hi,I have the following tableMEMBER_ID PROV_ID FLAG 110 PCP001 F 110 NPR001 R 111 PCP001 F 111 PCP001 R 112 NPR001 F 112 NPR001 R 113 NPR001 F 113 PCP001 F 113 NPR001 RNow I want to update the Flag column as per the below conditions:1.Whenever the PROV_ID field is same and Flag value is 'F' and 'R' then that Flag value for the meber should be changed to 'B'I want the result as follows :MEMBER_ID PROV_ID FLAG 110 PCP001 F 110 NPR001 R 111 PCP001 B 112 NPR001 B 113 NPR001 F 113 PCP001 F 113 NPR001 R can u please provide me the solutionBe happy.. :) |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2011-04-26 : 08:43:18
|
| Post this as a different question as its totally irrelevant to the current post...Vaibhav TIf I cant go back, I want to go fast... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-04-26 : 08:44:25
|
[code]; with tbl as( select MEMBER_ID, PROV_ID = max(PROV_ID), FLAG = 'B' from yourtable group by MEMBER_ID having min(PROV_ID) = max(PROV_ID) and min(FLAG) = 'F' and max(FLAG) = 'R')select MEMBER_ID, PROV_ID, FLAGfrom yourtable twhere not exists (select * from tbl x where x.MEMBER_ID = t.MEMBER_ID)union allselect MEMBER_ID, PROV_ID, FLAGfrom tblorder by MEMBER_ID[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Naveen Merupula
Starting Member
6 Posts |
Posted - 2011-04-26 : 08:59:27
|
| Will u please explain how can we use aggregate functions like min() and max() on datatypes like varchar...and usage of min(prov_id) and min(flag) and max(flag) in the above solutionBe happy.. :) |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-04-26 : 09:49:24
|
min() and max() also works on string not only on numbers. It will be based on the ascii code of the character KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|