SQL Stored Procedure With One To Many Relationship Return Only Most Recent Record - sql

I've been having a terrible time with this. I created a stored procedure to return a corresponding record, but one of the tables has multiple print jobs so it returns multiple records. I only want the most recent requested print job to return and I can't get it to work correctly.
Please help!
SELECT PrintJobs.WORDERKey,
PrintJobs.JobNumber,
WORDER.U_DateRequired AS DateRequired,
ProductMaster.PartFileItemID,
WORDER.ManufacturingDepartment AS MfgDept,
PrintJobs.Copies AS LabelCopies,
PrintJobs.ExpiryDate AS JobExpiryDate,
PrintJobs.Batch,
PrintJobs.JulianDate AS JobJulianDate,
ProductMaster.Description,
ProductMaster.PackFormat,
ProductMaster.IngDec,
ProductMaster.BarCodeNumber,
ProductMaster.CustomerProductCode,
ProductMaster.CriticalInstruction,
ProductMaster.StorageInstruction,
ProductMaster.MixedCaseTitle,
ProductMaster.MixedCode1,
ProductMaster.MixedCode2,
ProductMaster.MixedCode3,
ProductMaster.MixedCode4,
ProductMaster.MixedDescription1,
ProductMaster.MixedDescription2,
ProductMaster.MixedDescription3,
ProductMaster.MixedDescription4,
ProductMaster.MixedIngDec1,
ProductMaster.MixedIngDec2,
ProductMaster.MixedIngDec3,
ProductMaster.MixedIngDec4,
ProductMaster.CookingInstruction,
ProductMaster.LogoFilename,
ProductMaster.ProductExpiryRule,
ExpiryRulesMaster.GS1_AppID,
ProductMaster.LabelTemplateID,
CASE WHEN ProductMaster.PartFileItemID IS NULL THEN 'N' ELSE 'Y' END AS LabelDefExists,
LabelTemplateMaster.FileName,
CASE WHEN [DateFormat] = 'Default' THEN ExpiryRulesMaster.DateFormatString ELSE [DateFormat] END AS DateFormatString,
ExpiryRulesMaster.DateRounding,
ExpiryRulesMaster.ExcludeXmasNewYear,
PART.ExternalShelfLifeTotalDays,
CustomOptions.Tagline,
CustomOptions.ShortName AS CustShortName,
ExpiryRulesMaster.LabelText AS ExpiryRuleLabelText,
ProductMaster.inner_hDescription,
ProductMaster.inner_vDescription,
ProductMaster.inner_CustCode,
ProductMaster.inner_Storage,
ProductMaster.inner_WarningMsg,
ProductMaster.inner_Dateformat,
ProductMaster.inner_SpecialCode,
ProductMaster.LabelType,
ProductMaster.inner_MicroSuiteCode,
ProductMaster.inner_PackWeight,
ProductMaster.inner_JDateFormat,
ProductMaster.CustomOptionID,
ProductMaster.inner_PrintLogo,
PART.NominalWeight,
ProductMaster.IngDec_html,
ProductMaster.IngDec_Active,
PrintJobs.PrintJobsID,
MaxJobs.MaxJob,
PrintJobs.DateEntered
FROM ProductMaster RIGHT OUTER JOIN
(
SELECT Partfileitemid,
--JobNumber,
MAX(dateentered) MaxJob
FROM PrintJobs
GROUP BY PartFileItemID
)
MaxJobs ON ProductMaster.PartFileItemID = MaxJobs.PartFileItemID RIGHT OUTER JOIN
PrintJobs ON PrintJobs.PartFileItemID = MaxJobs.PartFileItemID RIGHT OUTER JOIN
LabelTemplateMaster ON ProductMaster.LabelTemplateID = LabelTemplateMaster.LabelTemplateID LEFT OUTER JOIN
CustomOptions ON ProductMaster.CustomOptionID = CustomOptions.CustomOptionID LEFT OUTER JOIN
ExpiryRulesMaster ON ProductMaster.ProductExpiryRule = ExpiryRulesMaster.ExpiryRule LEFT OUTER JOIN
[F8Extract].[dbo].[WORDER] ON PrintJobs.WORDERKey = WORDER.WORDERKey LEFT OUTER JOIN
[F8Extract].[dbo].[PART] ON ProductMaster.PartFileItemID = Part.PartFileItemID
WHERE PrintJobs.WORDERKey = #WORDERKey
END
Thanks,
Stacy

