sql query to return single value based on column it matches - sql

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.

Related

Query with 'OR' condition returns no records

I tried to get some records from PostgreSQL DB but it returns no records when I add OR condition.
e.g.:
select * from table where col1 = 'A'
returns: 5k records
select * from table where col1 = 'A' and col2 <> 1
returns: 3k records
select * from table where (col1 = 'A' and col2 <> 1) or (col1 = 'B')
returns: 0 records
Please advise what am I doing wrong?

insert select alias value into column

Not sure if this is the best title, but i want to select string values into an int column of a new table (the reason is to use keys with int data types rather than strings, so there are more columns not shown in this example)
table1.key1 table2.key2
a 1
b 2
c 3
a 1
one way i can do this is as follows but the syntax is very very long in some scenarios
insert into table2 (key2)
select 1
from table1
where key1 = 'a'
insert into table2 (key2)
select 2
from table1
where key1 ='b'
etc...
can someone show me how i could use a syntax that is shorter? also i have to keep identity insert set to off so an update statement will not work from what i understand.
SQL Fiddle Demo
Use a CASE expresion
insert into table2 (key2)
select CASE WHEN key1 = 'a' THEN 1
WHEN key1 = 'b' THEN 2
WHEN key1 = 'c' THEN 3
.....
ELSE -1
END as key2
from table1

Sql: How to properly check if a record exists that matches 3 column

I have read through the forum but i could not find any example or answer for check existing record for multiple column.
Question:
To check if an entry exist if match 3 column. There is 9 column in the table if 2 column entry match are not counted as existing record.
SELECT COUNT(id) AS existing_row_count FROM tablename WHERE col1 = ? AND col2 = ? AND col3 = ?
If existing_row_count returns greater than zero then we can say that there is at least one match. You can try this also:
SELECT id FROM tablename WHERE col1 = ? AND col2 = ? AND col3 = ?
Thus, you can get the matching row id numbers. And if we combine those two queries:
SELECT id, COUNT(id) AS existing_row_count FROM tablename WHERE col1 = ? AND col2 = ? AND col3 = ?

how to select the records whose several fields' combination will equal to a specific value

Assume I have the following style table, col1 col2 and col3 have same value scopes, I want to select the records when two of the 3 columns have a value combination such as ('ab' and 'bc'), in the following example, the first 3 records should be selected. Any good way to do this? I am using Sybase.
| id | col1 | col2 | col3 |
1 ab bc null
2 null ab bc
3 ab ab bc
4 de ab xy
Thanks.
I don't have Sybase to check, but you can Try this:
select * from Table where (col1 = "ab" or col2 = "ab" or Col3 = "ab")
and (col1 = "bc" or col2 = "bc" or Col3 = "bc")
I agree that the answer given here is totally acceptable, however I feel that if there were more than a few columns that needed to be evaluated, nesting the comparisons in the WHERE clause could become a bit cumbersome and illegible. I had to overcome a similar problem and I found that the following technique was quite helpful (I have adapted it to solve the issue listed above). Note that the evaluation of Total in the outer query can be easily adapted to increase or decrease the number of columns that contain the criteria being evaluated:
SELECT *
FROM
(SELECT id
, col1
, col2
, col3
, SUM(CASE WHEN UPPER(col1) IN ('AB', 'BC') THEN 1 ELSE 0 END
+ CASE WHEN UPPER(col2) IN ('AB', 'BC') THEN 1 ELSE 0 END
+ CASE WHEN UPPER(col3) IN ('AB', 'BC') THEN 1 ELSE 0 END) as Total
FROM <table>
GROUP BY id
, col1
, col2
, col3) as results
WHERE Total >= 2
ORDER BY id

How to choose a columns of a select statement based on conditions?

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.