| Author |
Topic |
|
sqlfish
Starting Member
9 Posts |
Posted - 2010-06-04 : 10:26:22
|
| Hi there,I'm new to SQL Server and have a problem. I would like to select only columns of a table which have a specifc number of NULL values. So I tried something like this:SELECT CASE WHEN (true) THEN columnName ENDFROM tableNameThis works fine but in the new table all column names are missing. I'm afraid that this is not working:SELECT CASE WHEN (true) THEN columnName AS columnName ENDFROM tableNameHas anyone an idea how to solve this problem? I guess there should be simple solution.Thanks |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-06-04 : 10:33:20
|
| SELECTCASEWHEN (true) THEN columnName END AS columnName FROM tableNameLimitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
sqlfish
Starting Member
9 Posts |
Posted - 2010-06-04 : 10:37:42
|
| Thanks Idera, sometimes it can be so easy. ;-) |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-06-04 : 10:44:09
|
quote: Originally posted by sqlfish Thanks Idera, sometimes it can be so easy. ;-)
Yup happens to everyone sometimes. Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-04 : 11:02:48
|
| Note whenever there is a function,calculation or case expression, column name wont be displayed until you alias itMadhivananFailing to plan is Planning to fail |
 |
|
|
sqlfish
Starting Member
9 Posts |
Posted - 2010-06-07 : 09:15:53
|
| I have one more question regarding this topic. The problem with the column names is solved but all columns are selected and the ones which don't satisfy the expression get NULL values in each row. Any ideas? |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-06-07 : 09:20:18
|
Now it is about time to give example data, wanted output in relation to the sample data and what you have coded so far. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
sqlfish
Starting Member
9 Posts |
Posted - 2010-06-07 : 09:37:06
|
| Ok, here some sample code:CREATE TABLE #Test ( bloodflow int, heartrate int)INSERT INTO #Test (bloodflow, heartrate) VALUES (200,50)INSERT INTO #Test (bloodflow, heartrate) VALUES (300,60)DECLARE @var realSET @var = 100SELECT CASE WHEN (SELECT MIN(bloodflow) from #Test) > @var THEN bloodflow END AS bloodflow, CASE WHEN (SELECT MIN(heartrate) from #Test) > @var THEN heartrate END AS heartrateFROM#TestSo I start with a table like this:200|50300|60and I get200|NULL300|NULLI just want to get the first column. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-06-07 : 09:46:30
|
| What you mean by "first column"?Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
sqlfish
Starting Member
9 Posts |
Posted - 2010-06-07 : 09:50:48
|
| I don't wantbloodflow|heartrate200 |null300 |nulljustbloodflow200300The heartrate column shouldn't be selected. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-07 : 09:58:51
|
quote: Originally posted by sqlfish I don't wantbloodflow|heartrate200 |null300 |nulljustbloodflow200300The heartrate column shouldn't be selected.
What if there is value for heartrate?MadhivananFailing to plan is Planning to fail |
 |
|
|
sqlfish
Starting Member
9 Posts |
Posted - 2010-06-07 : 10:09:48
|
| Perhaps my intention is not clear and I do something totally wrong. Regarding the example I only want to select the columns of the table which have a min value > 100. Starting frombloodflow|heartrate200 |50300 |60I only want to select the column bloodflow because in column heartrate there is no value higher than 100.So I only wantbloodflow200300 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-07 : 10:24:53
|
quote: Originally posted by sqlfish Perhaps my intention is not clear and I do something totally wrong. Regarding the example I only want to select the columns of the table which have a min value > 100. Starting frombloodflow|heartrate200 |50300 |60I only want to select the column bloodflow because in column heartrate there is no value higher than 100.So I only wantbloodflow200300
What do you want to return for this?bloodflow|heartrate200 |150300 |60MadhivananFailing to plan is Planning to fail |
 |
|
|
sqlfish
Starting Member
9 Posts |
Posted - 2010-06-07 : 10:28:13
|
| Then I would like to get both columns back:bloodflow|heartrate200 |150300 |60 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-07 : 10:32:40
|
| If exists(select * from #table where heartrate>1000)select bloodflow,heartrate from #tableelseselect bloodflow from #tableMadhivananFailing to plan is Planning to fail |
 |
|
|
sqlfish
Starting Member
9 Posts |
Posted - 2010-06-07 : 10:44:30
|
| Thanks but that was just a toy example. I have not only 2 columns but many more. So this won't work for me. I thought it would be possible to use a 'select case' statement like the one I posted. So I could check each column and when the condition is satisfied the column will be selected, if not it is not selected. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-07 : 10:52:26
|
quote: Originally posted by sqlfish Thanks but that was just a toy example. I have not only 2 columns but many more. So this won't work for me. I thought it would be possible to use a 'select case' statement like the one I posted. So I could check each column and when the condition is satisfied the column will be selected, if not it is not selected.
Your method wont work. You need to simulate what I postedMadhivananFailing to plan is Planning to fail |
 |
|
|
sqlfish
Starting Member
9 Posts |
Posted - 2010-06-08 : 09:39:40
|
| Ok, thanks anyway. |
 |
|
|
|