2011-11-18 30 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