SQL server Select Distinct with Order By - sql

I am not very known to DB query but getting error
"ORDER BY items must appear in the select list if SELECT DISTINCT is
specified."
with below query. I searched on google and found that order by need to group if using Distinct but still not able to get it. Can anyone help me out.
SELECT DISTINCT
P1.*
FROM T_PRD P1
LEFT JOIN T_PRD P2 ON P1.baseprdid = P2.prdid
INNER JOIN T_PRD_NM_VENDOR prdNmVendor ON P1.prdId = prdNmVendor.prdId
INNER JOIN T_VENDOR_NM vendorNM ON prdNmVendor.vendorNMId = vendorNM.vendorNMId
INNER JOIN T_NM nm ON vendorNM.NMId = nm.NMId
INNER JOIN T_PRD_VENDOR prdVendor ON prdVendor.PRDId = P1.PRDId
INNER JOIN T_VENDOR vendor ON prdVendor.vendorId = vendor.vendorId
INNER JOIN T_CSTMR_PRD_REF custPrd ON custPrd.ProductId = P1.PRDId
INNER JOIN T_CSTMR cstmr ON custPrd.ChennelCstrId = cstmr.cstmrid
WHERE 1 = 1
AND P1.Lifecycle = 2
AND P1.AutoCreated = 0
AND vendor.vendorId = 1
AND P1.VendorEnfId = 1
AND cstmr.cstmrid = 2008
ORDER BY CASE
WHEN P1.BASEPRDID = 0
THEN P1.PRDNAME
ELSE P2.PRDNAME
END ASC,
BASEPRDID;

Your ORDER BY depends on expression which is missing from SELECT DISTINCT list . Add it
SELECT DISTINCT P1.*
,CASE
WHEN P1.BASEPRDID = 0
THEN P1.PRDNAME
ELSE P2.PRDNAME
END col

Related

Single-row subquery returns more than one row - Case statement

