如何获取不同的id列以防止重复结果?

时间:2017-09-22 23:03:29

标签: sql sql-server

SELECT DISTINCT
    id, name, floors, rooms, bathrooms, 
    singlebeds, doublebeds, tollboys, tvs, 
    conditontypes_id, roomtypes_id, roomstates_id, 
    internet, carstop, elevator, cleanservice, 
    phoneguide, botagaz, magazeen, maqroeev, salahdir, 
    clotheswasher, foodlist, makoa, thalaga, foodtable, 
    saala, kitchen, notations, stopstate, contract_id, 
    enterstate, aqed_id, aqedtype, datefrom, dateto, daynumber, 
    monthnumber, room_name, roomprice, cus_name, company, 
    discounttitle, discounttype, discountprice, total, totalend, 
    aqed_note, addeddate, entertime, state, nationalname, 
    sejelnumber, diffarest, diffbusy, dayprice, monthprice, hourprice
FROM            
    rooms_monitorview
WHERE
    (state IS NULL) OR
    (state = 'محجوز') AND (diffarest < 0)

这是结果

12  102 0   0   0   0   0   0   0   1   1   1   False   False   False   False   False   False   False   False   False   False   False   False   False   False   False   False   ....    False   0   شاغر    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
14  104 0   0   0   0   0   0   0   1   1   1   False   False   False   False   False   False   False   False   False   False   False   False   False   False   False   False   ....    False   0   شاغر    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
15  105 0   0   0   0   0   0   0   1   1   1   False   False   False   False   False   False   False   False   False   False   False   False   False   False   False   False   ....    False   0   شاغر    15  يومي    25/09/2017  02/10/2017  10  0   105 0.0000  سالم علي محمد   بدون    بدون    $   0.0000  0.0000  0.0000  ............    22/09/2017 10:35:24 م   NULL    محجوز   سعودي   123456  -2  -9  0.00    0.00    0.00
15  105 0   0   0   0   0   0   0   1   1   1   False   False   False   False   False   False   False   False   False   False   False   False   False   False   False   False   ....    False   0   شاغر    16  يومي    28/09/2017  28/09/2017  5   0   105 0.0000  سالم علي محمد   بدون    بدون    $   0.0000  0.0000  0.0000  ............    23/09/2017 01:31:21 ص   NULL    محجوز   سعودي   123456  -5  -5  0.00    0.00    0.00
16  106 0   0   0   0   0   0   0   1   1   1   False   False   False   False   False   False   False   False   False   False   False   False   False   False   False   False   ....    False   0   شاغر    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL

我需要明确的id=15 - 我该怎么做?

有两种不同的结果,-3和-9

1 个答案:

答案 0 :(得分:1)

如果每个ID只需要一行,请使用row_number()

SELECT v.*
FROM (SELECT v.*,
             ROW_NUMBER() OVER (PARTITION BY id ORDER BY id) as seqnum
      FROM rooms_monitorview
      WHERE (state IS NULL OR state = 'محجوز') AND diffarest < 0
     ) v
WHERE seqnum = 1;

注意到我在WHERE条件中添加了括号。这个逻辑与你的逻辑略有不同,但这可能就是你真正想要的。