I really appreciate any help with this matter :)
Am Working on a Report now and I had faced some troubles
I have this Query and it work fine , now I want to add a coulmn that is already exist in the query(from the same table) , but this time i'll change the condition of it , BTW the conditions in both of the 2 column are based on one other column
like for example If I have this :
Select Price from ITM1 WHERE PriceList = '1'
and also this
Select Price from ITM1 WHERE PriceList = '10'
how I can write in the same query and let them display in two different column ?
I will put the Query here in case if some one can help me through it :
you can see THE Column Price & PriceList in the lower part of it ,Bolded.
I just need to make the samething again but with a new coulmn name thats it.
Using the IN Operator will give you what you want. However, there are other changes that you can make to your query which would boost performance - but it's out of scope to the question. I'm unclear as to what you're trying to do with the different "columns" Please help explain. Else see #Dave.Gugg's answer which does just that.
SELECT T0.ItemCode,
T0.ItemName,
T0.CardCode,
T0.CodeBars,
T2.UgpCode,
T3.AltQty,
T3.BaseQty,
CASE
WHEN T4.Uomentry = - 1
THEN T0.[BuyUnitMsr]
ELSE t4.UomName
END AS 'UoMName',
T4.UomEntry,
T0.U_CAT_CODE,
T0.U_CAT_NAME,
T1.CardName,
(
SELECT TOP (1) dbo.PDN1.U_AC_QTY_ORDER
FROM dbo.PDN1
INNER JOIN dbo.OPDN ON dbo.PDN1.DocEntry = dbo.OPDN.DocEntry
WHERE (dbo.PDN1.ItemCode = T0.ItemCode)
AND (dbo.OPDN.CardCode = T0.CardCode)
ORDER BY dbo.OPDN.DocDate DESC
) AS OQuantity,
(
SELECT TOP (1) PDN1_1.U_AC_QTY_BONUS
FROM dbo.PDN1 AS PDN1_1
INNER JOIN dbo.OPDN AS OPDN_1 ON PDN1_1.DocEntry = OPDN_1.DocEntry
WHERE (PDN1_1.ItemCode = T0.ItemCode)
AND (OPDN_1.CardCode = T0.CardCode)
ORDER BY OPDN_1.DocDate DESC
) AS BQuantity,
ITM1.Price,
T0.U_DISC_PER
FROM dbo.OITM AS T0
INNER JOIN dbo.OCRD AS T1 ON T0.CardCode = T1.CardCode
INNER JOIN dbo.OUGP AS T2 ON T0.UgpEntry = T2.UgpEntry
INNER JOIN dbo.UGP1 AS T3 ON T2.UgpEntry = T3.UgpEntry
INNER JOIN dbo.ITM1 ON T0.ItemCode = dbo.ITM1.ItemCode
AND dbo.ITM1.PriceList IN ('1', '10')
LEFT JOIN dbo.OUOM AS T4 ON T3.UomEntry = T4.UomEntry
WHERE (T0.Series = '65')
AND (
T4.UomEntry = 3
OR T4.UomEntry = '-1'
)
If you want a different column (this may perform better than two joins):
SELECT T0.ItemCode,
T0.ItemName,
T0.CardCode,
T0.CodeBars,
T2.UgpCode,
T3.AltQty,
T3.BaseQty,
CASE
WHEN T4.Uomentry = - 1
THEN T0.[BuyUnitMsr]
ELSE t4.UomName
END AS 'UoMName',
T4.UomEntry,
T0.U_CAT_CODE,
T0.U_CAT_NAME,
T1.CardName,
(
SELECT TOP (1) dbo.PDN1.U_AC_QTY_ORDER
FROM dbo.PDN1
INNER JOIN dbo.OPDN ON dbo.PDN1.DocEntry = dbo.OPDN.DocEntry
WHERE (dbo.PDN1.ItemCode = T0.ItemCode)
AND (dbo.OPDN.CardCode = T0.CardCode)
ORDER BY dbo.OPDN.DocDate DESC
) AS OQuantity,
(
SELECT TOP (1) PDN1_1.U_AC_QTY_BONUS
FROM dbo.PDN1 AS PDN1_1
INNER JOIN dbo.OPDN AS OPDN_1 ON PDN1_1.DocEntry = OPDN_1.DocEntry
WHERE (PDN1_1.ItemCode = T0.ItemCode)
AND (OPDN_1.CardCode = T0.CardCode)
ORDER BY OPDN_1.DocDate DESC
) AS BQuantity,
CASE
WHEN ITM1.PriceList = '1'
THEN ITM1.Price
ELSE '0'
END AS Price1,
CASE
WHEN ITM1.PriceList = '10'
THEN ITM1.Price
ELSE '0'
END AS Price2,
T0.U_DISC_PER
FROM dbo.OITM AS T0
INNER JOIN dbo.OCRD AS T1 ON T0.CardCode = T1.CardCode
INNER JOIN dbo.OUGP AS T2 ON T0.UgpEntry = T2.UgpEntry
INNER JOIN dbo.UGP1 AS T3 ON T2.UgpEntry = T3.UgpEntry
INNER JOIN dbo.ITM1 ON T0.ItemCode = dbo.ITM1.ItemCode
AND dbo.ITM1.PriceList IN ('1', '10')
LEFT JOIN dbo.OUOM AS T4 ON T3.UomEntry = T4.UomEntry
WHERE (T0.Series = '65')
AND (
T4.UomEntry = 3
OR T4.UomEntry = '-1'
)
You should be able to just join to the table a second time, but you will need to make the joins outer:
SELECT T0.ItemCode ,
T0.ItemName ,
T0.CardCode ,
T0.CodeBars ,
T2.UgpCode ,
T3.AltQty ,
T3.BaseQty ,
CASE WHEN T4.Uomentry = -1 THEN T0.[BuyUnitMsr]
ELSE t4.UomName
END AS 'UoMName' ,
T4.UomEntry ,
T0.U_CAT_CODE ,
T0.U_CAT_NAME ,
T1.CardName ,
( SELECT TOP ( 1 )
dbo.PDN1.U_AC_QTY_ORDER
FROM dbo.PDN1
INNER JOIN dbo.OPDN ON dbo.PDN1.DocEntry = dbo.OPDN.DocEntry
WHERE ( dbo.PDN1.ItemCode = T0.ItemCode )
AND ( dbo.OPDN.CardCode = T0.CardCode )
ORDER BY dbo.OPDN.DocDate DESC
) AS OQuantity ,
( SELECT TOP ( 1 )
PDN1_1.U_AC_QTY_BONUS
FROM dbo.PDN1 AS PDN1_1
INNER JOIN dbo.OPDN AS OPDN_1 ON PDN1_1.DocEntry = OPDN_1.DocEntry
WHERE ( PDN1_1.ItemCode = T0.ItemCode )
AND ( OPDN_1.CardCode = T0.CardCode )
ORDER BY OPDN_1.DocDate DESC
) AS BQuantity ,
dbo.ITM1.Price ,
ITM1Second.Price,
T0.U_DISC_PER
FROM dbo.OITM AS T0
INNER JOIN dbo.OCRD AS T1 ON T0.CardCode = T1.CardCode
INNER JOIN dbo.OUGP AS T2 ON T0.UgpEntry = T2.UgpEntry
INNER JOIN dbo.UGP1 AS T3 ON T2.UgpEntry = T3.UgpEntry
LEFT OUTER JOIN dbo.ITM1 ON T0.ItemCode = dbo.ITM1.ItemCode
AND dbo.ITM1.PriceList = '10'
LEFT OUTER JOIN dbo.ITM1 ITM1Second ON T0.ItemCode = ITM1Second.ItemCode
AND ITM1Second.PriceList = '1'
LEFT OUTER JOIN dbo.OUOM AS T4 ON T3.UomEntry = T4.UomEntry
WHERE ( T0.Series = '65' )
AND ( T4.UomEntry = 3
OR T4.UomEntry = '-1'
)
Related
I have a very simple Inventory in Warehouse query, and now I need to do a sum in the IsCommitted column with another query that I have called "Set Demand".
Like this = Sum( [IsCommited] + "Set Demand Query qty")
Warehouse query
SELECT T0.[ItemCode] AS 'Item No.',
T0.[WhsCode] AS 'Warehouse Code',
T0.[OnHand] AS 'In Stock',
T0.[IsCommited] AS 'Committed',
T0.[MinStock] AS 'Minimum Inventory',
T0.[MaxStock] AS 'Maximum Inventory',
T1.[ItmsGrpCod] AS 'Itemcode',
T2.Price AS 'StandardCost'
FROM [OITW] T0 INNER JOIN [OITM] T1 ON T0.ItemCode = T1.ItemCode LEFT JOIN [ITM1] T2 ON T1.ItemCode = T2.ItemCode and T2.PriceList = 26
WHERE (T0.[WhsCode] = (N'9500' )) AND (T1.[ItmsGrpCod] = (N'100' )) AND T0.[OnHand] > 0
Set Demand query.
SELECT T3.[ItemCode] ,
(-T3.[OnHand] + T1.[Quantity]) as 'Set Demand'
FROM [OITT] T0 WITH (NOLOCK) INNER JOIN [ITT1] T1 WITH (NOLOCK) ON T0.[Code] = T1.[Father], [OSRI] T2 WITH (NOLOCK), [OITW] T3 WITH (NOLOCK) INNER JOIN [OITM] T4 WITH (NOLOCK) ON T3.[ITEMCode] = T4.[ItemCode]
WHERE T0.[Code] = T2.[ItemCode] AND T1.[Code] = T3.[ItemCode] AND T2.[IntrSerial] = T3.[WhsCode] AND T2.[Status] <> 1 and T2.[U_IsCon] <> 'YES' and T3.[OnHand] - T1.[Quantity] < 0 and substring (T2.[WhsCode],8,1)<>'C' AND T2.[WhsCode] = '9000' AND t4.[ItmsGrpCod] = 100
What Im trying to do = ( [IsCommited] + "Set Demand Query qty")
SELECT T0.[ItemCode] AS 'Item No.',
T0.[WhsCode] AS 'Warehouse Code',
T0.[OnHand] AS 'In Stock',
(T0.[IsCommited] + (SELECT (-T3.[OnHand] + T1.[Quantity])
FROM [OITT] T0 WITH (NOLOCK) INNER JOIN [ITT1] T1 WITH (NOLOCK) ON T0.[Code] = T1.[Father], [OSRI] T2 WITH (NOLOCK), [OITW] T3 WITH (NOLOCK) INNER JOIN [OITM] T4 WITH (NOLOCK) ON T3.[ITEMCode] = T4.[ItemCode]
WHERE T0.[Code] = T2.[ItemCode] AND T1.[Code] = T3.[ItemCode] AND T2.[IntrSerial] = T3.[WhsCode] AND T2.[Status] <> 1 and T2.[U_IsCon] <> 'YES' and T3.[OnHand] - T1.[Quantity] < 0 and substring (T2.[WhsCode],8,1)<>'C' AND T2.[WhsCode] = '9000' AND t4.[ItmsGrpCod] = 100) as '**Commited&SetDemand**',
T0.[MinStock] AS 'Minimum Inventory',
T0.[MaxStock] AS 'Maximum Inventory',
T1.[ItmsGrpCod] AS 'Itemcode',
T2.Price AS 'StandardCost'
FROM [OITW] T0 INNER JOIN [OITM] T1 ON T0.ItemCode = T1.ItemCode LEFT JOIN [ITM1] T2 ON T1.ItemCode = T2.ItemCode and T2.PriceList = 26
WHERE (T0.[WhsCode] = (N'9500' )) AND (T1.[ItmsGrpCod] = (N'100' )) AND T0.[OnHand] > 0
Your help will be greatly appreciated. Thank you very much!!!!! :)
Try Something like the Below, I have added the second query with a join on the Item Code.
SELECT T0.[ItemCode] AS 'Item No.',
T0.[WhsCode] AS 'Warehouse Code',
T0.[OnHand] AS 'In Stock',
T0.[IsCommited] AS 'Committed',
T0.[MinStock] AS 'Minimum Inventory',
T0.[MaxStock] AS 'Maximum Inventory',
T1.[ItmsGrpCod] AS 'Itemcode',
T2.Price AS 'StandardCost'
, t0.[IsCommited] + demand.[Set Demand]
FROM [OITW] T0 INNER JOIN [OITM] T1 ON T0.ItemCode = T1.ItemCode LEFT JOIN
[ITM1] T2 ON T1.ItemCode = T2.ItemCode and T2.PriceList = 26
Inner Join
(
SELECT T3.[ItemCode] ,
(-T3.[OnHand] + T1.[Quantity]) as 'Set Demand'
FROM [OITT] T0 WITH (NOLOCK) INNER JOIN [ITT1] T1 WITH (NOLOCK) ON T0.[Code] =
T1.[Father], [OSRI] T2 WITH (NOLOCK), [OITW] T3 WITH (NOLOCK) INNER JOIN [OITM]
T4 WITH (NOLOCK) ON T3.[ITEMCode] = T4.[ItemCode]
WHERE T0.[Code] = T2.[ItemCode] AND T1.[Code] = T3.[ItemCode] AND T2.
[IntrSerial] = T3.[WhsCode] AND T2.[Status] <> 1 and T2.[U_IsCon] <> 'YES' and T3.[OnHand] - T1.
[Quantity] < 0 and substring (T2.[WhsCode],8,1)<>'C' AND T2.[WhsCode] = '9000'
AND t4.[ItmsGrpCod] = 100
) as demand on demand.ItemCode = t0.ItemCode
WHERE (T0.[WhsCode] = (N'9500' )) AND (T1.[ItmsGrpCod] = (N'100' )) AND T0.
[OnHand] > 0
Thank you all for your answers, much appreciated.
And also sorry for my delay in answering.
I was able to solve my problem through this code.
Please look below.
What sawsine said, was exactly that, the relationship between items was missing.
SELECT
OITW.ItemCode,
SUM(CONVERT(INT, (ITT1.quantity - OITW.OnHand))) as 'Set Demand'
FROM OSRI
inner join OSRN on OSRN.ItemCode = OSRI.ItemCode AND OSRN.SysNumber = OSRI.SysSerial
INNER JOIN OITT ON OSRI.ItemCode = OITT.Code
INNER JOIN ITT1 ON ITT1.Father = OITT.Code
INNER JOIN OITW ON ITT1.Code = OITW.ItemCode AND OSRI.IntrSerial = OITW.WhsCode
inner join OWHS on OSRI.WhsCode = OWHS.WhsCode
inner join OITM on ITT1.Code = OITM.ItemCode
WHERE 1=1
AND OSRI.WhsCode = N'9000'
and OSRI.[Status] = 0 and
(OSRN.U_Condition <> N'2' or OSRN.U_Condition is Null) and
OITM.ItmsGrpCod IN (100, 102, 104) and
(LEN(ISNULL(OITM.CardCode,'''')) = 0 or ISNULL(OITM.CardCode,'''') = 'NL20')
AND (ITT1.quantity - OITW.OnHand) > 0
Group by OITW.ItemCode
I have created the following case statement to do some multiplication depending on the items sales unit of measure and for this example, even though I am declaring the SalesUoM Variable as Gallon, it is doing the math for both the gallons and the lbs.
declare #SalesUOM as nvarchar(30)
declare #drumqty as nvarchar(30)
Set #SalesUOM = 'GAL'
Set #drumqty = '3'
SELECT DISTINCT
Case
--- when #SalesUOM = 'DRUM' then ( #drumqty * T1.BaseQty )
-- when #SalesUOM = 'PALLET' then ( #drumqty * T4.BaseQty )
--when #SalesUOM = 'PAIL' then ( #drumqty * T6.BaseQty )
--when #SalesUOM = 'TOTE' then ( #drumqty * T8.BaseQty )
when #SalesUOM = 'LB' then ( #drumqty * T10.BaseQty )
When #SalesUOM = 'GAL' then (#drumQty * T12.BaseQty)
Else '0' End as 'Item Qty'
FROM OUGP T0
--INNER JOIN UGP1 T1 ON T0.[UgpEntry] = T1.[UgpEntry]
--INNER JOIN UGP1 T4 on T0.[UgpEntry] = T4.[UgpEntry]
--INNER JOIN UGP1 T6 on T0.[UgpEntry] = T6.[UgpEntry]
--INNER JOIN UGP1 T8 on T0.UgpEntry = T8.UgpEntry
inner JOIN UGP1 T10 on T0.UgpEntry = T10.UgpEntry
inner JOIN UGP1 T12 on T0.UgpEntry = T12.UgpEntry
--INNER JOIN OUOM T2 ON T0.[BaseUom] = T2.[UomEntry]
--Inner Join OUOM T3 on T1.[UomEntry] = T3.[UomEntry] AND T3.UOMCODE in ('DR-15', 'DR-30', 'DR-55')
--Inner Join OUOM T5 on T4.[UomEntry] = T5.[UomEntry] and T5.UOMCODE = 'PALLET'
--left Join OUOM T7 on T6.UoMEntry = T7.UomEntry and T7.UOMCODE = 'PAIL-5'
--left Join OUOM T9 on T8.UomEntry = T9.UomEntry AND T9.UomCode = 'TOTE'
Left Join OUOM T11 on T10.UomEntry = T11.UomEntry and T11.UomCode = 'LB'
Left Join OUOM T13 on T12.UomEntry = T13.UomEntry and T13.UomCode = 'GAL'
Left join OITM on T0.UgpCode = OITM.ItemCode
WHERE
OITM.ItemCode = '0000000'
because you are not filtering the result set on #SalesUOM in where clause or inner join.
T10 and T12 are pointing to the same table UGP1.
below are two ways that can resolve your problem
1. Add T11.UomCode = #SalesUOM And T13.UomCode = #SalesUOM in where clause
2. Replace below Statements
"Left Join OUOM T11 on T10.UomEntry = T11.UomEntry and T11.UomCode = 'LB'
Left Join OUOM T13 on T12.UomEntry = T13.UomEntry and T13.UomCode = 'GAL'"
with below statements
Inner Join OUOM T11 on T10.UomEntry = T11.UomEntry and T11.UomCode = #SalesUOM
I have three queries,query 1 and query 2 have to be inner joined on Customer_number
Resultant output from query1 and query2 will be further inner joined on 3 keys customer_number,account_number,bank_number.
Can anyone help me to write the queries for these.I am not able to derive it.
Thanks in advance,
Bhaskar
Query1 :
SELECT
CUST.BANK_NUMBER as CUSTBANKNUMBER,
CUST.CUSTOMER_NUMBER,
CUST.DATE_DECEASE,
FROM
SIGN_LND.T_CUSTOMER_MASTER_FILE_LND CUST
LEFT OUTER JOIN
SIGN_LND.T_CUST_USER_DEF_FIELDS_LND USER_DEF
ON (CUST.CUSTOMER_NUMBER = USER_DEF.CUSTOMER_NUMBER AND CUST.EFF_DT = USER_DEF.EFF_DT)
WHERE
CUST.EFF_DT = TO_DATE('2015-01-22','YYYY-MM-DD')
Query 2 :
SELECT
S.CUSTOMER_NUMBER,
S.ALIAS_ACCOUNT_NBR,
M.BANK_NUMBER,
M.ACCOUNT_NUMBER,
M.PRODUCT_CODE,
M.APPLICATION_NUMBER,
M.APPL_ID
FROM
(
SELECT T1.BANK_NUMBER,
T1.ACCOUNT_NUMBER,
T1.PRODUCT_CODE,
CAST (NULL AS VARCHAR(5)) AS SUB_PRODUCT_CODE,
T1.APPLICATION_NUMBER,
T1.APPL_ID
FROM SIGN_LND.T_LOAN_MASTER_FILE_LND T1
INNER JOIN SIGN_LND.T_LOAN_PRODUCT_DEFINITION_LND T2
ON T1.BANK_NUMBER = T2.BANK_NUMBER
AND T1.PRODUCT_CODE = T2.PRODUCT_CODE
AND T1.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
AND T2.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
WHERE T1.PRODUCT_CODE IS NOT NULL
AND T1.DATE_OPEN IS NOT NULL
UNION ALL
SELECT T1.BANK_NUMBER,
T1.ACCOUNT_NUMBER,
T1.PRODUCT_CODE,
T1.PRODUCT_CODE AS SUB_PRODUCT_CODE,
T1.APPLICATION_NUMBER,
T1.APPL_ID
FROM SIGN_LND.T_TA_MASTER_FILE_LND T1
INNER JOIN SIGN_LND.T_TA_PRODUCT_DEFINITION_LND T2
ON T1.BANK_NUMBER = T2.BANK_NUMBER
AND T1.PRODUCT_CODE = T2.PRODUCT_CODE
AND T1.ACCOUNT_TYPE = T2.ACCOUNT_TYPE
AND T1.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
AND T2.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
LEFT JOIN SIGN_LND.T_BONUS_INT_MASTER_FILE_LND T3
ON T1.ACCOUNT_NUMBER = T3.ACCOUNT_NUMBER
AND T1.BANK_NUMBER = T3.BANK_NUMBER
AND T3.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
WHERE T1.PRODUCT_CODE IS NOT NULL
AND T1.ACCOUNT_STATUS_CODE IS NOT NULL
AND T1.DATE_OPEN IS NOT NULL
UNION ALL
SELECT T1.BANK_NUMBER,
T1.ACCOUNT_NUMBER,
T1.PRODUCT_CODE,
T1.PRODUCT_CODE AS SUB_PRODUCT_CODE,
T1.APPLICATION_NUMBER,
T1.APPL_ID,
FROM SIGN_LND.T_TIME_MASTER_FILE_LND T1
INNER JOIN SIGN_LND.T_TM_PRODUCT_DEFINITION_LND T2
ON T1.BANK_NUMBER = T2.BANK_NUMBER
AND T1.PRODUCT_CODE = T2.PRODUCT_CODE
AND T1.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
AND T2.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
LEFT JOIN SIGN_LND.T_BONUS_INT_MASTER_FILE_LND T3
ON T1.ACCOUNT_NUMBER = T3.ACCOUNT_NUMBER
AND T1.BANK_NUMBER = T3.BANK_NUMBER
AND T3.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
WHERE T1.PRODUCT_CODE IS NOT NULL
AND T1.ACCOUNT_STATUS_CODE IS NOT NULL
AND T1.DATE_OPEN IS NOT NULL
)M
INNER JOIN
(
SELECT
T1.ACCOUNT_NUMBER,
T1.CUSTOMER_NUMBER,
T1.RELATIONSHIP ,
T2.ALIAS_ACCOUNT_NBR,
T2.APPLICATION_NUMBER
FROM SIGN_LND.T_CUSTOMER_CROSS_REF_LND T1
INNER JOIN
SIGN_LND.T_ALIAS_ACCT_NBR_CRSS_REF_LND T2
ON T1.ACCOUNT_NUMBER=T2.ACCOUNT_NUMBER
WHERE ALIAS_ACCOUNT_NBR_TYPE='UK'
GROUP BY T1.ACCOUNT_NUMBER,
T1.CUSTOMER_NUMBER,
T1.RELATIONSHIP ,
T2.ALIAS_ACCOUNT_NBR,
T2.APPLICATION_NUMBER
)S
ON M.ACCOUNT_NUMBER=S.ACCOUNT_NUMBER
AND
M.APPLICATION_NUMBER=S.APPLICATION_NUMBER
)
Query3:
SELECT
SPLIT.BANK_NUMBER,
SPLIT.CUSTOMER_NUMBER,
SPLIT.DATE_MOVING_IN,
SPLIT.TITLE,
SPLIT.FIRST_NAME,
SPLIT.SECOND_NAME,
SPLIT.SURNAME,
SPLIT.CURR_APT_NUMBER_UPS,
SPLIT.CURR_HOUSE_NAME_UPS,
SPLIT.CURR_HOUSE_NBR,
SPLIT.CURR_STREET_UPS,
SPLIT.CURR_DISTRICT_UPS,
SPLIT.CURR_POST_TOWN_UPS,
SPLIT.POSTAL_CODE,
SPLIT.CURR_COUNTRY,
CUST_EXT.LOYALTY_CARD_NBR,
XREF.ACCOUNT_NUMBER,
XREF.RELATIONSHIP as RELATIONSHIP_RB,
RB_ALIAS_XREF.APPL_ID as RBXREFAPPID,RB_ALIAS_XREF.ALIAS_ACCOUNT_NBR AS ALIAS_ACCOUNT_NBR_RB,
PROD_OFFER.DATE_CLOSE
FROM
(
SIGN_LND.T_CIF_SPLIT_NAME_ADDR_UPS_LND SPLIT
LEFT OUTER JOIN
SIGN_LND.T_CUST_MASTER_EXTENSION_LND CUST_EXT
ON (SPLIT.CUSTOMER_NUMBER = CUST_EXT.CUSTOMER_NUMBER AND SPLIT.EFF_DT = CUST_EXT.EFF_DT)
)
LEFT OUTER JOIN
(
SIGN_LND.T_CUSTOMER_CROSS_REF_LND XREF
LEFT OUTER JOIN
(
SELECT * FROM SIGN_LND.T_ALIAS_ACCT_NBR_CRSS_REF_LND WHERE EFF_DT = TO_DATE('2015-01-22','YYYY-MM-DD') AND ALIAS_ACCOUNT_NBR_TYPE = 'RB'
) RB_ALIAS_XREF
ON (XREF.ACCOUNT_NUMBER = RB_ALIAS_XREF.ACCOUNT_NUMBER AND XREF.EFF_DT = RB_ALIAS_XREF.EFF_DT)
LEFT OUTER JOIN
SIGN_LND.T_PRODUCT_OFFER_PURCHASE_LND PROD_OFFER
ON (XREF.BANK_NUMBER = PROD_OFFER.BANK_NUMBER AND XREF.ACCOUNT_NUMBER = PROD_OFFER.ACCOUNT_NUMBER AND XREF.EFF_DT = PROD_OFFER.EFF_DT)
)
ON (SPLIT.CUSTOMER_NUMBER = XREF.CUSTOMER_NUMBER AND SPLIT.EFF_DT = XREF.EFF_DT)
WHERE SPLIT.EFF_DT = TO_DATE('2015-01-22','YYYY-MM-DD')``
This should work
WITH query1 as
(your query 1 code),
query2 as
(you query 2 code),
query3 as
(your query 3 code)
select * from query1
join query2 on query1.Customer_number=query2.Customer_number
join query3 on query3.customer_number=query2.customer_number
and query3.account_number=query2.account_number
and query3.bank_number=query2.bank_number
this Query have one problem with it , except FOR Column
it show multiple values in this coulmn , put as perthe condition it should only show one value '7' , ddepending on this value the coulmn value will be set , but now as it show all the value it cuase too much duplication and other issues
here the query :
SELECT T0.ItemCode, T0.ItemName, T0.CardCode, T0.CodeBars, T0.U_VEN_CODE, T2.UgpCode, T3.AltQty, T3.BaseQty, CASE WHEN T4.Uomentry = - 1 THEN T0.[BuyUnitMsr] ELSE t4.UomName END AS 'UoMName',
T4.UomEntry, T0.U_CAT_CODE, T0.U_CAT_NAME, T1.CardName,
(SELECT TOP (1) PDN1.U_AC_QTY_ORDER
FROM PDN1 INNER JOIN
OPDN ON PDN1.DocEntry = OPDN.DocEntry
WHERE (PDN1.ItemCode = T0.ItemCode) AND (OPDN.CardCode = T0.CardCode)
ORDER BY OPDN.DocDate DESC) AS OQuantity,
(SELECT TOP (1) PDN1_1.U_AC_QTY_BONUS
FROM PDN1 AS PDN1_1 INNER JOIN
OPDN AS OPDN_1 ON PDN1_1.DocEntry = OPDN_1.DocEntry
WHERE (PDN1_1.ItemCode = T0.ItemCode) AND (OPDN_1.CardCode = T0.CardCode)
ORDER BY OPDN_1.DocDate DESC) AS BQuantity, ITM1_1.Price, T0.U_DISC_PER, SMMU01.WhsCode, SMMU01.OnHand, SMAB01.WhsCode AS Expr1, SMAB01.OnHand AS Expr2,
SMKH01.WhsCode AS Expr3, SMKH01.OnHand AS Expr4, ITM9.PriceList, ITM9.Price AS Expr5, ITM1.PriceList AS Expr6, ITM1.Price AS Expr7
FROM OITM AS T0 INNER JOIN
OCRD AS T1 ON T0.CardCode = T1.CardCode INNER JOIN
OUGP AS T2 ON T0.UgpEntry = T2.UgpEntry INNER JOIN
UGP1 AS T3 ON T2.UgpEntry = T3.UgpEntry INNER JOIN
OITW AS SMMU01 ON T0.ItemCode = SMMU01.ItemCode INNER JOIN
OITW AS SMAB01 ON SMMU01.ItemCode = SMAB01.ItemCode INNER JOIN
OITW AS SMKH01 ON SMAB01.ItemCode = SMKH01.ItemCode INNER JOIN
ITM9 ON T0.ItemCode = ITM9.ItemCode AND ITM9.PriceList = '7' INNER JOIN
ITM1 ON T0.ItemCode = ITM1.ItemCode LEFT OUTER JOIN
ITM1 AS ITM1_1 ON T0.ItemCode = ITM1_1.ItemCode AND ITM1_1.PriceList = '10' LEFT OUTER JOIN
OUOM AS T4 ON T3.UomEntry = T4.UomEntry
WHERE (T0.Series = '65') AND (T4.UomEntry = 3 OR
T4.UomEntry = '-1') AND (SMMU01.WhsCode = 'W-SMMU01') AND (SMAB01.WhsCode = 'W-SMAB01') AND (SMKH01.WhsCode = 'W-SMKH01')
and here is the result of the coulmn
Expr6
1
2
3
4
5
6
7
8
9
10
how it possiable to let only shown '7' as decided in the condition ?
thx
SELECT [...]
, ITM1.PriceList AS Expr6
, ITM1.Price AS Expr7
FROM
OITM AS T0
INNER JOIN OCRD AS T1
ON T0.CardCode = T1.CardCode
INNER JOIN OUGP AS T2
ON T0.UgpEntry = T2.UgpEntry
INNER JOIN UGP1 AS T3
ON T2.UgpEntry = T3.UgpEntry
INNER JOIN OITW AS SMMU01
ON T0.ItemCode = SMMU01.ItemCode
INNER JOIN OITW AS SMAB01
ON SMMU01.ItemCode = SMAB01.ItemCode
INNER JOIN OITW AS SMKH01
ON SMAB01.ItemCode = SMKH01.ItemCode
INNER JOIN ITM9
ON T0.ItemCode = ITM9.ItemCode
AND ITM9.PriceList = '7'
INNER JOIN ITM1
ON T0.ItemCode = ITM1.ItemCode
LEFT OUTER JOIN ITM1 AS ITM1_1
ON T0.ItemCode = ITM1_1.ItemCode
AND ITM1_1.PriceList = '10'
LEFT OUTER JOIN OUOM AS T4
ON T3.UomEntry = T4.UomEntry
Your INNER JOIN ITM1 has not PriceList = '7' filter
You should be able to fix it with :
INNER JOIN ITM1
ON T0.ItemCode = ITM1.ItemCode
AND ITM1.PriceList = '7'
The question is now : why ITM1 and ITM9 are duplicated ?
I'm trying to create a view where I union 4 tables. One of the fields is a document number. The only way I can pull the related document number from one of the tables is by running a LEFT on the 6 left-most characters. But when I do this and then CAST to integer I still cannot query the field. It's like it's looking past my where clause and trying to convert ALL of the characters in the field.
This is what I'm doing:
SELECT dbo.OSLP.U_RepStatus AS RepStatus, dbo.OSLP.SlpCode, dbo.OPCH.CardCode, dbo.OPCH.CardName, 'IN' AS DocTypeDesc, dbo.OPCH.DocNum,
dbo.OPCH.DocEntry, dbo.OPCH.DocDate, dbo.OPCH.NumAtCard, dbo.OPCH.DocStatus, dbo.OPCH.DocTotal, dbo.OPCH.U_CommInvNo AS ARInvNo
FROM dbo.OPCH INNER JOIN
dbo.OCRD ON dbo.OPCH.CardCode = dbo.OCRD.CardCode INNER JOIN
dbo.OSLP ON dbo.OCRD.U_SalesRepNumber = dbo.OSLP.SlpCode
WHERE (dbo.OSLP.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (dbo.OPCH.U_CommInvNo IS NOT NULL)
UNION
SELECT OSLP_1.U_RepStatus AS RepStatus, OSLP_1.SlpCode, dbo.ORPC.CardCode, dbo.ORPC.CardName, 'CM' AS DocTypeDesc, dbo.ORPC.DocNum, dbo.ORPC.DocEntry,
dbo.ORPC.DocDate, dbo.ORPC.NumAtCard, dbo.ORPC.DocStatus, - 1 * dbo.ORPC.DocTotal AS DocTotal, dbo.ORPC.U_CommInvNo AS ARInvNo
FROM dbo.ORPC INNER JOIN
dbo.OCRD AS OCRD_1 ON dbo.ORPC.CardCode = OCRD_1.CardCode INNER JOIN
dbo.OSLP AS OSLP_1 ON OCRD_1.U_SalesRepNumber = OSLP_1.SlpCode
WHERE (OSLP_1.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (dbo.ORPC.U_CommInvNo IS NOT NULL)
UNION
SELECT T3.U_RepStatus AS RepStatus, T3.SlpCode, T0.ContraAct AS CardCode, T2.CardName, 'JE' AS DocTypeDesc, T1.TransId AS DocNum, T1.TransId AS DocEntry,
T1.RefDate AS DocDate, T0.LineMemo AS NumAtCard, T1.BtfStatus AS DocStatus, T0.Debit AS DocTotal, CAST(LEFT(T0.LineMemo, 6) AS INT) AS ARInvNo
FROM dbo.JDT1 AS T0 INNER JOIN
dbo.OJDT AS T1 ON T0.TransId = T1.TransId INNER JOIN
dbo.OCRD AS T2 ON T0.ContraAct = T2.CardCode INNER JOIN
dbo.OSLP AS T3 ON T2.U_SalesRepNumber = T3.SlpCode
WHERE (T0.ShortName = 'CompanyName') AND (T1.TransType = 30) AND (T0.Account = '_SYS00000000624') AND (T3.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (T0.Debit > 0)
UNION
SELECT T7.U_RepStatus AS RepStatus, T7.SlpCode, T4.ContraAct AS CardCode, T6.CardName, 'JE' AS DocTypeDesc, T5.TransId AS DocNum, T5.TransId AS DocEntry,
T5.RefDate AS DocDate, T4.LineMemo AS NumAtCard, T5.BtfStatus AS DocStatus, - (1 * T4.Credit) AS DocTotal, CAST(LEFT(T4.LineMemo, 6) AS INT) AS ARInvNo
FROM dbo.JDT1 AS T4 INNER JOIN
dbo.OJDT AS T5 ON T4.TransId = T5.TransId INNER JOIN
dbo.OCRD AS T6 ON T4.ContraAct = T6.CardCode INNER JOIN
dbo.OSLP AS T7 ON T6.U_SalesRepNumber = T7.SlpCode
WHERE (T4.ShortName = 'CompanyName') AND (T5.TransType = 30) AND (T4.Account = '_SYS00000000624') AND (T7.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (T4.Credit > 0)
If I query this view with WHERE ARInvNo = 295696 I get this error:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'A/P In' to data type int.
If I take out the bottom 2 unions from my view I can query with WHERE ARInvNo = 295696 without an issue. If I run the second 2 parts of the query, nothing in my results includes 'A/P In', although that does exist in rows in JDT1 outside of the WHERE criteria I'm using. Any idea why SQL is looking past my WHERE statement and trying to LEFT and CAST every field before it returns any data?
To clarify, this returns no results:
SELECT T0.LineMemo
FROM JDT1 T0 INNER JOIN OJDT T1 ON T0.TransId = T1.TransId INNER JOIN OCRD T2 ON T0.ContraAct = T2.CardCode INNER JOIN OSLP T3 ON T2.U_SalesRepNumber = T3.SlpCode
WHERE T0.ShortName = 'CompanyName' AND T1.TransType = 30 AND T0.Account = '_SYS00000000624' AND T3.U_RepStatus IN ('Dir', 'Ind', 'Sal') AND left(T0.LineMemo,3)='A/P In'
I see it now.
It's seeing 'A/P in' in your values. It can't CAST the '/' character to INT
MSSQL cast( [varcharColumn] to int) in SELECT gets executed before WHERE clause filters out bad values
In short you will want to do this:
select (case when isnumeric(TO.LineMemo) = 1 then cast(TO.LineMemo as int) end)