Coalesce in duplicated values - sql

I have a table like this:
And I want to transform for each value a column, to become something like this:
If I do a query like this:
Select "_sdc_source_key_id",
COALESCE(value='Integrity',null) as cia_security
,COALESCE (value='Confidentiality',null) as cia_conf
,COALESCE (value='Availability',null) as cia_availability
FROM
staging_jira.issues__fields__customfield_10420
where _sdc_source_key_id='201496'
That is my result, I have duplicated rows:
What should be the best solution to achieve my transformation?
Thanks a lot!

You can GROUP By "_sdc_source_key_id" and use MAX of your values
Select "_sdc_source_key_id",
MAX(COALESCE(value='Integrity',null)) as cia_security
,MAX(COALESCE (value='Confidentiality',null)) as cia_conf
,MSX(COALESCE (value='Availability',null)) as cia_availability
FROM
staging_jira.issues__fields__customfield_10420
where _sdc_source_key_id='201496'
GROUP BY "_sdc_source_key_id"
If your databse doesn't support MAX from boolean switch to Int
Select "_sdc_source_key_id",
MAX(CASE WHEN value='Integrity' THEN 1 ELSE null END) as cia_security
,MAX(CASE WHEN value='Confidentiality' THEN 1 ELSE null END) as cia_conf
,MSX(CASE WHEN value='Availability' THEN 1 ELSE null END) as cia_availability
FROM
staging_jira.issues__fields__customfield_10420
where _sdc_source_key_id='201496'
GROUP BY "_sdc_source_key_id"

Related

Is there a way to Pivot Oracle SQL results to show 1 unique ID and the non null values in each column?

I am an Oracle SQL beginner and I have an issue with the code below:
SELECT unique_id,
CASE
WHEN type LIKE 'E-%' THEN
'electric'
ELSE
null
END electric_flag,
CASE
WHEN type LIKE 'G-%' THEN
'gas'
ELSE
null
END gas_flag,
CASE
WHEN type LIKE 'W-%' THEN
'water'
ELSE
null
END water_flag,
CASE
WHEN type LIKE 'S-%' THEN
'wastewater'
ELSE
null
END wastewater_flag
FROM (SELECT unique_id, type, end_dt
FROM table
WHERE end_dt IS NULL)
Which gives me the following results:
My goal is to have the results show like this:
It's almost like I want to group the results by the id, ignore rows that are all null, but combine the rows that return with the flag into a single row.
Any help would be greatly appreciated!
Here's a pseudo-code:
SELECT
*
FROM
(
SELECT
unique_id,
CASE type WHEN LIKE 'E-%' THEN 'electric'
WHEN LIKE 'G-%' THEN 'gas'
WHEN LIKE 'W-%' THEN 'water'
WHEN LIKE 'S-%' THEN 'wastewater'
ELSE NULL
END flag
FROM {table}
WHERE end_dt IS NULL
)
PIVOT
(
MAX(flag)
FOR flag IN ('electric' electric_flag, 'gas' gas_flag, 'water' water_flag, 'wastewater' wastewater_flag)
)
Here's an Oracle SQL Fiddle
You're almost there;
Conditionals are needed but they should be aggregated
a Grouping By unique_id clause should be added during the aggregation
a subquery is not needed
using ELSE null cases are redundant
it's suitable to add a ROW_NUMBER() analytic function in order to generate a column with ordinal values
So, use the following SQL Select Statement of Conditional Aggregation :
SELECT ROW_NUMBER() OVER (ORDER BY unique_id) AS id,
MAX(CASE
WHEN type LIKE 'E-%' THEN
'electric'
END) AS electric_flag,
MAX(CASE
WHEN type LIKE 'G-%' THEN
'gas'
END) AS gas_flag,
MAX(CASE
WHEN type LIKE 'W-%' THEN
'water'
END) AS water_flag,
MAX(CASE
WHEN type LIKE 'S-%' THEN
'wastewater'
END) AS wastewater_flag
FROM t
WHERE end_dt IS NULL
GROUP BY unique_id
Demo

How can I use pivot to find the records with the most columns populated?

I have a problem where I have 5 columns.
What I want to do is add a count on the end with the number of columns where there is no null value.
I am trying to use pivot as this seems to be the most logical SQL clause. Any ideas on this? I haven't used Pivot in many instances so this is new for me.
An inline pivot/conditional aggregate and a COUNT seems to be what you want here. As all your columns have different data types, you need to also use some CASE expressions. Something like this:
SELECT ID,
a,
...
(SELECT COUNT(V.C)
FROM (VALUES(CASE WHEN a IS NOT NULL THEN 1 END),
(CASE WHEN b IS NOT NULL THEN 1 END),
(CASE WHEN c IS NOT NULL THEN 1 END),
(CASE WHEN d IS NOT NULL THEN 1 END),
(CASE WHEN e IS NOT NULL THEN 1 END),
(CASE WHEN f IS NOT NULL THEN 1 END))V(C)) AS NonNullColumns
FROM dbo.YourTable;

Is there a way to contruct this kind of result using group by in sql?

I have a table which consists of data where in I'm having trouble counting the corresponding rows.
Here is the sample table:
I am expecting an output like this:
You can do conditional aggregation:
select
sum(case when result = 'X' then 1 else 0 end) count_x,
sum(case when result is null then 1 else 0 end) count_blank
from mytable
I assume that by blank you mean null. If not, then you can change the condition in the second sum() from result is null to result = ''.
If you are running MySQL, this can be shortened a little:
select
sum(result = 'X') count_x,
sum(result is null) count_blank
from mytable

Showing sum of conditions as one row

Hi I have a table like this
select id, value, condition from mytable
result
and I need a query to make it like this
is it possible?
Just use conditional aggregation:
select id,
sum(case when condition = 'true' then value else 0 end) as num_true,
sum(case when condition = 'false' then value else 0 end) as num_false
from t
group by id;
You have tagged the question with both Oracle and SQL Server. Neither supports a boolean type directly in SQL, so I am guessing that the condition is a string.
yes possible
select id "id", sum(decode(condition,'TRUE',value,0)) "sum_of_condition_true",
sum(decode(condition,'FALSE',value,0)) "sum_of_condition_false"
from mytable
group by id
order by id;

SQL Query to add result set values as column values

I want to add the below queries as the column values without creating a table.
Select 'NetworkKey' as AuthKey
Select count(NetworkSK) as Totalcount from EDW.Fact.AuthorizationRequest
Select count(*) as NUllcount from EDW.Fact.AuthorizationRequest where NetworkSK is NULL
Select count(*) as NotNullcount from EDW.Fact.AuthorizationRequest where NetworkSK is Not NULL
My result should look like this without creating a table physically...
AuthKey Totalcount Nullcount NotNullCount
NetworkKey 100 5 95
YOU WANT TO DO IT LIKE THIS BECAUSE THIS WILL WORK TO SOLVE YOUR PROBLEM.
SELECT 'NetworkKey' AS AuthKey,
COUNT(*) AS TotalCount,
SUM(CASE WHEN NetworkSK IS NULL THEN 1 ELSE 0 END) AS NUllcount,
SUM(CASE WHEN NetworkSK IS NOT NULL THEN 1 ELSE 0 END) AS NotNullcount
FROM EDW.Fact.AuthorizationRequest
Happy holidays.