UNION在SQL SERVER中查询

时间:2014-03-18 01:37:11

标签: sql sql-server sql-server-2000

我是SQL SERVER的初学者。我有这2个查询:

第一次查询:

SELECT 'OK' AS STATUS, COUNT(*) AS BIL 
FROM 
(SELECT D.DAJ_NODAFTAR 
FROM BATM112_DES_AMJENTERA AS D 
WHERE D.DAJ_KODKAT='VTC' 
AND (SELECT TOP 1 DAJ_KLASIFIKASI FROM BATM112_RKDKLASIFIKASI WHERE     
DAJ_NODAFTAR=D.DAJ_NODAFTAR ORDER BY DAJ_TKHDE DESC )='OK') D

第二次查询:

SELECT 'NOT OK' AS STATUS, COUNT(*) AS BIL 
FROM 
(SELECT D.DAJ_NODAFTAR 
FROM BATM112_DES_AMJENTERA AS D 
WHERE D.DAJ_KODKAT='VTC' 
AND (SELECT TOP 1 DAJ_KLASIFIKASI FROM BATM112_RKDKLASIFIKASI WHERE     
DAJ_NODAFTAR=D.DAJ_NODAFTAR ORDER BY DAJ_TKHDE DESC )='NOT OK') D

当我尝试运行这两个查询时,结果显示完美但当我尝试使用UNION这样的两个查询组合时:

SELECT 'OK' AS STATUS, COUNT(*) AS BIL 
FROM 
(SELECT D.DAJ_NODAFTAR 
FROM BATM112_DES_AMJENTERA AS D 
WHERE D.DAJ_KODKAT='VTC' 
AND (SELECT TOP 1 DAJ_KLASIFIKASI FROM BATM112_RKDKLASIFIKASI WHERE     
DAJ_NODAFTAR=D.DAJ_NODAFTAR ORDER BY DAJ_TKHDE DESC )='OK') D
UNION
SELECT 'NOT OK' AS STATUS, COUNT(*) AS BIL 
FROM 
(SELECT D.DAJ_NODAFTAR 
FROM BATM112_DES_AMJENTERA AS D 
WHERE D.DAJ_KODKAT='VTC' 
AND (SELECT TOP 1 DAJ_KLASIFIKASI FROM BATM112_RKDKLASIFIKASI WHERE     
DAJ_NODAFTAR=D.DAJ_NODAFTAR ORDER BY DAJ_TKHDE DESC )='NOT OK') D

此错误消息显示:

  

Msg 104,Level 15,State 1,Line 1
  如果语句包含UNION运算符,则ORDER BY项必须出现在选择列表中。

有人可以告诉我如何修复此错误吗?

5 个答案:

答案 0 :(得分:0)

错误消息已指出答案。只需删除“DAJ_TKHDE DESC”的“订单”即可。在查询中。这就对了!然后再次运行union查询。

SELECT 'OK' AS STATUS, COUNT(*) AS BIL 
FROM 
(SELECT D.DAJ_NODAFTAR 
FROM BATM112_DES_AMJENTERA AS D 
WHERE D.DAJ_KODKAT='VTC' 
AND (SELECT TOP 1 DAJ_KLASIFIKASI FROM BATM112_RKDKLASIFIKASI WHERE     
DAJ_NODAFTAR=D.DAJ_NODAFTAR)='OK') D

UNION

SELECT 'NOT OK' AS STATUS, COUNT(*) AS BIL 
FROM 
(SELECT D.DAJ_NODAFTAR 
FROM BATM112_DES_AMJENTERA AS D 
WHERE D.DAJ_KODKAT='VTC' 
AND (SELECT TOP 1 DAJ_KLASIFIKASI FROM BATM112_RKDKLASIFIKASI WHERE     
DAJ_NODAFTAR=D.DAJ_NODAFTAR)='NOT OK') D

更新

提供第二种解决方案(替代方法),

declare @MyTable table
(
    Status varchar(25),
    Count int
)

insert into @MyTable
your first query

insert into @MyTable
your second query

select * from @MyTable

答案 1 :(得分:0)

您应该能够首先使用窗口CTE来消除子查询中对订单的需求。字段复杂性使我无法构建样本数据以进行验证,但类似于:

