How to choose a columns of a select statement based on conditions? - sql-server-2005

I have to create an SQL Server 2005 query which checks for the value of one attribute in the table and based on its value, select different sets of columns.
How can I do that?
for e.g.
In table 'car', if the values of 'type' attribute are 1 and 2
when type = 1, i want to execute a select 'query1' with 3 columns.
when type = 2, i want to execute another select 'query2' with 4 other columns.
How do I do that?
Please help.

I think you're looking at a Stored Procedure with an If statement. CASE will work, but it can't change the number of columns returned.

SELECT
Col1 = CASE WHEN Type = 1 THEN (SELECT Null FROM T1)
ELSE (SELECT Col1 FROM T2) END
, Col2 = CASE WHEN Type = 1 THEN (SELECT Col1 FROM T1)
ELSE (SELECT Col2 FROM T2) END
, Col3 = CASE WHEN Type = 1 THEN (SELECT Col2 FROM T1)
ELSE (SELECT Col4 FROM T2) END
, Col4 = CASE WHEN Type = 1 THEN (SELECT Col3 FROM T1)
ELSE (SELECT Col4 FROM T2) END
FROM Cars
If you would show us the DDL of all tables involved, you'd probably get a better answer or a different (read better) approach.

Related

How to fill values in condition in sql

I have tables like following
I'd like to fill col2in9999/12/31 by referring to col1==A
col1 col2
A 2011/1/1
B 2013/4/1
C 2000/1/1
A 2010/1/1
Therefore my desired result is following
Are there any way to achieve this?
col1 col2
A 9999/12/31
B 2013/4/1
C 2000/1/1
A 9999/12/31
If someone has opinion,please let me know
Thanks
If you have one table and that's all that needs updating then:
UPDATE [table] SET col2 = '31-DEC-9999' WHERE col1 = 'A';
If they're two different tables you could just run CREATE TABLE table2 AS SELECT * FROM table1 and then just run that UPDATE above ^. Sure it's an extra step but it does the job.
Simply add case statement to your query:
SELECT COL1, CASE WHEN COL1 = 'A' THEN '9999/12/31' ELSE COL2 END AS COL2 FROM TABLE1;

Need to select a column value based on another column value if its true in sql?

Table structure
id col1 col2
1 data1 false
2 data2 true
I tried
select id, case col2 when true then col1 from table
and it is showing an internal server error. I want to select the col1 from table when the corresponding col2 is true.
probably just a simple syntax error in your case statment.
try this..
select
id,
case when col2=1 then col1 else 'some other value' end as computedCaseCol
from table1
see: SQL fiddle
Do you mean that ?
select id,col1 from tabe where col2 = 'true'
or
select id,col1 from tabe where col2 is true
??

Getting parent data if child data is null in Oracle hierarchical table

