SQL Server Rowcount of column value - sql

I am trying to write a simple SQL code that can get the desired output, from this table -
Original Table :
id | type
123 0
123 1
123 1
345 0
345 0
What I'm trying to get is:
id | zeroCount | oneCount
123 1 2
345 2 0
I tried to group by the id and also type but, both of them gives the same thing!
How to get desired output?

Here is a simple way, assuming the values are only 0 and 1:
select id, sum(1 - type) as zerocount, sum(type) as onecount
from t
group by id;
A more typical approach would use case (or even pivot):
select id, sum(case when type = 0 then 1 else 0 end) as zerocount,
sum(case when type = 1 then 1 else 0 end) as onecount
from t
group by id;

You could get fancy and use a pivot function. But this will work:
Select id,
sum(case when type = 0 then 1 else 0 end) as ZeroCount,
Sum(case when type = 1 then 1 else 0 end) as OneCount
from table
group by id
order by id

Related

Case-when error when creating individual monthly values

If I have the following
ID Disputes_Count Month-Year
123 45 12020
123 85 32020
I want the result to be
ID Jan_2020_Disputes Mar_2020_Disputes
123 45 85
I tried this:
proc sql;
create table disputes_count as
select ID,
sum(case when Month_Year = '12020' then Dispute_Count else 0 end) as January_2020_Disputes,
sum(case when Month_Year = '32020' then Dispute_Count else 0 end) as Mar_2020_Disputes,
from rb.disputes
group by ID;
quit;
However the output I got with this is the following:
ID January_2020_Disputes Mar_2020_Disputes
0 7
Please help!

Enquiry on Query in Oracle SQL

I have data as below:
Category | Type | Rank
Milk 1 1
Milk 2 2
Milk 3 3
Chocolate 1 2
Candy 1 1
Any idea to achieve the output of below with a flat SQL query:
Category
Milk
Query must satisfy the below conditions:
1. Only Type 1 and Rank 1 will be selected.
2. Only Category that has Type 1 and Type 2 will be selected.
In the sample data above, only Milk that satisfy the conditions mentioned above.
My query is below. But it's incorrect, because it will return Candy as well.
SELECT DISTINCT Category
FROM table
WHERE Type = 1 AND rank = 1
Thanks in advance!
You can try below -
DEMO
select distinct category
from table a
WHERE Type = 1 AND rank = 1
and exists
(select 1 from table b where a.category=b.category and type in (1,2)
group by category having count(distinct type)=2)
OUTPUT:
category
Milk
You can use aggregation:
select category
from t
group by category
having sum(case when type = 1 and rank = 1 then 1 else 0 end) > 0 and
sum(case when type = 2 then 1 else 0 end) > 0;
Assuming no duplicates, this can be simplified to:
select category
from t
where (type = 1 and rank = 1) or type = 2
group by category
having count(distinct type) = 2;

SQL table data horizontally using PIVOT

My SQL table is
GUID Step_ID Value
----------------------------------------------
ADFE12-ASDER-... 1 10
ADFE12-ASDER-... 2 20
ADFE12-ASDER-... 3 30
ADFE12-ASDER-... 4 160
CD4563-FG567-... 1 20
CD4563-FG567-... 2 80
Q23RT5-GH678... 1 30
Q23RT5-GH678-... 2 80
Q23RT5-GH678-... 3 20
And Expected result should be
GUID 1 2 3 4
---------------------------------------------------
ADFE12-ASDER-... 10 20 30 160
CD4563-FG567-... 20 80 NULL NULL
Q23RT5-GH678-... 30 80 20 NULL
Here I need to get the details on the basis of column whose data type is GUID. I tried using PIVOT table but getting an exception because I cannot use an aggregate function on GUID column. Is there any other alternative or approach I can use to get the above desired result.
Try this:
select [GUID],[1],[2],[3],[4]
from
(
select [GUID], Step_ID, Value
from test
) d
pivot
(
max(Value)
for Step_ID in ([1],[2],[3],[4])
) piv;
SQL FIDDLE DEMO
You could try manually pivotting, I don't have a SQL Server handy to check the behavior on a GUID but this has worked well for me in the past.
select guid,
max(case when step_ID = 1 then value else null end) step_1,
max(case when step_ID = 2 then value else null end) step_2,
max(case when step_ID = 3 then value else null end) step_3,
max(case when step_ID = 4 then value else null end) step_4
from your_table
group by guid;
HTH

