一个很难的问题,如下SQL
SELECT LotNM, HistoryMainLineId,
CASE TxnDate WHEN '1900-01-01 00:00:00.000 ' THEN getdate()
ELSE txndate END AS Expr1, TxnId, WorkflowStepNM
FROM (SELECT t .LotNM, t .HistoryMainLineId, t .TxnDate, t .TxnId,
t .WorkflowStepNM
FROM dbo.UNIT_Shipped t
UNION ALL
SELECT k.LotNM, k.HistoryMainLineId, k.TxnDate, k.TxnId, k.WorkflowStepNM
FROM dbo.UNIT_mainline k
UNION ALL
SELECT x.lotnm, x.historymainlineid, x.txndate, x.txnid, x.workflowstepnm
FROM dbo.unit_dpshipped x) a
ORDER BY TxnDate
上面的视图中当第一行为空时,因为拿不到日期就变成1900年,我本意是如果这行为1900年的话,就拿最后一行的时间.即拿k.txndate但怎么拿都拿不到.因为是从三个视图的联合得来的反映到一个视图里来
thanks
------解决方案--------------------SELECT a.LotNM, a.HistoryMainLineId,
CASE WHEN a.TxnDate = '1900-01-01 00:00:00.000 ' THEN b.TxnDate
ELSE a.txndate END AS Expr1, TxnId, WorkflowStepNM
FROM (SELECT t .LotNM, t .HistoryMainLineId, t .TxnDate, t .TxnId,
t .WorkflowStepNM
FROM dbo.UNIT_Shipped t
UNION ALL
SELECT k.LotNM, k.HistoryMainLineId, k.TxnDate, k.TxnId, k.WorkflowStepNM
FROM dbo.UNIT_mainline k
UNION ALL
SELECT x.lotnm, x.historymainlineid, x.txndate, x.txnid, x.workflowstepnm
FROM dbo.unit_dpshipped x) a inner join dbo.UNIT_mainline b on a.lotnm=b.lotnm order by a.TxnDate