Average and case in SQL Server 2012 - sql

I'd like to have the average of a column when its bigger than zero.
Select Avg(Case when Column > 0 then Column else 0 end) as Avg
but I'm afraid the else clause is not correct. I want to ignore the zero values in the average.

Remove else part from case statement so the values less than 1 will be NULL.
Null values will be eliminated by the Avg aggregate. So you will get the average of values which are greater then 0. Try this.
Select Avg(Case when [Column]>0 then [Column] end) as [Avg]
DEMO
Without else part in case statement (Expected Average)
SELECT Avg(CASE WHEN a > 0 THEN a END) [Avg]
FROM (SELECT 2 a UNION ALL SELECT 2 UNION ALL SELECT -1) bb
Result : 2
With else part in case statement.
SELECT Avg(CASE WHEN a > 0 THEN a ELSE 0 END) [Avg]
FROM (SELECT 2 a UNION ALL SELECT 2 UNION ALL SELECT -1) bb
Result : 1

Related

How to aggregate unioned tables with count and dummy values?

I am counting in two tables some stuff and want one aggregated result (one row). I write this SQL for this purpose:
SELECT sum (Amount_New) Amount_New,
sum(Import_Dropout) Import_Dropout,
sum(Import)Import,
sum(Processing)Processing,
sum(Processing_Dropout)Processing_Dropout,
sum(Matching)Matching,
sum(Matching_Dropout)Matching_Dropout,
sum(Export)Export,
sum(Exported)Exported,
sum(Rejected)Rejected,
sum(AmountSubTotal)AmountSubTotal,
sum(AmountTotal)AmountTotal
FROM (
SELECT COUNT(CASE WHEN ProcessStatus='_New' THEN 1 ELSE null END) AS Amount_New,
COUNT(CASE WHEN ProcessStatus='Import_Dropout' THEN 1 ELSE null END) AS Import_Dropout,
COUNT(CASE WHEN ProcessStatus='Import' THEN 1 ELSE null END) AS Import,
COUNT(CASE WHEN ProcessStatus='Processing' THEN 1 ELSE null END) AS Processing,
COUNT(*) AS AmountTotal,
0 as Processing_Dropout,
0 as Matching,
0 as Matching_Dropout,
0 as Export,
0 as Export_Dropout,
0 as Exported,
0 as Rejected,
0 as AmountSubTotal,
0 as UnionOrder
FROM "fileimport$marketscanimportcsv"
WHERE ft_boekdat like '%2018%'
UNION
SELECT 0 AS Amount_New,
0 AS Import_Dropout,
0 AS Import,
COUNT(CASE WHEN ProcessStatus='Processing_Dropout' THEN 1 ELSE null END) AS Processing_Dropout,
COUNT(CASE WHEN ProcessStatus='Processing' THEN 1 ELSE null END) AS Processing,
COUNT(CASE WHEN ProcessStatus='Matching' THEN 1 ELSE null END) AS Matching,
COUNT(CASE WHEN ProcessStatus='Matching_Dropout' THEN 1 ELSE null END) AS Matching_Dropout,
COUNT(CASE WHEN ProcessStatus='Export' THEN 1 ELSE null END) AS Export,
COUNT(CASE WHEN ProcessStatus='Export_Dropout' THEN 1 ELSE null END) AS Export_Dropout,
COUNT(CASE WHEN ProcessStatus='Exported' THEN 1 ELSE null END) AS Exported,
COUNT(CASE WHEN ProcessStatus='Rejected' THEN 1 ELSE null END) AS Rejected,
COUNT(CASE WHEN ProcessStatus!= '_New' and ProcessStatus!= 'Import_Dropout' and ProcessStatus!= 'Import' THEN 1 ELSE null END) AS AmountSubTotal,
COUNT(*) AS AmountTotal,
1 as UnionOrder
FROM "matching$marketscanmovement"
WHERE date_part ('year', BookingDate_BA_MS)= 2018
) SK GROUP by unionorder order by unionorder asc
1) The result is 2 rows, which is not one value as total of that column.
Why is this query not summarizing the unioned same column values? How should it be written?
2) When I try sum "(Amount_New1+Amount_New2) Amount_New" (and changing the subquery column names to amount_new1 / amount_new2) it is not working neither. Why?
If you want to return a single row, conceptually representing an aggregate over the entire union table, then remove GROUP BY:
SELECT SUM(Amount_New) Amount_New,
SUM(Import_Dropout) Import_Dropout,
SUM(Import) Import,
SUM(Processing) Processing,
SUM(Processing_Dropout) Processing_Dropout,
SUM(Matching) Matching,
SUM(Matching_Dropout) Matching_Dropout,
SUM(Export) Export,
SUM(Exported) Exported,
SUM(Rejected) Rejected,
SUM(AmountSubTotal) AmountSubTotal,
SUM(AmountTotal) AmountTotal
FROM
(
-- ... your current query
) SK;
You also may want to remove the UnionOrder computed column, since there is no need to sort a single row result set.

