好友
我有一个MSSQL查询。 就是这样......
SELECT DISTINCT resource.locationurl,
resource.resourcename,
resource.anwserid,
checktotal.total
FROM resource
INNER JOIN (SELECT Count(DISTINCT anwserid) AS total,
resourcename
FROM resource AS Resource_1
WHERE ( anwserid IN (SELECT Cast(value AS INT) AS Expr1
FROM dbo.Udf_split(@sCategoryID, ',')
AS
udf_Split_1) )
GROUP BY resourcename) AS checktotal
ON resource.resourcename = checktotal.resourcename
WHERE ( resource.anwserid IN (SELECT Cast(value AS INT) AS Expr1
FROM dbo.Udf_split(@sCategoryID, ',') AS
udf_Split_1)
)
AND ( checktotal.total = @Total )
ORDER BY resource.resourcename
我运行此查询但它给了我重复的Resource.LocationURL列。
您可以直接查看http://www.ite.org/visionzero/toolbox/default2.aspx
检查上面的链接,你可以在哪里开火选择一些类别,但结果并不明显..
我尝试了大部分时间,但现在我不在乎请帮助我。
答案 0 :(得分:1)
您误解了DISTINCT
在获取多个列时的含义。
如果您运行此查询:
SELECT DISTINCT col1, col2 FROM table
您选择了每个不同的组合。可接受的结果是
value 1_1, value 2_1
value 1_1, value 2_2,
value 2_1, value_2_1
在此示例中,value 1_1
出现两次,但两列合并是唯一的。
我的猜测是你实际上是在尝试进行分组:
SELECT resource.locationurl,
resource.resourcename,
resource.anwserid,
Sum(checktotal.total)
FROM resource
INNER JOIN (SELECT Count(DISTINCT anwserid) AS total,
resourcename
FROM resource AS Resource_1
WHERE ( anwserid IN (SELECT Cast(value AS INT) AS Expr1
FROM dbo.Udf_split(@sCategoryID, ',')
AS
udf_Split_1) )
GROUP BY resourcename) AS checktotal
ON resource.resourcename = checktotal.resourcename
WHERE ( resource.anwserid IN (SELECT Cast(value AS INT) AS Expr1
FROM dbo.Udf_split(@sCategoryID, ',') AS
udf_Split_1)
)
AND ( checktotal.total = @Total )
GROUP BY resource.locationurl,
resource.resourcename,
resource.anwserid
答案 1 :(得分:1)
首先,您关联的网站没有做任何事情。
其次,DISTINCT
确保唯一的行。它不会使所有列中的值都是唯一的。考虑一下!它会如何工作?您有两行具有相同的locationurl
字段,但具有其他不同元素。你不包括哪一个?
最后,请更加谨慎地处理您的问题。
答案 2 :(得分:0)
因为我看到您的查询是在多列上选择DISTINCT, 所以如果任何记录至少有一个col的差异,那么它就会传递DISTINCT条件
例如:
record1 : locationurl | resourcename | anwserid | Sum(checktotal.total)
loc1 res1 1 100
record2 : locationurl | resourcename | anwserid | Sum(checktotal.total)
loc1 res1 2 100