Filter based on all columns - sql

I have a table with several columns and each column can have a value of 0 OR 1 ..I just want to eliminate the rows where all the columns have 0 .. I just want to consider the rows if at least one column have a value of 1

SELECT *
FROM A_TABLE_YOU_DID_NOT_NAME
WHERE col1 + col2 + ... + colN >= 1
Will not return all 0 and at least one of them will be 1.

IN SQL Server, you can make use of OR:
SELECT *
FROM table
WHERE [column1] = 1 OR [column2] = 1...

Related

SQL getting the minimum out of calculation of 3 values

I have multiple rows that each have 3 columns and what I want to do is calculate these 3 columns at each row and see which row has the lowest sum
for example:
row 1 - col1(1)+col2(3)+col3(0) = 4
row 2 - col1(50) + col2(3) + col3(4) = 57
row 3 - col1(2) + col2(0) + col3(0) = 2
the selected row (row 3) will show me the calculated value = 2
currently I have been calculating only by 1 column but now I need 3
this is what I was doing:
select min(Bonus) from dbo.Bonuses AS c1
my table (Bonuses)
ID Bonus Double Bonus Master Bonus
1 1 3 0
2 50 3 4
3 2 0 0
expected results:
the row 3 is the minimum in value will print
2
you can do it with:
select min(col1 + col2 + col3) from table;
col1 + col2 + col3 - calculate sum of every line (row) of the table;
min(col1 + col2 + col3) - determine minimal value.
In your case:
select min("Bonus" + "Double Bonus" + "Master Bonus") from "Bonuses";
You shoul get the minimum among columns first, for each row, then get the minimum from result column:
select min(least(col1, col2, col3)) from yourTable;
Just in case, you should know that NULL values could have an unexpected result. The safer calculation is:
select min(coalesce(col1, 0) + coalesce(col2, 0) + coalesce(col3, 0))
The issue with NULL values is that the + returns NULL if any values are NULL. That means that all values from that row would be ignored.
The sample data in the question doesn't have NULL values. But it also doesn't specify that the values are never NULL, so I think this is important to point out.

Returning the maximum of non-null columns ​from a table

I have a table with 4 columns, one of which is a non-null column, all others can be given a null value.
I just want a SELECT that returns results that have as many non-null columns as possible.
For example:
If I have 5 records in my Table, 1 of those records will have 3 columns with data, 2 of them will have 2 columns with data and 2 of them will have only 1 column with data. In my select, I want as a result only the first option: to bring me 3 columns with data. But it can be just 2 columns with data, it will be dynamic according to the table to be updated.
I'm using Oracle SQL.
One option is a conditional sort based on the count of non-null values in each row, and then a row-limiting clause (available since Oracle 12c). Assuming that the 3 nullable columns are col1, col2 and col3, that would be:
select *
from mytable
order by
case when col1 is null then 0 else 1 end
+ case when col2 is null then 0 else 1 end
+ case when col3 is null then 0 else 1 end desc
fetch first row with ties
If you are running an older version of Oracle, you can get the same result with window function rank():
select *
from (
select
t.*,
rank() over(order by
case when col1 is null then 0 else 1 end
+ case when col2 is null then 0 else 1 end
+ case when col3 is null then 0 else 1 end desc
) rn
from mytable t
) t
where rn = 1

How to get count values for multiple values in one column

My data is like this
Name Values
A Val1
A Val1
A Val2
A Val2
A Val2
B Val1
B Val2
I want to ouput my data is this way
Name Val1Count Val2Count
A 2 3
B 1 1
I can get the Name and count(*) for Val1 with this query.
select [Name],count(*) FROM [table1]
where [values]='Val1'
group by [Name]
But I am not sure how to get the count(*) for val2 in the same row.
I tried doing this, but looks like this is not supported
select [name],#val1count= (above query for val1), #val2count = (above query for val2)
Please help. Thanks for looking.
This is called pivoting. Some databases provide a PIVOT function. However, you can also do this manually.
SELECT [Name],
SUM ( CASE WHEN [Values]='VAL1' THEN 1 ELSE 0 END ) AS Val1Count,
SUM ( CASE WHEN [Values]='VAL2' THEN 1 ELSE 0 END ) AS Val2Count
FROM [table1]
GROUP BY [Name]
Explanation:
The CASE WHEN ... END gives each row a "boolean" value for whether or not the row matches your condition.
The SUM ( ... ) counts the number of rows which returned "true" (or 1).
The GROUP BY [Name] consolidates the rows down to one row per distinct name.
If you add conditions to a WHERE clause, the CASE WHEN will only see the rows matching your WHERE conditions.

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.

Check any 4 out 10 conditions are satisfied in SQL

I apologize for asking a very vague question but here it is.
I have to write a SQL query in SQL Server as follows.
I have a table say tblA having 10 columns from col1, col2,.....col10.
Each column is not null and definitely holds some value and all of type int.
The query should be to select all such records in which at least 4 columns are matching with given filter criteria, where the filter criteria has values for all 10 columns.
I googled dint get a clue. It needs to be done in SQL server and single query.
Please suggest.
Thanks in advance.
SELECT *
FROM
yourtable
WHERE
case col1 when #value1 then 1 else 0 end +
case col2 when #value2 then 1 else 0 end +
...
case col10 when #value10 then 1 else 0 end
>=4
You can use CASE expressions to determine if four or more columns match:
SELECT *
FROM YourTable
WHERE CASE WHEN Col1 = Filter1 THEN 1 ELSE 0 END +
CASE WHEN Col2 = Filter2 THEN 1 ELSE 0 END +
....
CASE WHEN Col10 = Filter10 THEN 1 ELSE 0 END >= 4
You can do something like this:
select *
from (select t.*,
(case when col1 <whatever> then 1 else 0 end) as col01_matches,
(case when col2 <whatever> then 1 else 0 end) as col02_matches,
. . .
from t
) t
where (col1_matches + col2_matches + col3_matches . . .) >= 4
This creates a separate indicator variable for each match. You could also do the sum in the subquery, in a single variable. I would prefer to have each match separately, just in case the logic gets more complicated or I want to see what matches.