My following code returns the error stated in the title, however the error does not show when the case statement is ran by itself.
I've tried to dismantle the case statement, however this isn't my query but IS being used elsewhere, i'm wondering how they manage to get it to return rows in its current state.
SELECT DISTINCT V55."INPUTSPECNAME",
V55."FKMATERIAL",
V55."INPUTQUANTITYVALUE",
B.ABBREVIATION,
(CASE
WHEN z.COST IS NULL
THEN (SELECT DISTINCT (CASE
WHEN MatOut_D.PERVALUEQUANTITY > 0 THEN
ROUND(MatOut_D.PRICE /
MatOut_D.PERVALUEQUANTITY /
10 /
MatOut_E.USDCONVERSIONFACTOR *
C.USDCONVERSIONFACTOR, 5)
ELSE 0
END)
FROM V55_FORMULATIONJOINMATOUTPUT MatOut_B,
V55_FORMULATIONINPUTOUTPUT MatOut_C,
THEORETICALCOSTS MatOut_D,
COMMONCURRENCIES MatOut_E,
UOMML MatOut_F
WHERE MatOut_B.FKREFERENCEDMATERIAL = V55.FKMATERIAL
AND MatOut_B.FKOWNER = MatOut_C.FKOWNER
AND MatOut_C.FORMULATIONOUTPUTPKID = MatOut_D.FKPARENT
AND MatOut_D.FKCURRENCY = MatOut_E.PKID
AND MatOut_D.FKPERVALUEUOM = MatOut_F.FKUOM
AND MatOut_F.LANGID = 0)
ELSE ROUND(z.COST*100/D.USDCONVERSIONFACTOR*C.USDCONVERSIONFACTOR,5)
END) AS COST_100G
FROM V55_FORMULATIONINPUTOUTPUT V55
INNER JOIN UOM A
on V55.FKINPUTUOM = A.PKID
INNER JOIN UOMML B
on a.pkid = B.FKUOM
INNER JOIN DWBSpecificationRoots dwbRoot
on dwbRoot.fkOwner = V55.FKOWNER
INNER JOIN dwbSpecifications dwbSpec
ON dwbSpec.fkWorkingVersionParent = dwbRoot.pkid
LEFT OUTER JOIN specLegacySpecJoin y
on dwbSpec.fkdisplayedlegacyprofile = y.fklegacyprofileid
and y.fkspecid = V55.fkMaterial
LEFT OUTER join COSTITEMS z
on z.equivalent = y.equivalent
and z.fklegacyprofile = dwbSpec.fkdisplayedlegacyprofile
and z.FKSCRMENTITY = dwbSpec.FKSCRMSUPPLIERBASE
and z.COSTTYPE = dwbSpec.COSTTYPE
LEFT OUTER JOIN COMMONCURRENCIES C
ON C.PKID = dwbSpec.FKCURRENCY
LEFT OUTER JOIN COMMONCURRENCIES D
ON D.PKID = Z.FKCURRENCY
The problem is that the subquery in your CASE statement is returning multiple rows in some situations and as has been stated in the question's comments that's a no-no.
I suggest that you pull the subquery out of the CASE statement and make it a Common Table Expression, then join it into the query as you would any other table:
WITH cteSubexpr AS (SELECT DISTINCT MatOut_B.FKREFERENCEDMATERIAL,
(CASE
WHEN MatOut_D.PERVALUEQUANTITY > 0 THEN
ROUND(MatOut_D.PRICE /
MatOut_D.PERVALUEQUANTITY /
10 /
MatOut_E.USDCONVERSIONFACTOR *
C.USDCONVERSIONFACTOR, 5)
ELSE 0
END) AS CALC_QTY
FROM V55_FORMULATIONJOINMATOUTPUT MatOut_B
INNER JOIN V55_FORMULATIONINPUTOUTPUT MatOut_C
ON MatOut_C.FKOWNER = MatOut_B.FKOWNER
INNER JOIN THEORETICALCOSTS MatOut_D
ON MatOut_D.FKPARENT = MatOut_C.FORMULATIONOUTPUTPKID
INNER JOIN COMMONCURRENCIES MatOut_E
ON MatOut_E.PKID = MatOut_D.FKCURRENCY
INNER JOIN UOMML MatOut_F
ON MatOut_F.FKUOM = MatOut_D.FKPERVALUEUOM
WHERE MatOut_F.LANGID = 0)
SELECT DISTINCT V55.INPUTSPECNAME,
V55.FKMATERIAL,
V55.INPUTQUANTITYVALUE,
B.ABBREVIATION,
(CASE
WHEN z.COST IS NULL
THEN cs.CALC_QTY
ELSE ROUND(z.COST * 100 / D.USDCONVERSIONFACTOR * C.USDCONVERSIONFACTOR, 5)
END) AS COST_100G
FROM V55_FORMULATIONINPUTOUTPUT V55
INNER JOIN cteSubexpr cs
ON cs.FKREFERENCEDMATERIAL = V55.FKMATERIAL
INNER JOIN UOM A
on V55.FKINPUTUOM = A.PKID
INNER JOIN UOMML B
on a.pkid = B.FKUOM
INNER JOIN DWBSpecificationRoots dwbRoot
on dwbRoot.fkOwner = V55.FKOWNER
INNER JOIN dwbSpecifications dwbSpec
ON dwbSpec.fkWorkingVersionParent = dwbRoot.pkid
LEFT OUTER JOIN specLegacySpecJoin y
on dwbSpec.fkdisplayedlegacyprofile = y.fklegacyprofileid
and y.fkspecid = V55.fkMaterial
LEFT OUTER join COSTITEMS z
on z.equivalent = y.equivalent
and z.fklegacyprofile = dwbSpec.fkdisplayedlegacyprofile
and z.FKSCRMENTITY = dwbSpec.FKSCRMSUPPLIERBASE
and z.COSTTYPE = dwbSpec.COSTTYPE
LEFT OUTER JOIN COMMONCURRENCIES C
ON C.PKID = dwbSpec.FKCURRENCY
LEFT OUTER JOIN COMMONCURRENCIES D
ON D.PKID = Z.FKCURRENCY
Best of luck.

SQL Server Query returning duplicate rows

