函数参数列表出错:'='无法识别。函数参数列表出错:')'无法识别。无法解析查询文本

时间:2013-09-30 09:49:44

标签: sql sql-server date datetime

SELECT Name, 
  SUM(IIf([Date] = DateAdd('d', - 7, DATE ()),  PBal,0)) AS Opening, 
  SUM(IIf([Date] = DateAdd('d', - 7, DATE ()), NetAmount,0) ) AS DAY1,
  sum(IIf([Date] =DateAdd('d',-6,Date()),NetAmount ,0))AS DAY2,
  sum(IIf([Date] =DateAdd('d',-5,Date()),NetAmount ,0))AS DAY3,
  sum(IIf([Date] =DateAdd('d',-4,Date()),NetAmount,0 ))AS DAY4,
  sum(IIf([Date] =DateAdd('d',-3,Date()),NetAmount,0 ))AS DAY5,
  sum(IIf([Date] =DateAdd('d',-2,Date()),NetAmount,0 ))AS DAY6,
  sum(IIf([Date] =DateAdd('d',-1,Date()),NetAmount,0 ))AS DAY7,
  (Day1+Day2+Day3+Day4+Day5+Day6+Day7) AS Billtotal,
  sum(IIf([Date] =DateAdd('d',-7,Date()),(NetAmount-BBal),0)) AS Cash1,
  sum(IIf([Date] =DateAdd('d',-6,Date()),(NetAmount-BBal),0)) AS Cash2,
  sum(IIf([Date] =DateAdd('d',-5,Date()),(NetAmount-BBal),0)) AS Cash3,
  sum(IIf([Date] =DateAdd('d',-4,Date()),(NetAmount-BBal),0)) AS Cash4,
  sum(IIf([Date] =DateAdd('d',-3,Date()),(NetAmount-BBal),0)) AS Cash5,
  sum(IIf([Date] =DateAdd('d',-2,Date()),(NetAmount-BBal),0)) AS Cash6,
  sum(IIf([Date] =DateAdd('d',-1,Date()),(NetAmount-BBal),0)) AS Cash7,
  Cash1+Cash2+Cash3+Cash4+Cash5+Cash6 +Cash7) as CashPaid,
  (Billtotal-CashPaid) + Opening AS Currentbalance 
FROM Customer 
GROUP BY Name

1 个答案:

答案 0 :(得分:2)

  1. Date()不是函数。你想要GetDate()
  2. dateadd中的第一个参数不带引号:dateadd(d, -4, getdate())
  3. 如果您不使用SQL 2012,则IIF不是函数。
  4. 您可能会在pivot上找到datediff提供更简单的查询。