How to get sum of the same Unit's - sql

I have a table which contains a college's departments and their units and sub-units.
OrganizationID ParentUnit Unit ChildUnit UnitName
10 1 0 0 Education
12 1 1 0 Sports
24 1 2 0 Mathmatics
28 1 3 0 Science
35 1 3 1 Physics
51 1 4 0 Arts
66 1 4 1 Music
69 1 4 2 Painting
84 8 0 0 Business & Administration
88 8 1 0 Administration
96 8 1 1 Public Administration
107 8 1 2 Local Managements
110 8 2 0 Finance
119 8 2 1 Accounting
124 8 2 2 Marketing
I have another table which contains the student information of that college.
StudentID OrganizationID
1 12
2 12
3 24
5 28
6 35
8 51
9 66
31 69
34 96
45 88
57 96
66 107
69 110
72 69
74 124
I want to get student counts for each unit. If a studutent's Organization is a ChildUnit it should be added to current Unit. If ChildUnit is greater than0 corresponding student count should be added to same Unit For example Physics is a child of Science. Then Science student count should return 2.
My target data table should look like as the following
ParentUnit UnitName StudentCount
------------------------------------------------------
Education Sports 2
Education Mathmatics 1
Education Science 2
Education Arts 4
Business & Administration Administration 4
Business & Administration Finance 2
I have done it in programmatic way. There are many for and if loops. Then I started to think whether it could be done with a smarter sql query.

That doesn't look so difficult. You are looking for the student count per ParentUnit + Unit. Then the name for such a group is the record where the level (the ChildUnit) is zero. You get that record with a CASE construct, then use MIN or MAX, because you need an aggregate function here (there should be exactly one record per group anyhow, so MIN = MAX).
select
min(case when o.childunit = 0 then o.unitname end) as unitname,
count(*) as studentcount
from organization o
inner join student s on s.organizationid = o.organizationid
group by o.parentunit, o.unit;
To include the parent unit name:
select
(
select unitname
from organization po
where po.parentunit = o.parentunit
and po.unit =0
and po.childunit = 0
) as parentunitname,
min(case when o.childunit = 0 then o.unitname end) as unitname,
count(*) as studentcount
from organization o
inner join student s on s.organizationid = o.organizationid
group by o.parentunit, o.unit;
Or:
select
min(po.unitname) as parentunitname,
min(case when o.childunit = 0 then o.unitname end) as unitname,
count(*) as studentcount
from organization o
inner join student s on s.organizationid = o.organizationid
inner join
(
select parentunit, unitname
from organization
where unit = 0 and childunit = 0
) po on po.parentunit = o.parentunit
group by o.parentunit, o.unit;

Related

SQL Query group by Postcode multiple Sums

I have following data:
ID
Weight
Postcode
Year
1
23
56222
2022
2
24
56332
2022
3
50
56442
2022
4
22
62331
2022
5
80
72130
2022
and i want to query it that i get the data like this:
Grouped by Postcode and splitted in different weight ranges.
and then just Count of the amount of entrys.
Postcode/Weight
0-20
21-40
41-60
61-80
81-100
56
0
2
1
0
0
62
0
1
0
0
0
72
0
0
0
1
0
Is there any way to query this in SQL?
Try this one.
Query:
SELECT
p.postcode,
COUNT(p20.id) as "0-20",
COUNT(p40.id) as "21-40",
COUNT(p60.id) as "41-60",
COUNT(p80.id) as "61-80",
COUNT(p100.id) as "81-100"
FROM packs p
LEFT JOIN packs p20 ON p20.postcode=p.postcode AND p20.weight < 20
LEFT JOIN packs p40 ON p40.postcode=p.postcode AND p40.weight >= 21 AND p40.weight <= 40
LEFT JOIN packs p60 ON p60.postcode=p.postcode AND p60.weight >= 41 AND p60.weight <= 60
LEFT JOIN packs p80 ON p80.postcode=p.postcode AND p80.weight >= 61 AND p80.weight <= 80
LEFT JOIN packs p100 ON p100.postcode=p.postcode AND p100.weight >= 81 AND p100.weight <= 100
GROUP by postcode;
Result:
Table

