I have an employee table called resources. the resources table has info about the resources and their managers along with their levels.
I need a query to always return the L7 manager (not the immediate manager)
This is what I've come up with so far.
But the problem in this is because of the where condition where mananger_level = 7 and Im joining cte with resource_alias, a few resources are getting filtered out
with recursive cte (resource_alias, manager_alias, manager_level) as (
select resource_alias,
manager_alias,
manager_level
from rpt.resources
union all
select r.resource_alias,
r.manager_alias,
r.manager_level
from rpt.resources r
inner join cte
on r.resource_alias = cte.manager_alias
)
select rr.resource_full_name,
m.resource_id,
rr.manager_level,
m.resource_alias,
r.resource_type,
r.resource_home_city,
r.resource_home_state,
r.resource_home_country,
r.resource_home_zip,
r.eligible_program_id,
rr.manager_alias,
rr.manager_name,
m.skip_reason
from rpt.model_input_resources m
join rpt.resource_pool r on m.resource_alias = r.resource_alias
join rpt.programs p on find_in_set(p.id, r.eligible_program_id) > 0
join rpt.resources rr on rr.resource_alias = r.resource_alias
left join cte on cte.resource_alias = rr.resource_alias
where m.run_id in ('202301291674972257') and cte.manager_level = 7
and m.skip_reason <> 'Good' and r.portfolio in ('NACF') and r.resource_type in ('Launch Manager') and
m.resource_id not in ( select distinct(resource_id) from rpt.project_resource_recommendation_runs where run_id in ('202301291674972257') ) -- and p.program in (?)
group by m.resource_alias,
r.resource_type,
r.resource_home_city,
r.resource_home_state,
r.resource_home_country,
r.resource_home_zip,
rr.manager_alias,
m.skip_reason
Related
I want to SUM these two different queries. I joined both with USING function. but it shows the two results in two different cells.
I need a single result to SUM these two queries where the answer should be 4069.
The CODE
SELECT COUNT(*) Active_Projects FROM
(SELECT ProjectID, ProjectStatusID, ClientID
FROM project) a
INNER JOIN
(SELECT ProjectStatusID, ClientID, ProjectStatusName
FROM ProjectStatus) b
ON (a.ProjectStatusID = b.ProjectStatusID AND a.ClientID = b.ClientID)
WHERE ProjectStatusName LIKE '%active%'
AND a.ClientID = 4
UNION
SELECT COUNT(*) Total_Projects
FROM Project
WHERE ClientID=4
Picture
Screenshot
Screenshot Here
Try this. select query1+ query2;
select (
SELECT COUNT(*) Active_Projects FROM
(SELECT ProjectID, ProjectStatusID, ClientID
FROM project) a
INNER JOIN
(SELECT ProjectStatusID, ClientID, ProjectStatusName
FROM ProjectStatus) b
ON (a.ProjectStatusID = b.ProjectStatusID AND a.ClientID = b.ClientID)
WHERE ProjectStatusName LIKE '%active%'
AND a.ClientID = 4
) + (
SELECT COUNT(*) Total_Projects
FROM Project
WHERE ClientID=4) ;
I have four tables, three of which are pretty static: haul_types, dumpster_type_team (the dumpster_type_team has the many-to-many relationship between dumpster_types and teams), and users. The fourth table, hauls, has transactional data.
haul_types:
id
name
dumpster_type_team:
id
dumpster_type_id
team_id
users:
id
first_name
last_name
is_driver
team_id
hauls:
haul_type_id
haul_status_id
set_dumpster_type_id
completed_driver_id
team_id
I would like a query that has a combination of dumpster_types, haul_types, and drivers (users) and a count of the hauls they were involved in. In some cases, there should be a count of zero because some drivers haven't completed hauls for every haul_type / dumpster type combination.
Here's the query I have so far that seems to be behaving as if it is an inner join because the records are getting filtered to only show where there are matches:
SELECT
c.haul_type_id,
c.dumpster_type_id,
c.driver_id,
count(h.id) AS haul_count
FROM
hauls h
RIGHT JOIN ( SELECT DISTINCT
ht.id AS haul_type_id,
dtt.dumpster_type_id AS dumpster_type_id,
dtt.team_id AS team_id,
u.id AS driver_id
FROM
haul_types ht
CROSS JOIN dumpster_type_team dtt
CROSS JOIN users u
WHERE
u.team_id = dtt.team_id
AND u.is_driver = TRUE) c ON c.haul_type_id = h.haul_type_id
AND c.dumpster_type_id = h.set_dumpster_type_id
AND c.driver_id = h.completed_driver_id
AND c.team_id = h.team_id
WHERE
h.team_id = 9
AND h.haul_status_id = 3
AND h.completed_driver_id IS NOT NULL
GROUP BY
c.haul_type_id, c.dumpster_type_id, c.driver_id
When I run the subquery in isolation:
SELECT DISTINCT
ht.id AS haul_type_id,
dtt.dumpster_type_id AS dumpster_type_id,
dtt.team_id AS team_id,
u.id AS driver_id
FROM
haul_types ht
CROSS JOIN dumpster_type_team dtt
CROSS JOIN users u
WHERE
u.team_id = dtt.team_id
AND u.is_driver = TRUE
I get the results I want: a row for each permutation of haul_type, dumpster_type, driver_id, and team_id. However, when I run the entire query, I get filtered results despite the right join.
What I would like to have is the following:
If I have 4 haul_types: delivery, swap, live, pickup
and 2 dumpster_types: 10YD, 15YD
and 2 drivers: 1, 2
I would like a haul count for the combination of haul_type, dumpster_type, and driver. If there are no hauls matching the row, show 0:
Any help is appreciated. Thank you
The description of the question and the query seem to have little to do with each other. I don't know what a "pivot table" is supposed to be.
I would like a query that has a combination of dumpster_types, haul_types, and drivers (users) and a count of the hauls they were involved in.
This sounds like a cross join to generate the rows and then a left join/group by to calculate the results:
select d.dumpster_id, ht.haul_type_id, d.driver_id, count(h.driver_id)
from dumpster_types d cross join
haul_types ht cross join
drivers d left join
hauls h
on h.dumpster_id = d.dumpster_id and
h.haul_type_id = ht.haul_type_id and
h.driver_id = d.driver_id
group by d.dumpster_id, ht.haul_type_id, d.driver_id;
Running the query #GordonLinoff provided, exposed the issue I was facing - when applying a where clause on the top level query, the results were getting filtered to only matches. I moved the where clause to individual subqueries and now I am getting all expected results.
Not sure if this is the most efficient way to write it but it yields the correct results:
SELECT
d.dumpster_type_id,
ht.id AS haul_type_id,
u.id AS driver_id,
count(h.id) AS haul_count
FROM (
SELECT
dumpster_type_id,
team_id
FROM
dumpster_type_team
WHERE
team_id = 9) d
CROSS JOIN haul_types ht
CROSS JOIN (
SELECT
users.id
FROM
users
WHERE
users.is_driver = TRUE
AND users.team_id = 9) u
LEFT JOIN (
SELECT
id, set_dumpster_type_id, haul_type_id, completed_driver_id, team_id
FROM
hauls
WHERE
haul_status_id = 3
AND team_id = 9) h ON h.set_dumpster_type_id = d.dumpster_type_id
AND h.haul_type_id = ht.id
AND h.completed_driver_id = u.id
AND h.team_id = d.team_id
GROUP BY
d.dumpster_type_id,
ht.id,
u.id
I've the following SQL Query which runs perfectly fine but now i want to calculate the count based on the following scenario:
SELECT d.vseverity, v.vulnstatus, v.vtitleid, d.vtitle
FROM vulnsummary v
JOIN project p ON v.projid = p.projid
AND v.stagename = p.currentstage
JOIN datasets d ON v.vtitleid = d.datasetid
The current Output is:
Now i want to show the count like this way:
High (Open) - 2
High (Closed) - 0
Medium (Open) - 1
Medium (Closed) - 0
Low (Open) - 3
Low (Closed) - 1
Please help me to solve this query, Thank You
You need to CROSS JOIN the distinct sets of severity and status values and then LEFT JOIN that to your table to allow you to count the values of each severity/status combination. Without sample data it's hard to be certain but something like this should work:
SELECT sv.vseverity, st.vulnstatus, COUNT(v.vseverity) AS count
FROM (
SELECT DISTINCT vseverity
FROM datasets
) sv
CROSS JOIN (
SELECT DISTINCT vulnstatus
FROM vulnsummary
) st
LEFT JOIN (
SELECT d.vseverity, v.vulnstatus
FROM vulnsummary v
JOIN project p ON v.projid = p.projid
AND v.stagename = p.currentstage
JOIN datasets d ON v.vtitleid = d.datasetid
) v ON v.vseverity = sv.vseverity AND v.vulnstatus = st.vulnstatus
GROUP BY sv.vseverity, st.vulnstatus
I don't have your full dataset, however, a RIGHT OUTER JOIN to a master volnstatus table will enable (the volnstatus table showing all options i.e. 'Open', 'Closed'). A rough draft example, with only the volnstatus table populated:
SELECT COUNT(s.vulnstatus) CountOf, t.vtype
FROM dbo.vusummary s
RIGHT OUTER JOIN
vusummarytype t
ON s.vulnstatus = t.vtype
GROUP BY t.vtype
My query is as follows, and contains a subquery within it:
SELECT
dbo.Lawsuit.LawsuitNUM, dbo.Lawsuit.LawsuitYear,
dbo.Groups.GroupName, dbo.LawsuitType.LawsuitType,
dbo.Courts.CourtName,
(select
LawsuitID, DOJ, NextMeeting, ReceiptNUM, ExportNUM, ExportDate
from
(select
dbo.LawsuitExport.LawsuitID,
dbo.LawsuitExport.DOJ,
dbo.LawsuitExport.NextMeeting,
dbo.LawsuitExport.ReceiptNUM,
dbo.LawsuitExport.ExportNUM,
dbo.LawsuitExport.ExportDate,
row_number() over(partition by dbo.LawsuitExport.LawsuitID
order by dbo.LawsuitExport.ExportDate desc) as rn
from
dbo.LawsuitExport) as T
where
rn = 1)
FROM
dbo.Courts
INNER JOIN
dbo.LawsuitType ON dbo.Courts.CourtID = dbo.LawsuitType.CourtID
INNER JOIN
dbo.Groups ON dbo.LawsuitType.LawsuitTypeID = dbo.Groups.LawsuitTypeID
INNER JOIN
dbo.Lawsuit ON dbo.Groups.GroupID = dbo.Lawsuit.GroupID
INNER JOIN
dbo.LawsuitExport ON dbo.Lawsuit.LawsuitID = dbo.LawsuitExport.LawsuitID
The error I am receiving is:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS
The line
select LawsuitID,DOJ,NextMeeting,ReceiptNUM,ExportNUM,ExportDate doesnt work because you can only have 1 item returned from a sub query when it is trying to be used in another select statement. For example
SELECT X FROM Y GOOD
SELECT X, (SELECT A,B,C FROM FOO) FROM Y NOT GOOD
A,B,C cannot be mapped to 1 single element so that is invalid
If that's what is your intention then consider modifying your query like
SELECT
dbo.Lawsuit.LawsuitNUM, dbo.Lawsuit.LawsuitYear,
dbo.Groups.GroupName, dbo.LawsuitType.LawsuitType,
dbo.Courts.CourtName,XXX.LawsuitID, XXX.DOJ, XXX.NextMeeting, XXX.ReceiptNUM, XXX.ExportNUM, XXX.ExportDate
FROM
dbo.Courts
INNER JOIN
dbo.LawsuitType ON dbo.Courts.CourtID = dbo.LawsuitType.CourtID
INNER JOIN
dbo.Groups ON dbo.LawsuitType.LawsuitTypeID = dbo.Groups.LawsuitTypeID
INNER JOIN
dbo.Lawsuit ON dbo.Groups.GroupID = dbo.Lawsuit.GroupID
INNER JOIN
dbo.LawsuitExport ON dbo.Lawsuit.LawsuitID = dbo.LawsuitExport.LawsuitID
INNER JOIN (select
LawsuitID,
DOJ,
NextMeeting,
ReceiptNUM,
ExportNUM,
ExportDate,
row_number() over(partition by LawsuitID
order by ExportDate desc) as rn
from
dbo.LawsuitExport) XXX ON dbo.Lawsuit.LawsuitID = XXX.LawsuitID
WHERE XXX.rn = 1;
I'm trying to make a count within several table with JOIN, but when I made several JOINs the COUNTs got wrongly counted.
Basically I've got 4 tables, named:
predective_search
predective_to_product
predective_to_category
predective_to_manufacturer
I want to count the total number of products, categories and manufacturer which has same id in table predective_search.
Here's my code:
SELECT * ,
COUNT(pp.predictive_id) AS total_products,
COUNT(pc.predictive_id) AS total_categories,
COUNT(pm.predictive_id) AS total_manufacturers
FROM predictive_search ps
LEFT JOIN predictive_to_product pp ON (ps.predictive_id = pp.predictive_id)
LEFT JOIN predictive_to_category pu ON (ps.predictive_id = pc.predictive_id)
LEFT JOIN oc_predictive_to_manufacturer pm ON (ps.predictive_id = pm.predictive_id)
GROUP BY ps.predictive_id
Also the GROUP BY is needed I think. I'm stuck at this as I'm not getting any way to do this
SELECT
ps.*,
agg_pp.total_products,
agg_pc.total_categories,
agg_pm.total_manufacturers
FROM predictive_search ps
LEFT JOIN (
SELECT pp.predictive_id, COUNT(*) AS total_products
FROM predictive_to_product pp
GROUP BY pp.predictive_id
) agg_pp ON ps.predictive_id = agg_pp.predictive_id
LEFT JOIN (
SELECT pc.predictive_id, COUNT(*) AS total_categories
FROM predictive_to_category pc
GROUP BY pc.predictive_id
) agg_pc ON ps.predictive_id = agg_pc.predictive_id
LEFT JOIN (
SELECT pm.predictive_id, COUNT(*) AS total_manufacturers
FROM predictive_to_category pm
GROUP BY pm.predictive_id
) agg_pm ON ps.predictive_id = agg_pm.predictive_id