Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to find the max value from a join query within a query

$
0
0

I have the following join query within a query which produces the correct results but I cannot work out how to get the max value of MaxInvQty.

Can anybody help or guide me please?

regards

Ron

leftjoin (select  invit_invno, invit_partno,sum(invit_qty)as MaxInvQty

from InvoiceItems

join Invoiceson inv_no= invit_invno

join stock son s.stock_no= invit_partno

where invit_partno= s.stock_noand inv_canind<>'Y'and inv_date>=DATEADD(yy,-1,getdate())

groupby invit_invno, invit_partno)

order by MaxInvQty desc )

as oaon stock_no= oa.invit_partno

The following is the entire query, the code above is the part that I need the Max for.

select

stock_no,

((

select isnull(sum(trc_qty),0)as sumOfTraceQty

from dbo.[Traces]

WHERE((trc_typin('S','N', 'H')or trc_typ='M'  and trc_cert <> 'WS1000'))and trc_cust<>'SAYSTK' and trc_part = stock_no  AND(CAST(TRC_DATEASDATE)>cast(dateadd(mm,-6,getDate()) asdate) andCAST(trc_dateasDATE)<=cast(getDate()asdate)))

-

(

select isnull(sum(trc_qty),0)as sumOfTraceQty

from dbo.[Traces]

WHERE trc_typ='D'and trc_part= stock_no  and trc_cust<>'SAYSTK' AND(CAST(TRC_DATEASDATE)>cast(dateadd(mm,-6,getDate()) asdate) andCAST(trc_dateasDATE)<=cast(getDate()asdate))

))

as'd6m',

(

select isnull(sum(trc_qty),0)as sumOfTraceQty

from dbo.[Traces]

WHERE((trc_typin('S','N', 'H')or trc_typ='M'  and trc_cert <> 'WS1000'))and trc_cust<>'SAYSTK' and trc_part = stock_no  AND(CAST(TRC_DATEASDATE)>cast(dateadd(mm,-12,getDate()) asdate) andCAST(trc_dateasDATE)<cast(dateadd(mm,-0,getDate()) asdate)))

-

(

select isnull(sum(trc_qty),0)as sumOfTraceQty

from dbo.[Traces]

WHERE trc_typ='D' and trc_cust <> 'SAYSTK'and trc_part = stock_no  AND(CAST(TRC_DATEASDATE)>cast(dateadd(mm,-12,getDate()) asdate) andCAST(trc_dateasDATE)<cast(dateadd(mm,-0,getDate()) asdate))

)

as'd12m',

(

select isnull(sum(trc_qty),0)as sumOfTraceQty

from dbo.[Traces]

WHERE((trc_typin('S','N', 'H')or trc_typ='M'  and trc_cert <> 'WS1000'))and trc_cust<>'SAYSTK' and trc_part = stock_no AND(CAST(TRC_DATEASDATE)>cast(dateadd(mm,-24,getDate()) asdate) andCAST(trc_dateasDATE)<cast(dateadd(mm,-0,getDate()) asdate)))

-

(

select isnull(sum(trc_qty),0)as sumOfTraceQty

from dbo.[Traces]

WHERE trc_typ='D' and trc_part = stock_no  and trc_cust <> 'SAYSTK'  AND(CAST(TRC_DATEASDATE)>cast(dateadd(mm,-24,getDate()) asdate) andCAST(trc_dateasDATE)<cast(dateadd(mm,-0,getDate()) asdate))

)

as'd24m',

isnull

(InvCount,0)as'Invoices',

(

selectcount(distinct inv_cust)from invoices i2 join invoiceitems ii2on ii2.invit_invno= i2.inv_no where ii2.invit_partno = stock_no and inv_canind <>'Y'and inv_date>=DATEADD(yy,-1, getdate()))as'CustCnt',

isnull

(MaxInvQty,0)as'MaxInvQty',

cast

(ISNULL(TotSold/ InvCount,0)asdecimal(18,2))as'AvgInQty',

case

when MaxInvQty>((ISNULL(TotSold/ InvCount,0)* 2))

then cast(isnull((TotSold- MaxInvQty) /(InvCount- 1),0)ASdecimal(18,2))

elsecast(isnull(TotSold/ InvCount,0)asdecimal(18,2))

endas'AdjustedAvg',

stock_qoh

,

cast

(isnull(stock_qoh/((selectSUM(invit_qty)from InvoiceItems join Invoiceson inv_no = invit_invnowhere invit_partno = stock_noand inv_canind <>'Y'and inv_date >=DATEADD(yy,-1, getdate()))/

(selectCOUNT(invit_partno)from InvoiceItemsjoin Invoices on inv_no= invit_invno where invit_partno= stock_no and inv_canind<>'Y' and inv_date >= DATEADD(yy,-1, getdate()))),0)asdecimal(18,2))as AvgInvInStock,

stock_reordlv

,

stock_reordqt

,

isnull

(changedBy,'')as ChangedBy,

isnull

(oldQuantity,0)as OldQty,

isnull

(oldLevel,0)as OldLvl,

stock_prodcd

as ProdGrp,

isnull

((selecttop 1 leadtimedays from StockLeadTimeswhere partNumber = stock_no),0)as LeadDays,

stock_costavg

as CurrentCost,

stock_purcurr

as CstCurr,

convert

(varchar,stock_cstavgdt,105)as CurrCstDate,

case

when stock_bufferdaysISnull

then prod_bufferdays

when stock_bufferdays= 0

then prod_bufferdays

else stock_bufferdays

end

as BufferDays,

stock_pkgqty

as PkgQty

from stock s

join ProductGroupson prod_key = stock_prodcd

leftjoin  (select  invit_invno, invit_partno,sum(invit_qty)as MaxInvQty

from InvoiceItems

join Invoiceson inv_no = invit_invno

join stock son s.stock_no = invit_partno

where invit_partno= s.stock_no and inv_canind <> 'Y'and inv_date >=DATEADD(yy,-1, getdate())

groupby invit_invno, invit_partno)

as oaon stock_no = oa.invit_partno

leftjoin(select invit_partno,avg(invit_qty)as AvgInvQty from InvoiceItemsjoin Invoices on inv_no= invit_invno join stock son s.stock_no = invit_partno where invit_partno = s.stock_no and inv_canind<>'Y' and inv_date >= DATEADD(yy,-1, getdate())groupby invit_partno) as ob on stock_no = ob.invit_partno

leftjoin(select invit_partno,sum(invit_qty)as TotSold from InvoiceItemsjoin Invoices on inv_no= invit_invno join stock son s.stock_no = invit_partno where invit_partno = s.stock_no and inv_canind<>'Y' and inv_date >= DATEADD(yy,-1, getdate())groupby invit_partno) as oc on stock_no = oc.invit_partno

left

join(selectcount(distinct invit_invno)as InvCount, invit_partnofrom InvoiceItems join Invoiceson inv_no = invit_invnojoin stock s on s.stock_no= invit_partno where invit_partno= s.stock_no and inv_canind <> 'Y'and inv_date >=DATEADD(yy,-1, getdate())groupby invit_partno )as od on stock_no= od.Invit_partno

left

join StockReorderLevelChange LBon stock_no = LB.partNumberand changedAt >dateadd(year,-1,GETDATE())

where stock_prodcdIN('CLV','TIM')

and stock_no='29590'

and((selectCOUNT(invit_partno)from InvoiceItemsjoin Invoices on inv_no= invit_invno where invit_partno= stock_no and inv_canind<>'Y' and inv_date >= DATEADD(yy,-1, getdate()))> 4or stock_reordqt > 0)

orderby stock_prodcd, stock_nodesc


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>