Use of sum and count inside a case statement in sql - sql

select req.code ,res.code,
case
(
when (req.code==res.code) then 'pass'
when (req.code<>res.code) then 'fail'
/*....2 more case 'when' 'then' statements here*/
end ) result,
req.country ,res.country,
case (when then staments as above)result,
/*.......case stmts upto 70 statemnts*/
from requesttable req full outer join responsetable res on
req.id=res.id
and ....some conditions.
Can anyone tell me how can I sum every column and display the sum as well as the count of records in every column of both tables simultaneously and display count in my query?
My result should be of this sort
code code1 result sum sum1 equivalence country country1 result1 sum sum1
100 100 pass 200000 25000 fail ind aus fail 800000 800000
equivalence
pass
I am trying to prepare a report joining two tables. I am using multiple case statements to accomplish this. I want to display sum of each column and count of each column of both the tables together in a single report. The query that I have is of the following type.

I think this is kind of what you're looking for. For the code and country values displayed on the line it would give you the pass and fail accounts for the combinations displayed and you would have uniqueness on the columns the aggregate is defined on.
Select req.code,
res.code,
Sum(Case When req.code = res.code) Then 1 Else 0 End) As [Pass],
Sum(Case When req.code <> res.code) Then 1 Else 0 End) As [Fail],
req.country,
res.country,
Sum(Case When req.country = res.country) Then 1 Else 0 End) As [Pass],
Sum(Case When req.country <> res.country) Then 1 Else 0 End) As [Fail]
From requesttable req
Full Outer Join responsetable res
On req.id = res.id
Where ...
Group By req.code,
res.code,
req.country,
res.country

Related

Count the occurrences of a given list of values in a column using a single SQL query

I would like to get the count of occurrences of a given list of values in a column using a single SQL query. The operations must be optimised for performance.
Please refer the example given below,
Sample Table name - history
code_list
5lysgj
627czl
1lqnd8
627czl
dtrtvp
627czl
esdop9
esdop9
3by104
1lqnd8
Expected Output
Need to get the count of occurrences for these given list of codes 627czl, 1lqnd8, esdop9, aol4m6 in the format given below.
code
count
627czl
3
esdop9
2
1lqnd8
2
aol4m6
0
Method I tried in show below but the count of each input is shown as a new column using this query,
SELECT
sum(case when h.code_list = 'esdop9' then 1 else 0 end) AS count_esdop9,
sum(case when h.code_list = '627czl' then 1 else 0 end) AS count_627czl,
sum(case when h.code_list = '1lqnd8' then 1 else 0 end) AS count_1lqnd8,
sum(case when h.code_list = 'aol4m6' then 1 else 0 end) AS count_aol4m6
FROM history h;
Note - The number inputs need to be given in the query in 10 also the real table has millions of records.
If i properly understand you need to get the count of occurrences for the following codes: 627czl, 1lqnd8, esdop9.
In this case you can try this one:
SELECT code_list, count(*) as count_
FROM history
WHERE code_list in ('627czl','1lqnd8','esdop9')
GROUP BY code_list
ORDER BY count_ DESC;
dbfiddle
If you need to get the count of occurrences for all codes you can run the following query:
SELECT code_list, count(*) as count_
FROM history
GROUP BY code_list
ORDER BY count_ DESC;
you can try to use GROUP BY
Something like this
SELECT code_list, COUNT(1) as 'total' ROM h GROUP by code_list order by 'total' ;

Using a case statement to show the count of two types of values in a column

SELECT
qt.name,
CASE
WHEN qr.isfinished = 0 THEN COUNT(qr.resultid)
END AS 'Attempted',
CASE
WHEN qr.isfinished = 1 THEN COUNT(qr.resultid)
END AS 'Completed'
Need it to show attempted and completed values on the same row
Name attempted Completed
--------------------------------
Algebra I 114 NULL
Algebra II 47 NULL
ASVAB 55 NULL
Algebra I NULL 69
Algebra II NULL 55
ASVAB NULL 84
Thank you for the help!
If isfinished is bit, you can't aggregate on it.
And the CASE goes inside the COUNT
SELECT qt.name,
count(Case when qr.isfinished = 0 THEN 1 END) as 'Attempted',
count(Case when qr.isfinished = 1 THEN 1 END) as 'Completed'
FROM
...
GROUP BY
qt.name
There are multiple ways you could do this, for example you could do this with joins, or you can use group by -- like so:
SELECT
qt.name,
SUM(CASE qr.isfinsihed WHEN 1 THEN 1 ELSE 0) AS 'Attempted',
SUM(CASE qr.isfinished WHEN 0 THEN 1 ELSE 0) AS 'Completed'
FROM -- what ever your from clause is, it goes here --
GROUP BY
qt.name
In order to have them on the same row, you will need to group by what they have in common. From what you have given in the question, I am assuming that is the qt.name.
Next, you can use the SUM aggregate to get each field count. All of the records that meet the criteria for each item count towards the sum, the others don't. You can also use count with 1's and Null's, I prefer using Sum because it can allow for weighted totals if I need them.

