這是我的一段SQL語法
SELECT Top 1
mV.CID ,mV.HID ,mV.SYSTEM ,mV.VGUID ,mV.HisGUID
,mv.RDate
,mv.RTime
,mV.IpdGUID
,mv.AD_Ideal_Wei
,mv.AD_Knee
,mv.AD_Height
,mv.AD_Regular_Wei
,mv.AD_Current_Wei
,mv.AD_IBW
,mv.AD_Adjust_Wei
,mv.AD_Oth
,mv.Lab
,mv.AD_BMI
,ptype ='Second'
FROM viewNIS_GetNutSecendVisitViewNew As mV
WHERE mV.CID= @CID AND mV.HID= @HID AND mV.SYSTEM = @System
AND mV.IpdGUID= @IpdGUID
ORDER BY mV.RDate DESC, mV.RTime DESC,mv.CreDate desc ,mv.ModDate desc
SELECT Top 1
mV.CID ,mV.HID ,mV.SYSTEM ,mV.VGUID ,mV.HisGUID
,mv.RDate
,mv.RTime
,mV.IpdGUID
,mv.AD_Ideal_Wei
,mv.AD_Knee
,mv.AD_Height
,mv.AD_Regular_Wei
,mv.AD_Current_Wei
,mv.AD_IBW
,mv.AD_Adjust_Wei
,mv.AD_Oth
,mv.Lab
,mv.AD_BMI
,ptype ='First'
FROM viewNIS_GetNutFirstVisitViewNew As mV
WHERE mV.CID= @CID AND mV.HID= @HID AND mV.SYSTEM = @System
AND mV.IpdGUID= @IpdGUID
ORDER BY mV.RDate DESC, mV.RTime DESC,mv.CreDate desc ,mv.ModDate desc
執行結果大略如圖
我能請問我該如何做才能只顯示最近日期的資料呢?
我有想過用union 來處理只是這樣我好像就不能用order by
目前顯示的都是已經篩選過的只差做判斷說要只顯示哪一筆
你可以把Order放在Row_Number()Over(order by ....)
然後Union起來~下where是排序 1 就可以帶出來~
select top 1 *
from ((
SELECT
Row_Number()Over(ORDER BY mV.RDate DESC, mV.RTime DESC,mv.CreDate desc ,mv.ModDate desc) Sort
,mV.CID ,mV.HID ,mV.SYSTEM ,mV.VGUID ,mV.HisGUID
,mv.RDate
,mv.RTime
,mV.IpdGUID
,mv.AD_Ideal_Wei
,mv.AD_Knee
,mv.AD_Height
,mv.AD_Regular_Wei
,mv.AD_Current_Wei
,mv.AD_IBW
,mv.AD_Adjust_Wei
,mv.AD_Oth
,mv.Lab
,mv.AD_BMI
,ptype ='Second'
FROM viewNIS_GetNutSecendVisitViewNew As mV
WHERE mV.CID= @CID AND mV.HID= @HID AND mV.SYSTEM = @System
AND mV.IpdGUID= @IpdGUID
)union all(
SELECT
Row_Number()Over(ORDER BY ORDER BY mV.RDate DESC, mV.RTime DESC,mv.CreDate desc ,mv.ModDate desc) Sort
,mV.CID ,mV.HID ,mV.SYSTEM ,mV.VGUID ,mV.HisGUID
,mv.RDate
,mv.RTime
,mV.IpdGUID
,mv.AD_Ideal_Wei
,mv.AD_Knee
,mv.AD_Height
,mv.AD_Regular_Wei
,mv.AD_Current_Wei
,mv.AD_IBW
,mv.AD_Adjust_Wei
,mv.AD_Oth
,mv.Lab
,mv.AD_BMI
,ptype ='First'
FROM viewNIS_GetNutFirstVisitViewNew As mV
WHERE mV.CID= @CID AND mV.HID= @HID AND mV.SYSTEM = @System
AND mV.IpdGUID= @IpdGUID
)) k
where Sort = 1
order by RDate desc
或者~簡單用in....
SELECT top 1
mV.CID ,mV.HID ,mV.SYSTEM ,mV.VGUID ,mV.HisGUID
,mv.RDate
,mv.RTime
,mV.IpdGUID
,mv.AD_Ideal_Wei
,mv.AD_Knee
,mv.AD_Height
,mv.AD_Regular_Wei
,mv.AD_Current_Wei
,mv.AD_IBW
,mv.AD_Adjust_Wei
,mv.AD_Oth
,mv.Lab
,mv.AD_BMI
,ptype in('Second','First')
FROM viewNIS_GetNutFirstVisitViewNew As mV
WHERE mV.CID= @CID AND mV.HID= @HID AND mV.SYSTEM = @System
AND mV.IpdGUID= @IpdGUID
ORDER BY ORDER BY mV.RDate DESC, mV.RTime DESC,mv.CreDate desc ,mv.ModDate desc
CREATE TABLE TableX (
FA Nvarchar(20) NULL , -- 品名
FB Nvarchar(20) NULL, -- 開始日期
FC Nvarchar(20) NULL, -- 完成日期
FD Nvarchar(20) NULL); -- 階段
INSERT INTO TableX (FA,FB,FC,FD)
VALUES
(N'配電盤','2006-03-01','2006-03-15','1'),
(N'配電盤','2006-03-15','2006-03-18','2'),
(N'配電盤','2006-03-18','2006-03-20','3'),
(N'發電機','2006-03-05','2006-03-10','1');
CREATE TABLE TableY (
FA Nvarchar(20) NULL , -- 品名
FB Nvarchar(20) NULL, -- 開始日期
FC Nvarchar(20) NULL, -- 完成日期
FD Nvarchar(20) NULL); -- 階段
INSERT INTO TableY (FA,FB,FC,FD)
VALUES
(N'配電盤','2006-03-01','2006-03-15','1'),
(N'發電機','2006-03-05','2006-03-10','1'),
(N'發電機','2006-03-10','2006-03-15','2');
SELECT FA AS '品名',FB AS '開始日期',FC AS '完成日期', FD AS '階段'
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY FA ORDER BY FD DESC) AS ROWNUM,*
FROM (
SELECT FA,FB,FC,FD
FROM TableX
UNION ALL
SELECT FA,FB,FC,FD
FROM TableY) AS TempK
) AS TempM
WHERE ROWNUM = 1
ORDER BY FA DESC