I'm having difficulty with a query which displays records according to their fill rate.
For instance, a vacancy can have no bookings or some bookings. If a vacancy has bookings, they can be in the form of 'active [1]', 'pending [0]'. The query I have written so far works if the vacancy has booking records but I can't get it to work if it doesn't have booking records.
My query (which works) for vacancies with a booking is as follows:-
SELECT v.*, j.job_category_name, bu.business_unit_name
FROM vacancy v
INNER JOIN job_category j ON j.job_category_id = v.job_category_id
INNER JOIN business_unit bu ON bu.business_unit_id = v.business_unit_id
INNER JOIN booking b ON b.vacancy_id = v.vacancy_id
INNER JOIN booking_status bs ON bs.id = b.booking_status_id
WHERE
v.vacancy_status <> 'revoked' AND
v.vacancy_reference <> 'auto-generated booking' AND
v.business_unit_id IN (series of primary keys) AND
(bs.booking_status_type_id = 1 OR bs.booking_status_type_id = 2)
GROUP BY v.vacancy_id
HAVING v.vacancy_limit > count(b.booking_id)
ORDER BY v.vacancy_id DESC
I thought by changing the join of b and bs to LEFT JOIN would have worked, but it hasn't.
Any ideas?
Without a copy of your schema to work from, it's difficult to tell exactly, but when you changed booking and bookingstatus to LEFT JOINs, did you also modify your WHERE clause so that it read something like:
WHERE
v.vacancy_status <> 'revoked' AND
v.vacancy_reference <> 'auto-generated booking' AND
v.business_unit_id IN (series of primary keys) AND
(ISNULL(bs.booking_status_type_id, 1) = 1 OR ISNULL(bs.booking_status_type_id, 2) = 2)
i.e. Ensured that the full WHERE clause would be satisfied, thus not stripping out the records where all the values for columns from booking and bookingstatus were NULL?
Try LEFT OUTER JOIN for the tables for which the joins may return 0 matches.
For eg:- in your case have LEFT OUTER JOIN for b and bs and check
Related
So i have a query and it need to return a JSON object but the DISTINCT doesn't work no matter what I try. I've been trying a series of other tests and no matter what the 'WBS' always shows up with 3 or more duplicated columns. Anyone got any ideas?
I am working in Asp.net 6 MVC
PROCEDURE GET_BASELINE_RPT (in_WBS_LEVEL_ID IN NUMBER, in_FISCAL_YEAR IN VARCHAR2,
in_FISCAL_MONTH IN VARCHAR2, RET OUT CLOB) AS
BEGIN
WITH cte AS (
SELECT /*+MATERIALIZE*/ DISTINCT L.WBS_LEVEL_ID
FROM
WBS_LEVEL L
)
SELECT
JSON_ARRAYAGG (
JSON_OBJECT (
'WBS' VALUE L.WBS_LEVEL_NAME,
'Title' VALUE W.DESCRIPTION,
'Rev' VALUE B.REV_NUMBER,
'ScopeStatus' VALUE W.STATUS,
'BCP' VALUE CASE WHEN BC.FISCAL_YEAR = 0 THEN '' ELSE
SUBSTR(BC.FISCAL_YEAR,3,2)||'-'||LPAD(BC.BCP_FISCAL_ID, 3, '0') END,
'BCPApprovalDate' VALUE BC.APPROVAL_DATE,
'Manager' VALUE P1.NICK_NAME,
'ProjectControlManager' VALUE P2.NICK_NAME,
'ProjectControlEngineer' VALUE P3.NICK_NAME,
'FiscalYear' VALUE W.FISCAL_YEAR,
'FiscalMonth' VALUE W.FISCAL_MONTH,
'WBSNumber' VALUE L.WBS_LEVEL_ID
)RETURNING CLOB)
INTO RET
FROM WBS_LEVEL L
LEFT OUTER JOIN BASELINE_RPT B ON L.WBS_LEVEL_ID = B.WBS_LEVEL_ID
JOIN BCP BC ON BC.BCP_ID = B.BCP_ID
LEFT OUTER JOIN WBS_TREE_MOD W ON L.WBS_LEVEL_ID = W.WBS_LEVEL_ID
LEFT OUTER JOIN VW_SITEPEOPLE P1 ON W.WBS_MANAGER_SNUMBER = P1.SNUMBER
LEFT OUTER JOIN VW_SITEPEOPLE P2 ON W.PCM_SNUMBER = P2.SNUMBER
LEFT OUTER JOIN VW_SITEPEOPLE P3 ON W.PCE_SNUMBER = P3.SNUMBER
ORDER BY L.WBS_LEVEL_NAME, B.REV_NUMBER DESC;
END GET_BASELINE_RPT;
so it turns out I wasn't getting duplicates at all. There were differences in the data but there were so many columns that had the same data that I didn't notice the differences until another review. I will try to restart my query but to be honest I may just put in a filter in my C#.
I am querying the static backend db for a game I play (trying to keep up on my coding), and I am having an issue getting the full results that I want.
So the query that I have so far is:
select ms.security, mc.constellationName, mr.regionName, ms.solarSystemName, count(it.typename) as NumberOfBelts
from mapSolarSystems as ms
join mapConstellations as mc on ms.constellationID == mc.constellationID
join mapRegions as mr on ms.regionID == mr.regionID
join invItems as ii on ii.locationID = ms.solarSystemID
join invTypes as it on it.typeID == ii.typeID
where it.groupID = 9
group by solarSystemName
the problem comes when there are no rows where it.groupID == 9. What I need is for the count to return 0 and I can't for the life of me figure out how to get this to work.
I tried doing left outer join on the final join statement, but no joy.
Change the last join to a left join and set the condition in the on clause istead of the where clause:
select ms.security, mc.constellationName, mr.regionName, ms.solarSystemName, count(it.typename) as NumberOfBelts
from mapSolarSystems as ms
join mapConstellations as mc on ms.constellationID = mc.constellationID
join mapRegions as mr on ms.regionID = mr.regionID
join invItems as ii on ii.locationID = ms.solarSystemID
left join invTypes as it on it.typeID = ii.typeID and it.groupID = 9
group by solarSystemName
I have two subqueries. as shown below. the first query works fine but the second query which is basically the first query that I modified to use AND & OR, doesn't work in the sense that it doesn't return ID as expected. any suggestions on what is happening here?
1. (SELECT * FROM (SELECT EMPID FROM EVENT_F
INNER JOIN WCINFORMATION_D
ON EVENT_F.JOB_INFO_ROW_WID= WCINFORMATION_D.ROW_WID
INNER JOIN WCANDIDATE_D ON WCCANDIDATE_D.ROW_WID = VENT_F.CANDIDATE_ROW_WID
WHERE STEP_NAME = 'Offer'
AND WCINFORMATION_D.JOB_FAMILY_NAME IN ('MDP','ELP','Emerging Leader Program','Other')
AND TITLE NOT IN ('Student Ambassador Program for Eligible Summer Interns','Student Ambassador')
AND PI_CANDIDATE_NUM = OUTERAPP.PI_CANDIDATE_NUM
--limit 1
ORDER BY CREATION_DT ASC
) T1 WHERE ROWNUM=1) AS A_ID,
2.(SELECT * FROM (SELECT EMPID FROM EVENT_F
INNER JOIN WCINFORMATION_D
ON EVENT_F.JOB_INFO_ROW_WID= WCINFORMATION_D.ROW_WID
INNER JOIN WCANDIDATE_D ON WCCANDIDATE_D.ROW_WID = VENT_F.CANDIDATE_ROW_WID
WHERE STEP_NAME = 'Offer'
AND WCINFORMATION_D.JOB_FAMILY_NAME IN ('MDP','ELP','Emerging Leader Program','Other') or WCINFORMATION_D.JOB_FAMILY_NAME NOT IN ('MDP','ELP','Emerging Leader Program','Other')
AND TITLE NOT IN ('Student Ambassador Program for Eligible Summer Interns','Student Ambassador')
AND PI_CANDIDATE_NUM = OUTERAPP.PI_CANDIDATE_NUM
--limit 1
ORDER BY CREATION_DT ASC
) T1 WHERE ROWNUM=1) AS A_ID,
If you're wanting to get the count of people in one set of job families, plus a count of people in another set, you need to use a conditional count, e.g. something along the lines of:
SELECT COUNT(CASE WHEN wid.job_family_name IN ('MDP', 'ELP', 'Emerging Leader Program', 'Other') THEN 1 END) job_family_grp1,
COUNT(CASE WHEN wid.job_family_name IS NULL OR wid.job_family_name NOT IN ('MDP', 'ELP', 'Emerging Leader Program', 'Other') THEN 1 END) job_family_grp2
FROM event_f ef
INNER JOIN wcinformation_d wid
ON ef.job_info_row_wid = wid.row_wid
INNER JOIN wcandidate_d wcd
ON wcd.row_wid = ef.candidate_row_wid
WHERE step_name = 'Offer' -- alias this column name
AND title NOT IN ('Student Ambassador Program for Eligible Summer Interns', 'Student Ambassador') -- alias this column name;
You will most likely need to amend this to work for your particular case (it'll have to go as a join into your main query, given there are two columns being selected) since you didn't provide enough information in your question to give us the wider context.
Here is a simplified sqlfiddle as basis for my question:
http://sqlfiddle.com/#!15/c114a/7
The first invoice have two reminders sent from two other dates:
I want to get all invoice that should be reminded - once. Now it duplicates by reminders.
I want only the last reminder
SELECT inv_id, inv_due, rem_type
FROM inv
LEFT JOIN remrec ON remrec_inv = inv_id
LEFT JOIN rem ON rem_id = remrec_rem
WHERE inv_due < '2015-01-02'
And I know that i can compare and get the latest date from invoice and all reminders using greatest(inv_due,rem1,rem2,rem3):
SELECT inv_id,rem_type,greatest(inv_due,
(CASE WHEN rem_date<'2015-01-02' AND rem_type='1' THEN rem_date ELSE NULL END),
(CASE WHEN rem_date<'2015-01-02' AND rem_type='2' THEN rem_date ELSE NULL END),
(CASE WHEN rem_date<'2015-01-02' AND rem_type='3' THEN rem_date ELSE NULL END)) as max
FROM inv
LEFT JOIN remrec ON inv_id=remrec_inv
LEFT JOIN rem ON rem_id=remrec_id
WHERE inv_due < '2015-01-02'
So each reminder could consists of several invoices. Hence the rem and remrec.
The question is: How do I get only the unique invoices that should be reminded?
Thank you in advance for any clue.
I think I found a solution:
SELECT inv_id, inv_due, max(rem_type)
FROM inv
LEFT JOIN remrec ON inv_id=remrec_inv
LEFT JOIN rem ON rem_id=remrec_rem
WHERE greatest(inv_due,rem_date) < '2015-01-05'
GROUP BY inv_id, inv_due
Edit: Works better with several reminders:
SELECT DISTINCT inv_id, inv_due
FROM inv
LEFT JOIN remrec ON inv_id=remrec_inv
LEFT JOIN rem ON rem_id=remrec_rem
WHERE inv_due<'2015-01-05'
GROUP BY inv_id,inv_due
HAVING greatest(max(rem_date),inv_due)<'2015-01-05'
FWIW.
I have 3 Data Table Claim, Part and Labor.
In this Claim is parent table and Part and Labor is mapping tables of Claim and they have Part and Labor has the ClaimId as a Foreign Key.
Claim has data like:
Part has data Like
Labor table has data Like
Target Output would be:
Can anyone help me to achieve this in SQL server.
I have tried to solve with the Union/CTE but it did not gives the result as I want.
I got the same output (for your updated output screen) for this specific case. I don't know if any other data will work for you.
SELECT TMP.ClaimId
, CASE WHEN TMP.RowNum = 1 THEN TMP.Name ELSE NULL END AS ClaimName
, CASE WHEN TMP.RowNum = 1 THEN TMP.Note ELSE NULL END AS Note
, TMP.PartId
, TMP.PartNumber
, TMP.PartCost
, JOIN_L.LaborId
, JOIN_L.LaborCost
FROM (
SELECT C.ClaimId, C.Name, C.Note, P.PartId, P.PartNumber, P.PartCost
, ROW_NUMBER() OVER(PARTITION BY C.ClaimId ORDER BY P.PartId) AS RowNum
FROM Claim AS C
LEFT JOIN Part AS P ON C.ClaimId = P.ClaimId
)AS TMP
LEFT JOIN (
SELECT *
, ROW_NUMBER() OVER(PARTITION BY L.ClaimId ORDER BY L.ClaimId) AS RowNum
FROM Labor AS L
) AS JOIN_L ON (TMP.ClaimId = JOIN_L.ClaimId AND TMP.RowNum = JOIN_L.RowNum)
ORDER BY TMP.ClaimId
Not sure why you tried CTE here
Select C.ClaimId,C.name,C.Note,P.PartId,P.PartNumber,P.PartCost,L.LabourId,L.LabourCost
From Claim C
Left Outer Join Part P On P.ClaimId = C.ClaimId
Left Outer Join Labor L On L.ClaimId=C.ClaimId