SQL错误:没有为< qry'的第1列指定列名。

时间:2018-01-23 09:13:26

标签: sql-server

我收到了“No Column name”错误'在这个SQL查询的qry部分。我试过命名qry列,但没有任何运气。任何帮助将不胜感激。

WITH qry as ( 
SELECT getdate(), cast(convert(varchar(10), getdate(), 110) as datetime) AS GetDate
      ,T1.[Account] 
      ,t2.AcctName
      ,T1.[RefDate] RefDate
      ,SUM(T1.[Debit])De
      ,SUM(T1.[Credit])Cr
FROM   OJDT T0
       INNER JOIN JDT1 T1
            ON  T0.TransId = T1.TransId
       INNER JOIN oact t2
            ON  t1.Account = t2.AcctCode
                AND t2.Finanse = 'Y'
                AND t2.FrozenFor = 'N'
                AND not t2.AcctName like '%Petty Cash%'
WHERE T1.[RefDate] < CONVERT(date,GETDATE() -7)
GROUP BY
       T1.[Account]
      ,T1.[RefDate]
      ,t2.AcctName
      ) 

SELECT 'TEST' as company
      ,c.Account AS Accountcode
      ,c.AcctName
      ,sum(de) - sum(cr) AS Closing
FROM   qry c
GROUP BY c.Account, c.AcctName
ORDER BY 
  case
    when CHARINDEX('Clearing', c.AcctName) > 1 then 'z'+ c.AcctName
    else c.AcctName
  end

1 个答案:

答案 0 :(得分:1)

您应该在cte:

中为getdate()列添加名称
WITH qry as ( 
SELECT getdate() AS CurrDate
      , cast(convert(varchar(10), getdate(), 110) as datetime) AS GetDate
      ,T1.[Account] 
      ,t2.AcctName
      ,T1.[RefDate] RefDate
      ,SUM(T1.[Debit])De
      ,SUM(T1.[Credit])Cr
FROM   OJDT T0
       INNER JOIN JDT1 T1
            ON  T0.TransId = T1.TransId
       INNER JOIN oact t2
            ON  t1.Account = t2.AcctCode
                AND t2.Finanse = 'Y'
                AND t2.FrozenFor = 'N'
                AND not t2.AcctName like '%Petty Cash%'
WHERE T1.[RefDate] < CONVERT(date,GETDATE() -7)
GROUP BY
       T1.[Account]
      ,T1.[RefDate]
      ,t2.AcctName
      )