In my query I need to join with a sub query on a derived column :
select w1.wk_id,
(floor(td1.military_hour/4)*4) as military_hour_group ,
w1.end_date as end_date
from work_instances w1
inner join time_table td1 on w1.end_time = td1.time_id
inner join
(
select (floor(td2.military_hour/4)*4) as td2_military_hour_group,
(floor(td3.military_hour/4)*4) as td3_military_hour_group, wk_id
from task_instances t1
inner join time_table td2 on t1.end_time = td1.time_id
inner join time_table td3 on t1.start_time = td3.time_id
) tq1
on tq1.td2_military_hour_group = military_hour_group
and tq1.td3_military_hour_group = military_hour_group
and tq1.wk_id = w1.wf_id
It says Invalid operation: column "military_hour_group" does not exist in w1, td1, unnamed_join, tq1;
What am I doing wrong?
Please help.
Try below: military_hour_group is your calculated column and that's why it is showing that error
select w1.wk_id,
(floor(td1.military_hour/4)*4) as military_hour_group ,
w1.end_date as end_date
from work_instances w1
inner join time_table td1 on w1.end_time = td1.time_id
inner join
(
select (floor(td2.military_hour/4)*4) as td2_military_hour_group,
(floor(td3.military_hour/4)*4) as td3_military_hour_group, wk_id
from task_instances t1
inner join time_table td2 on t1.end_time = td1.time_id
inner join time_table td3 on t1.start_time = td3.time_id
) tq1
on tq1.td2_military_hour_group = (floor(td1.military_hour/4)*4)
and tq1.td3_military_hour_group = (floor(td1.military_hour/4)*4)
and tq1.wk_id = w1.wf_id
Okay Use like below mention
select *
from (select w1.wk_id, (floor(td2.military_hour/4)*4) as military_hour_group , w1.end_date as end_date
from work_instances w1 inner join time_table td1 on w1.end_time = td1.time_id) table1 inner join (select (floor(td2.military_hour/4)*4) as td2_military_hour_group, (floor(td3.military_hour/4)*4) as td3_military_hour_group, wk_id
from task_instances t1 inner join time_table td2 on t1.end_time = td1.time_id inner join time_table td3 on t1.start_time = td3.time_id ) tabl2 on tabl2.td2_military_hour_group = table1.military_hour_group and tabl2.td3_military_hour_group = table1.military_hour_group and tabl2.wk_id = table1.wf_id
Related
I have a following query:
SELECT t.date,
t.transactionId,
t.channelGrouping,
tp.itemRevenue,
t.transactionItemQuantity,
t.company,
t.campaign,
ifnull(replace((regexp_extract(tp.productCategory, r"(^[a-zA-Z0-9_.&;+ -]+)")),"&", "&"), "N/A") as productCategory,
ifnull(sum(a.adCost),0) as adCost
FROM `kpi.TransactionsByChannel` t left join kpi.TransactionProducts tp
on tp.transactionId = t.transactionId
left join kpi.Ads a
on a.company = t.company and a.date = t.date and a.campaign = t.campaign group by 1,2,3,4,5,6,7,8
which returns me result:
However, as you can see, adCost is not correct and duplicated. I need to get ratio of it based on itemRevenue or just write first row and other rows should be 0.
I tried following query:
SELECT t.date,
t.transactionId,
t.channelGrouping,
tp.itemRevenue,
t.transactionItemQuantity,
t.company,
t.campaign,
ifnull(replace((regexp_extract(tp.productCategory, r"(^[a-zA-Z0-9_.&;+ -]+)")),"&", "&"), "N/A") as productCategory,
round(adCost*(itemRevenue/SUM(itemRevenue) over (partition by tp.company, tp.productCategory, tp.date)),2) as adCost_adjusted,
FROM kpi.TransactionsByChannel t left join kpi.TransactionProducts tp
on tp.transactionId = t.transactionId
left join kpi.Ads a
on a.company = t.company and a.date = t.date and a.campaign = t.campaign group by 1,2,3,4,5,6,7,8,9
but I am getting an error of:
Column 9 contains an analytic function, which is not allowed in GROUP BY at [13:99]
Any helps would be really appreciated!
Try something like this approach:
with ttr as
(
select
t.transactionId,
sum(tp.itemRevenue) as TotalItemRevenue
from
`kpi.TransactionsByChannel` t
inner join
kpi.TransactionProducts tp
on
tp.transactionId = t.transactionId
group by
t.transactionId
)
SELECT
t.date,
t.transactionId,
t.channelGrouping,
tp.itemRevenue,
t.transactionItemQuantity,
t.company,
t.campaign,
ifnull(replace((regexp_extract(tp.productCategory, r"(^[a-zA-Z0-9_.&;+ -]+)")),"&", "&"), "N/A") as productCategory,
ifnull(a.adCost,0) * tp.itemRevenue / ttr.TotalItemRevenue as adCost_adjusted
FROM
`kpi.TransactionsByChannel` t
inner join
ttr
on
t.transactionId = ttr.transactionId
inner join
kpi.TransactionProducts tp
on
tp.transactionId = t.transactionId
left join
kpi.Ads a
on
a.company = t.company
and a.date = t.date
and a.campaign = t.campaign
I am trying to read my previous value of "OPENSEATING" and store it into the variable #OPENSEATING. For the second query I want it to check first the value of #OPENSEATING if match condition do the below query.
i believe my declaration is correct, is there any syntax error?
DECLARE #OPENSEATING AS BIT
SELECT
'2019-01-31' AS DATE,
(SELECT
THEATRES.OPENSEATING
FROM
SEAT LEFT JOIN THEATRES ON SEAT.ROOMID = THEATRES.ID
LEFT JOIN SHOWTIMES AS B ON THEATRES.ID = B.THEATREID
LEFT JOIN TICKET_ITEMS ON TICKET_ITEMS.SEATNO = SEAT.SEATNO AND B.ID = TICKET_ITEMS.SHOWTIMESID
WHERE
B.ID = A.ID
GROUP BY THEATRES.OPENSEATING) AS #OPENSEATING,
IF(#OPENSEATING =0)
BEGIN
(SELECT
(THEATRES.CAPACITY - COUNT(TICKET_ITEMS.ID)) AS SEATREMAINING
FROM
SEAT LEFT JOIN THEATRES ON SEAT.ROOMID = THEATRES.ID
LEFT JOIN SHOWTIMES ON THEATRES.ID = SHOWTIMES.THEATREID
LEFT JOIN TICKET_ITEMS ON TICKET_ITEMS.SEATNO = SEAT.SEATNO AND SHOWTIMES.ID = TICKET_ITEMS.SHOWTIMESID
WHERE
SHOWTIMES.ID = A.ID
GROUP BY THEATRES.CAPACITY) AS REMAININGSEAT
END
ELSE IF(#OPENSEATING =1)
BEGIN
(SELECT
(THEATRES.CAPACITY - COUNT(TICKET_ITEMS.ID)) AS SEATREMAINING
FROM
SHOWTIMES LEFT JOIN THEATRES ON THEATRES.ID = SHOWTIMES.THEATREID
LEFT JOIN TICKET_ITEMS ON TICKET_ITEMS.SEATNO = SEAT.SEATNO AND SHOWTIMES.ID = TICKET_ITEMS.SHOWTIMESID
WHERE
SHOWTIMES.ID = A.ID
GROUP BY THEATRES.CAPACITY) AS REMAININGSEAT
END
SHOWSCHEDULES.ID AS SHOWSCHEDULE_ID,
A.ID AS SHOWTIME_ID,
A.THEATREID
FROM
SHOWSCHEDULES JOIN SHOWTIMES AS A ON SHOWSCHEDULES.ID = A.SHOWSCHEDULEID
JOIN SHOWLANGUAGES ON A.SHOWLANGUAGEID = SHOWLANGUAGES.ID
WHERE
'2019-01-31' BETWEEN CONVERT(VARCHAR(10), SHOWSCHEDULES.SHOWFROMDATE, 20) AND CONVERT(VARCHAR(10), SHOWSCHEDULES.SHOWTODATE, 20)
AND CONVERT(VARCHAR(8), getdate(), 8) < CONVERT(VARCHAR(10), A.SHOWENDTIME,8)
ORDER BY SHOWBEGINTIME_24 ASC
;
AS SUGGESTED BY #Slava Murygin I tried creating CTE TABLE as below but even this is showing error, what is wrong with my simple cte table. The inner query is working fine.
WITH cte_name (OPENSEATING) AS (
SELECT
(SELECT
THEATRES.OPENSEATING
FROM
SEAT LEFT JOIN THEATRES ON SEAT.ROOMID = THEATRES.ID
LEFT JOIN SHOWTIMES AS B ON THEATRES.ID = B.THEATREID
LEFT JOIN TICKET_ITEMS ON TICKET_ITEMS.SEATNO = SEAT.SEATNO AND B.ID = TICKET_ITEMS.SHOWTIMESID
WHERE
B.ID = A.ID
GROUP BY THEATRES.OPENSEATING) AS OPENSEATING
FROM
SHOWSCHEDULES JOIN SHOWTIMES AS A ON SHOWSCHEDULES.ID = A.SHOWSCHEDULEID
JOIN SHOWLANGUAGES ON A.SHOWLANGUAGEID = SHOWLANGUAGES.ID
WHERE
'2019-01-31' BETWEEN CONVERT(VARCHAR(10), SHOWSCHEDULES.SHOWFROMDATE, 20) AND CONVERT(VARCHAR(10), SHOWSCHEDULES.SHOWTODATE, 20)
AND CONVERT(VARCHAR(8), getdate(), 8) < CONVERT(VARCHAR(10), A.SHOWENDTIME,8)
)
I have made changes to your CTE as:
WITH cte_name (OPENSEATING) AS (
SELECT
THEATRES.OPENSEATING as OPENSEATING
FROM
SEAT LEFT JOIN THEATRES ON SEAT.ROOMID = THEATRES.ID
LEFT JOIN SHOWTIMES AS B ON THEATRES.ID = B.THEATREID
LEFT JOIN TICKET_ITEMS ON TICKET_ITEMS.SEATNO = SEAT.SEATNO AND B.ID = TICKET_ITEMS.SHOWTIMESID
WHERE
B.ID = A.ID
GROUP BY THEATRES.OPENSEATING
)
select openseating from
cte_name, SHOWSCHEDULES JOIN SHOWTIMES AS A ON SHOWSCHEDULES.ID = A.SHOWSCHEDULEID
JOIN SHOWLANGUAGES ON A.SHOWLANGUAGEID = SHOWLANGUAGES.ID
WHERE
'2019-01-31' BETWEEN CONVERT(VARCHAR(10), SHOWSCHEDULES.SHOWFROMDATE, 20) AND CONVERT(VARCHAR(10), SHOWSCHEDULES.SHOWTODATE, 20)
AND CONVERT(VARCHAR(8), getdate(), 8) < CONVERT(VARCHAR(10), A.SHOWENDTIME,8)
You can't do that in one query. You have to get variable in the first query and then use it in IF statement.
If you WANT to use ONLY ONE statement then you have to use CTEs, but it will hide the logic and will be hard to debug.
It might be something like this, but no guarantee it will work, because I have no idea about your business logic and data behind:
;WITH OPENSEATING as (
SELECT THEATRES.OPENSEATING, B.ID
FROM SEAT
LEFT JOIN THEATRES ON SEAT.ROOMID = THEATRES.ID
LEFT JOIN SHOWTIMES AS B ON THEATRES.ID = B.THEATREID
LEFT JOIN TICKET_ITEMS ON TICKET_ITEMS.SEATNO = SEAT.SEATNO AND B.ID = TICKET_ITEMS.SHOWTIMESID
GROUP BY THEATRES.OPENSEATING),
REMAININGSEAT0 as (
SELECT st.ID, (t.CAPACITY - COUNT(ti.ID)) AS SEATREMAINING
FROM SEAT as s
LEFT JOIN THEATRES as t ON s.ROOMID = t.ID
LEFT JOIN SHOWTIMES as st ON t.ID = st.THEATREID
LEFT JOIN TICKET_ITEMS ON ti.SEATNO = s.SEATNO AND st.ID = ti.SHOWTIMESID
GROUP BY t.CAPACITY),
REMAININGSEAT1 as (
SELECT st.ID, (t.CAPACITY - COUNT(ti.ID)) AS SEATREMAINING
FROM SHOWTIMES as st
LEFT JOIN THEATRES as t ON t.ID = st.THEATREID
LEFT JOIN SEAT as s ON s.ROOMID = t.ID
LEFT JOIN TICKET_ITEMS as ti ON ti.SEATNO = s.SEATNO AND st.ID = ti.SHOWTIMESID
GROUP BY t.CAPACITY)
SELECT '2019-01-31' AS DATE
, ss.ID AS SHOWSCHEDULE_ID
, A.ID AS SHOWTIME_ID
, A.THEATREID
, SEATREMAINING = CASE OS.OPENSEATING WHEN 0 THEN RS0.SEATREMAINING ELSE RS1.SEATREMAINING END
FROM SHOWSCHEDULES as ss
INNER JOIN SHOWTIMES AS A ON ss.ID = A.SHOWSCHEDULEID
INNER JOIN SHOWLANGUAGES as SL ON A.SHOWLANGUAGEID = SL.ID
INNER JOIN OPENSEATING as OS ON OS.ID = A.ID
LEFT JOIN REMAININGSEAT0 as RS0 ON RS0.ID = A.ID
LEFT JOIN REMAININGSEAT1 as RS1 ON RS1.ID = A.ID
WHERE '2019-01-31' BETWEEN CONVERT(VARCHAR(10), ss.SHOWFROMDATE, 20) AND CONVERT(VARCHAR(10), ss.SHOWTODATE, 20)
AND CONVERT(VARCHAR(8), getdate(), 8) < CONVERT(VARCHAR(10), A.SHOWENDTIME,8)
ORDER BY SHOWBEGINTIME_24 ASC;
I have table task_instances that has task_id, end_time , start_time.
end_time and start_time links to time_table which contains military_hour format (24 hour ranging from 0 -23).
I want to know all the tasks where each tasks start time and end_time belong to the same military hour group ( there are 6 groups , each containing 4 hours).
select (floor(td1.military_hour/4)*4) as td1_military_hour_group,
(floor(td2.military_hour/4)*4) as td2_military_hour_group, wk_id
from task_instances t1
inner join time_table td1 on t1.end_time = td1.time_id
inner join time_table td2 on t1.start_time = td2.time_id
where td1_military_hour_group = td2_military_hour_group
Is it required to join same time_table twice. Can we better the query or even the query seems correct?
You can't use the derived values td1_military_hour_group and td2_military_hour_group in the WHERE clause or you'll get an error, e.g. "Unknown column 'td1_military_hour_group' in 'where clause'". You'll need to repeat the calculations in the WHERE clause:
select wk_id,
(floor(td1.military_hour/4)*4) as td1_military_hour_group,
(floor(td2.military_hour/4)*4) as td2_military_hour_group
from task_instances t1
inner join time_table td1
on t1.end_time = td1.time_id
inner join time_table td2
on t1.start_time = td2.time_id
where (floor(td1.military_hour/4)*4) = (floor(td2.military_hour/4)*4)
Or else use a common table expression:
WITH cteTime_groups AS (SELECT time_id,
floor(military_hour/4)*4 AS military_hour_group
FROM time_table)
select wk_id,
td1.military_hour_group as td1_military_hour_group,
td2.military_hour_group as td2_military_hour_group
from task_instances t1
inner join cteTime_groups td1
on t1.end_time = td1.time_id
inner join cteTime_groups td2
on t1.start_time = td2.time_id
where td1.military_hour_group = td2.military_hour_group
SQLFiddle here
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 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'
)