Count days on a column case - sql

I would like to create a SQL script that counts the number of age days and the something like:(The catch is I got the days value of breaking value of one column by case to three columns) This is the output I have created.
Days0To30 Days30to60 Daysto60to90
----------------------------------
50$ | 10$ | 90$
60$ | 0 | 10$
0 | 0 | 5$
0 | 10$ | 0
10$ | 0 | 0
0$ | 0 | 0
1240 | 0 | 0
I would like to create a SQL script that counts the number of age days and the something like:
------------------------------------------
Days0To30 | 4
Days30to60 | 2
Daysto60to90 | 3

You can use conditional aggregation:
select count(case when Days0To30 <> 0 then 1 end) as Days0To30,
count(case when Days30to60 <> 0 then 1 end) as Days30to60,
count(case when Daysto60to90 <> 0 then 1 end) as Daysto60to90
from yourtable

SELECT SUM(CASE WHEN Days0To30 > 0 THEN 1 ELSE 0 END) as Days0To30 ,
SUM(CASE WHEN Days30to60 > 0 THEN 1 ELSE 0 END) as Days30to60,
SUM(CASE WHEN Daysto60to90 > 0 THEN 1 ELSE 0 END) as Daysto60to90
FROM SOME_TABLE

Related

How to do multiple actions in case when then in sql?

I want to do something like this:
select sum(case ttt.ind = 1 then 1 else 0 end) from ttt
I want to add a column to this query, called myresult which indicates if the value of ttt.istry is equal to 1.
Maybe like:
select
sum(case ttt.ind = 1 then 1, ttt.istry as myresult else 0 end)
from ttt
of course I got an error...
How would I do that?
My data is:
ttt.ind | ttt.istry
--------+----------
1 | 0
0 | 1
1 | 1
and so on...
Expected result:
ttt.ind | ttt.istry | myresult | sum
--------+-----------+----------+------
1 | 0 | 0 | 2
0 | 1 | null | 2
1 | 1 | 1 | 2
You don't say which database so I'll assume it's a modern one. You can use a window function and a CASE clause to do this.
For example:
select
ind,
istry,
case when ind = 1 then istry end as myresult,
sum(ind) over() as sum
from ttt
See live example at SQL Fiddle.
Your logic is a bit hard to follow, but your result set suggests:
select ind, istry,
(case when istry = 1 then 1
when sum(istry) over (partition by ind) = 1 then 0
end),
sum(ttt.ind) over () as sum_ind
from ttt;

Counting occurrences of a value in multiple columns - postgres

I have a table called fixtures (I have simplified for this example) and would like to populate the last two columns (*_plus_mc_per) with the percentage of times occurred for each number with a query run against the mc_* columns. It would look like this as an example
#mc = Match Corner # mc_per = Match Corner Percentage
| mc_0 | mc_1 | mc_3 | mc_4 | match_count | one_plus_mc_per | two_plus_mc_per |
| 1 | 4 | 3 | null | 3 | 100 | 66 |
At the point where I run my query it looks like
#mc = Match Corner # mc_per = Match Corner Percentage
| mc_0 | mc_1 | mc_3 | mc_4 | match_count | one_plus_mc_per | two_plus_mc_per |
| 1 | 4 | 3 | null | 3 | null | null |
So starting with the query for one_plus_mc_per I can do this
SELECT COUNT(*) FROM fixtures WHERE coalesce(mc_0,0) >= 1 AND id = 182;
# Using coalesce for dealing with null, will return a 0 if value null
This returns
| count |
| 1 |
If I run this query on each column individually the results returned would be
| count | count | count | count |
| 1 | 1 | 1 | 0 |
Thus enabling me to add all the column values up and divide by my match count. This makes sense (and I thank dmfay for getting me to think about his suggestion in a previous question)
My problem is I can't run this query 4 times for example as that is very ineffective. My SQL fu is not strong and was looking for a way to do this in one call to the database, enabling me to then take that percentage value and update the percentage column
Thanks
Try this:
SELECT
SUM(CASE WHEN coalesce(mc_0,0) >= 1 THEN 1 ELSE 0 END) count_0,
SUM(CASE WHEN coalesce(mc_1,0) >= 1 THEN 1 ELSE 0 END) count_1,
SUM(CASE WHEN coalesce(mc_3,0) >= 1 THEN 1 ELSE 0 END) count_3,
SUM(CASE WHEN coalesce(mc_4,0) >= 1 THEN 1 ELSE 0 END) count_4,
FROM
fixtures
WHERE id = 182;
It will return count of all the columns in single query
I am not sure though, whats the use of id = id in your query as it will always be true.
If you want count of columns *_mc for every row with > 0 condition, try this:
SELECT
(CASE WHEN coalesce(mc_0,0) >= 1 THEN 1 ELSE 0 END +
CASE WHEN coalesce(mc_1,0) >= 1 THEN 1 ELSE 0 END +
CASE WHEN coalesce(mc_3,0) >= 1 THEN 1 ELSE 0 END +
CASE WHEN coalesce(mc_4,0) >= 1 THEN 1 ELSE 0 END) as count
FROM
fixtures
WHERE id = 182;
UPDATE:
Calculating one_plus_mc_per
SELECT
CAST((CASE WHEN coalesce(mc_0,0) >= 1 THEN 1 ELSE 0 END +
CASE WHEN coalesce(mc_1,0) >= 1 THEN 1 ELSE 0 END +
CASE WHEN coalesce(mc_3,0) >= 1 THEN 1 ELSE 0 END +
CASE WHEN coalesce(mc_4,0) >= 1 THEN 1 ELSE 0 END)AS DECIMAL)/match_count as one_plus_mc_per
FROM
fixtures
WHERE id = 182;
Postgres has very nice capabilities for answering this type of question:
SELECT COUNT(*) FILTER (WHERE mc_0 >= 1) as count_0,
COUNT(*) FILTER (WHERE mc_1 >= 1) as count_1,
COUNT(*) FILTER (WHERE mc_3 >= 1) as count_3,
COUNT(*) FILTER (WHERE mc_4 >= 1) as count_4,
AVG ( (mc_0 >= 1)::int + (mc_1 >= 1)::int + (mc_3 >= 1)::int + (mc_4 >= 1)::int
) as one_plus_mc_per
FROM fixtures
WHERE id = 182;
The FILTER is ANSI-standard syntax. The conversion of booleans to numbers is a very convenient construct.

