SQL Grouping with a logic check on another column? - sql

Lets say that I have the following table:
id param result
1 AAA TRUE
1 AAA FALSE
1 AAA FALSE
1 BBB TRUE
1 CCC TRUE
2 AAA TRUE
2 CCC TRUE
3 AAA FALSE
3 AAA FALSE
3 AAA FALSE
3 CCC TRUE
3 BBB TRUE
Is there a way to group by the [param] column so that the [result] will show TRUE if any of the corresponding results are TRUE and show FALSE if none are TRUE? Below is an example of what I am aiming for:
id param result
1 AAA TRUE
1 BBB TRUE
1 CCC TRUE
2 AAA TRUE
2 CCC TRUE
3 AAA FALSE
3 CCC TRUE
3 BBB TRUE

Here is a sketch of code that will work. You just need to put in the right constants for result:
select id, param,
(case when sum(case when result = TRUE then 1 else 0 end) > 0
then TRUE else FALSE
end)
from table t
group by id, param;
SQL Server does not have a native boolean type and doesn't understand "true" and "false". For some representations (such as 'TRUE' and 'FALSE'), the following will work:
select id, param, max(result) as result
from table t
group by id, param;

Related

How to count and group not null elements in SQL Server?

I have a table where each row represents a user and each column represents a service that the customer may have hired. I need to count how many customers hired 1 service, how many hired 2 and so on. It doesn't matter what service you hire. And there is no identifier column.
In Python I was able to do this with
result = services.count(axis = 1).value_counts()
result = pd.DataFrame(result, columns = ['n_clients'])
where 'result' is the csv with the database.
The result, in this case, are like:
n_client
1
928459
2
280235
3
53731
4
16042
Edit: an example of the database to clarify:
product1
product2
product3
product4
True
True
True
True
True
True
True
True
True
True
True
In this case, the result should look like:
n_client
1
4
2
2
3
1
4
0
It looks like you want to just calculate how many products per row, then group by that number
SELECT
v.CountProducts
n_client = COUNT(*)
FROM YourTable
CROSS APPLY (VALUES (
CASE WHEN product1 = 'True' THEN 1 ELSE 0 END +
CASE WHEN product2 = 'True' THEN 1 ELSE 0 END +
CASE WHEN product3 = 'True' THEN 1 ELSE 0 END +
CASE WHEN product4 = 'True' THEN 1 ELSE 0 END
) ) v(CountProducts)
GROUP BY
CountProducts;

T-SQL - Flagging Common Values in One Column Based on ID and updating the database

