Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
prapto
Starting Member
6 Posts |
Posted - 2011-08-21 : 17:15:12
|
Somebody can help meI have code and not result querySELECT LASTNAME, FIRSTNAME, EVENTIME, DEVID, Status FROM (SELECT ROW_NUMBER() OVER (PARTITION BY EMP.LASTNAME, EMP.FIRSTNAME, CASE WHEN EVENTS.DEVID IN ("1", "2", "3", "4", "5", "6", "7") THEN "OUT" ELSE "IN" END, DATEADD(dd, DATEDIFF(dd, 0, EVENTIME), 0) ORDER BY EVENTIME DESC) AS BSeq, ROW_NUMBER() OVER (PARTITION BY EMP.LASTNAME, EMP.FIRSTNAME, CASE WHEN EVENTS.DEVID IN ("1", "2", "3", "4", "5", "6", "7") THEN "OUT" ELSE "IN" END, DATEADD(dd, DATEDIFF(dd, 0, EVENTIME), 0) ORDER BY EVENTIME ASC) AS Seq, EMP.LASTNAME, EMP.FIRSTNAME, EVENTS.EVENTIME, EVENTS.DEVID, EVENTS.SERIALNUM, CASE WHEN EVENTS.DEVID IN ("1", "2", "3", "4", "5", "6", "7") THEN "OUT" ELSE "IN" END AS Status FROM dbo.EMP AS EMP INNER JOIN dbo.EVENTS AS EVENTS ON EMP.ID = EVENTS.EMPID WHERE (EVENTS.EVENTIME >= CONVERT(DATETIME, "2011-07-01 00:00:00", 120)) AND (EVENTS.EVENTIME < CONVERT(DATETIME, "2011-08-30 00:00:00", 101)) AND CARDNUM = "3370") t WHERE (Seq=1 AND Status="IN") OR (BSeq=1 AND Status="OUT") ORDER BY LASTNAME, EVENTIMEMy problem why status "IN" not result in query?please help mesory my english very badly |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-08-21 : 17:24:55
|
Let's start by making that readable...SELECT LASTNAME , FIRSTNAME , EVENTIME , DEVID , StatusFROM ( SELECT ROW_NUMBER() OVER ( PARTITION BY EMP.LASTNAME, EMP.FIRSTNAME, CASE WHEN EVENTS.DEVID IN ( "1", "2", "3", "4", "5", "6", "7" ) THEN "OUT" ELSE "IN" END, DATEADD(dd, DATEDIFF(dd, 0, EVENTIME), 0) ORDER BY EVENTIME DESC ) AS BSeq , ROW_NUMBER() OVER ( PARTITION BY EMP.LASTNAME, EMP.FIRSTNAME, CASE WHEN EVENTS.DEVID IN ( "1", "2", "3", "4", "5", "6", "7" ) THEN "OUT" ELSE "IN" END, DATEADD(dd, DATEDIFF(dd, 0, EVENTIME), 0) ORDER BY EVENTIME ASC ) AS Seq , EMP.LASTNAME , EMP.FIRSTNAME , EVENTS.EVENTIME , EVENTS.DEVID , EVENTS.SERIALNUM , CASE WHEN EVENTS.DEVID IN ( "1", "2", "3", "4", "5", "6", "7" ) THEN "OUT" ELSE "IN" END AS Status FROM dbo.EMP AS EMP INNER JOIN dbo.EVENTS AS EVENTS ON EMP.ID = EVENTS.EMPID WHERE ( EVENTS.EVENTIME >= CONVERT(DATETIME, "2011-07-01 00:00:00", 120) ) AND ( EVENTS.EVENTIME < CONVERT(DATETIME, "2011-08-30 00:00:00", 101) ) AND CARDNUM = "3370" ) tWHERE ( Seq = 1 AND Status = "IN" ) OR ( BSeq = 1 AND Status = "OUT" )ORDER BY LASTNAME , EVENTIME Can you explain the problem more?--Gail ShawSQL Server MVP |
 |
|
prapto
Starting Member
6 Posts |
Posted - 2011-08-21 : 18:23:33
|
Sory my code can't readable clearlymore my problem is I have table from finger accesscontrol and I want to query :1. time first entry every day in 1 month for every persone2. time last entry (exit) every day in 1 month for every peronemy table is:dbo.EMP ==> ID, Firstname, Lastnamedbo.EVENTs ==> eventime (time when somebody take finger) devid => (id finger print) empid => (id from dbo.EMP)Sory GilaMonster can help meThank's |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-21 : 21:43:29
|
why do you need that convert? why not pass dates like below...WHERE ( EVENTS.EVENTIME >= '2011-07-01 00:00:00' AND EVENTS.EVENTIME < '2011-08-30 00:00:00') ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
prapto
Starting Member
6 Posts |
Posted - 2011-08-21 : 22:50:52
|
Thank Visakh16I am copy paste from "http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=139384"because my problem same. Mr. Visakh16 why status IN not result in query? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-21 : 23:29:04
|
can you show some sample data? without that we cant say why its not returning status of IN------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
prapto
Starting Member
6 Posts |
Posted - 2011-08-22 : 03:06:07
|
Mr. Visak this sample data: dbo.EVENTSEVENTIME DEVID CARDNUM EMPID2011-08-03 12:56:36.000 5 3370 72011-08-03 15:10:39.000 6 3370 72011-08-03 15:10:51.000 6 3370 72011-08-05 08:58:00.000 1 3370 72011-08-05 15:21:29.000 6 3370 72011-08-05 15:40:34.000 6 3370 72011-08-08 08:35:50.000 3 3370 72011-08-08 14:50:43.000 5 3370 72011-08-08 14:57:01.000 6 3370 72011-08-08 15:10:01.000 6 3370 72011-08-08 15:15:50.000 6 3370 72011-08-09 09:15:53.000 1 3370 72011-08-09 12:28:19.000 5 3370 72011-08-09 14:04:17.000 5 3370 7dbo.EMP:ID LASTNAME FIRSTNAME MIDLENAME1 Lake Lisa A3 Purwanto Sri NULL4 Yusuf NULL NULL6 Trimarsanto Hidayat NULL7 Sukirman NULL NULL8 Deniarsah Erman NULL9 Syafruddin Din NULL10 Setia Asih Puji Budi11 Coutrier Farah N |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-22 : 03:16:14
|
the below conditionCASE WHEN EVENTS.DEVID IN ( "1", "2", "3", "4", "5", "6", "7" ) THEN "OUT" ELSE "IN" ENDsuggests any eventid from 1 to 7 should be regarded as OUT and you've only these values (no value > 6 in your sample data) in your event table thats why all comes along as OUT and you dont get any IN events.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
prapto
Starting Member
6 Posts |
Posted - 2011-08-22 : 03:30:32
|
Mr. Visakh have any solution for me |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-22 : 04:33:27
|
using your sample data you wont get any records with IN status as you've considering eventids from 1 to 7 as type OUT and it has only data having values in this range in your EVENTS table.So unless you change your case when condition you will not get any records with IN. How to change depends on your business rules which we're not aware of and have no visibility.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
prapto
Starting Member
6 Posts |
Posted - 2011-08-22 : 11:07:21
|
OK Thank Mr. Visakh solution for me. See you other time with other question. sory very bad my english. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-22 : 12:13:55
|
thats ok. no problem------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|