格式化代码是关键:
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'
你确定这是你想要的吗?