Select sum shown null value in left join SQL statement

I have three tables and expecting the result as below but i do not know how to correct my sql statement.
select history.company,history.ghacct,rpt_revenue.revenue,rpt_revenue.other, isnull(guest.stay,0) as stay, isnull(guest.nights,0) as nights
from history
left join(select company,count(*) as stay,sum(nights) as nights from guest group by company) guest on guest.company=history.company
left join (select ghacct,sum(revenue) as revenue, sum(other) as other
from rpt_revenue group by ghacct) rpt_revenue on rpt_revenue.ghacct=history.ghacct
where history.type='c' group by history.company, history.ghacct,rpt_revenue.revenue, rpt_revenue.other,guest.stay,guest.nights order by history.company asc;
history
ghacct company type
33 JOINT LTD 10010205687 c
3B GLOBAL 10010350619 c
3E FASHION 10010244145 c
3P INT'L 10010112089 c
guest
company stay nights
33 JOINT LTD 01/01/2009 1
33 JOINT LTD 01/06/2009 1
3B GLOBAL 10/02/2019 2
3E FASHION 09/25/2008 6
3P INT'L 08/26/2009 3
3P INT'L 04/26/2010 9
rpt_revenue
ghacct revenue other
10010205687 20 10
10010205687 10 10
10010350619 30 2
10010244145 15 3
10010112089 16 8
10010112089 4 2
Result
company ghacct revenue other stay nights
33 JOINT LTD 10010205687 NULL NULL 2 2
3B GLOBAL 10010350619 NULL NULL 1 2
3E FASHION 10010244145 NULL NULL 1 6
3P INT'L 10010112089 NULL NULL 2 12
Expected result
company ghacct revenue other stay nights
33 JOINT LTD 10010205687 30 20 2 2
3B GLOBAL 10010350619 30 2 1 2
3E FASHION 10010244145 15 3 1 6
3P INT'L 10010112089 20 10 2 12
I think the main problem with your current query lies in the GROUP BY clause, which should really only be aggregating by company and account. In addition, you might want to use ISNULL for the revenue and other amount, as you are already doing so for stay and nights.
SELECT
h.company,
h.ghacct,
ISNULL(rr.revenue, 0) AS revenue,
ISNULL(rr.other, 0) AS other,
ISNULL(g.stay, 0) AS stay,
ISNULL(g.nights, 0) AS nights
FROM history h
LEFT JOIN
(
SELECT company, COUNT(*) AS stay, SUM(nights) AS nights
FROM guest
GROUP BY company
) g
ON g.company = h.company
LEFT JOIN
(
SELECT ghacct, SUM(revenue) AS revenue, SUM(other) AS other
FROM rpt_revenue
GROUP BY ghacct
) rr
ON rr.ghacct = h.ghacct
WHERE
h.type = 'c'
GROUP BY
h.company,
h.ghacct
ORDER BY
h.company;

SQL LOGIC using 3conditions

