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
Related
there is a table:
t1
Id | Acc Status| Acc Type|
-----------------------------------------
1 |Online | home
2 |Offine | work
3 |Declined | work
1 |Activated | home
.
Question is:
need to find all Id which has 'Acc Status' = Online AND 'Acc Status' = Activated
You must group by id and count the distinct values of accstatus:
select id
from t1
where accstatus in ('Online', 'Activated')
group by id
having count(distinct accstatus) = 2
If you need the ids that have only these accstatus values:
select id
from t1
group by id
having
count(distinct accstatus) = 2
and
sum(case when accstatus not in ('Online', 'Activated') then 1 else 0 end) = 0
You can try below query -
SELECT id
FROM T1
WHERE AccStatus IN ('Online', 'Activated')
GROUP BY id
HAVING COUNT(DISTINCT AccStatus) = 2;
Try this below query
SELECT id
FROM Table_name
WHERE AccStatus = 'Online'
and id IN (SELECT id FROM Table_name wHERE AccStatus = 'Activated')
select DiSTINCT Id
from t1
where [Acc Status] in ("Online", "Activated")
I have two columns, I want to get an output based on a comparative basis of both. My data is somewhat like:
Customer Id status
100 A
100 B
101 B
102 A
103 A
103 B
So a customer can have a status A or B or both, I have to segrerate them on customer id basis for a status. If status A and B then return happy, if only A, return Avg and if only B return Sad.
try the below query,
SELECT DISTINCT Customer_Id,
(CASE WHEN COUNT(*) OVER(PARTITION BY Customer_Id) > 1 THEN 'happy'
WHEN Tstatus = 'A' THEN 'Avg'
ELSE 'Sad'END) AS New_Status
FROM #table1
GROUP BY Customer_Id,Tstatus
if Customer Id and status is a unique combination then
STEP 1: use case to determine a or b
SELECT customer id
,CASE WHEN avg(case when [status] ='A' then 0 else 2 end)
FROM [Your Table]
group by[customer id]
and step 2 will be casing avg into result: like this
SELECT customer id
,CASE WHEN (avg(case when [status] ='A' then 0 else 2 end)) = 1 THEN 'happy' ELSE WHEN (avg(case when [status] ='A' then 0 else 2 end)) = 0 THEN 'Avg' ELSE 'Sad' END
FROM [Your Table]
group by[customer id]
I would do this simply as:
select customer_id,
(case when min(status) <> max(status) then 'happy'
when min(status) = 'A' then 'avg'
else 'sad'
end)
from t
where status in ('A', 'B')
group by customer_id
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;
Say for example I have a table that contains a description of a customer's activities while in a cafe. (Metaphor of the actual table I am working on)
Customer Borrowed Book Ordered Drink Has Company
1 1
1 1
1 Yes
2 1
3 1
3 Yes
4 1 1
4 1
I wish to combine the rows in this way
Customer Borrowed Book Ordered Drink Has Company
1 1 1 Yes
2 1
3 1 Yes
4 1 2
I did self join with coalesce, but it did not give my desired results.
You can do this by group by,
select Customer,sum([borrowed book]), sum([ordered drink]), max([has company])
from customeractivity group by Customer
As per your comment, initial table is a temp table,
Try to make the result as a cte result, then do aggregation on that, like the below query.
; WITH cte_1
AS
( //your query to return the result set)
SELECT customer,sum([borrowed book]) BorrowedBook,
sum([ordered drink]) OrderedDrink,
max([has company]) HasCompany
FROM cte_1
GROUP BY Customer
Use Group By:
DECLARE #tblTest as Table(
Customer INT,
BorrowedBook INT,
OrderedDrink INT,
HasCompany BIt
)
INSERT INTO #tblTest VALUES
(1,1,NULL,NULL)
,(1,NULL,1,NULL)
,(1,NULL,NULL,1)
,(2,NULL,1,NULL)
,(3,NULL,1,NULL)
,(3,NULL,NULL,1)
,(4,1,1,NULL)
,(4,NULL,1,NULL)
SELECT
Customer,
SUM(ISNULL(BorrowedBook,0)) AS BorrowedBook,
SUM(ISNULL(OrderedDrink,0)) AS OrderedDrink,
CASE MIN(CAST(HasCompany AS INT)) WHEN 1 THEN 'YES' ELSE '' END AS HasCompany
FROM #tblTest
GROUP BY Customer
Not sure, why you are getting error with group by.
Your coalesce should be correct. Refer below way.
Select customer
, case when [borrowed] = 0 then NULL else [borrowed] end as [borrowed]
, case when [ordered] = 0 then NULL else [ordered] end as [ordered]
, case when [company] = 1 then 'Yes' end as company
from
(
Select customer,
coalesce(
case when (case when borrowed = '' then null else borrowed end) = 1 then 'borrowed' end,
case when (case when ordered = '' then null else ordered end) = 1 then 'ordered' end,
case when (case when company = '' then null else company end) = 'Yes' then 'company' end
) val
from Table
) main
PIVOT
(
COUNT (val)
FOR val IN ( [borrowed], [ordered], [company] )
) piv
OUTPUT:
customer | borrowed | ordered | company
---------------------------------------
1 1 1 Yes
2 NULL 1 NULL
3 NULL 1 Yes
Does anyone know how can I do a count in SQL Server based on condition.
Example:
How can I do a column count for records with name 'system', and total CaseID records in the table?
Customer table
UserID CaseID Name
1 100 alan
1 101 alan
1 102 amy
1 103 system
1 104 ken
1 105 ken
1 106 system
The result will display like below:
UserID TotalCaseID TotalRecordsWithSystem
1 7 2
Use SUM/CASE...
SELECT
COUNT(*), --total
SUM(CASE WHEN name = 'system' THEN 1 ELSE 0 END) --conditional
FROM
myTable
I think he wanted user id in the results
SELECT
userid,
COUNT(*) as TotalcaseID, --total
SUM(CASE WHEN name = 'system' THEN 1 ELSE 0 END) as TotalRecordsWithSystem
FROM
myTable
group by userid
select
userid,
count('x') as TotalCaseID,
count(case when name = 'system' then 'x' else null end) as TotalRecordsWithSystem
from CustomerTable
group by userid
If you're on SQL Server 2012+, then you can use SUM/IIF
SELECT
COUNT(*) AS Total,
SUM(IIF(Name = 'system', 1, 0)) AS SystemTotal
FROM
CustomerTable