In Oracle 10g I have the following hierarchical table:
corp_id, parent_corp_id, col1, col2, col3
I want to flatten out the structure such that we get the first row's data where col1 OR col2 OR col3 is not null.
So for example, suppose I have:
corp_id = 1
parent_corp_id = null
col1 = 5
col2 = NULL
col3 = NULL
corp_id = 3
parent_corp_id = 1
col1 = NULL
col2 = NULL
col3 = NULL
the results of this query would give me:
corp_id, parent_corp_id, col1, col2, col3
3 , 1 , 5 , NULL, NULL
Another scenario:
Suppose I put col2 = 6 where corp_id = 3
Well, then the result set should be:
corp_id, parent_corp_id, col1, col2, col3
3 , 1 , NULL, 6, NULL
In other words, if the child has data in one of these three columns we grab it. Otherwise, we try the parent and so on and so forth. Shouldn't be more than 3 levels deep but it could have 3 levels to look into.
Pretty new to hierarchical queries, so pardon me if this is a rudimentary question.
Use the coalesce() function, which returns the first non-null value in its list:
select
c.corp_id,
c.parent_corp_id,
coalesce(c.col1, p.col1) col1,
coalesce(c.col2, p.col2) col2,
coalesce(c.col3, p.col3) col3
from mytable c
left join mytable p on p.corp_id = c.parent_corp_id
to get the "first row that has a not-null value", add:
where coalesce(c.col1, p.col1, c.col2, p.col2, c.col3, p.col3) is not null
and rownum = 1
You do need to use a hiearchial query (w/ the connect by clause) because of the fact that you have a parent with a child and that child is the parent of another child (although your example data doesn't bring that into play) however the requirement that you show the 'first not null col1' and the 'first not null col2' etc. is a separate issue from the hierarchical relationship altogether.
Try the following, I added some additional sample data to the fiddle (check the DDL on the left side) for illustrative purposes.
It looks like in your expected output you don't want to show the highest level parents, which is why I put "where s.parent_corp_id is not null" at the end. If you actually do want to show those, take that line out.
Otherwise, this will show the col1/col2/col3 values based on their group. Notice how in the example 2 is a high level parent and has 4 as a child, and 4 is also a parent of 8. So corp_id 8 and 4 are part of the same branch and they therefore show the same col1/col2/col3 values, and those are, based on your requirements, the first not null value of each throughout the branch.
Fiddle: http://sqlfiddle.com/#!4/ef218/14/0
with sub as
(select corp_id,
parent_corp_id,
col1,
col2,
col3,
level as lvl,
rownum - level as grp
from tbl
connect by prior corp_id = parent_corp_id
start with parent_corp_id is null),
col1_lvl as
(select grp, col1
from sub s
where s.lvl = (select min(x.lvl)
from sub x
where x.col1 is not null
and x.grp = s.grp)),
col2_lvl as
(select grp, col2
from sub s
where s.lvl = (select min(x.lvl)
from sub x
where x.col2 is not null
and x.grp = s.grp)),
col3_lvl as
(select grp, col3
from sub s
where s.lvl = (select min(x.lvl)
from sub x
where x.col3 is not null
and x.grp = s.grp))
select s.corp_id, s.parent_corp_id, c1.col1, c2.col2, c3.col3
from sub s
left join col1_lvl c1
on s.grp = c1.grp
left join col2_lvl c2
on s.grp = c2.grp
left join col3_lvl c3
on s.grp = c3.grp
where s.parent_corp_id is not null
If this doesn't provide the output you're expecting based on the sample data I used please provide the expected output for the data I used in the DDL on the fiddle.

sql query to return single value based on column it matches

I've a requirement where in if the query string matches column1 , return me 1. If it matches column 2 return 2 else if it matches column 3 return 3.
Table strunctre:
col1 col2 col3
11 12 13
22 23 24
If my query string is 23, then i'm expecting a return value of 2 as it matches col2.
Something like below:
select 1
from table1
where col1=querystring
and orderid=xxx
or select 2 from table1
where col2=querystring
and orderid=xxx
or select 3 from table1
where col3=querystring and orderid=xxx
Basically i'm expecting one query which return single value based on the column it matches.
Is it something doable in SQL as i'm not very good in DB skills.
Any help is highly appreciated.
There are a couple of approaches. If there is a guarantee that no more than one column will match at a time, a UNION will work:
SELECT 1 AS SomeCol
FROM table1
WHERE col1 = querystring
AND orderid = xxx
UNION
SELECT 2
FROM table1
WHERE col2 = querystring
AND orderid = xxx
UNION
SELECT 3
FROM table1
WHERE col3 = querystring
AND orderid = xxx;
If more than one match can happen, another approach is this (note the order of precedence is now col1, col2, col3 etc):
SELECT CASE
WHEN col1 = querystring THEN 1
WHEN col2 = querystring THEN 2
WHEN col3 = querystring THEN 3
END AS SomeCol
FROM table1
WHERE orderid = xxx;
Please try using case
declare #var int
set #var=23
select
case #var when col1 then 1
when col2 then 2
when col3 then 3 end
from YourTable
where
col1=#var OR
col2=#var OR
col3=#var
Try using IF-ELSE condition in your query and check with passed parameter.

SQL Server Select a row for where a column value does not exists

I have a table as
COl1____COL2____COL3
1_________1_____val1
1_________2_____val2
1_________3_____val3
2_________1_____val1
2_________2_____val2
2_________3_____val3
3_________1_____val1
3_________2_____val2
3_________4_____val4
No I want to have unique values from COL1 such that it should only show me COL1 value that does not have a value "3" under COL2
i.e. I want a query to return me
Result
3
Any help is much appreciated
SELECT col1
FROM YourTable
EXCEPT
SELECT col1
FROM YourTable
WHERE col2 = 3
Or
SELECT col1
FROM YourTable
GROUP BY col1
HAVING MAX(CASE
WHEN Col2 = 3 THEN 1
ELSE 0
END) = 0
select col1
from Tab
where col1 not in (select col1 from tab where col2 = 3)