how to group column as per its unique value and get count in to different column as per its value?

I am having a Category table as follows,
i want retrieve following results according to scStatusvalue and its count group by catID
catID | 0 | 2 | 3
----- |---|---|---
2 | 1 | 0 | 1
3 | 1 | 1 | 0
4 | 2 | 0 | 1
5 | 0 | 1 | 0
I tried this,select catID,count(scStatus) as [Count] from tableName group by catID,scStatus order by catID but i cant get into column that values.
`
Use a pivot query:
SELECT catID,
SUM(CASE WHEN scStatus = 0 THEN 1 ELSE 0 END) AS [0],
SUM(CASE WHEN scStatus = 2 THEN 1 ELSE 0 END) AS [2],
SUM(CASE WHEN scStatus = 3 THEN 1 ELSE 0 END) AS [3]
FROM Category
GROUP BY catID
pivot operator
select *
from (select catID,scStatusvalue from t) t
pivot (count(scStatusvalue) for scStatusvalue in ([0],[2],[3])) t

SQL Trying to flatten out data

I have a query that I have a SQL pivot in. My data looks like this
Select ROW_NUMBER() OVER (PARTITION BY m.[MKEY] ORDER BY es.[FDATE]) as 'Row', FDATE
PIVOT(
SUM(s.[EARNINGS])
FOR s.[row] IN ([1], [2], [3], [4] ))AS i
Person | FDATE| 1 | 2 | 3 | 4
Sam Smith| 2001 | 200.00 | Null | Null | Null
Sam Smith| 2002 | Null | 400.00| Null | Null
Sam Smith| 2003 | Null | Null | 500.00| Null
I'd like to have it like this
Person | FDATE| 1 |FDATE| 2 |FDATE| 3 |FDATE| 4
Sam Smith | 2001 | 200.00 | 2002 |400.00| 2003| 500.00|NULL| NULL
What's the best way of achieving this? Any help would be greatly appreciated.
try this:
select
Person
,max(case when coalesce(FDATE,0)=2001 then FDATE else 0 end) as FDATE_1
,sum(case when coalesce(FDATE,0)=2001 then Earnings else 0 end) as [1]
,max(case when coalesce(FDATE,0)=2002 then FDATE else 0 end) as FDATE_2
,sum(case when coalesce(FDATE,0)=2002 then Earnings else 0 end) as [2]
,max(case when coalesce(FDATE,0)=2003 then FDATE else 0 end) as FDATE_3
,sum(case when coalesce(FDATE,0)=2003 then Earnings else 0 end) as [3]
,max(case when coalesce(FDATE,0)=2004 then FDATE else 0 end) as FDATE_4
,sum(case when coalesce(FDATE,0)=2004 then Earnings else 0 end) as [4]
from <table>
group by
Person
Note that columns you don't actually ask for in the SELECT clause can never appear in the result.

Counting sum of items of type

What i what to do is from this :
|type|quantity|
+----+--------+
|shoe| 10 |
|hat | 2 |
|shoe| 7 |
|shoe| 1 |
|hat | 5 |
to get this :
|shoes|hats|
+-----+----+
| 18 | 7 |
How can i do that? So far I hadn't come up with a working query, I think it should look something like that:
SELECT
SUM(CASE type WHEN 'shoe' then quantity ELSE 0 END) AS "shoes",
SUM(CASE type WHEN 'hat' then quantity ELSE 0 END) AS "hats"
FROM items
GROUP BY type
Just drop the group by. You want only one row:
SELECT
SUM(CASE type WHEN 'shoe' then quantity ELSE 0 END) AS "shoes",
SUM(CASE type WHEN 'hat' then quantity ELSE 0 END) AS "hats"
FROM items ;