Sql conditional count - sql

Can anyone help me with the sql and sqlite query for the below condition. To be specific, i wanted to show the merged count of 2 categories.
**Table**
Category Sub_category
A 1
A 2
A 2
B 1
C 1
C 1
C 2
D 1
D 1
D 1
D 2
D 3
**Required Output**
Category Sub_category Count **condition(not part of o/p just instruction)**
A 1 1 -
A 2+ 2 -
B 1 1 -
C 1 2 -
C 2+ 1 -
D 1 3 -
D 2+ 1 should contain the count of 2 and more
i am able to achieve the same using below:
select Category,
count(CASE WHEN Sub_category = 1 THEN Sub_category END) AS '1',
count(CASE WHEN Sub_category >= 2 THEN Sub_category END) AS '2+'
from table
however output is little different hence looking for the first output only.
**Output**
Category 1 2+
A 1 2
B 1 0
C 1 2
D 3 2
Thanks in advance!

You want:
select Category,
(case when sub_category = 1 then '1' else '2+' end) as sub_category,
count(*)
from table
group by Category,
(case when sub_category = 1 then '1' else '2+' end);
That is, you want to put the sub category groups on rows, not as columns.

You are just missing one column. Add the last count without a filtering to get the total:
select Category,
count(CASE WHEN Sub_category = 1 THEN Sub_category END) AS '1',
count(CASE WHEN Sub_category >= 2 THEN Sub_category END) AS '2+',
count(*)
from table
group by Category, count(CASE WHEN Sub_category = 1 THEN Sub_category END), count(CASE WHEN Sub_category >= 2 THEN Sub_category END), count(*)

Related

Sqlite group by distinct count

I have table of customers operations:
date, client_id, gender
1 1 M
1 1 M
1 2 M
1 2 M
1 3 F
2 1 M
2 1 M
2 1 M
2 2 M
2 2 M
2 3 F
2 3 F
2 4 F
2 5 M
2 5 M
etc
Desired output is:
date, amount of males, (also need amount of females)
1 2 1
2 3 2
I need to group it by date, so i did it, then my goal is to find amount of each gender in each grouped group.
so i tried to do this to count amount of males:
sum(case when gender = 'M' then 1 else NULL end) as 'M%'
but its counted clients id 1 and 2 two times each, but i need to count it distinct.
On example above i expect this to return 2 because 2 male. But it return 4 because distinct construction doesnt work.
I tried this but it doesnt work and count '1' in result:
sum(distinct case when gender = 'M' then 1 else NULL end) as 'M%'
It's easier to count from the distinct rows of the table.
Also, use SQLite's feature to treat boolean expressions as 1 for true and 0 for false so you can sum them instead of using CASE expressions:
SELECT date,
SUM(gender = 'M') [amount of males],
SUM(gender = 'F') [amount of females]
FROM (SELECT DISTINCT date, client_id, gender FROM tablename)
GROUP BY date
See the demo.
You seem to want conditional count(distinct):
select date,
count(distinct case when gender = 'M' then client_id end) as count_m,
count(distinct case when gender = 'F' then client_id end) as count_f
from t
group by date;

Creating a view from two tables, with case and sum on one table

I'm having some troubles trying to create a view from two tables, which includes a sum + case for the first table. I've tried multiple different joins/unions, and I can get just the XTS table to come over, or just the case count scenarios to work, but I cannot get both.
here are the tables. For Table 1, UWI is non-unique. For Table 2, UWI is Unique. new_view is what I'm hoping to achieve for my view.
TABLE 1
UWI ET
1 A
1 B
1 B
2 B
2 C
2 C
TABLE 2
UWI XTS
1 10
2 20
3 10
4 30
new_view
UWI XTS B_COUNT C_COUNT
1 10 4 3
2 20 3 4
3 10 4 5
4 30 3 2
Here's what I'm currently working with.
CREATE VIEW new_view AS
SELECT t1.UWI,
sum(case when t1.ET='B' then 1 else 0 end) as B_COUNT,
sum(case when t1.ET='C' then 1 else 0 end) as C_COUNT,
sum(case when t1.ET='D' then 1 else 0 end) as D_COUNT,
sum(case when t1.ET='E' then 1 else 0 end) as E_COUNT,
sum(case when t1.ET='F' then 1 else 0 end) as F_COUNT
FROM TABLE_1 t1
INNER JOIN (SELECT t2.UWI, t2.XTS AS TSC
from TABLE_2 t2)
on t1.UWI = t2.UWI
group by t1.UWI;
Your sample select does not match your sample data, so this is a guess but I think you just need to move the aggregation into an apply()
select t2.UWI, t2.XTS, s.*
from Table_2 t2
outer apply (
select
sum(case when t1.ET='B' then 1 else 0 end) as B_COUNT,
sum(case when t1.ET='C' then 1 else 0 end) as C_COUNT,
sum(case when t1.ET='D' then 1 else 0 end) as D_COUNT,
sum(case when t1.ET='E' then 1 else 0 end) as E_COUNT,
sum(case when t1.ET='F' then 1 else 0 end) as F_COUNT
from table_1 t1
where t1.UWI = t2.UWI
group by t1.UWI
)s

