Error: "Multiple columns are specified in an aggregated expression containing an outer reference." - sql

I am receiving this error when trying to execute the query below. Any ideas or suggestions?
Error:
Multiple columns are specified in an aggregated expression containing an outer reference. If an expression being aggregated contains an outer reference, then that outer reference must be the only column referenced in the expression.
SELECT TestInstances.pkTestInstanceID AS 'pkTestInstanceID',
bands.pkPerformanceLevelReportBandID AS 'BandID',
bands.StackPosition AS 'StackPosition',
(SELECT TOP 100 PERCENT SUM(CASE WHEN bands.StackPosition = b.StackPosition THEN 1 ELSE 0 END) * 100/ CASE WHEN COUNT(StudentScores_Subject.pkStudentScoreID) = 0 THEN 1 ELSE COUNT(StudentScores_Subject.pkStudentScoreID) END
FROM PerformanceLevelReportBands b
WHERE b.fkPerformanceLevelReportID = #intPerfLevelReportId
ORDER BY SUM(CASE WHEN bands.StackPosition = b.StackPosition THEN 1 ELSE 0 END) * 100/ CASE WHEN COUNT(StudentScores_Subject.pkStudentScoreID) = 0 THEN 1 ELSE COUNT(StudentScores_Subject.pkStudentScoreID) END) AS 'Percent',
COUNT(StudentScores_Subject.pkStudentScoreID) AS 'Count'
FROM StudentScores_Subject
INNER JOIN StudentTests ON StudentScores_Subject.fkStudentTestID = StudentTests.pkStudentTestID
INNER JOIN TestInstances ON TestInstances.pkTestInstanceID = StudentTests.fkTestInstanceID
INNER JOIN CAHSEE_TestPeriods ON CAHSEE_TestPeriods.pkTestPeriodID = TestInstances.fkTestPeriodID
INNER JOIN PerformanceLevelReportBands bands ON bands.fkPerformanceLevelReportID = #intPerfLevelReportId
LEFT JOIN MMARS_Web_TestInfo_California.dbo.PerfLevelReportBandCutScores cutScores ON cutScores.fkPerformanceLevelReportBandID = bands.pkPerformanceLevelReportBandID
AND cutScores.fkGradeID = #intGradeId
AND cutScores.fkTestSubjectID IN (SELECT id FROM #tempSubs)
INNER JOIN PerfLevelReportBandComponents bandComponents ON bandComponents.fkPerformanceLevelReportBandID = bands.pkPerformanceLevelReportBandID
AND((bandComponents.ScoreValue = StudentScores_Subject.ScoreValue) OR
((CAST(StudentScores_Subject.ScoreValue AS INT) BETWEEN bandComponents.minScore and bandComponents.maxScore)
OR
(CAST(StudentScores_Subject.ScoreValue AS INT) BETWEEN cutScores.minScore and cutScores.maxScore)))
RIGHT JOIN MM_SchoolYears ON MM_SchoolYears.pkSchoolYearID = TestInstances.fkSchoolYearID
WHERE MM_SchoolYears.pkSchoolYearID IN (SELECT number FROM itot(#strYearIds, N','))
AND StudentScores_Subject.fkStudentTestID IN (SELECT id FROM #tempTests)
AND StudentScores_Subject.fkScoreTypeID = bandComponents.fkScoreTypeID
AND StudentScores_Subject.fkTest_SubjectID IN (SELECT id FROM #tempSubs)
GROUP BY TestInstances.pkTestInstanceID, bands.pkPerformanceLevelReportBandID, bands.StackPosition
ORDER BY TestInstances.pkTestInstanceID, bands.pkPerformanceLevelReportBandID, bands.StackPosition

The problem is here you can't combine an outer and inner reference in an aggregate function
(SELECT TOP 100 PERCENT SUM(CASE WHEN bands.StackPosition = b.StackPosition THEN 1 ELSE 0 END) * 100/ CASE
WHEN COUNT(StudentScores_Subject.pkStudentScoreID) = 0 THEN 1
ELSE COUNT(StudentScores_Subject.pkStudentScoreID)
END
FROM PerformanceLevelReportBands b
WHERE b.fkPerformanceLevelReportID = #intPerfLevelReportId
ORDER BY SUM(CASE WHEN bands.StackPosition = b.StackPosition THEN 1 ELSE 0 END) * 100/ CASE
WHEN COUNT(StudentScores_Subject.pkStudentScoreID) = 0 THEN 1
ELSE COUNT(StudentScores_Subject.pkStudentScoreID)
END) AS 'Percent'
So change it to
(SELECT TOP 100 PERCENT SUM(CASE WHEN bb.StackPosition = b.StackPosition THEN 1 ELSE 0 END) * 100/ CASE
WHEN COUNT(StudentScores_Subject.pkStudentScoreID) = 0 THEN 1
ELSE COUNT(StudentScores_Subject.pkStudentScoreID)
END
FROM PerformanceLevelReportBands b JOIN PerformanceLevelReportBands bb
ON bb.fkPerformanceLevelReportID =bands.fkPerformanceLevelReportID
AND b.fkPerformanceLevelReportID =bb.fkPerformanceLevelReportID
WHERE b.fkPerformanceLevelReportID = #intPerfLevelReportId
ORDER BY SUM(CASE WHEN bb.StackPosition = b.StackPosition THEN 1 ELSE 0 END) * 100/ CASE
WHEN COUNT(StudentScores_Subject.pkStudentScoreID) = 0 THEN 1
ELSE COUNT(StudentScores_Subject.pkStudentScoreID)
END) AS 'Percent'
Here is a more thorough explanation.

I'd recommend commenting out bandComponents then cutScores, rerunning after removing each components and seeing where the query fails. Once you figure out where it's failing, then you can fix it.
Also, could be this line, the query in your Percent column.
ORDER BY SUM(CASE WHEN bands.StackPosition = b.StackPosition THEN 1 ELSE 0 END) * 100
I tried to organize your query a bit better to make it more legible.
SELECT
TestInstances.pkTestInstanceID AS 'pkTestInstanceID'
, bands.pkPerformanceLevelReportBandID AS 'BandID'
, bands.StackPosition AS 'StackPosition'
, (
SELECT TOP 100 PERCENT
SUM( CASE
WHEN bands.StackPosition = b.StackPosition
THEN 1
ELSE 0
END) * 100 /
CASE
WHEN COUNT(StudentScores_Subject.pkStudentScoreID) = 0
THEN 1
ELSE COUNT(StudentScores_Subject.pkStudentScoreID)
END
FROM PerformanceLevelReportBands b
WHERE b.fkPerformanceLevelReportID = #intPerfLevelReportId
ORDER BY SUM(CASE WHEN bands.StackPosition = b.StackPosition THEN 1 ELSE 0 END) * 100
/
CASE
WHEN COUNT(StudentScores_Subject.pkStudentScoreID) = 0
THEN 1
ELSE COUNT(StudentScores_Subject.pkStudentScoreID)
END
) AS 'Percent'
, COUNT(StudentScores_Subject.pkStudentScoreID) AS 'Count'
FROM
StudentScores_Subject
INNER JOIN
StudentTests ON
StudentScores_Subject.fkStudentTestID = StudentTests.pkStudentTestID
INNER JOIN
TestInstances ON
TestInstances.pkTestInstanceID = StudentTests.fkTestInstanceID
INNER JOIN
CAHSEE_TestPeriods ON
CAHSEE_TestPeriods.pkTestPeriodID = TestInstances.fkTestPeriodID
INNER JOIN
PerformanceLevelReportBands bands ON
bands.fkPerformanceLevelReportID = #intPerfLevelReportId
LEFT JOIN
MMARS_Web_TestInfo_California.dbo.PerfLevelReportBandCutScores cutScores ON
cutScores.fkPerformanceLevelReportBandID = bands.pkPerformanceLevelReportBandID
AND cutScores.fkGradeID = #intGradeId
AND cutScores.fkTestSubjectID IN (SELECT id FROM #tempSubs)
INNER JOIN
PerfLevelReportBandComponents bandComponents ON
bandComponents.fkPerformanceLevelReportBandID = bands.pkPerformanceLevelReportBandID
AND(
(bandComponents.ScoreValue = StudentScores_Subject.ScoreValue) OR
(
(CAST(StudentScores_Subject.ScoreValue AS INT) BETWEEN bandComponents.minScore and bandComponents.maxScore) OR
(CAST(StudentScores_Subject.ScoreValue AS INT) BETWEEN cutScores.minScore and cutScores.maxScore)
)
)
RIGHT JOIN
MM_SchoolYears ON
MM_SchoolYears.pkSchoolYearID = TestInstances.fkSchoolYearID
WHERE
MM_SchoolYears.pkSchoolYearID IN (SELECT number FROM itot(#strYearIds, N','))
AND StudentScores_Subject.fkStudentTestID IN (SELECT id FROM #tempTests)
AND StudentScores_Subject.fkScoreTypeID = bandComponents.fkScoreTypeID
AND StudentScores_Subject.fkTest_SubjectID IN (SELECT id FROM #tempSubs)
GROUP BY TestInstances.pkTestInstanceID, bands.pkPerformanceLevelReportBandID, bands.StackPosition
ORDER BY TestInstances.pkTestInstanceID, bands.pkPerformanceLevelReportBandID, bands.StackPosition

I faced the same problem or something similar.
I left here both the initial code and the resolution in case it helps you.
Initial code:
SELECT ISNULL((SELECT DateName(mm,DATEADD(mm,Perioada - 1,0))),'Nedefinit') as [Period],MAX(t.TipPerioada) AS TipPerioada,t.Perioada,MAX(t.An) AS An, a.NumePrenume, CAST(t.Volum as float) as ValTip,
ISNULL((Select Sum(v.Cantitate*p.VolumProdus)
From Wme_Vanzari as v
WHERE ('ND'='ND' or v.MarcaAgent = t.MarcaAgent) and t.[An]=v.AnFactura and t.Perioada=v.Lunafactura),0) as Realizat,
'' as Diferenta,'' as DiferentaWD,'' as Procent
FROM [Memo_Target] t
--Aduce Suma de Cantitate si MarcaAgent pentru a nu dubla cu liniile din Vanzari
LEFT JOIN (Select SUM(Cantitate) as Cantitate, MarcaAgent
From Wme_Vanzari
WHERE AnFactura = #An
GROUP BY MarcaAgent) v on t.MarcaAgent = v.MarcaAgent
LEFT JOIN WME_Agenti a on a.Marca = t.MarcaAgent
--Aduce Suma de VolumProdus, CodProdus si Marca Agent Filtrat dupa Anul dat in parametru, Volum necesar pt a calcula Realizat si a scade din targetul setat
LEFT JOIN (SELECT SUM(ISNULL(n.VolumProdus, 0)) AS VolumProdus, v.CodProdus, v.MarcaAgent
FROM Wme_NomenclatorProduse n
LEFT JOIN Wme_Vanzari v ON v.CodProdus = n.CodIntern
WHERE v.MarcaAgent IS NOT NULL AND v.CodProdus IS NOT NULL AND v.AnFactura = #An
GROUP BY v.CodProdus, v.MarcaAgent ) p ON p.MarcaAgent = t.MarcaAgent
WHERE (t.Volum IS NOT NULL AND t.Volum > 0)
AND t.An = #An
AND (CAST(t.Perioada AS INT) BETWEEN #lunastart AND #lunaend)
AND TipTarget = #tiptarget
AND (a.Marca IN (SELECT * FROM dbo.GetIdsTableString(#marcaAgent)) OR #marcaAgent = 'ND')
GROUP BY a.NumePrenume, t.Volum, Perioada,t.An
Error message was the same, and this is the resolution code:
SELECT ISNULL((SELECT DateName(mm,DATEADD(mm,Perioada - 1,0))),'Nedefinit') as [Period],MAX(t.TipPerioada) AS TipPerioada,t.Perioada,MAX(t.An) AS An, a.NumePrenume, CAST(t.Volum as float) as ValTip,
(ISNULL((Select Sum(p.VolumProdus)
From Wme_Vanzari as v
WHERE ('ND'='ND' or v.MarcaAgent = t.MarcaAgent) and t.[An]=v.AnFactura and t.Perioada=v.Lunafactura),0) * sum(v.Cantitate)) as Realizat,
'' as Diferenta,'' as DiferentaWD,'' as Procent
FROM [Memo_Target] t
--Aduce Suma de Cantitate si MarcaAgent pentru a nu dubla cu liniile din Vanzari
LEFT JOIN (Select SUM(Cantitate) as Cantitate, MarcaAgent
From Wme_Vanzari
WHERE AnFactura = #An
GROUP BY MarcaAgent) v on t.MarcaAgent = v.MarcaAgent
LEFT JOIN WME_Agenti a on a.Marca = t.MarcaAgent
--Aduce Suma de VolumProdus, CodProdus si Marca Agent Filtrat dupa Anul dat in parametru, Volum necesar pt a calcula Realizat si a scade din targetul setat
LEFT JOIN (SELECT SUM(ISNULL(n.VolumProdus, 0)) AS VolumProdus, v.CodProdus, v.MarcaAgent
FROM Wme_NomenclatorProduse n
LEFT JOIN Wme_Vanzari v ON v.CodProdus = n.CodIntern
WHERE v.MarcaAgent IS NOT NULL AND v.CodProdus IS NOT NULL AND v.AnFactura = #An
GROUP BY v.CodProdus, v.MarcaAgent ) p ON p.MarcaAgent = t.MarcaAgent
WHERE (t.Volum IS NOT NULL AND t.Volum > 0)
AND t.An = #An
AND (CAST(t.Perioada AS INT) BETWEEN #lunastart AND #lunaend)
AND TipTarget = #tiptarget
AND (a.Marca IN (SELECT * FROM dbo.GetIdsTableString(#marcaAgent)) OR #marcaAgent = 'ND')
GROUP BY a.NumePrenume, t.Volum, Perioada,t.An
As you can see, I chose to leave a single column in the first subselect and i was forced to use multiplication operation after this subselect.
I hope it helps!

Related

A new row for each record

I would like the results to produce a new row in each instance where a condition is met. I'm using a CASE statement but this isn't the way to go since once the first condition is met it stops evaluating the field.
SELECT
Reviews.ReviewID,
CASE
WHEN Score_CorrectID = 0 THEN 'Correct ID Right Party Authentication'
WHEN Score_ProperlyIdentified = 0 THEN 'PCA Properly Identified Itself'
WHEN Score_MiniMiranda = 0 THEN 'Mini-Miranda'
END AS [Error Type]
FROM Reviews INNER JOIN PCAs ON Reviews.PCAID = PCAs.PCAID LEFT JOIN
PCARebuttal ON Reviews.ReviewID = PCARebuttal.ReviewID
WHERE
(Score_CorrectID = 0 OR Score_ProperlyIdentified = 0 OR Score_MiniMiranda =
0)
This produces this:
I would like this:
Use apply:
SELECT r.ReviewID, v.error_type
FROM Reviews r INNER JOIN
PCAs
ON r.PCAID = PCAs.PCAID LEFT JOIN
PCARebuttal pr
ON r.ReviewID = pr.ReviewID OUTER APPLY
(SELECT *
FROM (VALUES (Score_CorrectID, 'Correct ID Right Party Authentication'),
(Score_ProperlyIdentified, 'PCA Properly Identified Itself'),
(Score_MiniMiranda, 'Mini-Miranda')
) v(score, error_type)
WHERE score = 0
) v
WHERE (Score_CorrectID = 0 OR Score_ProperlyIdentified = 0 OR Score_MiniMiranda = 0);
That said, I would probably just concatenate the values into a single column:
SELECT r.ReviewID,
( (CASE WHEN Score_CorrectID = 0 THEN 'Correct ID Right Party Authentication; ' ELSE '' END) +
(CASE WHEN Score_ProperlyIdentified = 0 THEN 'PCA Properly Identified Itself; ' ELSE '' END) +
(CASE WHEN Score_MiniMiranda = 0 THEN 'Mini-Miranda;' ELSE '' END)
) as error_types
FROM Reviews r INNER JOIN
PCAs
ON r.PCAID = PCAs.PCAID LEFT JOIN
PCARebuttal pr
ON r.ReviewID = pr.ReviewID OUTER APPLY
(SELECT *
FROM (VALUES ()
) v(score, error_type)
WHERE score = 0
) v
WHERE (Score_CorrectID = 0 OR Score_ProperlyIdentified = 0 OR Score_MiniMiranda = 0);
Instead of using case, you can use union all instead:
SELECT
Reviews.ReviewID,
'Correct ID Right Party Authentication' AS [Error Type]
FROM Reviews
INNER JOIN PCAs ON Reviews.PCAID = PCAs.PCAID
LEFT JOIN PCARebuttal ON Reviews.ReviewID = PCARebuttal.ReviewID
WHERE Score_CorrectID = 0
UNION ALL
SELECT
Reviews.ReviewID,
'PCA Properly Identified Itself' AS [Error Type]
FROM Reviews
INNER JOIN PCAs ON Reviews.PCAID = PCAs.PCAID
LEFT JOIN PCARebuttal ON Reviews.ReviewID = PCARebuttal.ReviewID
WHERE Score_ProperlyIdentified = 0
UNION ALL
SELECT
Reviews.ReviewID,
'Mini-Miranda' AS [Error Type]
FROM Reviews
INNER JOIN PCAs ON Reviews.PCAID = PCAs.PCAID
LEFT JOIN PCARebuttal ON Reviews.ReviewID = PCARebuttal.ReviewID
WHERE Score_MiniMiranda = 0

ORA-01799: a column may not be outer-joined to a subquery

hi good people please find the code below is running in db2 but fails in oracle.
select * from
(select distinct e.time_stamp,e.applicationid, e.processname,
e.stage, e.initiatingsource, e.status, e.start_time, i.consultant,
g.cifnumber, g.applicantfirstname, g.applicantlastname,
case when e.branch is not null
then e.branch
else case when g.branch is not null
then g.branch
else i.branch
end
end as branch
from (select c.time_stamp, b.applicationid, b.processname,
b.stage, b.branch, b.initiatingsource,
case when d.status is null
then c.status
else d.status
end as status,
c.time_stamp as START_TIME,
case when d.time_stamp is not null
then d.time_stamp
else current_timestamp
ens as END_TIME
from (select distinct f.applicationid, f.branch, f.initiatingsource, f.processname,
case when f.stage in ('START''END')
then 'APPLICATION'
else f.stage, f.stagecounter
from processmetric f) b
left join processmetric c on b.applicationid = c.applicationid and b.processname = c.processname and (b.stage = c.stage or (b.stage = 'APPLICATION' and c.stage = 'START')) and b.stagecounter = c.stagecounter and c.phase = 'START'
left join processmetric d on b.applicationid = d.applicationid and b.processname = d.processname and (b.stage = d.stage or (b.stage = 'APPLICATION' and d.stage = 'END')) and b.stagecounter = d.stagecounter and d.phase ='END')e
left join applicationcustomerdata g on g.applicationid = e.applicationid
and g.time_stamp in (select max(x.time_stamp)
from applicationcustomerdata x
where x.applicationid = g.applicationid
)
left join applicationdata i on i.applicationid = e.applicationid
and i.time_stamp in (select max(z.time_stamp)
from applicationdata z
where z.applicationid = i.applicationid
)
order by e.start_time
) a
where a.start_time is not null and a.stage not in ('APPLICATION') and a.status not in ('COMPLETE' , 'COMPLETED' , 'CANCEL', 'FRAUD' , 'DECLINE') and a.stage = 'VERIFICATION';
Oracle don't allow to make outer join with subquery. Following 2 joins are problematic ones:
left join applicationcustomerdata g on g.applicationid = e.applicationid
and g.time_stamp in (select max(x.time_stamp)
from applicationcustomerdata x
where x.applicationid = g.applicationid
)
left join applicationdata i on i.applicationid = e.applicationid
and i.time_stamp in (select max(z.time_stamp)
from applicationdata z
where z.applicationid = i.applicationid
)
You need to rewrite statement (if you need this all in one SQL) or write some PL/SQL loops through data.

Syntax error on request optimisation

I'm trying to optimize a SQL query but I face a weird issue.
Here is my optimized request :
SELECT proj.ProjetId AS ProjetId,
SUM (CASE WHEN modTrans.Code != 'FoO' THEN 1 ELSE 0 END) AS VAL1,
SUM(CASE WHEN modTrans.Code = 'Foo' THEN 1 ELSE 0 END) AS VAL2
FROM Projet.Projet proj
INNER JOIN Workflow.PointControle ptCont
ON ptCont.ProjetId = proj.ProjetId
INNER JOIN Workflow.PointControleModele ptContMod
ON ptContMod.PointControleModeleId = ptCont.PointControleModeleId
INNER JOIN Commande.CommandeTAC cdeTAC
ON cdeTAC.ProjetId = proj.ProjetId
INNER JOIN Commande.CommandeTACCatalogue cdeTACCat
ON cdeTACCat.CommandeTACId = cdeTAC.CommandeTACId
INNER JOIN Fournisseur.Fournisseur four
ON four.FournisseurId = cdeTACCat.FournisseurId
INNER JOIN Fournisseur.ModeTransmission modTrans
ON modTrans.ModeTransmissionId = four.ModeTransmissionId
WHERE ptContMod.PointControleModeleCode = 'FFooooooooo'
AND ptCont.Etat = 'Foooooo'
AND proj.DateLivraison IS NOT NULL
AND [proj].[DateLivraison] <= DATEADD(DAY, 14, GETDATE())
GROUP BY proj.ProjetId
HAVING COUNT(VAL1) > 0 AND COUNT(VAL2) = 0
I got this error :
Column name invalid : 'VAL1' Column name invalid : 'VAL2'
The error is in the HAVING section.
After reading this documentation I dont undestand why there is an error because I respect the syntax : http://www.w3schools.com/sql/sql_having.asp
You can find the unoptimized complete request here :
BEGIN
WITH Trans(VAL1, VAL2, projetId) AS
(
SELECT SUM (CASE WHEN modTrans.Code != 'Foo' THEN 1 ELSE 0 END) AS NbNonSALM
, SUM(CASE WHEN modTrans.Code = 'Foo' THEN 1 ELSE 0 END) AS NbSALM
, proj.ProjetId AS projetId
FROM Projet.Projet proj
INNER JOIN Workflow.PointControle ptCont
ON ptCont.ProjetId = proj.ProjetId
INNER JOIN Workflow.PointControleModele ptContMod
ON ptContMod.PointControleModeleId = ptCont.PointControleModeleId
INNER JOIN Commande.CommandeTAC cdeTAC
ON cdeTAC.ProjetId = proj.ProjetId
INNER JOIN Commande.CommandeTACCatalogue cdeTACCat
ON cdeTACCat.CommandeTACId = cdeTAC.CommandeTACId
INNER JOIN Fournisseur.Fournisseur four
ON four.FournisseurId = cdeTACCat.FournisseurId
INNER JOIN Fournisseur.ModeTransmission modTrans
ON modTrans.ModeTransmissionId = four.ModeTransmissionId
WHERE ptContMod.PointControleModeleCode = 'Foooooo'
AND ptCont.Etat = 'Fooo'
AND proj.DateLivraison IS NOT NULL
AND DATEADD(DAY, -14, proj.DateLivraison) <= GETDATE()
GROUP BY proj.ProjetId
)
SELECT proj.ProjetId AS ProjetId
FROM Projet.Projet proj
INNER JOIN Workflow.PointControle ptCont
ON ptCont.ProjetId = proj.ProjetId
INNER JOIN Workflow.PointControleModele ptContMod
ON ptContMod.PointControleModeleId = ptCont.PointControleModeleId
INNER JOIN Commande.CommandeTAC cdeTAC
ON cdeTAC.ProjetId = proj.ProjetId
INNER JOIN Commande.CommandeTACCatalogue cdeTACCat
ON cdeTACCat.CommandeTACId = cdeTAC.CommandeTACId
INNER JOIN Fournisseur.Fournisseur four
ON four.FournisseurId = cdeTACCat.FournisseurId
INNER JOIN Fournisseur.ModeTransmission modTrans
ON modTrans.ModeTransmissionId = four.ModeTransmissionId
INNER JOIN Trans ON Trans.projetId = proj.ProjetId
WHERE ptContMod.PointControleModeleCode = 'Foo'
AND ptCont.Etat = 'Fooo'
AND proj.DateLivraison IS NOT NULL
AND DATEADD(DAY, -14, proj.DateLivraison) <= GETDATE()
GROUP BY proj.ProjetId
HAVING COUNT(trans.NbNonSALM) > 0 AND COUNT(trans.NbSALM) = 0
UNION
SELECT projet.ProjetId AS ProjetId
FROM Projet.Projet projet
INNER JOIN Workflow.PointControle ptControle
ON ptControle.ProjetId = projet.ProjetId
INNER JOIN Workflow.PointControleModele ptContMod
ON ptContMod.PointControleModeleId = ptControle.PointControleModeleId
INNER JOIN Commande.CommandeTAC cdeTAC
ON cdeTAC.ProjetId = projet.ProjetId
INNER JOIN Commande.CommandeTACCatalogue cdeTACCat
ON cdeTACCat.CommandeTACId = cdeTAC.CommandeTACId
INNER JOIN commande.AREntete arEnt
ON arEnt.ProjetIdTAC = cdeTAC.ProjetIdTAC
INNER JOIN Commande.Article art
ON art.AREnteteId = arEnt.AREnteteId AND art.CommandeTACCatalogueId = cdeTACCat.CommandeTACCatalogueId
INNER JOIN Fournisseur.Fournisseur four
ON four.FournisseurId = cdeTACCat.FournisseurId
INNER JOIN Fournisseur.ModeTransmission modTrans
ON modTrans.ModeTransmissionId = four.ModeTransmissionId
WHERE ptContMod.PointControleModeleCode = 'Fooo'
AND ptControle.Etat = 'Foo'
AND arEnt.SuppressionLogique = 0
AND arEnt.Statut IS NOT NULL
AND arEnt.Statut >= 4
AND arEnt.Statut != 9
AND modTrans.Code = 'Fooooooo'
AND projet.SiteId = #siteId
END
;
I am not try this, but I think it work for you.. Or you can give some sample data if error comes.
;WITH CTE AS
(
SELECT proj.ProjetId AS ProjetId,
SUM (CASE WHEN modTrans.Code != 'FoO' THEN 1 ELSE 0 END) AS VAL1,
SUM(CASE WHEN modTrans.Code = 'Foo' THEN 1 ELSE 0 END) AS VAL2
FROM Projet.Projet proj
INNER JOIN Workflow.PointControle ptCont
ON ptCont.ProjetId = proj.ProjetId
INNER JOIN Workflow.PointControleModele ptContMod
ON ptContMod.PointControleModeleId = ptCont.PointControleModeleId
INNER JOIN Commande.CommandeTAC cdeTAC
ON cdeTAC.ProjetId = proj.ProjetId
INNER JOIN Commande.CommandeTACCatalogue cdeTACCat
ON cdeTACCat.CommandeTACId = cdeTAC.CommandeTACId
INNER JOIN Fournisseur.Fournisseur four
ON four.FournisseurId = cdeTACCat.FournisseurId
INNER JOIN Fournisseur.ModeTransmission modTrans
ON modTrans.ModeTransmissionId = four.ModeTransmissionId
WHERE ptContMod.PointControleModeleCode = 'FFooooooooo'
AND ptCont.Etat = 'Foooooo'
AND proj.DateLivraison IS NOT NULL
AND [proj].[DateLivraison] <= DATEADD(DAY, 14, GETDATE())
GROUP BY proj.ProjetId
)
SELECT ProjetId, VAL1, VAL2
FROM CTE
GROUP BY ProjetId, VAL1, VAL2
HAVING COUNT(VAL1) > 0 AND COUNT(VAL2) = 0
You cannot use the alias in the HAVING clause. Replace with this instead:
HAVING
SUM(CASE WHEN modTrans.Code != 'FoO' THEN 1 ELSE 0 END) > 0
AND SUM(CASE WHEN modTrans.Code = 'Foo' THEN 1 ELSE 0 END) = 0
Replace your HAVING clause with this
HAVING
SUM (CASE WHEN modTrans.Code != 'FoO' THEN 1 ELSE 0 END)>0
AND
SUM(CASE WHEN modTrans.Code = 'Foo' THEN 1 ELSE 0 END)=0

Oracle SQL - connecting 2 dates

I have the following code.
It has 2 dates:
1.
(case
when not trunc(iv.dated) is null then trunc(iv.dated) else trunc(iv1.dated)
end) date_stock_received
2.
trunc(dh.actshpdate) SHIP_DATE
Is there a way to join the dates so they show in one column?
select unique li.catnr, li.av_part_no,
(select sum(pl.qty_onhand) from part_loc pl where li.av_part_no = pl.part_no) qty_onhand,
(case
when not trunc(iv.dated) is null then trunc(iv.dated) else trunc(iv1.dated)
end) date_stock_received,
(case
when not sum(iv.quantity) is null then sum(iv.quantity) else sum(iv1.quantity)
end) qty_received,
dp.delqty, od.ord_extordnr, trunc(dh.actshpdate) SHIP_DATE
from leos_item li
LEFT JOIN scm_packtyp sp
ON li.packtyp = sp.packtyp
LEFT JOIN invtran_view_oes iv
ON li.av_part_no = iv.part_no
and (iv.transaction = 'NREC' and iv.location_no = ' RETURNS W')
LEFT JOIN invtran_view_oes iv1
on li.av_part_no = iv1.part_no
and (iv1.transaction = 'CORR+' and iv1.remark like 'STOCK FROM SP PALLET%')
LEFT JOIN oes_delsegview od
ON od.catnr = li.catnr
and od.prodtyp = li.prodtyp
and od.packtyp = li.packtyp
LEFT JOIN oes_dpos dp
ON od.ordnr = dp.ordnr
and od.posnr = dp.posnr
and od.segnr = dp.segnr
LEFT JOIN oes_dhead dh
on dp.dheadnr = dh.dheadnr
and dh.shpfromloc = 'W'
where li.cunr = '816900'
and substr(li.catnr,1,5) in ('RGMCD','RGJCD')
and li.item_type = 'FP'
and li.catnr = 'RGJCD221'
group by li.catnr, li.av_part_no, iv.dated, iv.quantity, iv1.dated, iv1.quantity, dp.delqty,
dp.ordnr, dp.posnr, dp.segnr, od.ord_extordnr, dh.actshpdate
order by li.av_part_no
Current result is ...
... what I would like to see is ...
Is this possible ?
The coalesce function might be what you want.
select trunc(coalesce(iv.dated, iv1.dated, dh.actshpdate)) theDateYouMightWant

How to change this 'Not In' Query to Left Join query

I've been trying to change this query. I don't want to use 'Not In' in this query. Can anybody help me how to change this query to left join query?
SELECT t.date,t.ticket,t.weight,t.Count, td.description
FROM tblticket t inner join tblticketdetails td on t.ticket = td.ticket
WHERE t.ticket NOT IN (SELECT r.Original_ticket from tblRenew R where isnull(r.new_ticket,'') = '' and r.transaction_status = 'valid')
AND RTRIM(LTRIM(t.status)) = 'OPEN'
AND td.Type = 62
AND t.weight/t.Count >= 1000
AND t.date BETWEEN '2011-12-31' AND '2013-01-17'
Providing that you don't have multiple records in tblRenew for any ticket:
select
t.date, t.ticket, t.weight, t.Count, td.description
from
tblticket t
inner join tblticketdetails td on t.ticket = td.ticket
left join tblRenew R on isnull(r.new_ticket,'') = '' and r.transaction_status = 'valid' and r.Original_ticket = t.ticket
where
r.Original_ticket is not null
and RTRIM(LTRIM(t.status)) = 'OPEN'
and td.Type = 62
and t.weight/t.Count >= 1000
and t.date BETWEEN '2011-12-31' AND '2013-01-17'
SELECT t.date, t.ticket,t.weight, t.Count, td.description
FROM tblticket t inner join
tblticketdetails td
on t.ticket = td.ticket left outer join
(SELECT r.Original_ticket
from tblRenew R
where isnull(r.new_ticket,'') = '' and
r.transaction_status = 'valid'
) v
on t.ticket = v.Original_ticket
WHERE t.ticket NOT IN (SELECT r.Original_ticket from tblRenew R where isnull(r.new_ticket,'') = '' and r.transaction_status = 'valid')
AND RTRIM(LTRIM(t.status)) = 'OPEN'
AND td.Type = 62
AND t.weight/t.Count >= 1000
AND t.date BETWEEN '2011-12-31' AND '2013-01-17'
and v.original_tiket is null