我想用UNION对以下查询的订单号进行排序,但是虽然谷歌搜索很多,但是无法完成。
IF LEN(@_Order_No)=0
BEGIN
SELECT a._PROCESS_INST_NO,c._ISSUE_DATE,c._DELIVERY_DATE, a._Order_No,a._GOODS_CD,a._GOODS_NAME ,
--a._QTY,
CAST(a._QTY as DECIMAL (38,2)) as _QTY,
b._GOODS_CD as RM_CD,b._GOODS_NAME as RM_NAME,b._UNIT,
(CASE
WHEN LEFT(a._GOODS_NAME,2)='SS' OR LEFT(a._GOODS_NAME,2)='SN' THEN
CAST(a._QTY as DECIMAL (38,2)) * CAST(b._QTY as DECIMAL (38,2))/ NULLIF(SUM(CAST(a._QTY AS decimal(38,2) )) OVER (PARTITION BY a._Order_No ,b._GOODS_CD),0)
ELSE
CAST(b._QTY as DECIMAL (38,2))
END) as RM_QTY
FROM [ENVNDIVDB].[dbo].[TBL_PROC_PM] a
INNER JOIN [ENVNDIVDB].[dbo].[TBL_PROC_PMS] b
ON a._PROCESS_INST_NO=b._PROCESS_INST_NO
INNER JOIN [ENVNDIVDB].[dbo].[TBL_PROC_PH] c
ON a._PROCESS_INST_NO=c._PROCESS_INST_NO
WHERE c._DELIVERY_DATE BETWEEN @startDate AND @endDate
UNION ALL
SELECT a._PROCESS_INST_NO,c._ISSUE_DATE,c._DELIVERY_DATE, a._Order_No,a._GOODS_CD,a._GOODS_NAME ,
--a._QTY,
CAST(a._QTY as DECIMAL (38,2)) as _QTY,
b._GOODS_CD as RM_CD,b._GOODS_NAME as RM_NAME,b._UNIT,
(CASE
WHEN LEFT(a._GOODS_NAME,2)='SS' OR LEFT(a._GOODS_NAME,2)='SN' THEN
CAST(a._QTY as DECIMAL (38,2)) * CAST(b._QTY as DECIMAL (38,2))/ NULLIF(SUM(CAST(a._QTY AS decimal(38,2) )) OVER (PARTITION BY a._Order_No ,b._GOODS_CD),0)
ELSE CAST(b._QTY as DECIMAL (38,2))
END) as RM_QTY
FROM [ENVNDIVDB].[dbo].[TBL_PROC_PM] a
INNER JOIN [ENVNDIVDB].[dbo].[TBL_PROC_PMS] b
ON a._PROCESS_INST_NO=b._PROCESS_INST_NO
INNER JOIN [ENVNDIVDB].[dbo].[TBL_PROC_PH] c
ON a._PROCESS_INST_NO=c._PROCESS_INST_NO
WHERE c._DELIVERY_DATE BETWEEN @startDate AND @endDate
SELECT '' as _PROCESS_INST_NO,'' as _ISSUE_DATE,'' as _DELIVERY_DATE, a.X_PO_NO as _Order_No,a.[X_GOODS_CD] as _GOODS_CD ,a.[X_GOODS_NAME1] as _GOODS_NAME ,
CAST(X_QTY as DECIMAL (38,2)) as _QTY,'' as RM_CD,'' as RM_NAME , '' as _UNIT,
0.0 as RM_QTY
FROM [ENVIETNAMPO].[dbo].[_TBL_PO_M] a
END
ELSE
BEGIN
SELECT a._PROCESS_INST_NO,c._ISSUE_DATE,c._DELIVERY_DATE, a._Order_No,a._GOODS_CD,a._GOODS_NAME,CAST(a._QTY as DECIMAL (38,2)) as _QTY,
b._GOODS_CD as RM_CD,b._GOODS_NAME as RM_NAME,b._UNIT,
(CASE
WHEN LEFT(a._GOODS_NAME,2)='SS' OR LEFT(a._GOODS_NAME,2)='SN' THEN
CAST(a._QTY as DECIMAL (38,2)) * CAST(b._QTY as DECIMAL (38,2))/ NULLIF(SUM(CAST(a._QTY AS decimal(38,2) )) OVER (PARTITION BY a._Order_No ,b._GOODS_CD),0)
ELSE
CAST(b._QTY as DECIMAL (38,2))
END) as RM_QTY
FROM [ENVNDIVDB].[dbo].[TBL_PROC_PM] a
INNER JOIN [ENVNDIVDB].[dbo].[TBL_PROC_PMS] b
ON a._PROCESS_INST_NO=b._PROCESS_INST_NO
INNER JOIN [ENVNDIVDB].[dbo].[TBL_PROC_PH] c
ON a._PROCESS_INST_NO=c._PROCESS_INST_NO
WHERE c._DELIVERY_DATE BETWEEN @startDate AND @endDate AND a._Order_No=@_Order_No
UNION
SELECT '' as _PROCESS_INST_NO,'' as _ISSUE_DATE,'' as _DELIVERY_DATE, a.X_PO_NO as _Order_No,a.[X_GOODS_CD] as _GOODS_CD ,a.[X_GOODS_NAME1] as _GOODS_NAME ,
CAST(X_QTY as DECIMAL (38,2)) as _QTY,'' as RM_CD,'' as RM_NAME , '' as _UNIT, 0.0 as RM_QTY
FROM [ENVIETNAMPO].[dbo].[_TBL_PO_M] a
WHERE a.X_PO_NO=@_Order_No
END
答案 0 :(得分:1)
就这样做:
SELECT _Order_No FROM (values(1), (2), (4), (6)) [first](_Order_No)
UNION
SELECT _Order_No FROM (values(3), (7), (8), (5)) [second](_Order_No)
ORDER BY _Order_No;
输出:
_Order_No
-----------
1
2
3
4
5
6
7
8
答案 1 :(得分:0)
这是我的解决方案,它的工作原理
if(dbo.fn_LisIsActice()=1)
begin
select a, b, c from TableA
union all
select a, b, c from TableB
order by a
end
else
begin
select a, b, c from TableA order by a
end