Nested Select with Count Distinct and Group By - sql

So I'm trying to get 2 sets or results from the same table grouped by a 3rd column, it's best i let my example explain;
SELECT
(SELECT COUNT(DISTINCT id)
FROM Database
WHERE Status NOT LIKE 'closed') AS ColumnA,
(SELECT COUNT(DISTINCT id)
FROM Database
WHERE Status NOT LIKE 'closed' AND Datevalue <= getdate()) AS ColumnB
Group By ColumnC
Now I know this wont/doesn't work but it explains what I want. If I leave the group by out then i get the figures as a whole but i want them grouping by another Column.
Mind is melting, ready to be enlightened.

Is this what you want?
select columnC,
count(distinct case when Status <> 'closed' then id end) as columnA,
count(distinct case when Status <> 'closed' and datevalue <= getdate() then id end) as columnb
from database -- a very curious name for a table
group by ColumnC;

Try this
SELECT ColumnC,
COUNT(DISTINCT CASE WHEN Status NOT LIKE 'closed' THEN id END) as ColumnA,
COUNT(DISTINCT CASE WHEN Status NOT LIKE 'closed' AND Datevalue <= getdate() THEN id END) as ColumnB
FROM mydatabase
GROUP BY ColumnC

Related

Results from multiple queries as one result in Oracle

I have results from same query with multiple conditions as shown below:
SELECT COUNT(DISTINCT CASEID) FROM MYTABLE WHERE YR=2019 AND STATUS IN('W')
SELECT COUNT(DISTINCT CASEID) FROM MYTABLE WHERE YR=2019 AND STATUS IN('K') AND APSTAT='J'
SELECT COUNT(DISTINCT CASEID) FROM MYTABLE WHERE YR=2019 AND STATUS IN('L') AND RCODE='901'
SELECT COUNT(DISTINCT CASEID) FROM MYTABLE WHERE YR=2019 AND STATUS IN('L') AND
RCODE='910'
You could use conditional aggregation:
SELECT
COUNT(DISTINCT CASE WHEN STATUS IN('W') THEN CASEID END)
,COUNT(DISTINCT CASE WHEN STATUS IN('K') AND APSTAT='J' THEN CASEID END)
,COUNT(DISTINCT CASE WHEN STATUS IN('L') AND RCODE='901' THEN CASEID END)
,COUNT(DISTINCT CASE WHEN STATUS IN('L') AND RCODE='910' THEN CASEID END)
FROM MYTABLE
WHERE YR=2019;

aggregation functions with different where condition sentence

I have SQL query give table of 6 columns from
mytable:
TotalNumberOfRecords
TotalDurationOfCalls
AvgdurationPer
TotalCallednumbers
TotalCallednumbers
Ratiocalledtoallcalls
these columns resulted of aggregation functions. but each one have different where condition sentences in order to select from table.
MY Query like this:
select ID,
count(*) as TotalNumberOfRecords,
sum (isnull(cast(duration as int),0)) {where condition1} as TotalDurationOfCalls ,
AVG(isnull(cast(duration as int),0)){where condition2} as AvgdurationPer,
count(distinct IDS) {where condition3} as TotalCallednumbers ,
count(distinct CGI) {where condition4} as TotalOfLocations,
cast(count(distinct IDS) as float)/cast(count(*) as float) {where condition5} as Ratiocalledtoallcalls
from Mytable
group by ID
Now, my problem is, how can I execute this query in one query to get one table?
You want conditional aggregation. You do this by making a case expression the argument to an aggregation function:
select ID, count(*) as TotalNumberOfRecords,
sum(case when condition1 then cast(duration as int) else 0 end) as TotalDurationOfCalls ,
avg(case when condition2 then cast(duration as int) else 0 end) as AvgdurationPer,
count(distinct case when condition3 then IDS end) as TotalCallednumbers,
count(distinct case when condition4 then CGI end) as TotalOfLocations,
cast(distinct count(case when condition5 then IDS end) as float)/cast(count(*) as float) as Ratiocalledtoallcalls
from Mytable
group by ID

Group BY on Condition basis