course_completions CC
id coursemodid userid state timemodified
370 23 2 1 1433582890
329 24 89 1 1427771915
333 30 39 1 1428309816
332 32 39 1 1428303307
327 33 40 1 1427689703
328 34 89 1 1427710711
303 35 41 1 1410258482
358 36 99 1 1432020067
365 25 2 1 1433142455
304 26 69 1 1410717866
353 37 95 1 1430387005
416 38 2 1 1438972465
300 27 70 1 1409824001
302 29 74 1 1412055704
297 30 2 1 1409582123
301 133 41 1 1410255923
336 133 91 1 1428398435
364 133 40 1 1433142348
312 133 85 1 1425863621
course_modules CM
id course
23 6
24 6
25 6
26 6
27 6
28 6
29 8
30 8
31 8
32 8
33 8
34 5
35 5
36 5
37 5
38 5
39 9
40 9
41 9
course_mod_settings CMS
id course modinstance
27 8 30
28 8 31
29 8 32
30 8 33
31 6 23
32 6 24
33 6 25
34 6 26
35 6 27
36 6 28
37 9 39
38 9 40
39 9 41
I need the count of each user has Completed modules, Inprocess modules and Notstarted modules for each course, where getting the count of userids from table CC by taking courseia from table CM, get number of modules that an user has completed from each course.
(A course can have morethan one module and a course can have number of users attempted all modules, few modules or not attempted at all).
So, I need number of users - has done number of modules - in a course. (3 logics)
Completed.Users means : If number of modules attempted is equal to number of modinstance from table CMS (ex: no. of modules attempted by a user per course= 9, no.modinstance = 9. Because 7 is not equal to 9, They are completed.)
Inprocess.Users means : Number of modules attempted should be >0, but not equal to [count(modinstance) per course] (ex: no. of modules attempted by a user per course= 7 , no.modinstance = 9. Because 7 is not equal to 9, They are Inprocess.)
Notstarted.Users means : Number of modules attempted should be equal to 0, (ex: no. of modules attempted by a user per course= 0. They are Notstarted).
OUTPUT :
Course No.Completed.Users No.Inprocess.Users No.Notstarted.Users
5 65 32 6
6 40 12 15
8 43 56 0
9 0 7 9
Sir, this is a very critical logic that I was trying, I couldn't get a solution. I hope stackoverflow developers could help me out. I tried with my query :
SELECT cm.course AS "Course",
(CASE WHEN
(SELECT count(cms.id) FROM course_mod_settings cms) =
(SELECT count(cmc.coursemodid) FROM course_completions cc JOIN course_modules cm ON cmc.coursemodid = cm.id WHERE cmc.state=1 )
THEN COUNT(SELECT count(cmc.coursemodid) FROM course_completions cc JOIN course_modules cm ON cmc.coursemodid = cm.id WHERE cmc.state=1 ) END) AS "No.Completed.Users",
(CASE WHEN
(SELECT count(cms.id) FROM course_mod_settings cms) > 0 AND
(SELECT count(cms.id) FROM course_mod_settings cms) !=
(SELECT count(cmc.coursemodid) FROM course_completions cc JOIN course_modules cm ON cmc.coursemodid = cm.id WHERE cmc.state=1 )
THEN COUNT(SELECT count(cmc.coursemodid) FROM course_completions cc JOIN course_modules cm ON cmc.coursemodid = cm.id WHERE cmc.state=1 ) END) AS "No.Inprocess.Users",
(CASE WHEN
(SELECT count(cms.id) FROM course_mod_settings cms) = 0
THEN COUNT(SELECT count(cmc.coursemodid) FROM course_completions cc JOIN course_modules cm ON cmc.coursemodid = cm.id WHERE cmc.state=1 ) END) AS "No.Notstarted.Users"
FROM
mdl_course c
GROUP BY c.id
SQL Fiddle
SELECT course AS "Course",
SUM(CASE WHEN completion_count = module_count THEN 1 ELSE 0 END) AS "No.Completed.Users",
SUM(CASE WHEN completion_count > 0 AND completion_count < module_count THEN 1 ELSE 0 END) AS "No.Inprocess.Users",
SUM(CASE WHEN completion_count = 0 THEN 1 ELSE 0 END) AS "No.Notstarted.Users"
FROM (SELECT course, COUNT(*) AS module_count
FROM course_modules cm
GROUP BY course) course_module_counts JOIN
(SELECT cm.course AS courseid, users.id AS userid, SUM(CASE WHEN cc.state = 1 THEN 1 ELSE 0 END) completion_count
FROM ((SELECT DISTINCT userid AS id FROM course_completions) users CROSS JOIN course_modules cm) LEFT JOIN course_completions cc ON users.id = cc.userid AND cc.coursemodid = cm.id
GROUP BY cm.course, users.id) course_completion_counts
ON course_module_counts.course = course_completion_counts.courseid
GROUP BY course
gives this output, which matches the limited dataset that you provided in your question.
| course | No.Completed.Users | No.Inprocess.Users | No.Notstarted.Users |
|--------|--------------------|--------------------|---------------------|
| 5 | 0 | 5 | 7 |
| 6 | 0 | 4 | 8 |
| 8 | 0 | 4 | 8 |
| 9 | 0 | 0 | 12 |

