2012-04-20 118 views
0

我使用RFID技术在VS2010中制作考勤管理系统,在SQL Server 2005中执行所有后端数据操作,我知道基本的SQL,但我是这个问题需要一些帮助。将数据插入另一个表的表中,同时根据字段排序

我有以下表格:

facultymasterdata

fname 
fid 
dept 
title 
phone 
dob 

定时

fname 
fid 
intime_a 
outtime_d 
lunchout_b 
lunchin_c 

这里rawdumps

fid 
timecode 
currtime 

数据从一个串行端口数据记录器亲保存我在vb.net上写过,fid是RFID标签ID,时间码是A,B,C或D(用于标识时间为intime-A,lunchout-B,lunchin-C,outtime-D),currtime是当前的系统时间。我通过一个微控制器获取RFID标签ID,该微控制器根据物理按钮按下来添加时间码。

我需要做的是根据时间码对rawdumps中的所有数据进行排序并将其复制到timings中。时间代码为“A”的fidcurrtime字段在时间上被保存到intime_a字段中。以及对应的的fname

任何帮助的信息将不胜感激。

非常感谢。

+0

它是没有意义的,你要插入的行进行排序。一旦它们在目标表中,它们就不会被排序,因为关系数据库中的行不是**排序的。当你从目标表中检索它们时,你将不得不对它们进行排序。 – 2012-04-20 08:08:13

+0

a_horse_with_no_name,我的最终数据是表格定时,master数据表中的fname,rawdumps中的fid以及该人员的时间(从rawdata中排序)。 想想这样,只有一个人记录了他的时间,并且在原始数据中,我有4行,有4个不同的时间,分别具有时间码A,B,C和D.现在我想按时间排序将4行中的数据放入表格中,以便A旁边的时间位于intime_a字段之下,依此类推。 – abs1337 2012-04-20 08:45:46

回答

0

我不完全确定你的意思是[fid]和[curtime]字段的代码是“A”,并保存在[intime_a]中。这是一笔钱吗?

这应该让你走上正轨。

INSERT INTO timings(fname,fid,intime_a) 
    SELECT a.fname, b.fid, b.curtime 
    FROM facultymasterdata a, rawdumps b 
    WHERE a.fid = b.fid 
    ORDER BY b.timecode; 

编辑:读你的问题和评论的反应编辑后,我会写一个触发器来处理此插入。

+0

时间码是一个字段,据此我需要对数据进行排序,该字段中的数据是A,B,C或D. 如果它的A,我需要将currtime格式的rawdata放入字段intime_a in表的时间。同样,如果它的B,那么currtime应该放在午餐时间下午的场地。 – abs1337 2012-04-20 08:06:38

+1

列名不应包含在单引号中。单引号表示字符文字 – 2012-04-20 08:08:31

+0

你是对的a_horse_with_no_name。我补充'而不是'所以编辑的帖子:)谢谢你的提示 – GuZzie 2012-04-20 08:19:18

0

我可能会利用工会的选择,造成了很大的讨厌插入这样

insert into timings 
(fname,fid,intime_a,lunchout_b,lunchin_c, outtime_d) 
select 
    f.fname, 
    f.fid, 
    r.intime_a, 
    r.lunchout_b, 
    r.lunchin_c, 
    r.outtime_d, 
from 
    facultymasterdata as f 
inner join (
    select 
     fid as fid, 
     currtime as currtime, 
     currtime as intime_a, 
     null as lunchout_b, 
     null as lunchin_c, 
     null as outtime_d, 
    from 
     rawdumps 
    where timecode = 'A' 
    union select 
     fid as fid, 
     currtime as currtime, 
     null as intime_a, 
     currtime as lunchout_b, 
     null as lunchin_c, 
     null as outtime_d, 
    from 
     rawdumps 
    where timecode = 'B' 
    union select 
     fid as fid, 
     currtime as currtime, 
     null as intime_a, 
     null as lunchout_b, 
     currtime as lunchin_c, 
     null as outtime_d, 
    from 
     rawdumps 
    where timecode = 'C' 
    union select 
     fid as fid, 
     currtime as currtime, 
     null as intime_a, 
     null as lunchout_b, 
     null as lunchin_c, 
     currtime as outtime_d, 
    from 
     rawdumps 
    where timecode = 'D' 
    ) r on r.fid = f.fid 
order by r.currtime 
0

我得到了解决,有人帮了我,我需要做的是转换成列行排序。我需要使用交叉标签。

这是我需要做的:

SELECT F.fName, F.fID, 
MIN(CASE WHEN R.TimeCode = 'A' THEN CurrTime END) AS Intime_a, 
MAX(CASE WHEN R.TimeCode = 'D' THEN CurrTime END) AS outtime_d, 
MIN(CASE WHEN R.TimeCode = 'B' THEN CurrTime END) AS LunchOut_b, 
MAX(CASE WHEN R.TimeCode = 'C' THEN CurrTime END) AS LunchIn_c 
FROM dbo.facultymasterdata F LEFT JOIN dbo.rawdumps R ON F.fid = R.fid 
GROUP BY F.fid, F.fname 
相关问题