One way would be to look at TOP and ORDER BY, i.e.
SELECT TOP 1
PrintJobs.WORDERKey,
PrintJobs.JobNumber,
WORDER.U_DateRequired AS DateRequired,
ProductMaster.PartFileItemID,
WORDER.ManufacturingDepartment AS MfgDept,
PrintJobs.Copies AS LabelCopies,
PrintJobs.ExpiryDate AS JobExpiryDate,
PrintJobs.Batch,
PrintJobs.JulianDate AS JobJulianDate,
ProductMaster.Description,
ProductMaster.PackFormat,
ProductMaster.IngDec,
ProductMaster.BarCodeNumber,
ProductMaster.CustomerProductCode,
ProductMaster.CriticalInstruction,
ProductMaster.StorageInstruction,
ProductMaster.MixedCaseTitle,
ProductMaster.MixedCode1,
ProductMaster.MixedCode2,
ProductMaster.MixedCode3,
ProductMaster.MixedCode4,
ProductMaster.MixedDescription1,
ProductMaster.MixedDescription2,
ProductMaster.MixedDescription3,
ProductMaster.MixedDescription4,
ProductMaster.MixedIngDec1,
ProductMaster.MixedIngDec2,
ProductMaster.MixedIngDec3,
ProductMaster.MixedIngDec4,
ProductMaster.CookingInstruction,
ProductMaster.LogoFilename,
ProductMaster.ProductExpiryRule,
ExpiryRulesMaster.GS1_AppID,
ProductMaster.LabelTemplateID,
CASE WHEN ProductMaster.PartFileItemID IS NULL THEN 'N' ELSE 'Y' END AS LabelDefExists,
LabelTemplateMaster.FileName,
CASE WHEN [DateFormat] = 'Default' THEN ExpiryRulesMaster.DateFormatString ELSE [DateFormat] END AS DateFormatString,
ExpiryRulesMaster.DateRounding,
ExpiryRulesMaster.ExcludeXmasNewYear,
PART.ExternalShelfLifeTotalDays,
CustomOptions.Tagline,
CustomOptions.ShortName AS CustShortName,
ExpiryRulesMaster.LabelText AS ExpiryRuleLabelText,
ProductMaster.inner_hDescription,
ProductMaster.inner_vDescription,
ProductMaster.inner_CustCode,
ProductMaster.inner_Storage,
ProductMaster.inner_WarningMsg,
ProductMaster.inner_Dateformat,
ProductMaster.inner_SpecialCode,
ProductMaster.LabelType,
ProductMaster.inner_MicroSuiteCode,
ProductMaster.inner_PackWeight,
ProductMaster.inner_JDateFormat,
ProductMaster.CustomOptionID,
ProductMaster.inner_PrintLogo,
PART.NominalWeight,
ProductMaster.IngDec_html,
ProductMaster.IngDec_Active,
PrintJobs.PrintJobsID,
MaxJobs.MaxJob,
PrintJobs.DateEntered
FROM ProductMaster RIGHT OUTER JOIN
(
SELECT Partfileitemid,
--JobNumber,
MAX(dateentered) MaxJob
FROM PrintJobs
GROUP BY PartFileItemID
)
MaxJobs ON ProductMaster.PartFileItemID = MaxJobs.PartFileItemID RIGHT OUTER JOIN
PrintJobs ON PrintJobs.PartFileItemID = MaxJobs.PartFileItemID RIGHT OUTER JOIN
LabelTemplateMaster ON ProductMaster.LabelTemplateID = LabelTemplateMaster.LabelTemplateID LEFT OUTER JOIN
CustomOptions ON ProductMaster.CustomOptionID = CustomOptions.CustomOptionID LEFT OUTER JOIN
ExpiryRulesMaster ON ProductMaster.ProductExpiryRule = ExpiryRulesMaster.ExpiryRule LEFT OUTER JOIN
[F8Extract].[dbo].[WORDER] ON PrintJobs.WORDERKey = WORDER.WORDERKey LEFT OUTER JOIN
[F8Extract].[dbo].[PART] ON ProductMaster.PartFileItemID = Part.PartFileItemID
WHERE PrintJobs.WORDERKey = #WORDERKey
ORDER BY PrintJobs.Number DESC
Note change the PrintJobs.Number to whatever field specifies the time order.

Related

Conditional Inner Join with two Functions