SQL find total count of each type in a column

I'm learning SQL and am stumped on what should be a simple query. I have a table with the following pattern:
Id | Type
------------
1 | Red
2 | Blue
3 | Blue
4 | Red
..
I would like to write a query to return a table that counts the total number of instances of each type and returns a table with the following pattern, for example, if 'Blue' occurs in 12 rows, and 'Red' occurs in 16 rows in the table above, the result would be:
Blue | Red
-----------
12 | 16
You could do it this way:
SELECT Type, COUNT(*) FROM TABLE GROUP BY Type
If you'd like to see the Types in separate columns, you could do this:
SELECT SUM(CASE WHEN Type = 'Blue' THEN 1 ELSE 0 END) AS Blue, SUM(CASE WHEN Type = 'Red' THEN 1 ELSE 0 END) AS Red FROM TABLE
I suggest using count over partition by. Here's a code I wrote to help my company check for duplicate Technician EmployeeID's and Pincodes, including count and YES/NO columns to allow filtering in excel so they can see what corrections need to be made:
select
t.TechnicianId, t.TechnicianName, t.Pincode, t.EmployeeID
, [Pincode Count] = count(t.Pincode) over (partition by t.Pincode)
, [Duplicate Pincode?] = case count(t.Pincode) over (partition by t.Pincode) when 1 then 'NO' else 'YES' end
, [EmployeeID Count] = count(t.EmployeeID) over (partition by t.EmployeeID)
, [Duplicate EmployeeID?] = case count(t.EmployeeID) over (partition by t.EmployeeID) when 1 then 'NO' else 'YES' end
from Technicians t
group by t.TechnicianId, t.TechnicianName, t.Pincode, t.EmployeeID
order by 4

Grouping sub query in one row

ClientID Amount flag
MMC 600 1
MMC 700 1
FDN 800 1
FDN 350 2
FDN 700 1
Using sql server,Below query I am getting 2 rows fro FDN. I just would like to combine Client values in one row.
Output should be like
Client gtcount, totalAmountGreaterThan500 lscount,AmountLessThan500
MMC 2 1300 0 0
FDN 2 1500 1 350
SELECT
f.ClientID,f.flag,
case when flag = 1 then count(*) END as gtcount,
SUM(CASE WHEN flag = 1 THEN Amount END) AS totalAmountGreaterThan500,
case when flag = 2 then count(*) END as lscount,
SUM(CASE WHEN Flag = 2 THEN Amount END) AS AmountLessThan500,
from
( select ClientID, Amount,flag from #myTable)f
group by ClientID,f.flag
Try
SELECT ClientID,
SUM(CASE WHEN flag = 1 THEN 1 ELSE 0 END) AS gtcount,
SUM(CASE WHEN flag = 1 THEN Amount ELSE 0 END) AS totalAmountGreaterThan500,
SUM(CASE WHEN flag = 2 THEN 1 ELSE 0 END) AS lscount,
SUM(CASE WHEN Flag = 2 THEN Amount ELSE 0 END) AS AmountLessThan500
FROM Table1
GROUP BY ClientID
Output:
| CLIENTID | GTCOUNT | TOTALAMOUNTGREATERTHAN500 | LSCOUNT | AMOUNTLESSTHAN500 |
|----------|---------|---------------------------|---------|-------------------|
| FDN | 2 | 1500 | 1 | 350 |
| MMC | 2 | 1300 | 0 | 0 |
Here is SQLFiddle demo
Looks like your desired output is off -- there aren't any mmc records less than 500. You can accomplish this using sum with case for each of your fields, removing flag from the group by:
SELECT
ClientID,
SUM(CASE WHEN flag = 1 THEN 1 END) as gtcount,
SUM(CASE WHEN flag = 1 THEN Amount END) AS totalAmountGreaterThan500,
SUM(CASE WHEN flag = 2 THEN 1 END) as ltcount,
SUM(CASE WHEN Flag = 2 THEN Amount END) AS AmountLessThan500
from myTable
group by ClientID
SQL Fiddle Demo
On a different note, not sure why you need the Flag field. If it's just being used to denote less than records, just add the logic to the query:
SUM(CASE WHEN Amount <= 500 Then ...)