I'm trying to select some data but I'm getting 'almost duplicated rows' -
all of the columns are the same except the 'gtin' column, the result I'm expecting is to get only one line of each of those 'almost duplicated rows'
The result I'm trying to achieve is to only get row 1 & 3.
this is the query I'm using:
SELECT DISTINCT 'Y' AS isEnterprise,
ii.ITEM_ID AS itemId,
ii.PARENT_ITEM_ID as PID,
ii.ITEM_IDENTIFIER AS productNo,
id.DESCRIPTION_1 AS description,
id.DESCRIPTION_4 AS packValue,
pid.PACKAGE_IDENTIFIER AS gtin,
pac.PRE_PRICE_AMOUNT AS eachPrice
FROM PWRNXGDTA.ITEM_INFORMATION AS ii
JOIN PWRNXGDTA.ITEM_ASSORTMENT AS ia ON ii.ITEM_ID = ia.ITEM_ID
JOIN PWRNXGDTA.ITEM_DESCRIPTION AS id ON id.ITEM_ID = ii.ITEM_ID
JOIN PWRNXGDTA.BUSINESS_UNIT AS bu ON ii.LOGISTICS_BU_ID = bu.BU_ID
LEFT JOIN PWRNXGDTA.ITEM_EXTN_NONMDM AS ien ON ien.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_CLASS AS lcc ON lcc.CLASSIFICATION_CLASS_ID = ii.CLASSIFICATION_CLASS_ID
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBH AS lcp ON lcp.CLASSIFICATION_PBH_ID = ii.CLASSIFICATION_PBH_ID
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBHF AS lcpf ON lcpf.CLASSIFICATION_PBHF_ID = ii.CLASSIFICATION_PBHF_ID
LEFT JOIN PWRNXGDTA.LIST_PROPRIETARY_ITEM AS lpi ON lpi.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
JOIN PWRNXGDTA.PA_ASSORTMENT AS pa ON pa.PACKAGE_ASSORTMENT_ID = ia.PACKAGE_ASSORTMENT_ID
JOIN PWRNXGDTA.ITEM_PRODUCT AS ip ON ip.ITEM_ID = ii.ITEM_ID
JOIN PWRNXGDTA.PRODUCT_INFO AS pin ON pin.PRODUCT_ID = ip.PRODUCT_ID
LEFT JOIN PWRNXGDTA.PRODUCT_NUTRITIONAL AS pn ON pn.PRODUCT_ID = ip.PRODUCT_ID
LEFT JOIN PWRNXGDTA.LKP_BRAND_INFORMATION AS lbi ON lbi.BRAND_ID = pin.BRAND_ID
AND lbi.RECORD_STATUS = 'A'
JOIN PWRNXGDTA.PACKAGE_IDENTIFIER AS pid ON pid.PACKAGE_ID = pa.PACKAGE_ID
JOIN PWRNXGDTA.PA_CONSUMABLE AS pac ON pac.PACKAGE_ASSORTMENT_ID = pa.PACKAGE_ASSORTMENT_ID
where id.DESCRIPTION_1 like '%HAAGEN DAZ VAN MLK CHO BR%'
Please help, thanks.
You can't do it with distinct, since distinct works on full rows only.
Your best option is to use group by:
SELECT 'Y' AS isEnterprise,
ii.ITEM_ID AS itemId,
ii.PARENT_ITEM_ID as PID,
ii.ITEM_IDENTIFIER AS productNo,
id.DESCRIPTION_1 AS description,
id.DESCRIPTION_4 AS packValue,
MIN(pid.PACKAGE_IDENTIFIER) AS gtin, -- Or max, if you want to...
pac.PRE_PRICE_AMOUNT AS eachPrice
FROM PWRNXGDTA.ITEM_INFORMATION AS ii
JOIN PWRNXGDTA.ITEM_ASSORTMENT AS ia ON ii.ITEM_ID = ia.ITEM_ID
JOIN PWRNXGDTA.ITEM_DESCRIPTION AS id ON id.ITEM_ID = ii.ITEM_ID
JOIN PWRNXGDTA.BUSINESS_UNIT AS bu ON ii.LOGISTICS_BU_ID = bu.BU_ID
LEFT JOIN PWRNXGDTA.ITEM_EXTN_NONMDM AS ien ON ien.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_CLASS AS lcc ON lcc.CLASSIFICATION_CLASS_ID = ii.CLASSIFICATION_CLASS_ID
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBH AS lcp ON lcp.CLASSIFICATION_PBH_ID = ii.CLASSIFICATION_PBH_ID
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBHF AS lcpf ON lcpf.CLASSIFICATION_PBHF_ID = ii.CLASSIFICATION_PBHF_ID
LEFT JOIN PWRNXGDTA.LIST_PROPRIETARY_ITEM AS lpi ON lpi.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
JOIN PWRNXGDTA.PA_ASSORTMENT AS pa ON pa.PACKAGE_ASSORTMENT_ID = ia.PACKAGE_ASSORTMENT_ID
JOIN PWRNXGDTA.ITEM_PRODUCT AS ip ON ip.ITEM_ID = ii.ITEM_ID
JOIN PWRNXGDTA.PRODUCT_INFO AS pin ON pin.PRODUCT_ID = ip.PRODUCT_ID
LEFT JOIN PWRNXGDTA.PRODUCT_NUTRITIONAL AS pn ON pn.PRODUCT_ID = ip.PRODUCT_ID
LEFT JOIN PWRNXGDTA.LKP_BRAND_INFORMATION AS lbi ON lbi.BRAND_ID = pin.BRAND_ID
AND lbi.RECORD_STATUS = 'A'
JOIN PWRNXGDTA.PACKAGE_IDENTIFIER AS pid ON pid.PACKAGE_ID = pa.PACKAGE_ID
JOIN PWRNXGDTA.PA_CONSUMABLE AS pac ON pac.PACKAGE_ASSORTMENT_ID = pa.PACKAGE_ASSORTMENT_ID
where id.DESCRIPTION_1 like '%HAAGEN DAZ VAN MLK CHO BR%'
GROUP BY ii.ITEM_ID,
ii.PARENT_ITEM_ID,
ii.ITEM_IDENTIFIER,
id.DESCRIPTION_1,
id.DESCRIPTION_4,
pac.PRE_PRICE_AMOUNT
Another way is to use Row_Number to avoid this group by:
Select * from (
SELECT DISTINCT 'Y' AS isEnterprise,
ii.ITEM_ID AS itemId,
ii.PARENT_ITEM_ID as PID,
ii.ITEM_IDENTIFIER AS productNo,
id.DESCRIPTION_1 AS description,
id.DESCRIPTION_4 AS packValue,
pid.PACKAGE_IDENTIFIER AS gtin,
RowN = Row_Number() over (Partition by ii.Item_Id, ii.Parent_Item_id order by pid.PACKAGE_IDENTIFIER) --Added new row_number if you want max you can use desc
pac.PRE_PRICE_AMOUNT AS eachPrice
FROM PWRNXGDTA.ITEM_INFORMATION AS ii
JOIN PWRNXGDTA.ITEM_ASSORTMENT AS ia ON ii.ITEM_ID = ia.ITEM_ID
JOIN PWRNXGDTA.ITEM_DESCRIPTION AS id ON id.ITEM_ID = ii.ITEM_ID
JOIN PWRNXGDTA.BUSINESS_UNIT AS bu ON ii.LOGISTICS_BU_ID = bu.BU_ID
LEFT JOIN PWRNXGDTA.ITEM_EXTN_NONMDM AS ien ON ien.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_CLASS AS lcc ON lcc.CLASSIFICATION_CLASS_ID = ii.CLASSIFICATION_CLASS_ID
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBH AS lcp ON lcp.CLASSIFICATION_PBH_ID = ii.CLASSIFICATION_PBH_ID
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBHF AS lcpf ON lcpf.CLASSIFICATION_PBHF_ID = ii.CLASSIFICATION_PBHF_ID
LEFT JOIN PWRNXGDTA.LIST_PROPRIETARY_ITEM AS lpi ON lpi.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
JOIN PWRNXGDTA.PA_ASSORTMENT AS pa ON pa.PACKAGE_ASSORTMENT_ID = ia.PACKAGE_ASSORTMENT_ID
JOIN PWRNXGDTA.ITEM_PRODUCT AS ip ON ip.ITEM_ID = ii.ITEM_ID
JOIN PWRNXGDTA.PRODUCT_INFO AS pin ON pin.PRODUCT_ID = ip.PRODUCT_ID
LEFT JOIN PWRNXGDTA.PRODUCT_NUTRITIONAL AS pn ON pn.PRODUCT_ID = ip.PRODUCT_ID
LEFT JOIN PWRNXGDTA.LKP_BRAND_INFORMATION AS lbi ON lbi.BRAND_ID = pin.BRAND_ID
AND lbi.RECORD_STATUS = 'A'
JOIN PWRNXGDTA.PACKAGE_IDENTIFIER AS pid ON pid.PACKAGE_ID = pa.PACKAGE_ID
JOIN PWRNXGDTA.PA_CONSUMABLE AS pac ON pac.PACKAGE_ASSORTMENT_ID = pa.PACKAGE_ASSORTMENT_ID
where id.DESCRIPTION_1 like '%HAAGEN DAZ VAN MLK CHO BR%'
) a
Where a.RowN = 1
Related
SELECT distinct AD.ReferenceNumber, AD.ProjectTitle, Z.ZoneCode, C.CompanyName,SS.AssignedTo, ZG.ZoneGroupName,au.Amount
FROM ApplicationDetails AD
LEFT JOIN ApplicationFormsDetails AS b ON (AD.referencenumber = b.referencenumber)
LEFT JOIN ScheduleSummaries AS SS ON (AD.ReferenceNumber = SS.ReferenceNo)
INNER JOIN AppTypes as at on ss.ItemCode = at.Category
INNER JOIN Companies AS C ON (AD.CompanyId = C.CompanyID)
INNER JOIN Zones Z ON (C.ZoneCode = Z.ZoneCode)
INNER JOIN ZoneGroups ZG ON (Z.ZoneGroup = ZG.ZoneGroupId)
LEFT JOIN AssessmentUsedItems au on ah.AssessmentHeaderId = au.HeaderId
WHERE AD.ApplicationDate BETWEEN '2017-10-01' AND '2017-10-31' AND ZG.ZoneGroupCode = 'HO' and ah.referencenumber = 'N-101317-A1-02'
GROUP BY AD.ReferenceNumber, AD.ProjectTitle, Z.ZoneCode, C.CompanyName,SS.AssignedTo, ZG.ZoneGroupName,au.Amount--, ah.ApplicationForm,au.Amount
The output of this query is its duplicating the amount for every AssignTO.
Output :
Maybe you want to try using SUM(ISNULL(au.amount, 0)) AS amount instead of au.amount and remove au.amount from the GROUP BY as well...
Try this query:
SELECT AD.ReferenceNumber,
AD.ProjectTitle,
Z.ZoneCode,
C.CompanyName,
SS.AssignedTo,
ZG.ZoneGroupName,
SUM(COALESCE(au.Amount,0)) AS Amount
FROM ApplicationDetails AD
LEFT JOIN ApplicationFormsDetails AS b
ON (AD.referencenumber = b.referencenumber)
LEFT JOIN ScheduleSummaries AS SS
ON (AD.ReferenceNumber = SS.ReferenceNo)
INNER JOIN AppTypes AS at
ON ss.ItemCode = at.Category
INNER JOIN Companies AS C
ON (AD.CompanyId = C.CompanyID)
INNER JOIN Zones Z
ON (C.ZoneCode = Z.ZoneCode)
INNER JOIN ZoneGroups ZG
ON (Z.ZoneGroup = ZG.ZoneGroupId)
LEFT JOIN AssessmentUsedItems au
ON ah.AssessmentHeaderId = au.HeaderId
WHERE AD.ApplicationDate BETWEEN '2017-10-01' AND '2017-10-31'
AND ZG.ZoneGroupCode = 'HO'
AND ah.referencenumber = 'N-101317-A1-02'
GROUP BY
AD.ReferenceNumber,
AD.ProjectTitle,
Z.ZoneCode,
C.CompanyName,
SS.AssignedTo,
ZG.ZoneGroupName
Hi i have one sql query in sql server that looks like
SELECT
DTST.[Id],
FPT.[StartTime],
--FPT.[BusinessTypeId],
FPT.[PaymentMethodId],
FPT.[3DSecureId],
FPT.[ProductId],
DPT.[Id],
FPT.[TransactionStatusId],
FPT.[Amount],
DC.[Code],
FPT.[PlayerId],
DPL.[SourceOrigId],
DPL.[Username],
DPL.[FirstName],
DPL.[LastName],
DPL.[BrandId],
DPL.[VIPLevelId],
DPL.[MarketingChannelId],
DPL.[MarketingSourceId],
FPT.[PaymentReasonTextId],
FPT_Ref.[PaymentReasonTextId],
AD.Username,
FPT.[OriginalTransactionId],
FPT.[TransactionId],
FPT_Ref.[OriginalTransactionId],
FPT.[ProviderTransactionOrigId]
FROM WarehouseMgmt.FactPaymentTrans AS FPT
JOIN
(
SELECT TransactionId,MAX(TransactionStepId) TransactionStepId
FROM [WarehouseMgmt].[FactPaymentTrans]
WHERE FactType = ''SOURCE''
GROUP BY TransactionId
) AS FPT_Last ON FPT.TransactionId = FPT_Last.TransactionId AND FPT.TransactionStepId = FPT_Last.TransactionStepId
JOIN WarehouseMgmt.DimTransactionStepType AS DTST ON FPT.[TransactionStepTypeId] = DTST.[Id]
JOIN WarehouseMgmt.DimCurrency AS DC ON FPT.CurrencyId = DC.Id
JOIN WarehouseMgmt.DimPlayer AS DPL ON FPT.PlayerId = DPL.Id
JOIN WarehouseMgmt.DimProduct AS DP ON DP.Id = FPT.ProductId
JOIN WarehouseMgmt.DimProductType AS DPT ON DPT.Id = DP.ProductTypeId
JOIN [WarehouseMgmt].[DimLoyaltyProgramLevel] DLPL ON DLPL.[Id]=DPL.VipLevelId
JOIN WarehouseMgmt.DimAdmin AS AD ON AD.Id=FPT.CreatedByAdminId
JOIN WarehouseMgmt.DimPaymentTransactionBusinessType AS DPTBT ON DPTBT.Id = FPT.BusinessTypeId
JOIN WarehouseMgmt.DimTransactionStatus TS ON TS.Id= FPT.TransactionStatusId
JOIN WarehouseMgmt.DimPaymentMethod DPM ON DPM.Id = FPT.[PaymentMethodId]
JOIN WarehouseMgmt.DimTimeZone DTZ on FPT.[TimeId] = DTZ.TimeUTCId
LEFT JOIN [WarehouseMgmt].[FactPaymentTrans] FPT_Ref ON FPT_Ref.OriginalTransactionId = FPT.TransactionId AND FPT_Ref.FactType = ''SOURCE''
WHERE (FPT.FactType = ''SOURCE'')
AND DTZ.TimeId BETWEEN #DimStartDate AND #DimEndDate ' + #sqlFilters
With this query i have duplicated transactions, because of LEFT JOIN. How can i improve my LEFT JOIN to take only MAX TransactionId in from LEFT JOIN ?
select * from iiasa_inventory.inv_device d
join iiasa_inventory.inv_type ty on d.type_id = ty.id
join iiasa_inventory.inv_category c on ty.category_id = c.id
join iiasa_inventory.inv_device_2_barcode b on b.device_id = d.id
join iiasa_inventory.inv_barcodes bc on b.barcode_id = bc.id
join iiasa_inventory.inv_status s on d.status = s.id
join iiasa_inventory.inv_brand br on ty.brand_id = br.id
left join iiasa_inventory.inv_supplier su on su.id = d.supplier_id
left join iiasa_inventory.inv_supplier sup on sup.id = d.maintenance_with
left join (select distinct device_id from
iiasa_inventory.inv_device_2_persons_cc) dp
on dp.device_id = d.id
where dp.active = 1
I am trying to select my data but the where-clause says that "dp.active" is an INVALID Identifier. This is probably because the table dp is in the subquery. I have tried to give it an alias name and some other things I found while browsing stackoverflow, but I cant seem to find a solution. Any idea?
This is Oracle PL/SQL.
Put the check for active = 1 in the subquery as shown below.
select * from iiasa_inventory.inv_device d
join iiasa_inventory.inv_type ty on d.type_id = ty.id
join iiasa_inventory.inv_category c on ty.category_id = c.id
join iiasa_inventory.inv_device_2_barcode b on b.device_id = d.id
join iiasa_inventory.inv_barcodes bc on b.barcode_id = bc.id
join iiasa_inventory.inv_status s on d.status = s.id
join iiasa_inventory.inv_brand br on ty.brand_id = br.id
left join iiasa_inventory.inv_supplier su on su.id = d.supplier_id
left join iiasa_inventory.inv_supplier sup on sup.id = d.maintenance_with
left join (select distinct device_id from iiasa_inventory.inv_device_2_persons_cc where active = 1) dp on dp.device_id = d.id
That is because you are not selecting active column in dp.
select * from iiasa_inventory.inv_device d
join iiasa_inventory.inv_type ty on d.type_id = ty.id
join iiasa_inventory.inv_category c on ty.category_id = c.id
join iiasa_inventory.inv_device_2_barcode b on b.device_id = d.id
join iiasa_inventory.inv_barcodes bc on b.barcode_id = bc.id
join iiasa_inventory.inv_status s on d.status = s.id
join iiasa_inventory.inv_brand br on ty.brand_id = br.id
left join iiasa_inventory.inv_supplier su on su.id = d.supplier_id
left join iiasa_inventory.inv_supplier sup on sup.id = d.maintenance_with
left join (select distinct device_id,active from iiasa_inventory.inv_device_2_persons_cc) dp on dp.device_id = d.id
where dp.active = 1
OR you can just filter from the subquery itself. Like:
left join (select distinct device_id
from iiasa_inventory.inv_device_2_persons_cc
where active=1) dp on dp.device_id = d.id
I'm getting this error: Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.
I searched on internet, but i don't know how to apply the correction in my case. I found different types of query only.
I'm trying to get a Count result in a field (line 5), but when I add the Count query i'm getting this error.
I'm using SQL SERVER 2008 R2.
When I remove the Count from SELECT and GROUP BY the query run correctly.
SELECT TF.COD_FORNECEDOR AS 'Cód. Fornec.',
TF.NOM_FANTASIA AS 'Fornecedor',
DM.COD_CONTRATO_RELACIONADO AS 'CONTRATO',
UA2.NOM_USUARIO AS 'NOM_USUARIO_COMPRADOR',
(SELECT COUNT(COD_CONTRATO_RELACIONADO) FROM TB_DEMANDA D INNER JOIN TB_PROCESSO P ON D.COD_CONTRATO_RELACIONADO = P.NUM_CONTRATO WHERE D.COD_CONTRATO_RELACIONADO = DM.COD_CONTRATO_RELACIONADO) AS 'NUM_ADITIVO',
0 AS 'Qtd. Aditivo',
SUM(DM.VAL_TOTAL_ORCADO) AS 'Valor Inicial',
SUM(TP.VAL_CONTRATADO) AS 'Valor Final',
((SUM(TP.VAL_CONTRATADO) / SUM(DM.VAL_TOTAL_ORCADO)) -1) * 100 AS 'Var. %'
FROM TB_FORNECEDOR TF
LEFT JOIN TB_DEMANDA DM ON DM.COD_FORNECEDOR = TF.COD_FORNECEDOR
LEFT JOIN TB_PROCESSO TP ON DM.COD_DEMANDA = TP.COD_DEMANDA
LEFT JOIN TB_PROCESSO_FORNECEDOR PF ON PF.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN USUARIO UA ON UA.NUM_USUARIO = DM.NUM_USUARIO_COMPRADOR
LEFT JOIN USUARIO UA2 ON UA2.NUM_USUARIO = DM.NUM_USUARIO_COMPRADOR
LEFT JOIN TB_STATUS_DEMANDA_DATA SD ON SD.COD_DEMANDA = DM.COD_DEMANDA
LEFT JOIN TB_BASELINE BA ON BA.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN TB_BASELINE_TAREFAS BT ON BT.COD_BASELINE = BA.COD_BASELINE AND BT.COD_PROCESSO = BA.COD_PROCESSO
LEFT JOIN TB_STATUS_PROCESSO SP ON SP.COD_STATUS = SD.COD_STATUS
LEFT JOIN TB_MEDIO_STATUS MS ON MS.COD_MEDIO_STATUS = SP.COD_MEDIO_STATUS
left JOIN #TB_PROCESSO TT ON TT.COD_PROCESSO = TP.COD_PROCESSO
GROUP BY
TF.COD_FORNECEDOR,
TF.NOM_FANTASIA,
DM.COD_CONTRATO_RELACIONADO,
UA2.NOM_USUARIO,
(SELECT COUNT(COD_CONTRATO_RELACIONADO) FROM TB_DEMANDA D INNER JOIN TB_PROCESSO P ON D.COD_CONTRATO_RELACIONADO = P.NUM_CONTRATO WHERE D.COD_CONTRATO_RELACIONADO = DM.COD_CONTRATO_RELACIONADO)
ORDER BY TF.NOM_FANTASIA
Try this:
SELECT TF.COD_FORNECEDOR AS 'Cód. Fornec.',
TF.NOM_FANTASIA AS 'Fornecedor',
DM.COD_CONTRATO_RELACIONADO AS 'CONTRATO',
UA2.NOM_USUARIO AS 'NOM_USUARIO_COMPRADOR',
sq.cnt AS 'NUM_ADITIVO',
0 AS 'Qtd. Aditivo',
SUM(DM.VAL_TOTAL_ORCADO) AS 'Valor Inicial',
SUM(TP.VAL_CONTRATADO) AS 'Valor Final',
((SUM(TP.VAL_CONTRATADO) / SUM(DM.VAL_TOTAL_ORCADO)) -1) * 100 AS 'Var. %'
FROM TB_FORNECEDOR TF
LEFT JOIN TB_DEMANDA DM ON DM.COD_FORNECEDOR = TF.COD_FORNECEDOR
LEFT JOIN TB_PROCESSO TP ON DM.COD_DEMANDA = TP.COD_DEMANDA
LEFT JOIN TB_PROCESSO_FORNECEDOR PF ON PF.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN USUARIO UA ON UA.NUM_USUARIO = DM.NUM_USUARIO_COMPRADOR
LEFT JOIN USUARIO UA2 ON UA2.NUM_USUARIO = DM.NUM_USUARIO_COMPRADOR
LEFT JOIN TB_STATUS_DEMANDA_DATA SD ON SD.COD_DEMANDA = DM.COD_DEMANDA
LEFT JOIN TB_BASELINE BA ON BA.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN TB_BASELINE_TAREFAS BT ON BT.COD_BASELINE = BA.COD_BASELINE AND BT.COD_PROCESSO = BA.COD_PROCESSO
LEFT JOIN TB_STATUS_PROCESSO SP ON SP.COD_STATUS = SD.COD_STATUS
LEFT JOIN TB_MEDIO_STATUS MS ON MS.COD_MEDIO_STATUS = SP.COD_MEDIO_STATUS
LEFT JOIN #TB_PROCESSO TT ON TT.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN (
SELECT D.COD_CONTRATO_RELACIONADO, COUNT(COD_CONTRATO_RELACIONADO) AS cnt
FROM TB_DEMANDA D
INNER JOIN TB_PROCESSO P ON D.COD_CONTRATO_RELACIONADO = P.NUM_CONTRATO
GROUP BY D.COD_CONTRATO_RELACIONADO
) sq ON sq.COD_CONTRATO_RELACIONADO = DM.COD_CONTRATO_RELACIONADO
GROUP BY
TF.COD_FORNECEDOR,
TF.NOM_FANTASIA,
DM.COD_CONTRATO_RELACIONADO,
UA2.NOM_USUARIO,
sq.cnt
ORDER BY TF.NOM_FANTASIA
I integrated the num_aditivo subquery in a LEFT JOIN.
I have a query that I need to update to allow user to filter out pending applications. I have created the parameter and tried to implement using case but it is not working or giving any error messages on how to correct it. The code is:
select distinct pers.person_fname,
pers.person_mname,
pers.person_lname,
le.nationalprovidernumber NPN,
lic.licensenumber LICENSE_NUMBER,
adr.address_line1 ADDRESS1,
adr.address_line2 ADDRESS2,
adr.address_line3 ADDRESS3,
adr.city CITY,
sp.state_province_name STATE,
adr.postal_code ZIP_CODE,
eml.email,
rtp.residencetype_name RESIDENCY,
ltp.licensetype_name LICENSE_TYPE,
lic.expirationdate DATE_OF_EXPIRATION
from odilic_admin.license lic
inner join odilic_admin.licenseststimeline lst
on lic.license_id = lst.license_id
inner join odilic_admin.licenseststype lstp
on lst.licenseststype_id = lstp.licenseststype_id
inner join odilic_admin.licensedef ldef
on lic.licensedef_id = ldef.licensedef_id
inner join odilic_admin.licensetype ltp
on ldef.licensetype_id = ltp.licensetype_id
inner join odilic_admin.residencetype rtp
on ldef.residencetype_id = rtp.residencetype_id
inner join odilic_admin.licensingentity le
on lic.licensingentity_id = le.licensingentity_id
inner join odilic_admin.individual ind
on le.licensingentity_id = ind.licensingentity_id
inner join odidir_admin.person pers
on ind.person_id = pers.person_id
left outer join odidir_admin.person_address_rel par
on pers.person_id = par.person_id
left outer join odidir_admin.address adr
on par.address_id = adr.address_id
left outer join odidir_admin.address_type atp
on adr.address_type_id = atp.address_type_id
left outer join odidir_admin.state_province sp
on adr.state_province_id = sp.state_province_id
left outer join
(select pr.person_id, em.email_id, em.email
from odidir_admin.person pr,
odidir_admin.person_email_rel pe,
odidir_admin.email em
where pr.person_id = pe.person_id
and pe.email_id = em.email_id
and email_type_id = 2) eml
on pers.person_id = eml.person_id
where
ltp.licensetype_id in (:License_type)
and lstp.licenseststype_name = 'Active'
and atp.address_type_name = 'Mailing Licensing'
and (lic.expirationdate >= current_date and
trunc(lic.expirationdate) = :Expiration_Date)
and sysdate between lst.periodbegindate and lst.periodenddate
order by lic.licensenumber
In order to get applications that are pending I need to access the table odilic_admin.licenseappl and filter out all licenses with appststype = 2 (pending). To do this I added a join to the query before the last left outer join andt hen a case at bottom for when this parameter is selected.
select distinct pers.person_fname,
pers.person_mname,
pers.person_lname,
le.nationalprovidernumber NPN,
lic.licensenumber LICENSE_NUMBER,
adr.address_line1 ADDRESS1,
adr.address_line2 ADDRESS2,
adr.address_line3 ADDRESS3,
adr.city CITY,
sp.state_province_name STATE,
adr.postal_code ZIP_CODE,
eml.email,
rtp.residencetype_name RESIDENCY,
ltp.licensetype_name LICENSE_TYPE,
lic.expirationdate DATE_OF_EXPIRATION
from odilic_admin.license lic
inner join odilic_admin.licenseststimeline lst
on lic.license_id = lst.license_id
inner join odilic_admin.licenseststype lstp
on lst.licenseststype_id = lstp.licenseststype_id
inner join odilic_admin.licensedef ldef
on lic.licensedef_id = ldef.licensedef_id
inner join odilic_admin.licensetype ltp
on ldef.licensetype_id = ltp.licensetype_id
inner join odilic_admin.residencetype rtp
on ldef.residencetype_id = rtp.residencetype_id
inner join odilic_admin.licensingentity le
on lic.licensingentity_id = le.licensingentity_id
inner join odilic_admin.individual ind
on le.licensingentity_id = ind.licensingentity_id
inner join odidir_admin.person pers
on ind.person_id = pers.person_id
left outer join odidir_admin.person_address_rel par
on pers.person_id = par.person_id
left outer join odidir_admin.address adr
on par.address_id = adr.address_id
left outer join odidir_admin.address_type atp
on adr.address_type_id = atp.address_type_id
left outer join odidir_admin.state_province sp
on adr.state_province_id = sp.state_province_id
**left outer join odilic_admin.licenseappl appl
on lic.licensingentity_id = appl.licenseappl_id**
left outer join
(select pr.person_id, em.email_id, em.email
from odidir_admin.person pr,
odidir_admin.person_email_rel pe,
odidir_admin.email em
where pr.person_id = pe.person_id
and pe.email_id = em.email_id
and email_type_id = 2) eml
on pers.person_id = eml.person_id
where
ltp.licensetype_id in (:License_type)
and lstp.licenseststype_name = 'Active'
and atp.address_type_name = 'Mailing Licensing'
and (lic.expirationdate >= current_date and
trunc(lic.expirationdate) = :Expiration_Date)
and sysdate between lst.periodbegindate and lst.periodenddate
**case :pending when = yes then appl.applststype_id !=2
end**
order by lic.licensenumber
Instead of the case I have also tried using an IF with the same result. This looks like:
if :Pending = 1
then
and appl.applststype_id != 2;
end if;
Any help to get me past this is greatly appreciated and I will be sure to vote and select most correct answer to help me solve this.
Assuming that your :pending parameter is a numeric where a value of 1 indicates that pending licences are to be excluded and you only want to exclude licence applications that are pending, try adding the following condition in place of your existing case clause:
and (:pending <> 1 or appl.applststype_id !=2)