SQL help needed (oracle application express)

Given this information
Among all projects chen work on, list the name of project that has lowest budget.
EMPID NAME SALARY DID
---------------------------
1 kevin 32000 2
2 joan 42000 1
3 brian 37000 3
4 larry 82000 5
5 harry 92000 4
6 peter 45000 2
7 peter 68000 3
8 smith 39000 4
9 chen 71000 1
10 kim 46000 5
11 smith 46000 1
PID EMPID HOURS
-----------------
3 1 30
2 3 40
5 4 30
6 6 60
4 3 70
2 4 45
5 3 90
3 3 100
6 8 30
4 4 30
5 8 30
6 7 30
6 9 40
5 9 50
4 6 45
2 7 30
2 8 30
2 9 30
1 9 30
1 8 30
1 7 30
1 5 30
1 6 30
2 6 30
PID PNAME BUDGET DID
---------------------------------------
1 DB development 8000 2
2 network development 6000 2
3 Web development 5000 3
4 Wireless development 5000 1
5 security system 6000 4
6 system development 7000 1
This is what I've written so far:
select min(budget), pname
from employee e, workon w, project p
where e.empid = w.empid and p.pid = w.pid and name = 'chen'
group by pname
Which gives me
MIN(BUDGET) PNAME
--------------------------
8000 DB development
6000 security system
6000 network development
7000 system development
How can I select just the lowest (the minimum budget) from these values? Thanks for any help.
use explicit join syntax,
Here is one way using analytic function row_number, sequence number given based on budget with lowest being the first.
with cte
as
(
select row_number() over ( order by budget asc) as rn, pname, budget
from employee e
join workon w
on e.empid = w.empid
join project p
on p.pid = w.pid and name = 'chen'
)
select pname, budget from cte where rn =1
select budgets.budget, budgets.pname
from
(
select min(budget) as budget, pname
from employee e, workon w, project p
where e.empid = w.empid and p.pid = w.pid and name = 'chen'
group by pname
order by budget asc
) budgets
where rownum = 1

SQL terminology to combine a NOT EXIST query with latest value

I am a beginner with basic knowledge.
I have a single table that I am trying to pull all UID's that have not had a particular code in the table within the past year.
My table looks like this: (but much larger of course)
FACID DPID EID DID UID DT Code Units Charge ET Ord
1 1 6 2 1002 15-Mar-07 99204 1 180 09:36.7 1
1 1 7 5 10004 15-Mar-07 99213 1 68 02:36.9 1
1 1 24 55 25887 15-Mar-07 99213 1 68 43:55.3 1
1 1 25 2 355688 15-Mar-07 99213 1 68 53:20.2 1
1 1 26 5 555654 15-Mar-07 99213 1 68 42:22.6 1
1 1 27 44 135514 15-Mar-07 99213 1 68 00:36.8 1
1 1 28 2 3244522 15-Mar-07 99214 1 98 34:59.4 1
1 1 29 5 235445 15-Mar-07 99213 1 68 56:42.1 1
1 1 30 3 3214444 15-Mar-07 99213 1 68 54:56.5 1
1 1 33 1 221444 15-Mar-07 99204 1 180 37:44.5 1
I am attempting to use the following, but this is not working for my time frame limits.
select distinct UID from PtProcTbl
where DT<'20120101'
and NOT EXISTS (Select Distinct UID
where Code in ('99203','99204','99205','99213',
'99214','99215','99244','99245'))
I need to know how to make sure the UID's that I am pulling are the ones don't have a DT after the 1/1/2012 cut off date that contains one of the NOT Exists codes.
The above query returned UID's that actually dates after 1/1/2012 that does contain one of the above codes...
Not sure what I am doing wrong or if I am totally off base on this..
Thanks in advance.
Are you sure you need the NOT EXISTS? How about instead:
AND Code NOT IN ('99203','99204','99205','99213','99214','99215','99244','99245')