How to write a sql to select records with specific conditions

Need to select from a same column based on specific conditions from a same table
I tried using max case when status =‘OP’ then tot_amountelse 0
Max case when status =‘PR’ and curr_flag =‘Y’ thentot_amount else 0
Below is the table
ID Type Status tot_Amount curr_flag
1 Null OP 100 N
1. F. PR 60 N
1. H. PR. 0. Y
Expected output
ID Type. TotalAmt Bal-Amt
1. H. 100. 0
You want conditional aggregation :
select id,
max(case when curr_falg = 'Y' then Type end) as Type,
sum(case when Status = 'OP' then tot_Amount else 0 end) as TotalAmt,
sum(case when Status = 'PR' and curr_falg = 'Y' then tot_Amount else 0 end) as Bal_Amt
from table t
group by id;

Oracle (SQL): How to get SQL query syntax

A table (ex: A) has three columns id, name, amount. Amount has some +ve,-ve and zero values. How to write a query to get the amount in such a way that it gives as a result first +ve then -ve then zeros? PFA for the sample table.
Thanks in advance
If you want 3 groups one after the other:
SELECT *
FROM tablename
ORDER BY
(CASE
WHEN amount > 0 then 1
WHEN amount < 0 then 2
ELSE 3
END),
Id;
Are you just looking for order by with a case expression?
select a.*
from a
order by (case when a.amount > 0 then 1
when a.amount < 0 then 2
else 3
end),
a.amount desc;

Count rows for two columns using two different clauses

I'm after a CTE which I want to return two columns, one with the total number of 1's and one with the total number of 0's. Currently I can get it to return one column with the total number of 1's using:
WITH getOnesAndZerosCTE
AS (
SELECT COUNT([message]) AS TotalNo1s
FROM dbo.post
WHERE dbo.checkletters([message]) = 1
--SELECT COUNT([message]) AS TotalNo0s
--FROM dbo.post
--WHERE dbo.checkletters([message]) = 0
)
SELECT * FROM getOnesAndZerosCTE;
How do I have a second column called TotalNo0s in the same CTE which I have commented in there to show what I mean.
Using conditional aggregation:
WITH getOnesAndZerosCTE AS(
SELECT
TotalNo1s = SUM(CASE WHEN dbo.checkletters([message]) = 1 THEN 1 ELSE 0 END),
TotalNo0s = SUM(CASE WHEN dbo.checkletters([message]) = 0 THEN 1 ELSE 0 END)
FROM post
)
SELECT * FROM getOnesAndZerosCTE;
For using COUNT() directly just be aware that it counts any NON-NULL values. You can omit the ELSE condition which implicitly returns NULL if not stated
SELECT
COUNT(CASE WHEN dbo.checkletters([message]) = 1 THEN 1 END) TotalNo1s
, COUNT(CASE WHEN dbo.checkletters([message]) = 0 THEN 1 END) TotalNo0s
FROM post
or, explicitly state NULL
SELECT
COUNT(CASE WHEN dbo.checkletters([message]) = 1 THEN 1 ELSE NULL END) TotalNo1s
, COUNT(CASE WHEN dbo.checkletters([message]) = 0 THEN 1 ELSE NULL END) TotalNo0s
FROM post
You can do it without CTE
select count(message) total,
dbo.checkletters(message) strLength
from post
group by dbo.checkletters(message)
having dbo.checkletters(message) in (1, 2) //All the messages with length 1 or 2

SQL query to test my programming capabilities in different way

I have one table and one column in it. There is 15 rows (integers). I want to count
the positive numbers and negative numbers, and also sum of total numbers in one query.
Can any one help me?
Or...
SELECT
COUNT(CASE WHEN Col > 0 THEN 1 END) AS NumPositives,
COUNT(CASE WHEN Col < 0 THEN 1 END) AS NumNegatives,
SUM(Col) AS Tot
FROM TableName;
Or you could consider using SIGN(Col), which gives 1 for positive numbers and -1 for negative numbers.
I'll give you psudeo code to help you with your homework.
3 aggregates:
SUM
SUM (CASE < 0)
SUM (CASE > 0)
select (select sum(mycolumn) from mytable where mycolumn > 0) as positive_sum,
(select sum(mycolumn) from mytable where mycolumn < 0) as negative_sum,
sum(mycolumn) as total_sum
from mytable
Try this
SELECT SUM(CASE WHEN Col > 0 THEN 1 ELSE 0 END) AS Pos,
SUM(CASE WHEN Col < 0 THEN 1 ELSE 0 END) AS Neg,
SUM(Col) AS Tot
FROM Table