SQLITE PIVOT TABLE WITH COUNT - sql

Can you help me plase ?
I want a result like the table below with Sqlite
.
task_name
status_code
department
1
A
START
PUR
2
B
START
ENG
3
C
FINISH
ENG
4
D
NOT ACTIVE
DES
5
E
START
DES
6
F
NOT ACTIVE
ENG
.
START
FINISH
NOT ACTIVE
TOTAL
PUR
1
1
ENG
1
1
1
3
DES
1
1
2

You can use conditional aggregation such as
SELECT department,
SUM(CASE WHEN status_code = 'START' THEN 1 END) AS "START",
SUM(CASE WHEN status_code = 'FINISH' THEN 1 END) AS "FINISH",
SUM(CASE WHEN status_code = 'NOT ACTIVE' THEN 1 END) AS "NOT ACTIVE",
COUNT(*) AS total
FROM t
GROUP BY department
Demo

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;

Trouble ordering GROUP BY, ORDER BY AND JOIN

i'm having trouble ordering a query.
I have this table (AttendanceLog);
ClassID | StudentPin | Status
69 1 YES
8 2 NO
10 2 NO
17 3 NO
43 5 YES
58 6 YES
and this table (Students):
STUDENTPIN | FNAME | LNAME | INTERNATIONAL
1 X X NO
2 X X YES
3 X X NO
4 X X YES
I want to find out the which INTERNATIONAL students (Fname, Lname and StudentPIN) have missed 10 or more classes (attendancelog status being no).
Currently I have this (below) which tells me the studentPIN and the number of classes attended and no attended by each student, however I am unable to join the two tables together.
SELECT
ATTENDANCELOG.studentpin,
SUM(CASE WHEN status = 'YES' THEN 1 ELSE 0 END) AS number_of_yes,
SUM(CASE WHEN status = 'NO' THEN 1 ELSE 0 END) AS number_of_no
FROM attendancelog
GROUP BY ATTENDANCELOG.studentpin
ORDER BY ATTENDANCELOG.studentpin
Thanks!
you could use a join
SELECT
ATTENDANCELOG.studentpin,
Students.FNAME,
Students.LNAME,
SUM(CASE WHEN status = 'YES' THEN 1 ELSE 0 END) AS number_of_yes,
SUM(CASE WHEN status = 'NO' THEN 1 ELSE 0 END) AS number_of_no
FROM attendancelog
INNER JOIN Students ON Students.STUDENTPIN = attendancelog.StudentPin
and INTERNATIONAL='YES'
GROUP BY ATTENDANCELOG.studentpin, Students.FNAME, Students.LNAME
ORDER BY ATTENDANCELOG.studentpin
Join on student pin, put your international = 'YES' filter in the where clause, and filter for more than 10 misses in a having clause. You can also shorten the case expressions a little:
select a.studentpin
, s.fname, s.lname, s.international
, count(case a.status when 'YES' then 1 end) as attended
, count(case a.status when 'NO' then 1 end) as missed
from attendancelog a
join students s on s.studentpin = a.studentpin
where international = 'YES'
group by s.fname, s.lname, s.international, a.studentpin
having count(case a.status when 'NO' then 1 end) > 10
order by s.fname, s.lname, a.studentpin;

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

JOIN the same table

Hi I have table badge which has columns facility and date (other columns are not important).
I want to select count(*) which should look like this:
DATE | FACILITY 4 COUNT | FACILITY 1 COUNT
12/12/2012 | 234 | 647
I wrote two separate queries for facility 1 and 4 but how to join it to one Query;
SELECT
date,
COUNT(*)
FROM badge
WHERE facility = 3
AND xtype = 19
AND date BETWEEN 'some_date' AND 'some_date'
GROUP BY date
another query differs only in the value of facility.
EDIT: It's informix database.
SELECT date,
SUM(case when facility = 1 then 1 else 0 end) "FACILITY 1 COUNT",
SUM(case when facility = 4 then 1 else 0 end) "FACILITY 4 COUNT"
FROM badge
WHERE (facility = 1 OR facility = 4)
AND xtype = 19
AND date BETWEEN 'some_date' AND 'some_date'
GROUP BY date
give this a try,
SELECT date,
SUM(CASE WHEN facility = 3 THEN 1 ELSE 0 END) `Facility 3 Count`,
SUM(CASE WHEN facility = 1 THEN 1 ELSE 0 END) `Facility 1 Count`
FROM badge
WHERE xtype = 19 AND
date BETWEEN 'some_date' AND 'some_date'
GROUP BY date