WITH cte AS (
 SELECT DAJ_KLASIFIKASI
       ,DAJ_NODAFTAR
       ,ROW_NUMBER() OVER (PARTITION BY DAJ_NODAFTAR ORDER BY DAJ_TKHDE DESC)
   FROM BATM112_RKDKLASIFIKASI
)
SELECT 'OK' AS STATUS,
       COUNT(*) AS BIL
FROM   (SELECT D.DAJ_NODAFTAR
        FROM   BATM112_DES_AMJENTERA AS D
        WHERE  D.DAJ_KODKAT = 'VTC'
               AND (SELECT DAJ_KLASIFIKASI FROM cte WHERE DAJ_NODAFTAR = D.DAJ_NODAFTAR AND rn=1) = 'OK') AS D
UNION
SELECT 'NOT OK' AS STATUS,
       COUNT(*) AS BIL
FROM   (SELECT D.DAJ_NODAFTAR
        FROM   BATM112_DES_AMJENTERA AS D
        WHERE  D.DAJ_KODKAT = 'VTC'
               AND (SELECT DAJ_KLASIFIKASI FROM cte WHERE DAJ_NODAFTAR = D.DAJ_NODAFTAR AND rn=1) = 'NOT OK') AS D

sql-server-order-by-in-subquery-with-union进行了类似的讨论。

答案 2 :(得分:0)

您需要重新排列查询。 我在下面的查询中的目的是保持子查询值(在表变量中具有“order by”的查询)在表变量中,并且表变量将用“Order by”替换子查询

Declare @temp Table
(
 DAJ_KLASIFIKASI nvarchar(50)
)

Insert into @temp values
SELECT TOP 1 DAJ_KLASIFIKASI FROM BATM112_RKDKLASIFIKASI WHERE   
DAJ_NODAFTAR in ( SELECT D.DAJ_NODAFTAR FROM BATM112_DES_AMJENTERA AS D WHERE D.DAJ_KODKAT='VTC') and DAJ_Klasifikasi = 'NOT OK' ORDER BY DAJ_TKHDE DESC 

Insert into @temp values
SELECT TOP 1 DAJ_KLASIFIKASI FROM BATM112_RKDKLASIFIKASI WHERE   
DAJ_NODAFTAR in (SELECT D.DAJ_NODAFTAR FROM BATM112_DES_AMJENTERA AS D WHERE D.DAJ_KODKAT='VTC') and DAJ_Klasifikasi = 'OK' ORDER BY DAJ_TKHDE DESC 

你的修改后的查询:

SELECT 'OK' AS STATUS, COUNT(*) AS BIL 
FROM 
(SELECT D.DAJ_NODAFTAR 
FROM BATM112_DES_AMJENTERA AS D 
WHERE D.DAJ_KODKAT='VTC' 
AND ((SELECT DAJ_KLASIFIKASI FROM @temp where DAJ_Klasifikasi='OK')='OK') D
UNION
SELECT 'NOT OK' AS STATUS, COUNT(*) AS BIL 
FROM 
(SELECT D.DAJ_NODAFTAR 
FROM BATM112_DES_AMJENTERA AS D 
WHERE D.DAJ_KODKAT='VTC' 
AND ((SELECT DAJ_KLASIFIKASI FROM @temp where DAJ_Klasifikasi='NOT OK')='NOT OK') D

注意:由于我没有示例数据,因此未对此查询进行测试。所以请在使用前彻底测试。您需要在主查询中删除订单。

答案 3 :(得分:0)

你是否有可能因为不必要的联盟而过度复杂?我跟表/字段名称有点麻烦,但我认为你要做的就是冒泡好或不好并计算出现次数。也许是GROUP BY的东西呢?

SELECT STATUS,
       COUNT(*) AS BIL
  FROM (SELECT D.DAJ_NODAFTAR
              ,(SELECT TOP 1 
                       DAJ_KLASIFIKASI
                  FROM BATM112_RKDKLASIFIKASI
                 WHERE DAJ_NODAFTAR = D.DAJ_NODAFTAR
                 ORDER BY DAJ_TKHDE DESC) STATUS
          FROM BATM112_DES_AMJENTERA AS D
         WHERE D.DAJ_KODKAT = 'VTC') a
 GROUP BY STATUS

答案 4 :(得分:0)

只需从第一个选择查询中删除订单即可。使用union时,不允许这样做。所以,你得到了这个错误。

使用union时,您可以在最后一个(此处为第二个)选择查询中使用order by子句。

如果您的要求在第一个查询中确实需要订购,可以在此处找到解决方案。

SQL Query - Using Order By in UNION