将数据插入另一个表的表中,同时根据字段进行排序

时间:2012-04-20 07:46:21

标签: sql sql-server-2005

我正在使用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(用于识别时间为时间-A,午餐-B, lunchin-C,outtime-D)和currtime是当前的系统时间。我通过微控制器获取RFID标签ID,根据按下物理按钮添加时间码。

我需要做的是对来自rawdumps的所有数据进行排序,并根据时间码将其复制到timingsfid中时间码为“A”的currtimerawdumps字段会在时间中保存到字段intime_a中。以及fname对应facultymasterdata的{​​{1}}。

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

非常感谢。

3 个答案:

答案 0 :(得分: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;

编辑:在评论中阅读您对问题和回复的编辑后,我会写一个触发器来处理这个插入。

答案 1 :(得分: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

答案 2 :(得分: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