I have source table as shown below:
ID Status State_id
X Active 1
X Active 2
X Active 3
X Active 4
Y Active 2
Y Active 3
Z Active 1
Z Active 2
Z Active 3
A Active 2
A Active 3
I need 2 output based on State_id 2,3.
Set 1 for having only 2,3 state_id
ID Status
Y Active
A Active
Set 2 for having additional state id with 2,3
ID Status
X Active
Z Active
You can use this scripts.
Set 1
SELECT
ID, Status
FROM
#Tbl T
GROUP BY
ID, Status
HAVING
SUM(CASE WHEN State_id IN (2,3) THEN 1 ELSE 0 END) = 2
AND SUM(CASE WHEN State_id IN (2,3) THEN 0 ELSE 1 END) = 0
Result:
ID Status
---- ----------
A Active
Y Active
Set 2
SELECT
ID, Status
FROM
#Tbl T
GROUP BY
ID, Status
HAVING
SUM(CASE WHEN State_id IN (2,3) THEN 1 ELSE 0 END) = 2
AND SUM(CASE WHEN State_id IN (2,3) THEN 0 ELSE 1 END) > 0
Result:
ID Status
---- ----------
X Active
Z Active
/*Set 1*/
select t1.id, t1.status
from table1 t1
where t1.state_id in (2, 3) /*ids you want */
and t1.id not in ((
select distinct t2.id
from table1 t2
where t2.state_id in (1, 4) /*ids you dont want*/
))
group by t1.id, t1.status
Just flip the IDs to get set 2
I think it should give you expected result.
Updated
SqlFiddle
--Set 1
SELECT id ,status
FROM table1
-- WHERE status='Active'
GROUP BY id ,status
HAVING COUNT(DISTINCT state_id) =count(DISTINCT CASE WHEN state_id IN (2,3) THEN state_id END) ;
--Set2
SELECT id ,status
FROM table1
GROUP BY id ,status
HAVING COUNT(DISTINCT state_id)>count(DISTINCT CASE WHEN state_id IN (2,3)
THEN state_id END)
AND COUNT(DISTINCT CASE WHEN state_id IN (2,3) THEN state_id END) =2;
Related
I would like to get list of unique id that have 'condition=1' before 'condition=2'.
id
date
condition1
condition2
1
2022/02
1
0
1
2022/04
0
1
1
2022/05
0
0
2
2021/09
0
1
2
2022/01
1
0
3
2022/02
1
0
3
2022/05
0
1
In this case it would be 1 and 3.
SELECT id, MIN(date) FROM TABLE GROUP BY id
I know that i can do something like this to get first dates for id but i just cant figure out what to do for my problem
We can GROUP BY id and build two conditional MIN dates using CASE WHEN.
In the HAVING clause we say that the minimum date with condition 1 must appear before the minimum date with condition 2.
SELECT id
FROM yourtable
GROUP BY id
HAVING MIN(CASE WHEN condition1 = 1 THEN date END) <
MIN(CASE WHEN condition2 = 1 THEN date END)
ORDER BY id;
Try out here: db<>fiddle
Something like:
SELECT DISTINCT Id
FROM
(SELECT id, MIN(date)
FROM TheTable
WHERE Condition1 = 1
GROUP BY Id) c1
INNER JOIN
(SELECT Id, MIN(date)
FROM TheTable
WHERE Condition2 = 1
GROUP BY Id) c2
ON c1.Id=C2.Id AND c1.Date < c2.Date
Below is my data:
ID
request_type
1
3
1
2
1
1
1
4
1
5
2
3
2
2
3
4
3
2
I need a query to fetch IDs that never had a request type of 1 (e.g. 2,3 from the previous table).
With conditional aggregation:
select id
from tablename
group by id
having count(case when request_type = 1 then 1 end) = 0
SELECT DISTINCT `ID`
FROM `my_table`
WHERE `ID` NOT IN (
SELECT DISTINCT `ID`
FROM `my_table`
WHERE `request_type` = 1
)
If 1 is the lowest possible value:
select ID
from tab
group by ID
having min (request_type) > 1
Or more generic:
select ID
from tab
group by ID
having max(case when request_type = 1 then 1 else 0 end) = 0
I am able to count the occurrences of unique values per ID in the same record, but it seems there must be a more efficient way? Something like COUNT([Value],'2')?
Here's a simple example
ID | Value
1 2
1 3
1 3
1 2
2 2
2 3
2 3
3 3
And this is my current code:
SELECT ID, SUM(CASE WHEN Value = '2' THEN 1 ELSE 0 END) AS "COUNT2",
SUM(CASE WHEN Value = '2' THEN 1 ELSE 0 END) AS "COUNT3"
FROM TABLE
GROUP BY ID
The results are:
ID | Count2 | Count3
1 2 3
2 1 2
3 0 1
Is there a better way to get the count of unique values?
Try:
Select distinct Id, Count2, Count3
from Table
Outer Apply (select count(id) as Count2 from table t
where t.id = Table.id and value = 2) c2
Outer Apply (select count(id) as Count3 from table t
where t.id = Table.id and value = 3) c3
Order by Id asc
Typed from my phone so may need to be tweaked a little but something like this should work
You could use DECODE if by "better" you mean more terse
WITH I (ID, V) AS (VALUES
(1,2)
, (1,3)
, (1,3)
, (1,2)
, (2,2)
, (2,3)
, (2,3)
, (3,3)
)
SELECT
ID
, COUNT(DECODE(V,2,1)) AS "Count2"
, COUNT(DECODE(V,3,1)) AS "Count3"
FROM I
GROUP BY
ID
returns
ID Count2 Count3
-- ------ ------
1 2 2
2 1 2
3 0 1
If you are using Db2 LUW 11.1.1.1 onward, you could take advantage of the fact that you can SUM BOOLEAN values.
WITH I (ID, V) AS (VALUES
(1,2)
, (1,3)
, (1,3)
, (1,2)
, (2,2)
, (2,3)
, (2,3)
, (3,3)
)
SELECT
ID
, SUM(V=2) AS "Count2"
, SUM(V=3) AS "Count3"
FROM I
GROUP BY
ID
I'm beginner in Oracle SQL. I am using SQL Developer. This query is not executed. I need for each id like 1001,1002 how many no of yes status and how many no of no status. Thanks in advance....
I used this SQL:
SELECT ID, COUNT(STATUS)
FROM TABLE1
WHERE
GROUP BY ID, STATUS
HAVING STATUS = YES OR STATUS = NO;
I have table like this:
id school status
--------------------------
1001 vani YES
1002 sunbeam YES
1001 shristri YES
1002 jain NO
1001 holycross YES
1001 vani NO
I need output like
id yesstatus Nostatus
-------------------------
1001 3 1
1002 1 1
Your current query syntax is really wrong, but you can do conditional aggregation :
select id,
sum(case when status = 'YES' then 1 else 0 end) as yesstatus,
sum(case when status = 'NO' then 1 else 0 end) as Nostatus
from table1 t1
where status in ('YES', 'NO')
group by id;
select status, count(status)
from your_table
group by status
CREATE TABLE dbo.TABLE1
(
id INT,
school NVARCHAR(50) ,
status BIT
)
INSERT INTO dbo.TABLE1 ( id ,
school ,
status )
VALUES (1001,'vani',1),
(1002,'sunbeam', 1),
(1001,'shristri',1),
(1002,'jain',0),
(1001,'holycross',1),
(1001,'vani', 0)
SELECT id,
SUM(CASE WHEN status=1 THEN 1 ELSE 0 END) as yesstatus,
SUM(CASE WHEN status=0 THEN 1 ELSE 0 END) as Nostatus
FROM dbo.TABLE1
GROUP BY id
If I have a table
AgentID | IsNew | TeamID
1 N 1
2 Y 2
3 Y 2
4 N 2
5 Y 1
I want to return the following from a query:
Team | CountIsNew = N | CountIsNew = Y
1 1 1
2 1 2
Is there a way I can do this?
Using Oracle 10
SELECT team, SUM(DECODE(IsNew, 'N', 1, 0)), SUM(DECODE(IsNew, 'Y', 1, 0))
FROM mytable
GROUP BY
team
SELECT TeamId
, SUM(CASE WHEN IsNew = 'N' THEN 1 ELSE 0 END) AS CountIsNotNew
, SUM(CASE WHEN IsNew = 'Y' THEN 1 ELSE 0 END) AS CountIsNew
FROM Agent
GROUP BY TeamId
Yet another way - COUNT doesn't count NULLs (except for COUNT(*)):
SELECT TeamId,
COUNT(DECODE(IsNew,'N',1)) CountIsNotNew,
COUNT(DECODE(IsNew,'Y',1)) CountIsNew
FROM Agent
GROUP BY TeamId;
Or, if you prefer CASE:
SELECT TeamId,
COUNT(CASE IsNew WHEN 'N' THEN 1 END) CountIsNotNew,
COUNT(CASE IsNew WHEN 'Y' THEN 1 END) CountIsNew
FROM Agent
GROUP BY TeamId;
(note: the "1"s could be any literal value)