很抱歉下面的冗长查询,数据库的组织方式和我的小SQL知识,这是我能让它玩得很好的唯一方法。所以数据库有一个字段TicketDate
,其中我需要MAX值。但是,它被定义为char(8)变量而不是datetime。因此,获取MAX并不像MAX(TicketDate as DATETIME)
那么简单,它实际上并不会产生最大日期。
查询可单独运作:
--***SUB QUERY (TimeTicketDet)
SELECT DISTINCT
cast(TicketDate as DATE) as MAXtt,
tt.JobNo
FROM TimeTicketDet as tt
ORDER BY
cast(TicketDate as DATE) DESC OFFSET 0 ROWS
但是,当添加到主查询中时,我会为每个TimeTicketEntry获取重复的行。查询工作正常,没有最后一个子查询(没有重复)--***SUB QUERY (TimeTicketDet)
。 JobNo是TimeTicketDet表的唯一标识符,用作链接。
DECLARE @now DATETIME
DECLARE @90daysago DATETIME
SET @now = GETDATE()
SET @90daysago = DATEADD(day, -90, @now)
SELECT
o.PartNo,
o.JobNo,
ord.DateEnt,
por.MAXPONum,
po.DateEnt,
tt.MAXtt,
r.ReceiveDate,
CASE
WHEN po.DateEnt >= r.ReceiveDate AND po.DateEnt >= tt.MAXtt THEN cast(po.DateEnt as DATE)
WHEN r.ReceiveDate >= po.DateEnt AND r.ReceiveDate >= tt.MAXtt THEN cast(r.ReceiveDate as DATE)
WHEN tt.MAXtt >= po.DateEnt AND tt.MAXtt >= r.ReceiveDate THEN cast(tt.MAXtt as DATE)
ELSE po.DateEnt
END AS MostRecentDate,
POProrate.TotalCost,
WIPProrate.WIPProrateCost,
(POProrate.TotalCost+WIPProrate.WIPProrateCost) as ProratedCost,
(ROUND(cast((o.QtyToMake - o.QtyShipped2Stock) as FLOAT)/o.QtyToMake,3))*(POProrate.TotalCost+WIPProrate.WIPProrateCost) as TotalProratedCost
FROM
--***MAIN QUERY (OrderDet)***
OrderDet as o
--***SUB QUERY (Order)***
JOIN
(
SELECT
cast(DateEnt as DATE) as DateEnt,
OrderNo
FROM
Orders
) ord
ON ord.OrderNo = o.OrderNo
--***SUB QUERY (POReleases)***
JOIN
(
SELECT
MAX(PONum) as MAXPONum,
JobNo
FROM
POReleases
GROUP BY
JobNo
) por
ON por.JobNo = o.JobNo
--***SUB QUERY (PO)***
JOIN
(
SELECT
PONum,
cast(DateEnt as DATE) as DateEnt
FROM
PO
) po
ON po.PONum = por.MAXPONum
--***SUB QUERY (TimeTicketDet)
JOIN
(
SELECT DISTINCT
cast(TicketDate as DATE) as MAXtt,
tt.JobNo
FROM TimeTicketDet as tt
ORDER BY
cast(TicketDate as DATE) DESC OFFSET 0 ROWS
) tt
ON tt.JobNo = o.JobNo
--***SUB QUERY (Receiver)
JOIN
(
SELECT
rd.JobNo,
cast(MAX(r.ReceiveDate) as DATE) as ReceiveDate
FROM
Receiver as r
INNER JOIN
ReceiverDet as rd ON r.PONum = rd.PONum
GROUP BY
rd.JobNo
) r
ON r.JobNo = o.JobNo
--***SUB QUERY (POProrate)***
JOIN
(
SELECT
j.JobNo,
SUM(j.TotalCost) as TotalCost
FROM
(
SELECT
por.JobNo,
CASE
WHEN pod.Unit = 'LOT' THEN SUM(pod.UnitCost*1)
ELSE SUM(por.Qty*pod.UnitCost)
END as TotalCost
FROM
PODet as pod
INNER JOIN
POReleases as por ON pod.PONum = por.PONum and pod.partno=por.partno
GROUP BY por.JobNo, pod.Unit
) j
GROUP BY j.JobNo
) POProrate
ON o.JobNo = POProrate.JobNo
--***SUB QUERY (WIPProrate)***
JOIN
(
SELECT
j.JobNo,
SUM(j.ProratedCost) as WIPProrateCost
FROM
(
SELECT
tt.StepNo,
tt.JobNo,
tt.ActualPayRate,
tt.BurdenRate,
tt.CycleTime,
tt.SetupTime,
tt.CycleTime + tt.SetupTime as TotalTime,
(tt.CycleTime + tt.SetupTime) * tt.ActualPayRate as LaborCost,
(tt.CycleTime + tt.SetupTime) * tt.BurdenRate as BurdenCost,
((tt.CycleTime + tt.SetupTime) * tt.ActualPayRate) + ((tt.CycleTime + tt.SetupTime) * tt.BurdenRate) as ProratedCost
FROM
TimeTicketDet as tt
) j
GROUP BY j.JobNo
) WIPProrate
ON WIPProrate.JobNo = o.JobNo
WHERE
o.Status = 'Open'
AND o.JobNo <> ''
AND ord.DateEnt <= @90daysago
AND po.DateEnt <= @90daysago
AND tt.MAXtt <= @90daysago
AND r.ReceiveDate <= @90daysago
查询的目标是找到所有相关日期,&lt; = 90天之后,添加到列表中。我所缺少的是最后一个日期,它将从TimeTicketDet表中提取正确的TicketDate并将其加入JobNo。
我很乐意添加或删除所要求的任何信息。
答案 0 :(得分:2)
尝试将子查询更改为:
SELECT MAX(CAST(TicketDate as DATE)) as MaxTT,
JobNo
FROM TimeTicketDet as tt
GROUP BY JobNo
我认为您当前的查询必须为任何给定的MaxTT
JobNo
日期