SELECT MAX(t3.POORDN) AS ORDNUM,
MAX(t3.POORTY) AS ORDTYP,
MAX(t3.POOPRI) AS ORDPRI,
DECIMAL(COUNT(t2.ipordn),7,0) AS LINES,
SUM(t2.IPORDQ) AS EACHQTY,
/* In this section I belive a sub-query may be needed
The result valuess needed are two values:
Total full cases,
total "remainder" or "each" items
Each SKU on the order has a different items-per-case value in the master inventory table
*/ The code outside this block executes as expected.
MAX(t3.POGWGT) AS GROSWT,
MAX(t3.POGVOL) AS GROVOL,
MAX(t3.POGVOL)/61440 AS ESTPAL,
from hbcprodfl.witmb as t1
JOIN hbcprodfl.wopib AS t2 on T2.IPSKU = T1.ITSKU
JOIN HBCPRODFL.WOPHB AS t3 ON t2.IPORDN = t3.POORDN
WHERE t1.ITCSPK > 0
GROUP BY t2.IPORDN
ORDER BY t2.ipordn
SELECT MAX(t3.POORDN) AS ORDNUM,
MAX(t3.POORTY) AS ORDTYP,
MAX(t3.POOPRI) AS ORDPRI,
DECIMAL(COUNT(t2.ipordn),7,0) AS LINES,
SUM(t2.IPORDQ) AS EACHQTY,
-- Adding these three lines to your query
t1.ITCSPK as ITEMSPERCASE,
SUM(t2.IPORDQ) / t1.TICSPK as FULLCASES,
mod(SUM(t2.IPORDQ), T1.TICKSPK) as NONCASECOUNT,
--
MAX(t3.POGWGT) AS GROSWT,
MAX(t3.POGVOL) AS GROVOL,
MAX(t3.POGVOL)/61440 AS ESTPAL,
from hbcprodfl.witmb as t1
JOIN hbcprodfl.wopib AS t2 on T2.IPSKU = T1.ITSKU
JOIN HBCPRODFL.WOPHB AS t3 ON t2.IPORDN = t3.POORDN
WHERE t1.ITCSPK > 0
GROUP BY t2.IPORDN,
-- Adding these three lines to complete the GROUP BY requirement
t1.ITCSPK,
SUM(t2.IPORDQ) / t1.TICSPK,
mod(SUM(t2.IPORDQ), T1.TICKSPK)
--
ORDER BY t2.ipordn
WITH T (ORDNUM, ORDTYP, ORDPRI, LINES, EACHQTY, ITEMSPERCASE, GROSWT, GROVOL, ESTPAL)
AS
(
SELECT MAX(t3.POORDN) AS ORDNUM,
MAX(t3.POORTY) AS ORDTYP,
MAX(t3.POOPRI) AS ORDPRI,
DECIMAL(COUNT(t2.ipordn),7,0) AS LINES,
SUM(t2.IPORDQ) AS EACHQTY,
t1.ITCSPK as ITEMSPERCASE,
MAX(t3.POGWGT) AS GROSWT,
MAX(t3.POGVOL) AS GROVOL,
MAX(t3.POGVOL)/61440 AS ESTPAL,
FROM hbcprodfl.witmb as t1
JOIN hbcprodfl.wopib AS t2
ON T2.IPSKU = T1.ITSKU
JOIN HBCPRODFL.WOPHB AS t3
ON t2.IPORDN = t3.POORDN
WHERE t1.ITCSPK > 0
GROUP BY t2.IPORDN
)
SELECT *, EACHQTY / ITEMSPERCASE as FULLCASES, MOD (EACHQTY, ITEMSPERCASE) as NONCASECOUNT
FROM T0
ORDER BY LINES
SELECT MAX(t3.POORDN) AS ORDNUM,
MAX(t3.POORTY) AS ORDTYP,
MAX(t3.POOPRI) AS ORDPRI,
DECIMAL(COUNT(t2.ipordn),7,0) AS LINES,
SUM(t2.IPORDQ) AS EACHQTY,
SUM(t1.ITCSPK) as ITEMSPERCASE,
sum(SUM(t2.IPORDQ) / t1.ITCSPK) as FULLCASES,
SUM(mod(SUM(t2.IPORDQ), T1.ITCSPK)) as EACHCOUNT
from hbcprodfl.witmb as t1
JOIN hbcprodfl.wopib AS t2 on T2.IPSKU = T1.ITSKU
JOIN HBCPRODFL.WOPHB AS t3 ON t2.IPORDN = t3.POORDN
WHERE t1.ITCSPK > 0 AND t2.IPORDN = '7460816701'
GROUP BY t2.IPORDN,t2.IPORDQ,t1.ITCSPK
--
SELECT
ordnum,
ordtyp,
ordpri,
SUM (lines),
SUM (eachqty),
'n/a',
sum (fullcases),
sum (eachcount)
FROM
(
-- Insert your query here:
SELECT
MAX(t3.POORDN) AS ORDNUM,
MAX(t3.POORTY) AS ORDTYP,
MAX(t3.POOPRI) AS ORDPRI,
DECIMAL(COUNT(t2.ipordn),7,0) AS LINES,
SUM(t2.IPORDQ) AS EACHQTY,
SUM(t1.ITCSPK) as ITEMSPERCASE,
sum(SUM(t2.IPORDQ) / t1.ITCSPK) as FULLCASES,
SUM(mod(SUM(t2.IPORDQ), T1.ITCSPK)) as EACHCOUNT
from hbcprodfl.witmb as t1
JOIN hbcprodfl.wopib AS t2
on T2.IPSKU = T1.ITSKU
JOIN HBCPRODFL.WOPHB AS t3
ON t2.IPORDN = t3.POORDN
WHERE t1.ITCSPK > 0
AND t2.IPORDN = '7460816701'
GROUP BY t2.IPORDN,t2.IPORDQ,t1.ITCSPK
--
) t0
GROUP BY
ordnum,
ordtyp,
ordpri
If I understand this correctly, you're happy with the query so far, and just need to add ITCSPK to each line. (Any computations using ITEMQTY and ITCSPK is trivial once the data is associated with the row.)
Several different SQL structures/formats will do this. Perhaps the easiest one is to join WITMB into the current result set, presumably on the SKU. You've already done that, so it should be just a simple matter of including that value on the list of items selected.
It's interesting that you group by t2.IPORDN, but don't actually select the value of IPORDN. You'll need to add ITCSPK to the GROUP BY clause.
Try the query below and see if that doesn't come close. Then just modify it to compute the value that you need.
Good Luck,
Kent
Open in new window