I have a query of SQL that is a join of multiple tables, it is returning duplicate rows and after hours of going through it can't find out where its going wrong
SELECT
StkItem.iUOMStockingUnitID,
_etblUnits1.cUnitCode as 'parkSize',
_etblUnits2.cUnitCode as 'quantitySize',
InvNum.fInvTotExclForeign,
[_btblInvoiceLines].*,
[_rtblCountry].cCountryName,
[CurrencyHist].fBuyRate,
Vendor.Name,
InvNum.OrderDate,
InvNum.InvNumber
FROM
[dbo].[_btblInvoiceLines]
LEFT JOIN
StkItem ON StkItem.StockLink = [_btblInvoiceLines].iStockCodeID
LEFT JOIN
_etblUnits as _etblUnits1 ON _etblUnits1.idunits = StkItem.iUOMDefSellUnitID
LEFT JOIN
_etblUnits as _etblUnits2 ON _etblUnits2.idunits = StkItem.iUOMStockingUnitID
LEFT JOIN
InvNum ON iInvoiceID = AutoIndex
LEFT JOIN
Vendor ON Vendor.DCLink = InvNum.AccountID
LEFT JOIN
[_rtblCountry] ON [_rtblCountry].idCountry = Vendor.iCountryID
LEFT JOIN
[CurrencyHist] ON InvNum.ForeignCurrencyID = [CurrencyHist].iCurrencyID
WHERE
OrderNum = ''
AND [CurrencyHist].iCurrencyID = (SELECT TOP 1 iCurrencyID
FROM [CurrencyHist]
WHERE iCurrencyID = InvNum.ForeignCurrencyID
ORDER BY idCurrencyHist DESC)
Here is the query, any help will be highly appreciated, thanks in advance
From your previous comments, The problem is coming when you join [CurrencyHist]. From the name, it seems it's a history table and so must be having multiple rows as a history for each currency. To eliminate duplicate rows, you should join with the latest updated record for the particular currency. So, your query could be like below,
SELECT StkItem.iUOMStockingUnitID,
_etblUnits1.cUnitCode as 'parkSize',
_etblUnits2.cUnitCode as 'quantitySize',
InvNum.fInvTotExclForeign,
[_btblInvoiceLines].*,
[_rtblCountry].cCountryName,
[CurrencyHist].fBuyRate,
Vendor.Name,
InvNum.OrderDate,
InvNum.InvNumber
FROM [dbo].[_btblInvoiceLines]
LEFT JOIN StkItem ON StkItem.StockLink = [_btblInvoiceLines].iStockCodeID
LEFT JOIN _etblUnits as _etblUnits1 ON _etblUnits1.idunits = StkItem.iUOMDefSellUnitID
LEFT JOIN _etblUnits as _etblUnits2 ON _etblUnits2.idunits = StkItem.iUOMStockingUnitID
LEFT JOIN InvNum ON iInvoiceID = AutoIndex
LEFT JOIN Vendor ON Vendor.DCLink = InvNum.AccountID
LEFT JOIN [_rtblCountry] ON [_rtblCountry].idCountry = Vendor.iCountryID
LEFT JOIN (SELECT DENSE_RANK() over (partition by [CurrencyHist].iCurrencyID order by [CurrencyHist].LastUpdated desc) as rn,[CurrencyHist].iCurrencyID as 'iCurrencyID'
FROM [CurrencyHist] AS [CurrencyHist]
)[CurrencyHist] ON InvNum.ForeignCurrencyID = [CurrencyHist].iCurrencyID
and [CurrencyHist].rn=1
WHERE OrderNum = '' AND
[CurrencyHist].iCurrencyID = (SELECT TOP 1 iCurrencyID
FROM [CurrencyHist]
WHERE iCurrencyID = InvNum.ForeignCurrencyID
ORDER BY idCurrencyHist DESC)
Note : I have assumed that CurrencyHist table has a LastUpdated with DateTime datatype Column

SQl Error : Each GROUP BY expression must contain at least one column that is not an outer reference [duplicate]