Problem Statement:
There are Two Columns, VariableName and ID. I want to map variables in the same column with a two-character alpha code such as 'aa', 'ab', 'ac', 'ad'.... up to 'zz' in T-SQL.
To Illustrate,
Input Data:
VariableName ID MapFlag isMapped
var_1 1 NULL FALSE
var_2 1 NULL FALSE
var_3 1 NULL FALSE
var_4 1 NULL FALSE
var_5 1 NULL FALSE
var_1 2 NULL FALSE
var_2 2 NULL FALSE
var_3 2 NULL FALSE
var_10 2 NULL FALSE
var_11 2 NULL FALSE
var_1 3 NULL FALSE
var_10 3 NULL FALSE
var_3 3 NULL FALSE
var_7 3 NULL FALSE
var_1 4 NULL FALSE
var_2 4 NULL FALSE
var_4 4 NULL FALSE
Desired Output
VariableName ID MapFlag isMapped
var_1 1 aa TRUE
var_2 1 ab TRUE
var_3 1 ac TRUE
var_4 1 ad TRUE
var_5 1 NULL FALSE
var_1 2 aa TRUE
var_2 2 ab TRUE
var_3 2 ac TRUE
var_10 2 ae TRUE
var_11 2 NULL FALSE
var_1 3 aa TRUE
var_10 3 ae TRUE
var_3 3 ac TRUE
var_7 3 NULL FALSE
var_1 4 aa TRUE
var_2 4 ab TRUE
var_4 4 ad TRUE
As you can see above,
"var_1" , is present in ids 1, 2, 3, 4 and is tagged as 'aa'
"var_2" , is present in ids 1, 2 and 4 and is tagged as 'ab'
"var_3" , is present in ids 1, 2 and 3 and is tagged as 'ac'
"var_4" , is present in ids 1, and 4 and is tagged as 'ad'
"var_5" , is present in id 1 BUT IS NOT PRESENT IN THE REST OF THE
IDs, so hence NULL
"var_7" , is present in id 3 BUT IS NOT PRESENT IN THE REST OF THE
IDs, so hence NULL
"var_10" , is present in ids 2 and 3 and is tagged as 'ae'
"var_11" , is present in id 2 BUT IS NOT PRESENT IN THE REST OF THE
IDs, so hence NULL
To summarize: Essentially, I want to create a mapping flag with the pattern above 'aa', 'ab', 'ac'
among different IDs. If the mapFlag is NOT NULL, then isMapped TRUE else FALSE
Lastly, I want to write an "Update" stored procedure to convert the input "MapFlag" column with the Input Data and then update the table with the Output data
This is the query to get var marks.
select VariableName, char(ASCII('a')+rn/(ASCII('z') - ASCII('a') +1)) + char(ASCII('a') -1 + rn%(ASCII('z') - ASCII('a') +1)) y
from (
select VariableName, row_number() over (order by VariableName) rn
from mytable
group by VariableName
having count(distinct id) > 1
) t

how can i get true\false output in sqlserver

i have this values in my table:
id
-----
1
2
null
3
4
null
5
Now I want to run a command similar to the following command:
select id==null from table
And the output should be as follows:
id
-----
false
false
true
false
false
true
false
How do?
You can use CASE expressions:
SELECT id,
CASE WHEN id IS NULL THEN 'true' ELSE 'false' END AS id_Description
FROM dbo.Table1

Split single column to multiple

My table look like this:
id customer_code comments
-----------------------------------
1 Dilbert true
2 Dilbert false
3 Wally true
4 wally true
5 wally false
I need to results look like this:
id customer_code x1 x2
------------------------------
1 Dilbert ture
2 Dilbert false
3 wally ture
4 wally ture
5 wally false
SELECT [id], [customer_code],
CASE [comments] WHEN 'TRUE' THEN 'TRUE' ELSE NULL END AS X1,
CASE [comments] WHEN 'FALSE' THEN 'FALSE' ELSE NULL END AS X2
FROM Table1;
FIDDLE

SQL Server - SELECT DISTINCT depending on another column value

I got a table like this, and there's on input parameter RevParam:
Key Rev IsCurrent
=====================
abc 0 TRUE
def 0 TRUE
ghj 2 FALSE
ghj 0 TRUE
klm 0 TRUE
def 1 FALSE
abc 1 FALSE
abc 2 FALSE
Where I want Key to be a unique value. If RevParam is set all rows with that Rev value should be viewed, the missing Key should have: revision 0. Like this:
Result if RevParam = 1
abc 1 FALSE
def 1 FALSE
ghj 0 TRUE
klm 0 TRUE
Result if revisionParameter = 0
abc 0 TRUE
def 0 TRUE
ghj 0 TRUE
klm 0 TRUE
This has been buggering me all day, please help me out!
WITH records
AS
(
SELECT [key], [rev], [IsCurrent],
ROW_NUMBER() OVER(PARTITION BY [key]
ORDER BY CASE WHEN [rev] = 1 -- <<== change REV value here
THEN 0 ELSE 1 END) rn
FROM tableName
WHERE [Rev] IN (1,0)
)
SELECT [key], [rev], [IsCurrent]
FROM records
WHERE rn = 1
SQLFiddle Demo for 1
SQLFiddle Demo for 0
Result if RevParam = 1
abc 1 FALSE
def 1 FALSE
ghj 0 TRUE
klm 0 TRUE
it has rev value 0 as well as 1. Please specify clearly