2012-04-20 114 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 
相關問題