This question already has answers here:
Each GROUP BY expression must contain at least one column that is not an outer reference
(8 answers)
Closed 6 years ago.
I get this error
Each GROUP BY expression must contain at least one column that is not an outer reference
while running this query:
SELECT TOP 1
SUM(mla.total_current_attribute_value)
FROM
partstrack_machine_location_attributes mla (NOLOCK)
INNER JOIN
#tmpInstallParts_Temp installpartdetails ON mla.machine_sequence_id = installpartdetails.InstallKitToMachineSequenceId
AND (CASE WHEN mla.machine_side_id IS NULL THEN 1
WHEN mla.machine_side_id = installpartdetails.InstallKitToMachineSideId THEN 1 END
) = 1
INNER JOIN
partstrack_mes_attribute_mapping mam (NOLOCK) ON mla.mes_attribute = mam.mes_attribute_name
INNER JOIN
partstrack_attribute_type at (NOLOCK) ON mam.pt_attribute_id = at.pt_attribute_id
INNER JOIN
partstrack_ipp_mes_attributes ima(NOLOCK) ON at.pt_attribute_id = ima.pt_attribute_id
WHERE
mla.active_ind = 'Y' AND
ima.ipp_ID IN (SELECT ipp.ipp_id
FROM partstrack_individual_physical_part ipp
INNER JOIN #tmpInstallParts_Temp tmp ON (ipp.ipp_id = tmp.InstallingPartIPPId OR
(CASE WHEN tmp.InstallingPartIPKId = '-1' THEN 1 END) = 1
)
GROUP BY
ima.ipp_id
Can someone help me?
This is the text of the query from the first revision of the question.
In later revisions you removed the last closing bracket ) and the query became syntactically incorrect. You'd better check and fix the text of the question and format the text of the query, so it is readable.
SELECT TOP 1
SUM(mla.total_current_attribute_value)
FROM
partstrack_machine_location_attributes mla (NOLOCK)
INNER JOIN #tmpInstallParts_Temp installpartdetails
ON mla.machine_sequence_id = installpartdetails.InstallKitToMachineSequenceId
AND (CASE WHEN mla.machine_side_id IS NULL THEN 1
WHEN mla.machine_side_id = installpartdetails.InstallKitToMachineSideId THEN 1 END) = 1
INNER JOIN partstrack_mes_attribute_mapping mam (NOLOCK) ON mla.mes_attribute = mam.mes_attribute_name
INNER JOIN partstrack_attribute_type at (NOLOCK) ON mam.pt_attribute_id = at.pt_attribute_id
INNER JOIN partstrack_ipp_mes_attributes ima(NOLOCK) ON at.pt_attribute_id = ima.pt_attribute_id
WHERE
mla.active_ind = 'Y'
AND ima.ipp_ID IN
(
Select
ipp.ipp_id
FROM
partstrack_individual_physical_part ipp
INNER JOIN #tmpInstallParts_Temp tmp
ON (ipp.ipp_id = tmp.InstallingPartIPPId
OR (CASE WHEN tmp.InstallingPartIPKId = '-1' THEN 1 END) = 1)
GROUP BY
ima.ipp_id
)
With this formatting it is clear now that there is a subquery with GROUP BY.
Most likely it is just a typo: you meant to write GROUP BY ipp.ipp_id instead of GROUP BY ima.ipp_id.
If you really wanted to have the GROUP BY not in a subquery, but in the main SELECT, then you misplaced the closing bracket ) and the query should look like this:
SELECT TOP 1
SUM(mla.total_current_attribute_value)
FROM
partstrack_machine_location_attributes mla (NOLOCK)
INNER JOIN #tmpInstallParts_Temp installpartdetails
ON mla.machine_sequence_id = installpartdetails.InstallKitToMachineSequenceId
AND (CASE WHEN mla.machine_side_id IS NULL THEN 1
WHEN mla.machine_side_id = installpartdetails.InstallKitToMachineSideId THEN 1 END) = 1
INNER JOIN partstrack_mes_attribute_mapping mam (NOLOCK) ON mla.mes_attribute = mam.mes_attribute_name
INNER JOIN partstrack_attribute_type at (NOLOCK) ON mam.pt_attribute_id = at.pt_attribute_id
INNER JOIN partstrack_ipp_mes_attributes ima(NOLOCK) ON at.pt_attribute_id = ima.pt_attribute_id
WHERE
mla.active_ind = 'Y'
AND ima.ipp_ID IN
(
Select
ipp.ipp_id
FROM
partstrack_individual_physical_part ipp
INNER JOIN #tmpInstallParts_Temp tmp
ON (ipp.ipp_id = tmp.InstallingPartIPPId
OR (CASE WHEN tmp.InstallingPartIPKId = '-1' THEN 1 END) = 1)
)
GROUP BY
ima.ipp_id
In any case, proper formatting of the source code can really help.
Group By ima.ipp_id
should be applicable to outer query. Because of incorrect placement of '(' it was applying to inner query.
Now after correcting the query,it's working fine without any issues.
Final Query is :
SELECT TOP 1
SUM(mla.total_current_attribute_value)
FROM
partstrack_machine_location_attributes mla (NOLOCK)
INNER JOIN #tmpInstallParts_Temp installpartdetails
ON mla.machine_sequence_id = installpartdetails.InstallKitToMachineSequenceId
AND (CASE WHEN mla.machine_side_id IS NULL THEN 1
WHEN mla.machine_side_id = installpartdetails.InstallKitToMachineSideId THEN 1 END ) = 1
INNER JOIN partstrack_mes_attribute_mapping mam (NOLOCK) ON mla.mes_attribute = mam.mes_attribute_name
INNER JOIN partstrack_attribute_type at (NOLOCK) ON mam.pt_attribute_id = at.pt_attribute_id
INNER JOIN partstrack_ipp_mes_attributes ima(NOLOCK) ON at.pt_attribute_id = ima.pt_attribute_id
WHERE
mla.active_ind = 'Y'
AND ima.ipp_ID IN
(
Select
ipp.ipp_id
FROM
partstrack_individual_physical_part ipp
INNER JOIN #tmpInstallParts_Temp tmp
ON (ipp.ipp_id = tmp.InstallingPartIPPId
OR (CASE WHEN tmp.InstallingPartIPKId = '-1' THEN 1 END ) =1)
)
GROUP BY ima.ipp_id
Thank you all.

