Inside the JOIN (BB_VENDASDOCUMENTOS VD), I need to choose if VD.ENTRADASAIDA will be 'S' or 'E', what I actually need is, if there is at least one BB_VENDASDOCUMENTOS where ENTRADASAIDA = 'S', use 'S', else 'N'
I've tried to do a CASE WHEN counting the numer of VD.ENTRADASAIDA = 'S', if there is more then 0, It will use 'S', else 'N', but I'm getting an error that and aggregate cannot appear in an ON clause...
SELECT
CASE WHEN CENTROCUSTO.DESCRICAO IS NOT NULL THEN CENTROCUSTO.DESCRICAO + '|' ELSE '' END AS [text()]
FROM BB_PNRACCOUNTINGS ACC1
JOIN BB_VENDASDOCUMENTOS VD
ON ( VD.ACCOUNTING = ACC1.HANDLE
AND VD.ENTRADASAIDA = CASE WHEN count(case when VD.ENTRADASAIDA = 'S' THEN 1 ELSE NULL END ) > 0 THEN 'S' ELSE 'E' END
AND VD.PESSOA = ACC1.BB_CLIENTE
AND ( ( VD.CANCELADO = 'N' ) OR (VD.CANCELADO IS NULL) ) )
LEFT JOIN BB_CLIENTECC CENTROCUSTO
ON (CENTROCUSTO.Handle = VD.CENTRODECUSTO)
WHERE CENTROCUSTO.DESCRICAO IS NOT NULL
You will need to scan the table twice. Once to mark each group with a S or E and then to gather only the records that match the condition. I would do this by joining on a subquery and then join again on the condition. I am not sure the syntax below is 100% correct, however, the basic concept is there.
;WITH BB_VENDASDOCUMENTOSCalc AS
(
SELECT ACCOUNTING,PESSOA,
ENTRADASAIDACalc = count(case when ENTRADASAIDA = 'S' THEN 1 ELSE NULL END ) > 0 THEN 'S' ELSE 'E' END
FROM
BB_VENDASDOCUMENTOS
WHERE
(CANCELADO='N' OR CANCELADO IS NULL)
GROUP BY
ACCOUNTING,
PESSOA
)
SELECT
CASE WHEN CENTROCUSTO.DESCRICAO IS NOT NULL THEN CENTROCUSTO.DESCRICAO + '|' ELSE '' END AS [text()]
FROM BB_PNRACCOUNTINGS ACC1
JOIN BB_VENDASDOCUMENTOSCalc VD1 ON VD1.ACCOUNTING = ACC1.HANDLE AND VD1.PESSOA = ACC1.BB_CLIENTE
JOIN BB_VENDASDOCUMENTO VD ON VD.ACCOUNTING = ACC1.HANDLE AND VD.PESSOA = ACC1.BB_CLIENTE AND VD.ENTRADASAIDA = VD1.ENTRADASAIDACalc
LEFT JOIN BB_CLIENTECC CENTROCUSTO
ON (CENTROCUSTO.Handle = VD.CENTRODECUSTO)
WHERE CENTROCUSTO.DESCRICAO IS NOT NULL
Related
Needs to create view with below columns. Need to understand how to create view using with clause inside the query.
create
or replace view TEST_RRX.DW_RRX.V_SIL_RRX_PTS_BusinessLocation_Dim(
--ROW_WID,
--ADDRESS_TYPE,
BUSN_LOC_NAME,
BUSN_LOC_NUM,
BUSN_LOC_TYPE,
ST_ADDRESS1,
ST_ADDRESS2,
CITY_NAME,
POSTAL_CODE,
STATE_CODE,
COUNTRY_CODE,
ACTIVE_FLG,
CREATED_BY_WID,
CHANGED_BY_WID,
CREATED_ON_DT,
CHANGED_ON_DT,
AUX1_CHANGED_ON_DT,
AUX2_CHANGED_ON_DT,
AUX3_CHANGED_ON_DT,
AUX4_CHANGED_ON_DT,
SRC_EFF_FROM_DT,
SRC_EFF_TO_DT,
EFFECTIVE_FROM_DT,
EFFECTIVE_TO_DT,
DELETE_FLG,
CURRENT_FLG,
W_INSERT_DT,
W_UPDATE_DT,
DATASOURCE_NUM_ID,
--ETL_PROC_WID,
INTEGRATION_ID,
--TENANT_ID,
--X_CUSTOM,
--GEO_COUNTRY_WID,
--GEO_WID,
UPD_FLG
) as WITH CODE AS (
SELECT
W_CODE_D.MASTER_CODE AS MASTER_CODE,
W_CODE_D.MASTER_VALUE AS MASTER_VALUE,
W_CODE_D.SOURCE_CODE AS SOURCE_CODE,
W_CODE_D.DATASOURCE_NUM_ID AS DATASOURCE_NUM_ID,
W_CODE_D.SOURCE_CODE_1 as SOURCE_CODE_1,
W_CODE_D.SOURCE_CODE_2 as SOURCE_CODE_2,
W_CODE_D.SOURCE_NAME_1 as SOURCE_NAME_1,
W_CODE_D.CATEGORY
FROM
W_CODE_D
WHERE
W_CODE_D.LANGUAGE_CODE = 'E'
),
USER_TAB as (
SELECT
LOOKUP_TABLE.ROW_WID as ROW_WID,
LOOKUP_TABLE.DATASOURCE_NUM_ID as DATASOURCE_NUM_ID,
LOOKUP_TABLE.INTEGRATION_ID as INTEGRATION_ID,
LOOKUP_TABLE.EFFECTIVE_FROM_DT as EFFECTIVE_FROM_DT,
LOOKUP_TABLE.EFFECTIVE_TO_DT as EFFECTIVE_TO_DT
FROM
DW_RRX.W_USER_D LOOKUP_TABLE
),
BUSN_LOCATION as (
SELECT
sq.BUSN_LOC_NAME,
sq.BUSN_LOC_NUM,
sq.BUSN_LOC_TYPE,
sq.ST_ADDRESS1,
sq.ST_ADDRESS2,
case when CITY.MASTER_VALUE is null then (
case when sq.CITY_NAME is null
OR LENGTH(sq.CITY_NAME) = 0
OR REGEXP_INSTR(sq.CITY_NAME, ' ')> 0 then 'Unspecified' else sq.CITY_NAME end
) else CITY.MASTER_VALUE end as city_name,
sq.POSTAL_CODE,
case when STATE.MASTER_CODE is null then (
case when sq.STATE_CODE is null
OR LENGTH(sq.STATE_CODE) = 0
OR REGEXP_INSTR(sq.STATE_CODE, ' ')> 0 then 'Unspecified' else sq.STATE_CODE end
) else STATE.MASTER_CODE end as STATE_CODE,
case when COUNTRY.MASTER_CODE is null then (
case when sq.COUNTRY_CODE is null
OR LENGTH(sq.COUNTRY_CODE) = 0
OR REGEXP_INSTR(sq.COUNTRY_CODE, ' ')> 0 then 'Unspecified' else sq.COUNTRY_CODE end
) else COUNTRY.MASTER_VALUE end as COUNTRY_CODE,
'Y' as ACTIVE_FLG,
CREATED_BY.ROW_WID AS CREATED_BY_WID,
CHANGED_BY.ROW_WID AS CREATED_BY_WID,
sq.AUX1_CHANGED_ON_DT,
sq.AUX2_CHANGED_ON_DT,
sq.AUX3_CHANGED_ON_DT,
sq.AUX4_CHANGED_ON_DT,
sq.SRC_EFF_FROM_DT,
sq.SRC_EFF_TO_DT,
case when W_BUSN_LOCATION_D.INTEGRATION_ID is null then current_date else W_BUSN_LOCATION_D.W_INSERT_DT end as EFFECTIVE_FROM_DT,
TO_DATE('01013714', 'DDMMYYYY') as EFFECTIVE_TO_DT,
'N' As DELETE_FLG,
'Y' AS CURRENT_FLG,
case when W_BUSN_LOCATION_D.INTEGRATION_ID is null then current_date else W_BUSN_LOCATION_D.W_INSERT_DT end as W_INSERT_DT,
W_BUSN_LOCATION_D.W_UPDATE_DT as current_date,
sq.DATASOURCE_NUM_ID,
--sq.ETL_PROC_WID,
sq.INTEGRATION_ID,
CASE WHEN (
SELECT
ETL_PROC_WID
FROM
TEST_RRX.DW_RRX.W_PARAM_G
WHERE
ROW_WID = 1
) = W_BUSN_LOCATION_D.ETL_PROC_WID THEN 'X' WHEN W_BUSN_LOCATION_D.INTEGRATION_ID IS NULL THEN 'I' WHEN W_BUSN_LOCATION_D.INTEGRATION_ID IS NOT NULL
AND (
sq.CHANGED_ON_DT <> W_BUSN_LOCATION_D.CHANGED_ON_DT
OR sq.AUX1_CHANGED_ON_DT <> W_BUSN_LOCATION_D.AUX1_CHANGED_ON_DT
OR sq.AUX2_CHANGED_ON_DT <> W_BUSN_LOCATION_D.AUX2_CHANGED_ON_DT
OR sq.AUX3_CHANGED_ON_DT <> W_BUSN_LOCATION_D.AUX3_CHANGED_ON_DT
OR sq.AUX4_CHANGED_ON_DT <> W_BUSN_LOCATION_D.AUX4_CHANGED_ON_DT
) THEN 'U' END as UPD_FLG
FROM
DW_RRX.W_BUSN_LOCATION_DS sq
LEFT OUTER JOIN CODE COUNTRY ON COUNTRY.SOURCE_CODE = sq.country_code
AND COUNTRY.DATASOURCE_NUM_ID = sq.DATASOURCE_NUM_ID
AND COUNTRY.CATEGORY = 'COUNTRY'
LEFT OUTER JOIN CODE CITY ON CITY.SOURCE_CODE_1 = sq.country_code
AND CITY.DATASOURCE_NUM_ID = sq.DATASOURCE_NUM_ID
AND CITY.SOURCE_CODE_2 = sq.STATE_CODE
AND CITY.SOURCE_NAME_1 = sq.CITY_NAME
AND CITY.CATEGORY = 'CITY'
LEFT OUTER JOIN CODE STATE ON STATE.DATASOURCE_NUM_ID = sq.DATASOURCE_NUM_ID
AND STATE.CATEGORY = 'STATE'
LEFT OUTER JOIN USER_TAB CREATED_BY ON sq.DATASOURCE_NUM_ID = CREATED_BY.DATASOURCE_NUM_ID
AND CREATED_BY.INTEGRATION_ID = sq.CREATED_BY_ID
and CREATED_BY.EFFECTIVE_FROM_DT <= sq.CREATED_ON_DT
and CREATED_BY.EFFECTIVE_TO_DT >= sq.CREATED_ON_DT
LEFT OUTER JOIN USER_TAB CHANGED_BY ON sq.DATASOURCE_NUM_ID = CHANGED_BY.DATASOURCE_NUM_ID
AND CHANGED_BY.INTEGRATION_ID = sq.CHANGED_BY_ID
and CHANGED_BY.EFFECTIVE_FROM_DT <= sq.CHANGED_ON_DT
and CHANGED_BY.EFFECTIVE_TO_DT >= sq.CHANGED_ON_DT
LEFT OUTER JOIN W_BUSN_LOCATION_D ON sq.INTEGRATION_ID = W_BUSN_LOCATION_D.INTEGRATION_ID
AND sq.DATASOURCE_NUM_ID = W_BUSN_LOCATION_D.DATASOURCE_NUM_ID
)
SELECT
*
FROM
BUSN_LOCATION
WHERE
UPD_FLG <> 'X'
Getting SQL compilation error: Invalid column definition list
Needs to create view with above columns
I have a query as follows
SELECT
FGB.TEMPLATE_DETAILS_REF, FGB.TEMPLATE_STRUCTURE_REF, FGB.DFEE_ELEMENT,
CASE
WHEN Condition = 'A' THEN SUM(FGS.Weighted) END AS CondA,
CASE
WHEN Condition = 'B' THEN SUM(FGS.Weighted) END AS CondB,
CASE
WHEN Condition = 'C' THEN SUM(FGS.Weighted) END AS CondC,
CASE
WHEN Condition = 'D' THEN SUM(FGS.Weighted) END AS CondD
FROM FGBlockSummary AS FGB INNER JOIN
FGSurveyItem AS FGS ON FGB.TEMPLATE_DETAILS_REF = FGS.TEMPLATE_DETAILS_REF AND FGB.TEMPLATE_STRUCTURE_REF = FGS.TEMPLATE_STRUCTURE_REF AND
FGB.DFEE_ELEMENT = FGS.DFEE_ELEMENT
GROUP BY FGB.TEMPLATE_DETAILS_REF, FGB.TEMPLATE_STRUCTURE_REF, FGB.DFEE_ELEMENT, FGS.Condition
which produces results as follows:
16 109 Ceilings NULL 14101.47 NULL NULL
16 109 Ceilings NULL NULL 227.68 NULL
How can I amalgamate the results into a single row eg
16 109 Ceilings NULL 14101.47 227.68 NULL
Thanks
Is this what you're looking for?
I moved the SUM outside of the CASEs and eliminated FGS.Condition from the GROUP BY. It'll have the side-effect of changing your NULL values to '0', but that might be tolerable?
SELECT
FGB.TEMPLATE_DETAILS_REF
,FGB.TEMPLATE_STRUCTURE_REF
,FGB.DFEE_ELEMENT
,CondA = SUM(
CASE
WHEN FGS.Condition = 'A' THEN FGS.Weighted
ELSE 0
END)
,CondB = SUM(
CASE
WHEN FGS.Condition = 'B' THEN FGS.Weighted
ELSE 0
END)
,CondC = SUM(
CASE
WHEN FGS.Condition = 'C' THEN FGS.Weighted
ELSE 0
END)
,CondD = SUM(
CASE
WHEN FGS.Condition = 'D' THEN FGS.Weighted
ELSE 0
END)
FROM
FGBlockSummary AS FGB
INNER JOIN
FGSurveyItem AS FGS
ON
FGB.TEMPLATE_DETAILS_REF = FGS.TEMPLATE_DETAILS_REF
AND FGB.TEMPLATE_STRUCTURE_REF = FGS.TEMPLATE_STRUCTURE_REF
AND FGB.DFEE_ELEMENT = FGS.DFEE_ELEMENT
GROUP BY
FGB.TEMPLATE_DETAILS_REF
,FGB.TEMPLATE_STRUCTURE_REF
,FGB.DFEE_ELEMENT;
This is probably a stupid question but i cannot figure this out
I have query like this :
select TERM_SID
,max(PUBLICDATA) as PublicData
,max(PUBLIC_AGGREGATE) as Public_AGGregate
,max(INTERNAL) as Internal
,max(INTERNAL_AGGREGATE) as internal_Aggregate
,max(LIMITED) as Limited
,max(RESTRICTED) as Restricted
from
(
SELECT TERM_SID,
CASE WHEN d2.DATA_CLASSIFICATION_DESC='Public data' THEN 'Y' ELSE 'N' END AS 'PUBLICDATA',
CASE WHEN d2.DATA_CLASSIFICATION_DESC='Public data in aggregate' THEN 'Y' ELSE 'N' END as 'PUBLIC_AGGREGATE',
CASE WHEN d2.DATA_CLASSIFICATION_DESC='Internal data' THEN 'Y' ELSE 'N' END as 'INTERNAL',
CASE WHEN d2.DATA_CLASSIFICATION_DESC='Internal data in aggregate' THEN 'Y' ELSE 'N' END as 'INTERNAL_AGGREGATE',
CASE WHEN d2.DATA_CLASSIFICATION_DESC='Limited data' THEN 'Y' ELSE 'N' END as 'LIMITED',
CASE WHEN d2.DATA_CLASSIFICATION_DESC='Restricted data' THEN 'Y' ELSE 'N' END as 'RESTRICTED'
FROM [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION] d2
JOIN [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION_MAPPING] dm2 ON dm2.DATA_CLASSIFICATION_SID=d2.DATA_CLASSIFICATION_SID
) mat
group by Term_SID
and I have tried to convert it but i keep getting tripped up by the join in the inner select , is there a way to rebuild this with a join to improve performance.
Turn it into a derived table using a Common Table Expression (CTE) and you can reference it as if it were any other table:
with mat as
(
SELECT TERM_SID
,CASE WHEN d2.DATA_CLASSIFICATION_DESC = 'Public data' THEN 'Y' ELSE 'N' END AS PUBLICDATA
,CASE WHEN d2.DATA_CLASSIFICATION_DESC = 'Public data in aggregate' THEN 'Y' ELSE 'N' END as PUBLIC_AGGREGATE
,CASE WHEN d2.DATA_CLASSIFICATION_DESC = 'Internal data' THEN 'Y' ELSE 'N' END as INTERNAL
,CASE WHEN d2.DATA_CLASSIFICATION_DESC = 'Internal data in aggregate' THEN 'Y' ELSE 'N' END as INTERNAL_AGGREGATE
,CASE WHEN d2.DATA_CLASSIFICATION_DESC = 'Limited data' THEN 'Y' ELSE 'N' END as LIMITED
,CASE WHEN d2.DATA_CLASSIFICATION_DESC = 'Restricted data' THEN 'Y' ELSE 'N' END as RESTRICTED
FROM [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION] d2
JOIN [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION_MAPPING] dm2
ON dm2.DATA_CLASSIFICATION_SID = d2.DATA_CLASSIFICATION_SID
)
select m1.TERM_SID
,m1.max(PUBLICDATA) as PublicData
,m1.max(PUBLIC_AGGREGATE) as Public_AGGregate
,m1.max(INTERNAL) as Internal
,m1.max(INTERNAL_AGGREGATE) as internal_Aggregate
,m1.max(LIMITED) as Limited
,m1.max(RESTRICTED) as Restricted
,m2.count(1) as mat2count
from mat m1
left join mat m2
on(m1.TERM_SID = m2.TERM_SID)
group by Term_SID
Can someone help me optimize this query. I only wrote out 2 of the case statements, but it actually has like 30 such cases all nested as seen in this example (with 2) below.
SELECT *
FROM QS
INNER JOIN R ON (CASE
WHEN R.FieldCode = 'L'
THEN ---
CASE
when (
(R.Match = 1 AND QS.lineid
IN (SELECT CONVERT(int,s) FROM SplitFunc(',', R.FieldValue)))
OR (R.Match = 0 AND QS.lineid NOT IN (SELECT CONVERT(int,s) FROM SplitFunc(',', R.FieldValue)))) then 1
ELSE 0
END
----
WHEN R.FieldCode = 'D'
THEN
CASE
when ((R.Match = 1 AND QS.productid
IN (SELECT CONVERT(int,s) FROM SplitFunc(',', R.FieldValue)))
OR (R.Match = 0 AND QS.productid NOT IN (SELECT CONVERT(int,s) FROM SplitFunc(',', R.FieldValue)))) then 1
ELSE 0
END
END)=1
This is really bad SQL code, as the database engine cannot do any optimization at all since it needs to enumerate the entire table and go record by record doing multiple tests on each, twice.
You can do this without any CASE at all:
SELECT *
FROM QS
INNER JOIN R ON ( (R.FieldCode = 'L'
AND ( (R.Match = 1 AND QS.lineid IN (SELECT CONVERT(int,s) FROM SplitFunc(',', R.FieldValue)))
OR (R.Match = 0 AND QS.lineid NOT IN (SELECT CONVERT(int,s) FROM SplitFunc(',', R.FieldValue)))))
OR (R.FieldCode = 'D'
AND ( (R.Match = 1 AND QS.productid IN (SELECT CONVERT(int,s) FROM SplitFunc(',', R.FieldValue)))
OR (R.Match = 0 AND QS.productid NOT IN (SELECT CONVERT(int,s) FROM SplitFunc(',', R.FieldValue)))))
)
Does this perform better for you? Not knowing the relative row counts or underlying data, I would think this might be much faster and easier to maintain since you don't have to run the scalar function over and over again and should be more optimizer-friendly.
SELECT * -- Change to only get fields you need from QS.*
FROM (
SELECT
QS.*,
',' + CONVERT(VARCHAR(10),
CASE R.FieldCode
WHEN 'L' THEN QA.lineid
WHEN 'D' THEN QA.productid
-- ...
END + ',' AS MatchValue,
',' + R.FieldValue + ',' AS FieldValueCSV
R.Match
FROM QS
CROSS JOIN R
) T
WHERE (T.Match = 1 AND T.FieldValueCSV LIKE '%' + T.MatchValue + '%')
OR (T.Match = 0 AND NOT T.FieldValueCSV LIKE '%' + T.MatchValue + '%')
I have the following query which is part of a bigger XML. The issue is that is not writing the 0 value of the ELSE and writes an empty element <SellingLocation> in the XML. I can't find why. This is the query code.
SELECT (
SELECT TOP 1
CASE WHEN COUNT(ac.SKU) > 0
THEN l.ZoneId + ' ' + SUBSTRING(CONVERT(varchar(10), l.Bays), 0, 4)
ELSE 0
END
FROM #ARTSCreditTrans ac
INNER JOIN LocationSKU l ON ac.SKU = l.SKU
WHERE ac.PickTake = 'P'
GROUP BY ZoneId, Bays
)
FOR XML PATH('SellingLocation'), TYPE
#Devart, how can I make the same modification in the case of a query which has more that one option and the filter with the having doesn't look like the solution? For example:
SELECT [text()] = ISNULL((
SELECT TOP 1
CASE WHEN COUNT(oh.OrderNumber) > 0
THEN oh.Latitude
ELSE CASE WHEN td.RegisterID = 80
THEN SUBSTRING(tt.ShipMidInit, 1, CHARINDEX('|', tt.ShipMidInit) - 1)
END
END
FROM OrderHeader oh INNER JOIN TranDetail td ON oh.OrderNumber = td.DocNumber JOIN TranTotal tt ON td.DocNumber = tt.DocNumber
WHERE td.PickTake = 'P'
GROUP BY oh.Latitude, td.RegisterID, tt.ShipMidInit
), '')
FOR XML PATH('Latitude'), TYPE
Try wrapping the 0 in quotes ('):
SELECT (
SELECT TOP 1
CASE WHEN COUNT(ac.SKU) > 0
THEN l.ZoneId + ' ' + SUBSTRING(CONVERT(varchar(10), l.Bays), 0, 4)
ELSE '0'
END
FROM #ARTSCreditTrans ac INNER JOIN LocationSKU l ON ac.SKU = l.SKU
WHERE ac.PickTake = 'P'
GROUP BY ZoneId, Bays
)
FOR XML PATH('SellingLocation')
, TYPE
SELECT [text()] = ISNULL((
SELECT TOP 1 CAST(l.ZoneId AS VARCHAR(100)) + ' ' + CAST(l.Bays AS VARCHAR(5))
FROM #ARTSCreditTrans ac
JOIN dbo.LocationSKU l ON ac.SKU = l.SKU
WHERE ac.PickTake = 'P'
GROUP BY ZoneId, Bays
HAVING COUNT(ac.SKU) > 0
), '0')
FOR XML PATH('SellingLocation'), TYPE