字段上的SQL分组不起作用

时间:2014-05-19 10:36:53

标签: sql ms-access group-by

我遇到了下面写的sql行的问题。其中大部分是由Access的设计者完成的,我改变了一些像连接这样的东西。连接工作,但它似乎不想在本例中对周为tbl1的字段进行分组。

有人能看到我在这里失踪并指出答案吗?

SELECT tbl2.[name], 
       tbl1.[week], 
       tbl1.[production], 
       Sum(tbl3.[sales]) AS total sales, 
       (tbl1.[production]/tbl3.[sales]) AS Conversion
  FROM (tbl1 RIGHT JOIN tbl2 ON tbl1.[ID] = tbl2.[ID]) 
        RIGHT JOIN tbl3 ON tbl2.[ID] = tbl3.[ID]
  GROUP 
     BY tbl2.[name], 
        tbl1.[week], 
        tbl1.[production], 
        (tbl1.[production]/tbl3.[sales])
 HAVING (((tbl1.[week]) Between [Start week] And [end Week]))
 ORDER BY tbl1.[week];

2 个答案:

答案 0 :(得分:0)

根据我所知道的,当你通过你选择的相同字段进行分组时。所以,在你的情况下试试这个:

SELECT tbl2.[name], tbl1.[week], tbl1.[production], Sum(tbl3.[sales]) AS total sales, (tbl1.[production]/tbl3.[sales]) AS Conversion
FROM (tbl1 JOIN tbl2 ON tbl1.[ID] = tbl2.[ID]) JOIN tbl3 ON tbl2.[ID] = tbl3.[ID]
GROUP BY tbl2.[name], tbl1.[week], tbl1.[production], Sum(tbl3.[sales]), (tbl1.[production]/tbl3.[sales])
HAVING (((tbl1.[week]) Between [Start week] And [end Week]))
ORDER BY tbl1.[week]; 

希望它有效,我使用sql server,它可能会有所不同。

答案 1 :(得分:0)

您的查询看起来合理,除了:

HAVING (((tbl1.[week]) Between [Start week] And [end Week]))

[Start week][end Week]需要在group by子句中进行汇总或汇总。我不确定你真正需要的是哪种解决方案。这是第一种方法:

HAVING tbl1.[week] Between MIN([Start week]) And MAX([end Week])