查询返回错误的行数

时间:2013-12-30 06:52:28

标签: sql sql-server

我写了一个查询,因为我想把分数分为高,中,低,所以我可以看看这些组是否与另一个表比较(这个表在这里不重要)。

当我使用第二个查询时,它返回约3000行。我唯一能想到的就是这里有一个逻辑错误。似乎第一个和第二个查询应返回基本相同的信息,但不会。

我正在使用SQL Server 2012。

--query 1 output 12279
select count(*)
from quiz_scores
where item_id between 100 and 200
and score between 0 and 1


--query 2 output 7824 rows
(
select anon_user_id
,title
,'low' as achiever
from quiz_scores
where item_id between 100 and 200
and score between 0 and 0.33
group by anon_user_id
,title

union

select anon_user_id
,title
,'middle'
from quiz_scores
where item_id between 100 and 200
and score between 0.33 and 0.66
group by anon_user_id
,title

union

select anon_user_id
,title
,'high'
from quiz_scores
where item_id between 100 and 200
and score between 0.66 and 1.01
group by anon_user_id
,title
) 

2 个答案:

答案 0 :(得分:2)

--query 1 output 12279
select count(*)
from quiz_scores
where item_id between 100 and 200
and score between 0 and 1

这是您的第一个查询,它不包含group by子句,因此,无论重复数据如何,都将选择与where子句匹配的所有行

--query 2 output 7824 rows
(
select anon_user_id
,title
,'low' as achiever
from quiz_scores
where item_id between 100 and 200
and score between 0 and 0.33
group by anon_user_id
,title

union

select anon_user_id
,title
,'middle'
from quiz_scores
where item_id between 100 and 200
and score between 0.33 and 0.66
group by anon_user_id
,title

union

select anon_user_id
,title
,'high'
from quiz_scores
where item_id between 100 and 200
and score between 0.66 and 1.01
group by anon_user_id
,title
) 

这是你的第二个问题。查看每个片段,您将看到每个片段中都使用group by子句。 group by会忽略重复anon_user_id的行,因此如果表中存在重复anon_user_id的行,则此查询选择的行集合将小于第一个查询。< / p>

答案 1 :(得分:1)

如果没有找到丢失记录的原因(我认为@Doan Cuong做对了),我认为获得所需结果的最简单方法是在您的选择中使用CASE

SELECT   anon_user_id 
        ,title      
        ,(CASE 
           WHEN score  BETWEEN 0 AND 0.33 THEN 'LOW'
           WHEN score  BETWEEN 0.33 AND 0.66 THEN 'Medium'
           WHEN score  BETWEEN 0.66 AND 1.01 THEN 'High'
           ELSE 'Undefiend'
           END) as 'achiever'
FROM   quiz_scores
WHERE item_id BETWEEN 100 AND 200