开发者

sql server statement returning values 2 times

select
 loginname as 'Officer',
CONVERT(VARCHAR(10), visit.tmstamp, 105) as 'Date',
visit.queueno

from visit,event   where (  visit.branchno='1007'    )  
and  
(visit.wstation='1'  or visit.wstation='10'  or visit.wstation='11'  or visit.wstation='15'  or visit.wstation='2'  or visit.wstation='20'  or visit.wstation='21'  or visit.wstation='23'  or visit.wstation='24'  or visit.wstation='28'  or visit.wstation='29'  or visit.wstation='3'  or visit.wstation='30'  or visit.wstation='31'  or visit.wstation='32'  or visit.wstation='33'  or visit.wstation='4'  or visit.wstation='5'  or visit.wstation='6'  or visit.wstation='7'  or visit.wstation='8'  or visit.wstation='9'    )  and  (visit.catname='BY PASS'  or visit.catname='REG STORE'  or visit.catname='REGISTRATION'  or visit.catname='ROOM 1 to 4'  or visit.catname='ROOM A1 & A2'  or visit.catname='ROOM A3 & A4'  or visit.catname='ROOM A5-A7&A9-A11'  or visit.catname='ROOM A8-BMD'  or visit.catname='ROOM B20'  or visit.catname='ROOM B21 B23 B24'  or visit.catname='Ward Cases'    )  and  (visit.btnname='BY PASS'  or 开发者_JAVA技巧visit.btnname='REG STORE'  or visit.btnname='REGISTRATION'  or visit.btnname='ROOM 1 to 4'  or visit.btnname='ROOM A1 & A2'  or visit.btnname='ROOM A3 & A4'  or visit.btnname='ROOM A5-A7&A9-A11'  or visit.btnname='ROOM A8-BMD'  or visit.btnname='ROOM B20'  or visit.btnname='ROOM B21'  or visit.btnname='B23'  or visit.btnname='B24'  or visit.btnname='Ward Cases'  )   and (    (Day(visit.tmstamp)=1 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)      OR (DAY(visit.tmstamp)=2 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=3 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=4 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=5 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=6 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=7 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=8 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=9 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=10 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=11 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=12 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=13 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=14 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=15 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=16 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=17 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=18 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=19 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=20 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=21 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=22 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=23 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=24 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=25 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=26 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=27 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=28 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=29 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=30 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)       OR (DAY(visit.tmstamp)=31 AND MONTH(visit.tmstamp)=12 AND YEAR(visit.tmstamp)=2010)   ) 
and  
((loginname='Administrator')  or (loginname='121 JUDY YAP CHEO T')  or (loginname='044 Noor Faiza')  or (loginname='109 Jaya'))  
and 
(CONVERT(VARCHAR(10), visit.tmstamp, 105)=CONVERT(VARCHAR(10), event.tmstamp, 105)) 
and 
(visit.loginid=event.loginid) 

When I queried the above code, I get results twice.

If there is one one row for administrator with queueno 1005, I got twice.

Any idea?


If there is only one visit row with queueno 1005, then there must be 2 event rows in which loginid matches the loginid of the visit row.


How to shorten it?

and visit.wstation in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 15, 20, 21, 23, 24, 28, 29, 30, 31, 32, 33)
and visit.tmstamp between '20110101' and  '20101201' 

Similar things can be done with other criteria.

One good way to avoid a travesty like this is don't allow poorly written tools to generate your SQL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