I have a table with a column named direction. This columns has just 1 or 0 int value. For every 1 or zero there is and ID couple. For example:
ID1 ID2 direction
1 2 1
2 3 0
2 4 1
4 1 1
1 2 0
2 3 1
I need a select query in order to take 0 counts and 1 counts for every ID1 and ID2 pair. How can I do that?
Edit:
Result table should look like this: (Numbers does not match with above example)
ID1 ID2 0count 1count
1 2 1 4
2 3 2 2
2 2 1 1
Conditional aggregation is your friend:
SELECT ID1,
ID2,
SUM(CASE WHEN direction = 0 THEN 1 ELSE 0 END) As CountDirection0,
SUM(CASE WHEN direction = 1 THEN 1 ELSE 0 END) As CountDirection1,
FROM Table
GROUP BY ID1, ID2
Related
Having the following data table:
col1
days
A
2
B
3
C
1
C
5
D
3
A
3
B
7
A
4
I want to transform it into:
col1
<=2days
<=5days
<=7days
A
1
2
0
B
0
1
1
C
1
1
0
D
0
1
0
I used the below query to achieve this:
select col1,
CASE WHEN days <=2 then count(days) as "<=2days",
CASE WHEN days > 2 and days <=5 then count(days) as "<=5days",
CASE WHEN days > 5 and days <=7 then count(days) as "<=7days"
from tableA group by col1,days
But it returns a result like the following:
col1
<=2days
<=5days
<=7days
A
1
0
0
A
0
2
0
B
0
1
0
B
0
0
1
C
0
1
0
C
1
0
0
D
0
1
0
Can someone please help here?
You could use conditional aggregation as the following:
SELECT col1,
COUNT(CASE WHEN days<=2 THEN 1 END) AS '<=2days',
COUNT(CASE WHEN days<=5 and days>2 THEN 1 END) AS '<=5days',
COUNT(CASE WHEN days<=7 and days>5 THEN 1 END) AS '<=7days'
FROM tableA
GROUP BY col1
ORDER BY col1
See a demo on MySQL.
I have a Table like so:
ID1|ID2|ID3|Enabled
1 1 0 1
1 1 1 1
1 1 2 1
2 1 1 1
2 1 2 1
2 1 3 1
3 1 0 0
3 1 1 1
3 1 2 1
4 1 0 1
4 1 0 1
4 1 0 1
I want to group by first ID1 then ID2 and select all ID1 where within each group, ID3 contains both 0 (If Enabled column is 1) and nonzero value. So the table provided will return -
ID1
1
SELECT ID1 From Table1
GROUP BY ID1, ID2
WHERE
???
You want a having clause, if your definition of group is based on ID1/ID2:
SELECT ID1, ID2
FROM Table1
GROUP BY ID1, ID2
HAVING SUM(CASE WHEN ID3 = 0 AND Enabled = 1 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN ID3 <> 0 THEN 1 ELSE 0 END) > 0;
You need a group by with an alternative having as you would not be able to use having for id3 as id3 should be present in group by first to be used in having else ypu can use only aggregates in having. So, In order to make the id3 case work I have used here case when which is checking id3 if found as either 0 or 1 would be returning value as 1 in each such case. Therefore, the sum on top of case act as an aggregate in having hence would work as if sum obtained based on the cases comes as greater than or equal to 2 or more(if duplicates of 0 and 1) then output those rows of that partcular group.
select ID1
from Table1
where enabled = 1
group by ID1, ID2
having sum( case when ID3 in (1,0) then
1 else 0 end) >= 2
I have a series of Ids, some of them activate a product on certain month and that product remains activated for an X period of time, while others do not activate the product.
I want to create a column which indicates in which month the user activates the product or a NULL if the user doesn't activate it.
I've tried using a partition like the following:
SELECT id, fl_testdrive, month_dt,
CASE WHEN fl_testdrive = 1 then min(month_dt) OVER(PARTITION BY id ORDER BY month_dt ROWS UNBOUNDED PRECEDING) else 0 end as month_testdrive
FROM Table_1
However, when I try this solution, in the column month_testdrive, I do not obtain the first month in which the user appears, indepently of if he/she activated that product in that month or on a later one.
This is what I get with my query
Id flag_testdrive month_dt month_testdrive
1 0 1 1
1 0 2 1
1 1 3 1
1 1 4 1
2 0 2 0
2 0 3 0
3 1 4 4
3 1 5 4
What I'd expect:
Id flag_testdrive month_dt month_testdrive
1 0 1 3
1 0 2 3
1 1 3 3
1 1 4 3
2 0 2 0
2 0 3 0
3 1 4 4
3 1 5 4
This solution is a second best but is also fine:
Id flag_testdrive month_dt month_testdrive
1 0 1 0
1 0 2 0
1 1 3 3
1 1 4 3
2 0 2 0
2 0 3 0
3 1 4 4
3 1 5 4
You want CASE expression inside MIN():
MIN(CASE WHEN fl_testdrive = 1 THEN month_dt ELSE 0 END) OVER(PARTITION BY id, flag_testdrive ORDER BY month_dt ROWS UNBOUNDED PRECEDING)
Here's an option for you:
DECLARE #Testdate TABLE(
id INT
,flag_testdrive INT
,month_dt INT
)
INSERT INTO #Testdate (
[id]
, [flag_testdrive]
, [month_dt]
)
VALUES(1,0,1)
,(1,0,2)
,(1,1,3)
,(1,1,4)
,(2,0,2)
,(2,0,3)
,(3,1,4)
,(3,1,5)
SELECT
*
,COALESCE((SELECT MIN([aa].[month_dt]) FROM #Testdate aa
WHERE aa.[id] = a.id
AND aa.[flag_testdrive] = 1), 0) AS month_testdrive
FROM #Testdate a
Return the minimum month_dt for a given id only if flag_testdrive=1, wrapped in coalesce to return 0 instead of NULL.
I need to create query that has below result.
I add my table for example for better understanding.
QTable:
QID Name
-------
1 x
1 y
2 z
FTable:
QID FID
-------
1 1
1 2
2 3
Inner join with :
FID Type
-------
1 1
1 2
1 3
1 4
2 2
2 3
2 3
3 1
3 3
3 3
I need to result like this:
result:
QID FID-Count Type(1)-count Type(2)-count Type(3)-count Type(4)-count
-----------------------------------------------------------------
1 2 1 2 3 1
2 1 1 0 2 0
Thank u for help.
Ususally you can emulate a pivot with GROUP BY and SUM+CASE:
SELECT
T1.QID,
COUNT(DISTINCT T1.FID) as FIDCOUNT,
SUM(CASE WHEN Type=1 THEN 1 ELSE 0 END) as Type1Cnt,
SUM(CASE WHEN Type=2 THEN 1 ELSE 0 END) as Type2Cnt,
SUM(CASE WHEN Type=3 THEN 1 ELSE 0 END) as Type3Cnt,
SUM(CASE WHEN Type=4 THEN 1 ELSE 0 END) as Type4Cnt
FROM QFTable as T1
JOIN T2 ON
T1.FID=T2.FID
GROUP BY T1.QID
ORDER BY QID
SQLFiddle demo
I have a data in Microsoft SQL server with the following format:
id1 id2 month quantA quantB
1 10 1 5 15
1 10 1 10 20
1 10 2 5 10
1 10 2 10 NULL
1 11 1 NULL NULL
1 11 2 5 NULL
1 11 2 10 5
2 10 1 10 20
2 10 1 5 NULL
2 11 2 NULL NULL
I need to construct a table grouped by id1 and month with the following columns:
id1
month
var1 = count how many *distinct* id2 by month and id1 for which quantA!=Null
var2 = count how many *distinct* id2 by month and id1 for which quantB!=Null
You can construct the query basically how you have written it:
select id1, month,
count(distinct case when quantA is not null then id2 end) as var1,
count(distinct case when quantB is not null then id2 end) as var2
from t
group by id1, month
COUNT DISTINCT ignores NULLs when doing the count.