Finding fields to update based on combinations - sql

I need to be able to display in my results who needs updates. I have a temp table I created that looks like this. The rule is per ID they cannot have more than 1 MASTER = 1. They must have FULLTIME = 1 on that record and all other records will be FULLTIME = 0 and PARTTIME = 1. This is quite difficult because you have to compare across multiple IDs.
I've tried combinations using maxes, count distinct, subqueries, etc. No luck getting it done. I've even tried to do some manipulation in Excel but it's totally confusing to me.
select distinct
x.ID,
COUNT(x.ID) AS ID_Count
from #FT0PT1M1Version2 as x
join (
select
ID, NAME, MASTER, FULLTIME, PARTTIME
from #FT0PT1M1Version2
WHERE E = 'P'
GROUP BY
ID, NAME, MASTER, FULLTIME, PARTTIME
HAVING COUNT(ID) = '1'
) as y
on x.ID = y.ID
WHERE
x.PARTTIME = '1' and
x.MASTER = '1'
group by x.ID
HAVING COUNT(x.ID) = '1'
order by 1
Temp Table
ID NAME MASTER FULLTIME PARTTIME
1 JAMES JONES 0 1 0
1 JAMES JONES 1 0 1
1 JAMES JONES 0 0 1
2 MICHEAL JORDAN 1 1 0
2 MICHEAL JORDAN 0 0 1
2 MICHEAL JORDAN 0 0 1
3 JOHN DOE 1 1 0
3 JOHN DOE 0 0 1
Expected Results
ID NAME MASTER FULLTIME PARTTIME UPDATE
1 JAMES JONES 0 1 0 Y
1 JAMES JONES 1 0 1 Y
1 JAMES JONES 0 0 1 N
2 MICHEAL JORDAN 1 1 0 N
2 MICHEAL JORDAN 0 0 1 N
2 MICHEAL JORDAN 0 0 1 N
3 JOHN DOE 1 1 0 N
3 JOHN DOE 1 0 1 Y

You could try below query but I would prefer to put a check constraint on columns like check if master=1 then update ='Y' something like that.
SELECT ID, NAME,
CASE
WHEN (MASTER=1 AND FULLTIME=1 And PARTTIME=0)
OR (MASTER=0 AND FULLTIME=0 And PARTTIME=1)
Then 'N'
ELSE 'Y'
END as "Update"
from table group by ID, NAME, Update
Having sum(Master) = 1;

Related

Count children from hierarchy path

I have a table like this:
id
name
path
1
John
/1
2
Mark
/2
3
Kevin
/1/3
4
Sarah
/1/3/4
5
Andy
/2/5
...
...
...
So, I can say that Sarah is Kevin's child which is John's child.
I would like to have this:
id
name
path
number of children
1
John
/1
2
2
Mark
/2
1
3
Kevin
/1/3
1
4
Sarah
/1/3/4
0
5
Andy
/2/5
0
...
...
...
...
TASK NUMBER 2:
Let's say that I have this table too
id
income
user_id
1
200
1
2
120
1
3
340
2
4
500
3
5
600
5
6
80
5
I can say that John has a Total income of 320$, but if I also want to count John's children, it is 820$ (because id =3 is John's child). So, I would also like a query where I can count all the hierarchical incomes.
You can do:
select
t.*,
(select count(*) from t c where c.path like t.path || '/%') as c_count,
i.income + (
select coalesce(sum(i.income), 0) from t c join i on i.user_id = c.id
where c.path like t.path || '/%'
) as c_income
from t
left join (
select user_id, sum(income) as income from i group by user_id
) i on i.user_id = t.id
Result:
id name path c_count c_income
--- ------ ------- -------- --------
1 John /1 2 820
2 Mark /2 1 1020
3 Kevin /1/3 1 500
4 Sarah /1/3/4 0 null
5 Andy /2/5 0 680
See example at DB Fiddle.

Group by one column and return several columns on multiple conditions - T-SQL

