SQL using SUM in CASE in SUM - sql

I had this query select
sum(CASE WHEN kpi.average >= temp.average THEN 1 ELSE 0 END) AS recordOrder,
which worked fine, but I had to change it to this
sum(CASE WHEN sum(kpi.averageUse) / sum(kpi.averageTotal) >= temp.average THEN 1 ELSE 0 END) AS recordOrder,
These queries have to get number of rows, where some value (average) is greater than average from TEMP table. But in the second query I have more accurate data (weighted average).
but I am getting error
1111 invalid use of group function
Any ideas how to write SUM in CASE in SUM?
Thanks!

This code is just non-sensical because you have nested sum functions:
sum(CASE WHEN sum(kpi.averageUse) / sum(kpi.averageTotal) >= temp.average THEN 1 ELSE 0 END) AS recordOrder,
Without seeing your larger query, it is not possible to know what you really intend. But I would speculate that you don't want the internal sum()s:
sum(CASE WHEN (skpi.averageUse / kpi.averageTotal) >= temp.average THEN 1 ELSE 0 END) AS recordOrder,

Related

Sql loop over unique elements of a column

I am trying to get data of rows into columns. This is my data before query.
This is my query.
SELECT
Date,
SUM(CASE WHEN (Terms="Art") THEN 1 ELSE 0 END) AS Art,
SUM(CASE WHEN (Terms="Action") THEN 1 ELSE 0 END) AS Action,
SUM(CASE WHEN (Terms="Board") THEN 1 ELSE 0 END) AS Board,
SUM(CASE WHEN (Terms="Puzzle") THEN 1 ELSE 0 END) AS Puzzle,
SUM(CASE WHEN (Terms="Adventure") THEN 1 ELSE 0 END) AS Adventure,
SUM(CASE WHEN (Terms="Others") THEN 1 ELSE 0 END) AS Others
FROM
__table__
GROUP BY
Date
After Query this is my data.
Which is fine and intended data. But problem is in query as you can see I have to explicitly write each term. I want to automate that using loop. Theoretical solution is to write subquery to get all unique terms and then loop over them. But I don't know how.

How to calculate time

I made a table data like below and I want to filter time with category hour -8 and +8.
I made a query like this but wrong result
select resolved_by, count(*) as total,
count (resolution_time > HOUR (resolution_time -8)) as total_target,
count (resolution_time > HOUR (resolution_time +8)) as total_untarget
from all_ticket_closed
where resolved_by = 'Oktadika.Riptono'
group by resolved_by;
For the result, total_target should be 32 and total_untarget 10. how to query it.
Thanks in advance
Probably you may require CASE expression and SUM function for aggregation
SUM (CASE WHEN resolution_time > HOUR THEN (resolution_time -8) ELSE 0 END) as total_target,
SUM (CASE WHEN resolution_time > HOUR THEN (resolution_time +8) ELSE 0 END) as total_untarget

SQL statement selecting a count of a column

I have this SQL query:
SELECT
COUNT(SDDOCO) AS Total
FROM
KAIPRDDTA.F4211LA, KAIPRDDTA.Dates
WHERE
SDDRQJ = Day10
which returns a count of all the orders for today.
I am trying to get a second column so that I have this:
To get orders that are not completed would be: SDNXTR < '562'. How would I add this to my sql query so that I can accomplish this goal? Let me know if you need anymore information and thanks in advance for your responses.
You have two options here:
SELECT
COUNT(SDDOCO) AS Total,
SUM(CASE WHEN SDNXTR < '562' THEN 1 ELSE 0 END) AS Not_Finished_Yet_With_SUM,
COUNT(CASE WHEN SDNXTR < '562' THEN 1 ELSE NULL END) AS Not_Finished_Yet_With_COUNT,
FROM
KAIPRDDTA.F4211LA, KAIPRDDTA.Dates
WHERE
SDDRQJ = Day10
You can use case statement to count that SDNXTR < '562' values like below:
SELECT count(SDDOCO) as Total,
sum(case when SDNXTR < '562' then 1 else 0 end) as not_yet_finished
FROM KAIPRDDTA.F4211LA, KAIPRDDTA.Dates
WHERE SDDRQJ = Day10

Sum by Distinct Values

I have the below code which brings though a sum of where a day 'GRLastDt' has complete or not complete however I hadn't accounted for duplicated Return IDs. Is there a way to return a sum by day for each Return ID?
For example day 24/5/15 may have 5 Lines to the day which have 'X' in Complete however 2 lines have duplicated return ids 'RtnId' and therefore the sum total would be 4 rather than 5 for 24/5/15.
SELECT
CONVERT(varchar(15), GRLastDt, 111) AS Date_,
SUM(CASE WHEN Complete = 'X' THEN 1 ELSE 0 END) AS Complete,
SUM(CASE WHEN Complete <> 'X' /*and RtnDt <>GRLastDt*/THEN 1 ELSE 0 END) AS Due
FROM [dbo].[vw_AN_Admin_VendorReturns]
WHERE (GRLastDt >= GETDATE() - 30)
GROUP BY CONVERT(varchar(15), GRLastDt, 111),GRLastDt
If I understand your problem right I think you can change this line:
SUM(CASE WHEN Complete = 'X' THEN 1 ELSE 0 END) AS Complete,
to this:
COUNT(DISTINCT CASE WHEN Complete = 'X' THEN RtnId END) AS Complete,
You probably want the same change for the Due calculation.

Query from same table to extract different data

In a single table I have 3 columns. First defines a sector, second count and third amount. I need to extract 5 columns of data in the following manner. First column sector. Second and third to contains the values were amount is less than count and third and four to display were amount is more than count in the specific sectors. How should my query look?
Sample Data - 4 row data for sector one.
1,23,44
1,20,15
1,50,45
1,30,20
Result should be
1,100,80,23,44
You can get it done using a GROUP BY and SUM() aggregate function along with CASE statement like
SELECT sector,
SUM(case when count > amount then count else 0 end) as count1,
SUM(case when amount < count then amount else 0 end) as amount1,
SUM(case when count < amount then count else 0 end) as count2,
SUM(case when amount > count then amount else 0 end) as amount2
FROM mytable
GROUP BY sector;