I have two functions. I need to decide whether to join with these two based on a BIT Value.
ON APEL.data_Period_dataination_Lookup_ID = CCEL.data_Period_dataination_Lookup_ID
INNER JOIN markcommon.GetPredecessordatadataIds() AS PIDS
IF #Combined_Flag=1
BEGIN
ON PIDS.data_Period_dataination_Identifier = APEL.data_Period_dataination_Identifier
AND PIDS.data_Period_Identifier = APL.data_Period_Identifier
END
Basically
if BIT=0 join with function 1 else join with function 2
I tried putting an IF Clause .. but it does not seem to work. What is the proper way to do it?
Just add your static condition as part of the join condition and use a LEFT JOIN to ensure it works with the missing row. You can then use a case expression in your select to obtain the correct column e.g.
SELECT
CASE WHEN F1.id IS NOT NULL THEN F1.MyColumn ELSE F2.MyColumn END
FROM ...
LEFT JOIN markcommon.Function1() AS F1
ON #Combined_Cohort = 1
AND {The rest of the join conditions}
LEFT JOIN markcommon.Function2() AS F2
ON #Combined_Cohort = 0
AND {The rest of the join conditions}
I think you can just use a LEFT JOIN:
ON APEL.data_Period_dataination_Lookup_ID = CCEL.data_Period_dataination_Lookup_ID LEFT JOIN
markcommon.GetPredecessordatadataIds() PIDS
ON #Combined_Cohort = 1 AND
PIDS.data_Period_dataination_Identifier = APEL.data_Period_dataination_Identifier AND
PIDS.data_Period_Identifier = APL.data_Period_Identifier
. . .
WHERE #Combined_Cohort <> 1 OR PIDS.data_Period_dataination_Identifier IS NOT NULL
You can use the LEFT JOIN as follows:
APEL.data_Period_dataination_Lookup_ID = CCEL.data_Period_dataination_Lookup_ID
LEFT JOIN markcommon.GetPredecessordatadataIds() AS PIDS
ON (#Combined_Flag=1 AND PIDS.data_Period_dataination_Identifier = APEL.data_Period_dataination_Identifier
AND PIDS.data_Period_Identifier = APL.data_Period_Identifier
)
LEFT JOIN FUNCTION2() AS PIDS2
ON (#Combined_Flag=0 AND <<join condition for function 2>>
)
Please also check:
SELECT
IIF(F1.id IS NOT NULL,F1.MyColumn,F2.MyColumn)
FROM ...
LEFT JOIN markcommon.Function1() AS F1
ON #Combined_Cohort = 1
AND {The rest of the join conditions}
LEFT JOIN markcommon.Function2() AS F2
ON #Combined_Cohort != 1
AND {The rest of the join conditions}

Select statement where joins causing records to be excluded

I have the below query with multiple joins.The last 3 joins are required to get the g.fin_id value. This works fine (see results) BUT because some records in the ACCUM_ISS_CHAR_HIST table have e.char9_nme values of NULL, it excludes the records in the results altogether. So it seems as when the e.char9_nme value has a record then it will produce a result, but as soon as it has a Null value then it is excluded. I would still like to see the records even though the g.fin_id for those will then be blank because they have a e.char9_nme value of Null. How can I change the query to accomplish this?
select
a.acct_id,
c.fld3_txt,
b.issue_loc1_cde,
b.instr_id,
a.fld1_nme,
b.issue_cls2_nme,
g.fin_id,
e.char9_nme
from position_dg as a
inner join
infoportal..issue_dg as b on b.INSTR_ID = a.INSTR_ID
inner join
InfoPortal..IVW_ACCT as c on a.acct_id = c.acct_id
inner join
InfoPortal..DW_AcctCharDG as d on a.acct_id = d.acctid
inner join
ACCUM_ISS_CHAR_HIST as e on a.instr_id = e.instr_id
inner join
MD_FINANCIAL_ENTITY as f on e.char9_nme = f.fin_enty_name
inner join md_FINANCIAL_ENTITY_ALTERNATE_IDENTIFIER as g on
f.fin_enty_id = g.fin_enty_id
and b.MAT_EXP_DTE > getdate()
and b.issue_cls1_nme = 'Derivatives'
and a.as_of_tms >= getdate()-1
and b.iss_typ in ('FFX','IRS','EQF')
and d.AcctChrSetId = 'DerivativeRpt'
and d.EndTms IS NULL
and a.acct_id = 'FOGEMBLCR'
and g.id_ctxt_typ = 'LEGAL_ENTITY_IDENTIFIER'
and e.as_of_dte = (
select MAX (as_of_dte)-1
from accum_iss_char_hist
)
I expect the results to show fin_id records for some ond blank fin_id records for some, but at the moment only the ones with a fin_id record is hown and the rest is excluded from the results.
You are looking for a left join.
Join all those tables (last 3 as you said) as left join. For better clarity I have moved conditions of every tables in their ON clause and for base table a made a where clause.
select
a.acct_id,
c.fld3_txt,
b.issue_loc1_cde,
b.instr_id,
a.fld1_nme,
b.issue_cls2_nme,
g.fin_id,
e.char9_nme
from position_dg as a
inner join
infoportal..issue_dg as b on b.INSTR_ID = a.INSTR_ID
and b.MAT_EXP_DTE > getdate()
and b.issue_cls1_nme = 'Derivatives'
and b.iss_typ in ('FFX','IRS','EQF')
inner join
InfoPortal..IVW_ACCT as c on a.acct_id = c.acct_id
inner join
InfoPortal..DW_AcctCharDG as d on a.acct_id = d.acctid
and d.AcctChrSetId = 'DerivativeRpt'
and d.EndTms IS NULL
left join
ACCUM_ISS_CHAR_HIST as e on a.instr_id = e.instr_id
and e.as_of_dte = (
select MAX (as_of_dte)-1
from accum_iss_char_hist
)
left join
MD_FINANCIAL_ENTITY as f on e.char9_nme = f.fin_enty_name
left join
md_FINANCIAL_ENTITY_ALTERNATE_IDENTIFIER as g on f.fin_enty_id = g.fin_enty_id
and g.id_ctxt_typ = 'LEGAL_ENTITY_IDENTIFIER'
Where a.as_of_tms >= getdate()-1
and a.acct_id = 'FOGEMBLCR'

