how to know which column is matched when filter the records by multiple columns compare - sql

I have a table "table1" like
NAME COL1 COL2 COL3 COL4
A 1 2 3 4
I want to filter the table by
"SELECT * FROM table1 where (COL1=1 OR COL2=1 OR COL3=1 OR COL4=1)"
I expect the result is
NAME COL_NAME
A COL1
Is that possible by SQL statement?

You are not limited to selection of columns in your select list, you can use expressions. For example, you can use this:
SELECT
NAME, -- More columns as needed
CASE
WHEN COL1=1 THEN 'COL1'
WHEN COL2=1 THEN 'COL2'
WHEN COL3=1 THEN 'COL3'
WHEN COL4=1 THEN 'COL4'
ELSE NULL END AS COL_MATCH
FROM table1 where (COL1=1 OR COL2=1 OR COL3=1 OR COL4=1)

Related

Count number of entries where Col1 has multiple entries with different data in Col2

I have a table with two Columns, Col1 and Col2. I want to retrieve only the data when the Col1 has multiple entries with different data in Col2.
For example, I want to retain:
Col1 Col2 Col1 Col2
AB-123456 AP-321654 AB-123456 AP-321654
AB-123456 AP-321789 AB-123456 AP-321789
AB-123456 AC-321456 AB-123456 AC-321456
AB-951357 AP-989898 AB-456851 AP-110211
AB-753159 AC-956854 AB-456851 AP-110279
AB-456851 AP-110211
AB-456851 AP-110279
I created through the Report feature a report that groups by Col1 and creates a subcount on the number of entries in Col2. Using the report, I seem to be having a problem with using the subtotal counter. If the subtotal counter is > 1, I want to report, otherwise skip and go to the next Col1 data.
My next option was to write VBA code to read through the table and output the multiples to a new data, then I could run that data through the report to format, etc. To count the multiples and use criteria, I thought perhaps the DCount function would work. I have tried different variations, but to no avail.
Ex:
NUM_OF_MULTS = DCount("Col1", "TBL_Of_Col1_Col2", Current_Col2 = Prev_Col2)
For Index1 = 1 to NUM_OF_MULTS ......
I tried different criteria, but it is either all the records or none.
I think the following should perform as you require:
select t1.* from YourTable t1
where exists (select 1 from YourTable t2 where t1.col1 = t2.col1 and t1.col2 <> t2.col2)
Change both occurrences of YourTable to suit your table name.
For every record, this uses a correlated subquery to test whether another record exists in the dataset with the same col1 value as the current record, but a different col2 value, thus meeting your criteria.
The use of select 1 is purely for optimisation: we don't care what data is returned by the subquery, only that it has records.
If you want to do it with SQL code you can use EXISTS:
select t.* from tablename as t
where exists (
select 1 from tablename
where Col1 = t.Col1 and Col2 <> t.Col2
)
and another option with the IN operator:
select * from tablename
where Col1 in (
select Col1
from (select distinct Col1, Col2 from tablename)
group by Col1
having count(*) > 1
)

First column with NULL value in a table

I would like to get to know how I can get the first column with NULL value from the left for each row in my table, I've tried with SELECT CASE but it doesn't work the way I would like.
Guys, I'd like to be crystal-clear about what I want to accomplish. I have a table with 22 columns and there are rows in which last 10 columns have NULL values but I need to get to know only a name of the first column from the left with NULL value.
You get the value from the first non-NULL column using coalesce():
select coalesce(col1, col2, col3, . . .)
You can get the name using case logic:
select (case when col1 is not null then 'col1'
when col2 is not null then 'col2'
. . .
end)
Just specify NULL as your first field selection.
SELECT NULL, FieldA, FieldB, FieldC etc
FROM yourtable
The only general approach here is case statement:
Case
when col1 is null then 'col1'
when col2 is null then 'col2'
when col3 is null then 'col3'
end as frst_null
This way frst_null would contain the name of the first column containing Null value. You can order columns whichever order you like.

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
??

Get record with a field egal to a specific value else with null

I want in SQL : if a field is equal to a specific value, I want this record if not I want the record with this field equal to null.
I can try to that:
SELECT TOP 1 COL1, COL2, COL3
FROM TABLE1 WHERE (COL2 = MY_SPECIFIC_VALUE OR COL2 IS NULL) AND COL3 = '42'
AND COL1 = 3
But, what is the result returned? The smallest id? Or it is not specified?
Assuming that this is the real question:
If a field is equal to a specific value, I want this record if not I want the record with this field equal to null.
You can do this as:
SELECT TOP 1 COL1, COL2, COL3
FROM TABLE1
WHERE (COL2 = MY_SPECIFIC_VALUE or COL2 IS NULL) AND
COL3 = '42' AND COL1 = 3
ORDER BY (CASE WHEN COL2 = MY_SPECIFIC_VALUE THEN 1 ELSE 2 END);

Is there a SQL Statement that allows me to copy and insert existing rows but with one column change?

You might not understood what I want to ask from the title but ,here is the explanation.
I have a data in Oracle database table. What I wanted to do is insert a new data to the table. This new data is based on the existing data but I have to change the value of one columns. So if I have 10 rows in the database after the insertion i will have 20 rows but the new 10 rows contain the same data except on of the columns is changed.
E.g table before insertion a new data
Col1 Col2 Col3
a b AA
1 2 33
table after insertion a new data
Col1 Col2 Col3
a b **BB**
1 2 **44**
Provided that you can encode what the new value should be; yes.
INSERT INTO
myTable (
Col1,
Col2,
Col3
)
SELECT
Col1,
Col2, -- This is a specific example based on your comment.
Col3 + 6 -- This just adds 6 to the existing value, but any SQL
FROM -- could actually go here, such as a CASE statement...
myTable
So, the question becomes; Do you have rules that you can implement in SQL for calculating the new value for Col3?
The rules could be something basic like...
CASE WHEN Col3 = 'AA' THEN '**BB**'
WHEN Col3 = '33' THEN '**44**'
ELSE 'Unknown'
END,
Or you could have all the new values in another table and look them up using a join...
INSERT INTO
myTable (
Col1,
Col2,
Col3
)
SELECT
OldTable.Col1,
OldTable.Col2,
COALESCE(NewTable.Col3, 'Unknown')
FROM
myTable AS OldTable
LEFT JOIN
lookup AS NewTable
ON OldTable.Col1 = NewTable.Col1
AND OldTable.Col2 = NewTable.Col2
Or a whole bunch of other options.
It will depend on how you determine how to change the data. How do you know, for example, that AA should become BB or that 33 should become 44?
Something like this will work for the two cases you posted. You can adapt it to whatever rule you want by changing the CASE statement to compute the new value differently.
INSERT INTO table_name( col1, col2, col3 )
SELECT col1,
col2,
(CASE WHEN col3 = 'AA'
THEN 'BB'
WHEN col3 = '33'
THEN '44'
ELSE null
END)
FROM table_name;