How to make a histogram using sums of several columns in SQL

I often make simple count based histograms with SQL 2012 using case statements, like this:
SELECT
count (CASE WHEN a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]>=0 AND a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]< 5000000 THEN 1 END) as '5M',
count (CASE WHEN a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]>=5000000 AND a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]< 7500000 THEN 1 END) as '7.5M',
count (CASE WHEN a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]>=7500000 AND a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]< 10000000 THEN 1 END) as '10M',
count (CASE WHEN a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]>10000000 THEN 1 END) as '10M+'
FROM [DBNAME].[dbo].[tableone] a inner join [DBNAME].[dbo].[tabletwo] b
on a.IDfield=b.IDfield
Where b.psc=4
But now I need to make a histogram of sums, and I can't figure out how to sum these values by bin, rather than count them. This is what I have tried:
SELECT
CASE WHEN a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]>=0 AND a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]< 5000000 THEN sum(a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]) END as '5M',
CASE WHEN a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]>=5000000 AND a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]< 7500000 THEN sum(a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]) END as '7.5M',
CASE WHEN a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]>=7500000 AND a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]< 10000000 THEN sum(a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]) END as '10M',
CASE WHEN a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]>10000000 THEN sum(a.[Val1]+a.[Val2]+a.[Val3]+a.[Val4]) END as '10M+'
FROM [DBNAME].[dbo].[tableone] a inner join [DBNAME].[dbo].[tabletwo] b
on a.IDfield=b.IDfield
Where b.psc=4
But that throws a 8120 error - Column 'Val1' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Could someone point me in a direction as to how to proceed with this?

nested SQL queries on one table

I am having trouble formulating a query to get the desired output.
This query involves one table and two columns.
First column bld_stat has 4 different values Private, public, Public-Abandoned, Private-Abandoned the other column bld_type, single_flr, multi_flr, trailer, Whs.
I need to get results that look like this:
So far I can get the first two columns but after that I have not been able to logically get a query to work
SELECT bld_stat, COUNT(grade) AS single_flr
FROM (SELECT bld_stat,bld_type
FROM bld_inventory WHERE bld_type = 'single_flr') AS grade
GROUP BY bld_stat,bld_type,grade
The term you are going for is pivoting. I think this should work...no need for the subquery, and I've changed your group by to only bld_stat
SELECT bld_stat,
sum(case when bld_type = 'singl_flr' then 1 else 0 end) AS single_flr,
sum(case when bld_type = 'multi_flr' then 1 else 0 end) AS multi_flr,
sum(case when bld_type = 'trailer' then 1 else 0 end) AS trailer,
sum(case when bld_type = 'whs' then 1 else 0 end) AS WHS
FROM bld_inventory
GROUP BY bld_stat

How to COUNT column values differently in a VIEW?

I have a column in Datablase Table, Suppose its Observation which contains three types of values
Positive
Negative
NULL
Now I want to count the Total no of rows , Total Positive and Total Negative and some other columns. I can not use Where clause here. And its a view
So result should be like
Total Positive Negative SomeOtherCoulumn
255 80 120 Test1
315 135 65 Test2
I tried to use SQL COUNT here but could not get the desired results.
SELECT
COUNT(*) AS Total,
SUM(CASE WHEN Observation = 'Positive' THEN 1 ELSE 0 END) AS Positive,
SUM(CASE WHEN Observation = 'Negative' THEN 1 ELSE 0 END) AS Negative,
SomeOtherColumn
FROM your_view
GROUP BY SomeOtherColumn
There's an interesting technique of summing a case expression like so:
sum(case when Observation = 'Positive' then 1 else 0 end) 'TotalPositive'
The rest is easy.