为什么我没有在这里得到明确的分组?

时间:2014-02-06 20:02:09

标签: sql sql-server

SELECT "STORE VISIT",STORE,USERVISITED 
FROM ECRSURVEY 
WHERE "STORE VISIT" > '2014-01-01     00:00:00.000'
GROUP BY STORE, "STORE VISIT", UserVisited

这不应该返回列STORE的不同分组吗?我的结果是同一个商店的多个条目。

1 个答案:

答案 0 :(得分:1)

获得明确的STORE

SELECT STORE 
FROM   ECRSURVEY 
WHERE  "STORE VISIT" > '20140101'
GROUP BY STORE

SELECT DISTINCT STORE 
FROM   ECRSURVEY 
WHERE  "STORE VISIT" > '20140101'

如果您对其他两个字段进行聚合,那么您应该获得唯一的行

SELECT STORE 
       NumStoreVisit = COUNT("STORE VISIT"), 
       NumUserVisited = COUNT(UserVisited)
FROM   ECRSURVEY 
WHERE  "STORE VISIT" > '20140101'
GROUP BY STORE

但是如果您的数据结构如下,则您要求不可能:

enter image description here