Join questionn - Select must return only one record

I have 4 tables in my SQL Server 2008 database :
CONTACT
CONTACT_DETAILS
PLANS
PLANS_DETAILS
Every record is recorded in CONTACT and CONTACT_DATAILS but a CONTACT can have 0, 1, 2 or more records in PLANS, Active or Cancelled.
So I did this:
SELECT *
from CONTACT as c
left join PLANS as pp on pp.PKEY = c.PKEY
left join PLANS_DETAILS as pd on pd.PDKEY = p.PDKEY
inner join CONTACT_DETAILS as cd on cd.DKEY = c.DKEY
WHERE c.KEY = '267110' and PP.STATUS = 'Active'
"267110" have 1 active PLAN so it shows me 1 line, everything I need.
But if I put
WHERE c.KEY = '100003' and PP.STATUS = 'Active'
"100003" have 2 cancelled plans, so the result is empty. If I remove PP.STATUS = 'Active' , it returns me 2 identical results, but I need just one.
In resume: I need a select that returns me 1 row only. If there is an active plan, return the columns, if not, return the columns null. If someone have 1 cancelled and 1 active plan, return me only the active plan columns.
The answer to your question is to move the condition on pp to the on clause.
SELECT *
from CONTACT c inner join
CONTACT_DETAILS cd
on cd.DKEY = c.DKEY left join
PLANS pp
on pp.PKEY = c.PKEY AND PP.STATUS = 'Active' left join
PLANS_DETAILS pd
on pd.PDKEY = p.PDKEY
WHERE c.KEY = '267110' ;
In addition, when you have a series of inner and left joins, I recommend putting all the inner joins first, followed by the outer joins. That makes it clear which joins are used for keeping records and which for filtering.
Just add an ORDER BY PP.STATUS DESC and a TOP 1 clause, and delete the and PP.STATUS = 'Active', like this
SELECT TOP 1 * from CONTACT as c
left join PLANS as pp on pp.PKEY = c.PKEY
left join PLANS_DETAILS as pd on pd.PDKEY = p.PDKEY
inner join CONTACT_DETAILS as cd on cd.DKEY = c.DKEY
WHERE c.KEY = '100003'
ORDER BY PP.STATUS DESC
SELECT *
from CONTACT c
left join
(
select * from
(select
[whatever you need from this table]
,row_number over(partition by [keys in this table] order by status asc) rnk
from PLANS)
where rnk = 1
) pp
on pp.PKEY = c.PKEY
left join PLANS_DETAILS pd
on pd.PDKEY = p.PDKEY
inner join CONTACT_DETAILS cd
on cd.DKEY = c.DKEY
WHERE c.KEY = '100003'

SQL - left join generate duplicates

I have code to select some applications but LEFT JOIN is creating duplicates.
Question: How to get rid of duplicates generated by
LEFT JOIN
attachments as att
ON
(a.ssn = att.ssn AND att.doc_type = 'id_copy' AND att.source = 'cpt3')
My Work:
I am considering SELECT DISTINCT or GROUP BY but, can't figure out where to put them.
This is the query:
SELECT
a.*, c.code, l.who, l.locked_time, att.id AS attnew,
( SELECT
COUNT(*)
FROM
applications as a2
WHERE
a2.approved = 'Y'
AND
a2.paid = 'Y'
AND
a2.paid_back = 'Y'
AND
a2.ssn = a.ssn
) AS previous_apps
FROM
applications as a
LEFT JOIN
locked_by as l
USING(id)
LEFT JOIN
campaign_codes as c
ON
c.id = a.campaign
LEFT JOIN
attachments as att
ON
(a.ssn = att.ssn AND att.doc_type = 'id_copy' AND att.source = 'cpt3')
WHERE
a.closed='N'
AND
a.paid = 'N'
ORDER BY
a.arrived_date
DESC
Use GROUP BY
to avoid duplicates. In your case:
...
WHERE
a.closed='N'
AND
a.paid = 'N'
GROUP BY
a.id
ORDER BY
a.arrived_date
DESC
If you want to make an one to one relation to avoid duplicates and att.id is the same for each att.ssn or you need only one (MAX,MIN,..ANY?) att.id. Try this:
LEFT JOIN
(SELECT ssn,
MAX(id) as id,
FROM attachments
WHERE doc_type = 'id_copy' AND source = 'cpt3'
GROUP BY ssn
)as att
ON
(a.ssn = att.ssn)