I have data in following way....
ColumnA ColumnB
7675 22838
7675 24907
7675 NULL
I want the results in following way.....
ColumnA ColumnB
7675 2 (need total count for Not Null value)
7675 0 (need count 0 for NULL value)
SELECT ColumnA, COUNT(ColumnB) ColumnB
FROM YourTable
GROUP BY ColumnA
UNION ALL
SELECT ColumnA, 0
FROM YourTable
WHERE ColumnB IS NULL
GROUP BY ColumnA
You could introduce a calculated column indicating whether ColumnB is null or not and use it as a grouping criterion together with ColumnA:
SELECT
t.ColumnA,
ColumnB = COUNT(t.ColumnB)
FROM
dbo.YourTable AS t
CROSS APPLY
(SELECT CASE WHEN t.ColumnB IS NULL THEN 1 ELSE 0 END) AS x (SubGroup)
GROUP BY
t.ColumnA,
x.SubGroup
ORDER BY
t.ColumnA,
x.SubGroup
;
The COUNT(t.ColumnB) expression would always be NULL for a null subgroup, and for the corresponding non-null subgroup it would return the number of the non-null entries.
select columnA,
count(columnB) as non_null_count,
sum(columnB is null) as null_count
from your_table
group by ColumnA
you could easily do with a count and sum which may be faster if there are a lot of rows rather than selecting all of the rows twice with a UNION
SELECT columna, columnb, SUM(mycount)
FROM
( SELECT *, COUNT(columnb) as mycount
FROM test
GROUP BY columnb
)t
GROUP BY mycount
ORDER BY CASE WHEN mycount = 0 THEN 1 ELSE 2 END DESC;
Fiddle Demo

Duplicate Checks with Multiple Values

I am doing some manual duplicate checks on my database, and have a complicated case.
I need to check for duplicate rows based on a value in Column A, which I have done. However, in this specific case, there might be multiple records that have the same value for Column A but a different value for Column E.
Here is my original query:
SELECT ColumnA, COUNT(*) TotalCount
FROM TableA
INNER JOIN TableA_1 on fID = hID
WHERE dateCreated > '2013-05-08 00:00:00'
GROUP BY ColumnA
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
I now need to filter out duplicates for ColumnA where ColumnE is different, or unique. I have added psuedocode to my original query
SELECT ColumnA, COUNT(*) TotalCount
FROM TableA
INNER JOIN TableA_1 on fID = hID
WHERE dateCreated > '2013-05-08 00:00:00'
AND ColumnE is not unique
GROUP BY ColumnA
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
I hope this makes sense.
You need a GROUP BY clause on a ColumnA column and HAVING clause on DISTINCT ColumnE
SELECT ColumnA, COUNT(*) TotalCount
FROM TableA INNER JOIN TableA_1 on fID = hID
WHERE dateCreated > '2013-05-08 00:00:00'
GROUP BY ColumnA
HAVING COUNT(DISTINCT ColumnE) > 1
ORDER BY COUNT(*) DESC
You could just add ColumnE into the grouping, as shown below:
SELECT ColumnA, ColumnE, COUNT(*) TotalCount
FROM TableA
INNER JOIN TableA_1 on fID = hID
WHERE dateCreated > '2013-05-08 00:00:00'
GROUP BY ColumnA, ColumnE
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC

SQL Select for multiple where clause

I am trying to create SQL Select that returns counts of a certain field based on a field.
So, here is what I am trying to do.
Select count(distinct id) as TotalCount, -- this will be the total of id
count(distinct id where type='A') as TotalA, -- this will be total when type='A'
count(distinct id where type='B') as TotalB -- This will be total when type = 'B'
from MyTable
Basically, TotalCount = TotalA + TotalB.
How can I achieve this in SQL Select Statement?
Thanks.
Select count(distinct id) as TotalCount, -- this will be the total of id
count(distinct case type when 'A' then id else NULL end) as TotalA,
count(distinct case type when 'B' then id else NULL end) as TotalB
from MyTable;
Of course TotalCount may or may not be TotalA + TotalB, depending on the actual data.
You can do it like that:
SELECT
count(distinct id) as TotalCount,
sum(CASE WHEN type = 'A' THEN 1 ELSE 0) as TotalA,
sum(CASE WHEN type = 'B' THEN 1 ELSE 0) as TotalB,
FROM
MyTable
Count per type:
SELECT
type,
count(DISTINCT id)
FROM
MyTable
GROUP BY
type
Why not simply UNION the separate queries.
Select 'all' as which, count(distinct id) as Total from mytable
union
select 'a' as which, count(distinct id) where type='A' as Total from mytable
union
select 'b' as which, count(distinct id) where type='B' as Total from mytable