left outer join doesnt return all rows on left table

set #fuel_type='hsd';
select
ro.ro_id,
(case
when if(#fuel_type='ms',r.ms_field_input_id,r.hsd_field_input_id)= 0
then if(#fuel_type='ms',concat(r.remark_ms,r.remark_branded),r.remark_hsd)
else
f.field_name
end )
from
retail_outlets ro left join
volume_trackers r on ro.ro_id=r.ro_id,
field_inputs_master f
where
if(#fuel_type='ms',r.ms_field_input_id = f.field_id,r.hsd_field_input_id=f.field_id)
and ro.pricing_module=1 and if(#fuel_type='ms',ro.ms,ro.hsd)=1 and r.date_inserted='2017-02-06'
;
I need to show all the ro_id in the retail_outlets table but only the ro_id that match with the volume trackers table are displayes
Not positive but I think this is what you're after.
changes are the following:
removed , notation and inject a left join
moved the r.date_inserted to the join criteria.
moved where clause for , join to new left join and altered to case statement as to why I used a left join... Since we tie back to R which is already a left join it made sense to me to continue with left joins...
removed some unneeded ()'s
Assumptions r.date_inserted is a varchar2 data type or '2017-02-06' is implicitly being cast correctly.
.
SET #fuel_type='hsd';
SELECT
ro.ro_id ,
CASE
WHEN IF(#fuel_type='ms',r.ms_field_input_id,r.hsd_field_input_id)= 0
THEN IF(#fuel_type='ms',concat(r.remark_ms,r.remark_branded),r.remark_hsd)
ELSE f.field_name
END
FROM retail_outlets ro
LEFT JOIN volume_trackers r
ON ro.ro_id = r.ro_id
AND r.date_inserted = '2017-02-06'
LEFT JOIN field_inputs_master f
ON f.field_id = CASE WHEN #fuel_type='ms'
THEN r.ms_field_input_id
ELSE r.hsd_field_input_id
END
WHERE ro.pricing_module=1
AND IF(#fuel_type ='ms',ro.ms,ro.hsd)=1;

Microsoft SQL MAX() Not Including Rows with NULLs

I work in a law firm with a case management system which links an interface to a SQL db. I am writing queries in SSRS to run reports. I am trying to run a report on case information. Some of the information is right in the "cases" table (or vcases view), but I'm also trying to link information from a "demands_offers" table. Each case can have multiple demands and offers, so I am using a MAX function in a join to extract only the most recent demand record per case. Unfortunately, doing this eliminates cases which have no demands. I need all the cases to show up.
I have tried using a CASE statement nested in the MAX function to convert NULLS or empty fields to a random early date, but I still can't get all of the cases to appear in the report.
Any ideas? I am a relative newbie with SQL and have no formal training. Any help would be greatly appreciated. You can see the code below. (P.S. I don't think that I have the rights to create a temporary table.)
SELECT vc.case_number AS "Matter ID", vc.style, vc.atty2_name AS "Handling Attorney", m.max_demands_date, do.demands, do.demands_notes, sa.authorized,
(SELECT TOP 1 vl.computename
FROM vcases vca
LEFT OUTER JOIN case_parties cp
ON vca.case_sk = cp.case_sk
JOIN case_parties cpp
ON cp.parent_sk = cpp.case_parties_sk
JOIN vlegal_entity vl
ON vl.legal_entity_sk = cp.entity_sk
JOIN vlegal_entity vlp
ON vlp.legal_entity_sk = cpp.entity_sk
WHERE (vca.case_sk = vc.case_sk) AND (cpp.role_sk = '3557') AND (cp.role_sk = '3986') ) AS "Plaintiff//'s Attorney",
(SELECT cp.reference_number
FROM cases AS ca
LEFT OUTER JOIN case_parties AS cp
ON ca.case_sk = cp.case_sk
WHERE (cp.role_sk = '3706')
AND (ca.case_sk = vc.case_sk)) AS "Claim Number"
FROM
vcases vc
LEFT OUTER JOIN
case_parties cp ON vc.case_sk = cp.case_sk
LEFT OUTER JOIN
vlegal_entity vl ON cp.entity_sk = vl.legal_entity_sk
LEFT OUTER JOIN
settle_authority sa ON vc.case_sk = sa.case_sk
LEFT OUTER JOIN
demands_offers do ON vc.case_sk = do.case_sk
INNER JOIN
(SELECT DISTINCT max(
(CASE WHEN do.demands_date = '' THEN '1/1/1900 00:00:00'
ELSE do.demands_date
END)
) as "max_demands_date", vc.case_sk
FROM vcases AS vc
JOIN demands_offers AS do ON vc.case_sk = do.case_sk
GROUP BY vc.case_sk) AS m
ON vc.case_sk = m.case_sk AND
do.demands_date = m.max_demands_date
WHERE (vc.closed_ind = 'O') AND (cp.role_sk = '3816') AND (vl.client_number = 'EAS-01') AND (vc.lawtype_code <> 'FA')
ORDER BY vc.case_number
Your query can probably be written to be much more compact and better performing, but to start you off, this is the part that is removing the rows without demands:
INNER JOIN
(SELECT DISTINCT max(
(CASE WHEN do.demands_date = '' THEN '1/1/1900 00:00:00'
ELSE do.demands_date
END)
) as "max_demands_date", vc.case_sk
FROM vcases AS vc
JOIN demands_offers AS do ON vc.case_sk = do.case_sk
GROUP BY vc.case_sk) AS m
ON vc.case_sk = m.case_sk AND
do.demands_date = m.max_demands_date
It needs to be
LEFT JOIN
(SELECT DISTINCT max(
(CASE WHEN do.demands_date = '' THEN '1/1/1900 00:00:00'
ELSE do.demands_date
END)
) as "max_demands_date", vc.case_sk
FROM vcases AS vc
JOIN demands_offers AS do ON vc.case_sk = do.case_sk
GROUP BY vc.case_sk) AS m
ON vc.case_sk = m.case_sk AND
do.demands_date = m.max_demands_date
The reason is that an (A INNER JOIN B) keeps records only when it is possible to match rows in A to rows in B. When there is no demand, the derived table (subquery) returns NULL for max_demands_date, which cannot be matched by do.demands_date = m.max_demands_date. This causes the case record to get removed.

SQL with left outer join and 3 tables

I would like to add an ADD at the end of my code. Please have a look on my code and thanks for your support:
SELECT Area.org,
Supervisors.NomSup,
Supervisors.PrenomSup,
Employees.NomEmp,
Employees.PrenomEmp,
Employees.NoIdAlcanEmp,
Competencies.CodeCompetencies,
Competencies.CompetencyName,
LinkResultComp.AssNote,
LinkResultComp.AssDate
FROM ((((((
Area INNER JOIN Supervisors ON Area.IdArea = Supervisors.IdArea
)
INNER JOIN Employees ON Supervisors.IdSupervisor = Employees.IdSupervisor
)
INNER JOIN LinkProfilesEmployees ON Employees.IdEmp = LinkProfilesEmployees.IdEmp
)
INNER JOIN Profiles ON Profiles.IdProfiles = LinkProfilesEmployees.IdProfiles
)
INNER JOIN LinkProfComp ON Profiles.IdProfiles = LinkProfComp.IdProfiles
)
INNER JOIN Competencies ON Competencies.IdCompetencies = LinkProfComp.IdCompetencies
)
LEFT OUTER JOIN LinkResultComp ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies AND ON Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
WHERE Area.org LIKE "*20*" AND Competencies.CodeCompetencies LIKE "khse2010-05"
ORDER BY Supervisors.NomSup, Employees.NomEmp;
Just remove the extra ON that you added
So change this
LEFT OUTER JOIN LinkResultComp
ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies
AND ON Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
------^^ This one
to this
LEFT OUTER JOIN LinkResultComp
ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies
AND Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
Of course I assume you meant different fields for the second condition