我写了这个查询来查找Net的总和。但是我收到了一个错误。你能帮助我吗?
SELECT SUM(net) AS net
FROM (select SUM(FB.[Net]) as net FROM (([table1 AS FB INNER JOIN
Date ON FB.DateKey = Date.DateKey)
inner join [Store] on [Store].StoreKey = FB.StoreKey) WHERE (FullDate BETWEEN DATEADD(WEEK, -12, CONVERT(date, GETDATE()))
AND DATEADD(day, -1, CONVERT(date, GETDATE()))
AND SName = 'XYZ'
AND DayNameOfWeek = 'Monday')
GROUP BY FB.[Net])
亲切的问候
答案 0 :(得分:0)
格式化代码是关键:
SELECT SUM(net) AS net
FROM (
SELECT SUM(FB.[Net]) as net
FROM ((
[table1 AS FB
INNER JOIN Date
ON FB.DateKey = Date.DateKey
)
inner join [Store]
on [Store].StoreKey = FB.StoreKey
)
WHERE (FullDate BETWEEN DATEADD(WEEK, -12, CONVERT(date, GETDATE()))
AND DATEADD(day, -1, CONVERT(date, GETDATE()))
AND SName = 'XYZ'
AND DayNameOfWeek = 'Monday')
GROUP BY FB.[Net]
)
我现在可以看到很多错误。我认为这解决了大多数语法错误,但我不确定它是否会提供您想要的结果:
SELECT SUM(net) AS net
FROM (
SELECT SUM([Net]) as net
FROM (
SELECT FB.net
FROM table1 AS FB
INNER JOIN [Date]
ON FB.DateKey = [Date].DateKey
inner join [Store]
on [Store].StoreKey = FB.StoreKey
WHERE FullDate BETWEEN DATEADD(WEEK, -12, GETDATE()) AND DATEADD(day, -1, GETDATE())
AND SName = 'XYZ'
AND DayNameOfWeek = 'Monday'
GROUP BY FB.[Net]
) A
) B
编辑这是您自己的代码,没有语法错误(据我所知)。以下代码应提供相同的结果集:
SELECT SUM(DISTINCT FB.net)
FROM table1 AS FB
INNER JOIN [Date]
ON FB.DateKey = [Date].DateKey
INNER JOIN [Store]
ON [Store].StoreKey = FB.StoreKey
WHERE FullDate BETWEEN DATEADD(WEEK, -12, GETDATE()) AND DATEADD(day, -1, GETDATE())
AND SName = 'XYZ'
AND DayNameOfWeek = 'Monday'
你确定这是你想要的吗?
答案 1 :(得分:0)
尝试这样,删除不必要的括号并添加适当的别名。请将别名添加到"其中"子句过滤器也是如此。
SELECT SUM(net) AS net
FROM (
SELECT SUM(FB.[Net]) AS net
FROM [table1] FB
INNER JOIN [Date] D ON FB.DateKey = D.DateKey
INNER JOIN [Store] S ON S.StoreKey = FB.StoreKey
WHERE FullDate BETWEEN DATEADD(WEEK, - 12, CONVERT(DATE, GETDATE()))
AND DATEADD(day, - 1, CONVERT(DATE, GETDATE()))
AND SName = 'XYZ'
AND DayNameOfWeek = 'Monday'
GROUP BY FB.[Net]
) t