I have two tables which I can generate with SELECT statements (joining multiple tables) as follows:
Table 1:
ID
Site
type
time
1
Dallas
2
01-01-2021
2
Denver
1
02-01-2021
3
Chicago
1
03-01-2021
4
Chicago
2
29-11-2020
5
Denver
1
28-02-2020
6
Toronto
2
11-05-2019
Table 2:
ID
Site
collected
deposited
1
Denver
NULL
29-01-2021
2
Denver
01-04-2021
29-01-2021
3
Chicago
NULL
19-01-2020
4
Dallas
NULL
29-01-2019
5
Winnipeg
13-02-2021
17-01-2021
6
Toronto
14-02-2020
29-01-2020
I would like the result to be grouped by Site, having on each column the COUNT of type=1 , type=2, deposited and collected, all of the 4 columns between a selected time interval. Example: (interval between 01-06-2020 and 01-06-2021:
Site
type1
type2
deposited
collected
Dallas
0
1
0
0
Denver
1
0
2
1
Chicago
1
1
0
0
Toronto
0
0
0
0
Winnipeg
0
0
1
1
How about union all and aggregation?
select site,
sum(case when type = 1 then 1 else 0 end) as type_1,
sum(case when type = 2 then 1 else 0 end) as type_2,
sum(deposited) as deposited, sum(collected) as collected
from ((select site, type, 0 as deposited, 0 as collected
from table1
) union all
(select site, null,
(case when deposited is not null then 1 else 0 end),
(case when collected is not null then 1 else 0 end)
from table2
)
) t12
group by site;
Combine your tables 1 and 2 with a join on Site
Use COUNT(CASE WHEN type = 1 then 1 END) as type1 and a similar construct for type 2
Use COUNT(CASE WHEN somedate BETWEEN '2020-06-01' and '2021-06-01' then 1 END) as ... for your dates

getting count from multiple tables on the basis of gender and scored percentages

I have to use 3 tables
1. tbl_school_info
school_id school_name
1 St. Joseph
2 White Cross
2. tbl_student_info
student_id school_id student_name student_gender
1 1 maria liejal F
2 1 eerika carthy F
3 1 heron M
4 2 Glenn hui M
5 2 joseph M
6 2 christii F
7 2 hina moggy F
3. tbl_student_marks
marks_id school_id student_id scored_percentage
1 1 1 78
2 1 2 79
3 1 3 20
4 2 4 65
5 2 5 78
6 2 6 84
7 2 7 83
The result I need is the male, female and total student count in each school, male female passed student count and highest percentage scored male female students. The result will be like this ::
school_name || male_stud_cnt || female_stud_cnt || passed_male_cnt || passed_female_cnt || top_percentage_male ||top_percentage_female
St. Joseph 1 2 0 2 20 79
White Cross 2 2 2 2 78 84
The students whose score is below 35% has failed in the exam. How can I write query to get this result ? Is it possible to get such result using SQL query in MS SQL Server? I am unable to get the counts, how can I write such query ?
You can try using condtional aggregation with case when expression
with cte as
(
select school_name,a.student_id,student_gender,scored_percentage from
tbl_student_marks a inner join tbl_student_info b
on a.student_id=b.student_id
inner join tbl_school_info c on a.school_id=b.school_id
)
select school_name,
count(case when student_gender='M' then student_id end) as male_stud_cnt,
count(case when student_gender='F' then student_id end) as female_stud_cnt,
count(case when student_gender='M' and scored_percentage>35 then student_id end) as passed_male_cnt,
count(case when student_gender='F' and scored_percentage>35 then student_id end) as passed_female_cnt,
max(case when student_gender='M' then scored_percentage end) as top_percentage_male,
max(case when student_gender='F' then scored_percentage end) as top_percentage_female
from cte
group by school_name

Hive SQL: Select all rows before event

In Hive, I have the following data
sess,person,type,number
a mary I 1
a mary I 2
a mary V 3
a mary V 4
b mary I 1
b mary V 2
b mary C 3
a john I 1
a john I 2
a john V 3
a john V 4
b john I 1
b john V 2
b john C 3
How do I select everything for each person and session up to and including the first type=V? The output should look like
sess,person,type,number
a mary I 1
a mary I 2
a mary V 3
b mary I 1
b mary V 2
a john I 1
a john I 2
a john V 3
b john I 1
b john V 2
You can use window functions:
select t.*
from (select t.*,
min(case when type = 'V' then number end) over (partition by session, person order by number) as min_aid
from t
) t
where min_aid is null or number <= aid;

SQL: Select criteria for two tables, compare 1 field, return using condition

These are the tables in the query. Want to compare the ID_Skills in the following 2 tables. And in the returning table from the select query, display ID_Skills with condition saying whether or not TrainingRequired (Yes/No)
tblEmployeeCurrentSkills
ID_EmployeeCurrentSkills ID_Employee ID_Skills
1 1 1
2 1 2
3 2 1
tblSkillsRequired
ID_SkillsRequired ID_Employee ID_Skills ID_Position
1 1 1 1
2 1 2 1
3 1 3 1
4 2 3 2
tblSkills
ID_Skills Skill
1 Reading
2 Wiring
3 Stapling
tblPosition
ID_Position Position
1 Tech1
2 Stapler
tblEmployee
ID_Employee EmployeeName
1 Hannah
2 Bob
SQL for qrySkillsGap table - determines whether training is necessary
SELECT tblEmployee.[Employee Name],
tblSkillsRequired.ID_Skills,
tblSkills.Skill,
IIf([tblEmployeeCurrentSkills].[ID_Skills]
Like [tblSkillsRequired].[ID_Skills],"No","Yes") AS TrainingRequired
FROM (tblSkills
INNER JOIN tblSkillsRequired
ON tblSkills.ID_Skills = tblSkillsRequired.ID_Skills)
INNER JOIN (tblEmployee INNER JOIN tblEmployeeCurrentSkills
ON tblEmployee.ID_Employee = tblEmployeeCurrentSkills.ID_Employee)
ON tblSkills.ID_Skills = tblEmployeeCurrentSkills.ID_Skills;
This is the current output:
EmployeeName ID_Skill TrainingRequired
Hannah 1 No
Hannah 1 No
Hannah 2 No
Bob 1 No
Bob 1 No
I want it to display this:
EmployeeName ID_Skill TrainingRequired
Hannah 1 No
Hannah 2 No
Hannah 3 Yes
Bob 1 No
Bob 3 Yes
Thanks for any help!
I was able to create the tables you provided and used a union to bring together the employee skills and required skills.
SELECT te.EmployeeName
, emp.ID_Skills
, CASE WHEN MIN(emp.TrainingRequired) = 0 THEN 'No'
ELSE 'Yes'
END AS TrainingRequired
FROM dbo.tblEmployee AS te
JOIN (SELECT tecs.ID_Employee
, tecs.ID_Skills
, 0 AS TrainingRequired
FROM dbo.tblEmployeeCurrentSkills AS tecs
UNION
SELECT tsr.ID_Employee
, tsr.ID_Skills
, 1 AS TrainingRequired
FROM dbo.tblSkillsRequired AS tsr
) emp
ON te.ID_Employee = emp.ID_Employee
GROUP BY te.ID_Employee
, te.EmployeeName
, emp.ID_Skills
ORDER BY te.ID_Employee
, emp.ID_Skills