无法摆脱重复

时间:2015-02-06 08:14:29

标签: sql sql-server-2008 tsql duplicates

我有我的疑问:

SELECT   t.nCitiesId
    ,t.tmDate
    ,t.strShortWeekdayName
    ,t.nIsHoliday
FROM    #temp AS t
WHERE   1 = 1
AND     t.nCitiesId = 104
AND     t.tmDate = '2014-06-08'
GROUP BY t.tmDate, t.nCitiesId, t.strShortWeekdayName, t.nIsHoliday

这会产生以下结果:

nCitiesId         tmDate              strShortWeekdayName   nIsHoliday
104       2014-06-08 00:00:00.000            SUN                0
104       2014-06-08 00:00:00.000            SUN                1

我的问题是当nCitiesId和tmDate相等时,我不会是nIsHoliday = 0的那个。

我怎样才能顺利地做到这一点?

2 个答案:

答案 0 :(得分:1)

尝试使用having子句:

SELECT   t.nCitiesId
    ,t.tmDate
    ,t.strShortWeekdayName
    ,t.nIsHoliday
FROM    #temp AS t
WHERE   1 = 1
AND     t.nCitiesId = 104
AND     t.tmDate = '2014-06-08'
GROUP BY t.tmDate, t.nCitiesId, t.strShortWeekdayName, t.nIsHoliday
having max(t.nIsHoliday)

答案 1 :(得分:1)

选择具有最高值的t.nIsHoliday:

SELECT t.nCitiesId
      ,t.tmDate
      ,t.strShortWeekdayName
      ,max(t.nIsHoliday)
FROM   #temp AS t
WHERE  t.nCitiesId = 104
AND    t.tmDate = '2014-06-08'
GROUP BY t.nCitiesId, t.tmDate, t.strShortWeekdayName