Group by to include case statement inside SQL statement

I have a table which stores purchase info from sellers and table contains rating to every purchase out of 5 stars. I want to have output Group By sellers and Each sellers good(Above 3) and bad(Below 4) ratings count
PurchaseId SellerId PurchaseRating
1 ABC 2
2 ABC 5
3 DEF 1
4 XYZ 2
5 DEF 4
7 ABC 3
OUTPUT
SellerId TotalOrders AvgRating Above3*(4&5) Below4*(1 to 3)
ABC 3 3.3 1 2
DEF 2 2.5 1 1
XYZ 1 2 0 1
For first 3 columns I am getting result using this
Select SellerId, Count(P.Id) TotalOrders, Avg(PurchaseRating) AvgRating,
CASE WHEN P.PurchaseRating >= 4 THEN 1 ELSE 0 END AS Above3*(4&5)
from
[dbo].[AmazonAbeOrdersPurchaseInfo] P
Where PurchaseRating is not null
Group by P.SellerId
order by TotalOrders desc
Unable to identify how to include case in group by clause.
You are trying to do conditional aggreation. The important thing is that you want the conditional expression inside the aggregate function:
select
sellerId,
count(*) totalOrders,
avg(purchaseRating) avgRating,
sum(case when purchaseRating > 3 then 1 else 0 end) above3,
sum(case when purchaseRating < 4 then 1 else 0 end) below4
from [dbo].[AmazonAbeOrdersPurchaseInfo]
where purchaseRating is not null
group by sellerId
order by totalOrders desc

Complex Sql Query for my project

pls help me to execute this query. I already did everything but turns that my queries not working. Please help.
Table
ITEM
id Name
1 Computer 1
2 Computer 2
STATUS
id Stat_Name
1 Stock In
2 Stock Out
INVENTORY
id Item_id Status_id Quantity
1 1 1 100
2 1 2 200
3 1 1 30
4 2 1 35
5 2 2 36
I want this output
Output:
Item overall_stock_in overall_stock_out
Computer1 130 200
Computer2 35 36
Thanks Guys
Try this:
SELECT
B.Id,
B.Name,
SUM(CASE WHEN A.Status_id=1 THEN A.Quantity ELSE 0 END) AS overall_stock_in,
SUM(CASE WHEN A.Status_id=2 THEN A.Quantity ELSE 0 END) AS overall_stock_out
FROM INVENTORY A JOIN ITEM B ON A.Item_id=B.id
GROUP BY B.Id, B.Name
ORDER BY B.Name;
See DEMO on SQL Fiddle.
Using status table as well
(Updated #cdaiga's query):
SELECT
C.Name,
SUM(CASE WHEN B.Status_Name='Stock In' THEN A.Quantity ELSE 0 END) AS
overall_stock_in,
SUM(CASE WHEN B.Status_Name='Stock Out' THEN A.Quantity ELSE 0 END) AS
overall_stock_out
FROM INVENTORY A JOIN ITEM C ON A.Item_id=C.id
JOIN "status" B ON A.Status_id=B.id
GROUP BY C.Name
ORDER BY C.Name;

Counting the 1's and 2's in a Table

I have this table
SECTION
Which consist of field called
Semester
2
1
1
1
2
1
1
2
2
2
1
2
I need sql to count how many 1's and how many 2's are there
And out put like this
semester 1 | semester 2
6 | 6
Demo here:After testing
select
sum(Case when semester=1 then 1 else 0 end) as 'Semester1',
sum(Case when semester=2 then 1 else 0 end) as 'Semester2'
from section
Try out this:
Select sum(case when semster =1 then 1 else 0 end) as semster1 ,
sum(case when semster =2 then 1 else 0 end) as semster2
from section;
Assuming there are more values than 1 and 2, and that the values are integers:
SELECT semester, count(*)
FROM section
WHERE semester = 1 OR semester = 2
GROUP BY semester