如果在SQL中指定了SELECT DISTINCT,则ORDER BY项必须出现在选择列表中

时间:2016-02-22 06:06:36

标签: sql sql-server database sql-server-2008

我写了一个SQL查询但是我收到了这个错误:

Msg 145, Level 15, State 1, Procedure GetClientSpecificCollection, Line 18
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

我的查询就在这里:

    SELECT DISTINCT CONVERT(VARCHAR(11), CBPD.AddedDate, 106) AS [Date],
                CBD.BillNo                                AS [BillNo],
                CBPD.IsOpening                            AS [Opening],
                CASE
                  WHEN CBPD.PaymentMode = 1 THEN 'Cash Payment'
                  WHEN CBPD.PaymentMode = 2 THEN 'By Cheque, '
                                                 + Cast(CBPD.CheckNo AS VARCHAR(10)) + ', '
                                                 + CBPD.BankName
                  WHEN CBPD.PaymentMode = 3 THEN 'NEFT Payment'
                  WHEN CBPD.PaymentMode = 4 THEN 'Bank Draft'
                  WHEN CBPD.PaymentMode = 5 THEN 'Online Payment'
                  ELSE 'NA'
                END                                       AS [PaymentMode],
                CBPD.PayableAmount                        AS [BillingAmount],
                CBPD.PaymentAmount                        AS [PaymentAmount],
                CBPD.RemainAmount                         AS [BillAmount],
                CBD.RemainAmount                          AS [RemainForNext]
FROM   ClientBillingDetail CBD
       INNER JOIN ClientBillPaymentDetail CBPD
               ON CBD.ClientId = CBPD.ClientId
WHERE  CBD.ClientId = @ClientId
       AND CBD.CompanyNameId = 1
       AND ( IsOpening = 1
              OR IsRemaining = 1 )
ORDER  BY IsOpening DESC,
          CBPD.BillNo,
          CBPD.AddedDate 

这是我的整个查询,任何人都可以告诉此查询中的问题以及如何更正此查询。

4 个答案:

答案 0 :(得分:0)

您可以使用别名而不是使用CBPD.IsOpening;

喜欢这个

ORDER BY        Opening DESC, CBPD.BillNo, CBPD.AddedDate

我认为这解决了问题

答案 1 :(得分:0)

该错误是因为您要在CBPD.AddedDate声明中转换SELECT。试试这个查询

SELECT DISTINCT
    CONVERT(varchar(11), CBPD.AddedDate, 106) AS [Date],
    CBPD.BillNo AS [BillNo],
    CBPD.IsOpening AS [Opening],
    CASE
        WHEN CBPD.PaymentMode = 1 THEN 'Cash Payment'
        WHEN CBPD.PaymentMode = 2 THEN 'By Cheque, ' + CAST(CBPD.CheckNo AS varchar(10)) + ', ' + CBPD.BankName
        WHEN CBPD.PaymentMode = 3 THEN 'NEFT Payment'
        WHEN CBPD.PaymentMode = 4 THEN 'Bank Draft'
        WHEN CBPD.PaymentMode = 5 THEN 'Online Payment'
        ELSE 'NA'
    END AS [PaymentMode],
    CBPD.PayableAmount AS [BillingAmount],
    CBPD.PaymentAmount AS [PaymentAmount],
    CBPD.RemainAmount AS [BillAmount],
    CBD.RemainAmount AS [RemainForNext]
FROM ClientBillingDetail CBD
INNER JOIN ClientBillPaymentDetail CBPD
    ON CBD.ClientId = CBPD.ClientId
WHERE CBPD.ClientId = @ClientId
AND CBD.CompanyNameId = 1
AND (CBPD.IsOpening = 1
OR CBPD.IsRemaining = 1)
ORDER BY CBPD.IsOpening DESC,
CBPD.BillNo, 
CONVERT(varchar(11),CBPD.AddedDate, 106)

答案 2 :(得分:0)

现在无法测试,但如果您确实需要转换日期,则可能需要执行此操作:

SELECT 
  CONVERT(VARCHAR(11), CBPD.AddedDate, 106) AS [Date],
  [BillNo],
  [Opening],
  [PaymentMode],
  [BillingAmount],
  [PaymentAmount],
  [BillAmount],
  [RemainForNext]
from (
    SELECT DISTINCT CBPD.AddedDate AS [Date],
                CBD.BillNo                                AS [BillNo],
                CBPD.IsOpening                            AS [Opening],
                CASE
                  WHEN CBPD.PaymentMode = 1 THEN 'Cash Payment'
                  WHEN CBPD.PaymentMode = 2 THEN 'By Cheque, '
                                                 + Cast(CBPD.CheckNo AS VARCHAR(10)) + ', '
                                                 + CBPD.BankName
                  WHEN CBPD.PaymentMode = 3 THEN 'NEFT Payment'
                  WHEN CBPD.PaymentMode = 4 THEN 'Bank Draft'
                  WHEN CBPD.PaymentMode = 5 THEN 'Online Payment'
                  ELSE 'NA'
                END                                       AS [PaymentMode],
                CBPD.PayableAmount                        AS [BillingAmount],
                CBPD.PaymentAmount                        AS [PaymentAmount],
                CBPD.RemainAmount                         AS [BillAmount],
                CBD.RemainAmount                          AS [RemainForNext]
FROM   ClientBillingDetail CBD
       INNER JOIN ClientBillPaymentDetail CBPD
               ON CBD.ClientId = CBPD.ClientId
WHERE  CBD.ClientId = @ClientId
       AND CBD.CompanyNameId = 1
       AND ( IsOpening = 1
              OR IsRemaining = 1 )
) X
ORDER  BY X.Opening,
          X.BillNo,
          X.[Date]

这样,您在同一个选择中没有明确的顺序。

如果AddedDate是datetime,则应首先将其转换为日期,以便该distinct能够正常工作。

答案 3 :(得分:0)

您必须在select子句中使用别名,如下所示

SELECT DISTINCT CONVERT(VARCHAR(11), CBPD.AddedDate, 106) AS [Date],
            CBD.BillNo                                AS [BillNo],
            CBPD.IsOpening                            AS [Opening],
            CASE
              WHEN CBPD.PaymentMode = 1 THEN 'Cash Payment'
              WHEN CBPD.PaymentMode = 2 THEN 'By Cheque, '
                                             + Cast(CBPD.CheckNo AS VARCHAR(10)) + ', '
                                             + CBPD.BankName
              WHEN CBPD.PaymentMode = 3 THEN 'NEFT Payment'
              WHEN CBPD.PaymentMode = 4 THEN 'Bank Draft'
              WHEN CBPD.PaymentMode = 5 THEN 'Online Payment'
              ELSE 'NA'
            END                                       AS [PaymentMode],
            CBPD.PayableAmount                        AS [BillingAmount],
            CBPD.PaymentAmount                        AS [PaymentAmount],
            CBPD.RemainAmount                         AS [BillAmount],
            CBD.RemainAmount                          AS [RemainForNext]
FROM   ClientBillingDetail CBD
   INNER JOIN ClientBillPaymentDetail CBPD
           ON CBD.ClientId = CBPD.ClientId
WHERE  CBD.ClientId = @ClientId
   AND CBD.CompanyNameId = 1
   AND ( IsOpening = 1
          OR IsRemaining = 1 )
ORDER  BY [Opening] DESC,
      [BillNo],
      [Date]