2011-11-18 28 views
0
select convert(nvarchar(25),in_gentime, 107) AS LogInDate, 
    convert(nvarchar(25),in_gentime, 108) as LogInTime, 
    convert(nvarchar(25),out_gentime, 107) AS LogOutDate, 
    convert(nvarchar(25),out_gentime, 108) as LogOutTime 
    from in_time_temp i, out_time_temp o where in_gentime between '10/01/2011 00:01' and '11/18/2011 23:59' 
    and i.cardnumber = 'MCL1570' and out_gentime between '10/01/2011 00:01' and '11/18/2011 23:59' and o.cardnumber = 'MCL1570' 
    and month(in_gentime) = month(out_gentime) and day(in_gentime) = day(out_gentime) 
    and year(in_gentime) = year(out_gentime) order by year(in_gentime), year(out_gentime), 
    month(in_gentime), month(out_gentime), day(in_gentime), day(out_gentime) 

輸出這個查詢將返回這樣的:修改查詢以獲得這樣

Login Date  LoginTime   Logout date  logouttime 
Oct 11, 2011 08:06:00 Oct 11, 2011 22:02:00 
Oct 12, 2011 08:35:00 Oct 12, 2011 21:14:00 
Oct 14, 2011 08:21:00 Oct 14, 2011 21:59:00 
Oct 15, 2011 08:21:00 Oct 15, 2011 21:59:00 

假設如果該記錄是有in_time_temp和記錄是不是有out_time_temp表意味着它應該表現出像輸出在這個時間的情況下登錄這個....

是有二零一一年十月十三日在此in_time_temp但登錄退房時間尚未在此out_time_temp

如何修改脫身像這樣:

Login Date  LoginTime   Logout date  logouttime 
    Oct 11, 2011 08:06:00 Oct 11, 2011 22:02:00 
    Oct 12, 2011 08:35:00 Oct 12, 2011 21:14:00 
    Oct 13, 2011 08:21:00 
    Oct 14, 2011 08:21:00 Oct 14, 2011 21:59:00 
    Oct 15, 2011 08:21:00 Oct 15, 2011 21:59:00 

如何修改我的查詢?

+0

SQL服務器真的不打算做很多格式化 - 你應該在前端(應用程序,報告工具等)做到這一點 –

+0

如何在前端做我不做記錄或插入或不是 – Nathan

+0

報告工具像SQL Server Reporting Services或Crystal Reports通常具有特定的選項來抑制*重複值... –

回答

0

這將是最好的,如果你給我們提供兩種in_time_tempout_time_temp,使我們的工作更加高效的表定義。然而,在等待的時候,這裏是另一個嘗試。

以下是我的你的表的最低要求的假設:

CREATE TABLE in_time_temp (
     cardnumber VARCHAR(10) 
    , in_gentime DATETIME 
) 

CREATE TABLE out_time_temp (
     cardnumber VARCHAR(10) 
    , out_gentime DATETIME 
) 

這裏是代碼,應該給你你需要的答案。

SELECT 
    i.cardnumber 
, CONVERT(nvarchar(25),in_gentime, 107) AS LogInDate 
, CONVERT(nvarchar(25),in_gentime, 108) AS LogInTime 
, CONVERT(nvarchar(25),out_gentime, 107) AS LogOutDate 
, CONVERT(nvarchar(25),out_gentime, 108) AS LogOutTime 
FROM in_time_temp i LEFT OUTER JOIN out_time_temp o 
    ON i.cardnumber = o.cardnumber 
    AND CONVERT(VARCHAR, in_gentime, 112) = CONVERT(VARCHAR, out_gentime, 112) 
WHERE 
    i.cardnumber = 'MCL1570' 
ORDER BY 1, 2 

我儘可能簡化了您的查詢..請試試這個,讓我們知道它是怎麼回事。

[更多]: 使用monthdayyear函數時(根據您最初的SQL代碼),你只在out_time_temp表中的一個日匹配本身(這意味着登錄和註銷必須而不是發生在同一天),最好採取整個日期(只是日期,而不是時間),並將其用作過濾器。請參閱我從WHERE子句中刪除了日期匹配,並將其包含在LEFT OUTER JOIN部分中,因爲它也是決定JOIN如何工作的關鍵之一。同樣,我在最後(ORDER BY條款)做了一個快捷方式,但如果它不按照您想要的方式排序,請告訴我如何。

+0

ya在這個查詢中我沒有得到記錄,這是在in_time_temp表中的記錄.13 ..這裏的卡號是常見的對於卡號是員工號碼 – Nathan

+0

@Nathan ::對不起,我錯過了'o.cardnumber ='MCL1570''條件仍在WHERE條款中。你可以再試一次嗎? – Nonym

+0

嗯我沒有得到10月13,2011記錄仍然 – Nathan