通过查看其他示例无法在此处连接点。寻找一些帮助写这个,以便它只撤回每个9DVN的最新条目:也就是说,拉回最近生效日期的一行数据。
SELECT b.vndr_name AS Supplier,
a.VENDOR_NBR*1000 + a.VENDOR_DEPT_NBR*10 + a.VENDOR_SEQ_NBR AS "9DVN",
a.VENDOR_NBR AS "6DVN",a.VENDOR_DEPT_NBR AS "Dept",
a.VENDOR_SEQ_NBR AS "Seq" ,a.FREIGHT_FACTOR_PCT AS "Rate",
a.Effective_Date AS "Effect Date",
a.LAST_CHANGE_USERID AS "By",
COUNT(a.DC_NBR) AS "DC Count"
FROM MainTable.DC_FRT_FCTR a
INNER JOIN SecondaryTable ON b.vndr_nbr = a.vendor_nbr
ORDER BY a.Effective_Date DESC
GROUP BY B.vndr_name,
a.VENDOR_NBR,
a.VENDOR_DEPT_NBR,
a.VENDOR_SEQ_NBR,
a.FREIGHT_FACTOR_PCT,
Effective_Date,
a.LAST_CHANGE_USERID
答案 0 :(得分:0)
这是一种常见的查询模式:查找与特定列的极值相关联的详细信息行。
您需要一个子查询来查找每个供应商的极值 - 在您的情况下是最新生效日期。像这样:
SELECT MAX(Effective_Date) Effective_Date,
VENDOR_NBR*1000 + VENDOR_DEPT_NBR*10 + VENDOR_SEQ_NBR "9DVN"
FROM DC_FRT_FCTR
GROUP BY VENDOR_NBR*1000 + VENDOR_DEPT_NBR*10 + VENDOR_SEQ_NBR
您应该测试此子查询以确保它产生合理的结果。
然后你需要将它加入到极值的其余查询中,就像这样。
SELECT b.vndr_name AS Supplier,
a.VENDOR_NBR*1000 + a.VENDOR_DEPT_NBR*10 + a.VENDOR_SEQ_NBR AS "9DVN",
...etc...
COUNT(a.DC_NBR) AS "DC Count"
FROM MainTable.DC_FRT_FCTR a
JOIN (
SELECT MAX(Effective_Date) Effective_Date,
VENDOR_NBR*1000 + VENDOR_DEPT_NBR*10 + VENDOR_SEQ_NBR "9DVN"
FROM DC_FRT_FCTR
GROUP BY VENDOR_NBR*1000 + VENDOR_DEPT_NBR*10 + VENDOR_SEQ_NBR
) latest
ON latest.9DVN = a.VENDOR_NBR*1000 + a.VENDOR_DEPT_NBR*10 + a.VENDOR_SEQ_NBR
AND latest.Effective_Date = a.Effective_Date
JOIN SecondaryTable b
ON b.vndr_nbr = a.vendor_nbr
ORDER BY a.Effective_Date DESC
GROUP BY B.vndr_name,
... etc ...