日期:2014-05-18  浏览次数:20634 次

SQL查询语句该怎么写,请高手帮忙
表的关系:userinfo和checkinout关系如下
userinfo:
NAME USERID
刘一 1
周二 2
李三 3
徐四 4
王五 5
邹六 6
黄七 7
林八 8
易九 9
蔺十 10

checkinout:
CHECKTIME USERID
2011-11-1 8:25 1 
2011-11-1 17:33 1 
2011-11-1 8:31 2 
2011-11-1 17:42 2 
2011-11-1 8:26 4 
2011-11-1 19:50 4 
2011-11-1 8:25 5 
2011-11-1 17:32 5 
2011-11-1 8:26 7 
2011-11-1 17:32 7 

2011-11-2 13:57 3 
2011-11-2 17:48 3 
2011-11-2 8:26 4 
2011-11-2 18:51 4 
2011-11-2 8:26 5 
2011-11-2 17:30 5 
2011-11-2 8:27 6 
2011-11-2 18:34 6 
2011-11-2 8:26 7 
2011-11-2 19:49 7 
2011-11-2 8:28 8 


2011-11-3 8:27 3 
2011-11-3 18:47 3 
2011-11-3 8:10 4 
2011-11-3 17:36 4 
2011-11-3 8:28 7 
2011-11-3 17:38 7 
2011-11-3 8:24 8 
2011-11-3 17:31 8


查询结果如下:
MYDATE NAME MINTIME MAXTIME
2011-11-1 刘一 8:25 17:33
2011-11-1 周二 8:31 17:42
2011-11-1 李三 NULL NULL
2011-11-1 徐四 8:26 19:50
2011-11-1 王五 8:25 17:32
2011-11-1 邹六 NULL NULL
2011-11-1 黄七 8:26 17:32
2011-11-1 林八 NULL NULL
2011-11-1 易九 NULL NULL
2011-11-1 蔺十 NULL NULL

2011-11-2 刘一 NULL NULL
2011-11-2 周二 NULL NULL
2011-11-2 李三 13:57 17:48
2011-11-2 徐四 8:26 18:51
2011-11-2 王五 8:26 17:30
2011-11-2 邹六 8:27 18:34
2011-11-2 黄七 8:26 19:49
2011-11-2 林八 8:28 8:28
2011-11-2 易九 NULL NULL
2011-11-2 蔺十 NULL NULL

2011-11-3 刘一 NULL NULL
2011-11-3 周二 NULL NULL
2011-11-3 李三 8:27 18:47
2011-11-3 徐四 8:10 17:36
2011-11-3 王五 NULL NULL
2011-11-3 邹六 NULL NULL
2011-11-3 黄七 8:28 17:38
2011-11-3 林八 8:24 17:31
2011-11-3 易九 NULL NULL
2011-11-3 蔺十 NULL NULL

------解决方案--------------------
SQL code

select convert(varchar,a.CHECKTIME,23) 'MYDATE',
b.name,
min(CHECKTIME) 'MINTIME',
max(CHECKTIME) 'MAXTIME'
from checkinout a
inner join userinfo b
on a.USERID=b.USERID
group by convert(varchar,a.CHECKTIME,23),b.NAME

------解决方案--------------------
SQL code
----------------------------
-- Author  :fredrickhu(小F,向高手学习)
-- Date    :2011-12-07 15:17:09
-- Version:
--      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) 
--    Apr 22 2011 11:57:00 
--    Copyright (c) Microsoft Corporation
--    Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------
--> 测试数据:[userinfo]
if object_id('[userinfo]') is not null drop table [userinfo]
go 
create table [userinfo]([NAME] varchar(4),[USERID] int)
insert [userinfo]
select '刘一',1 union all
select '周二',2 union all
select '李三',3 union all
select '徐四',4 union all
select '王五',5 union all
select '邹六',6 union all
select '黄七',7 union all
select '林八',8 union all
select '易九',9 union all
select '蔺十',10
--> 测试数据:[checkinout]
if object_id('[checkinout]') is not null drop table [checkinout]
go 
create table [checkinout]([CHECKTIME] datetime,[USERID] int)
insert [checkinout]
select '2011-11-1 8:25',1 union all
select '2011-11-1 17:33',1 union all
select '2011-11-1 8:31',2 union all
select '2011-11-1 17:42',2 union all
select '2011-11-1 8:26',4 union all
select '2011-11-1 19:50',4 union all
select '2011-11-1 8:25',5 union all
select '2011-11-1 17:32',5 union all
select '2011-11-1 8:26',7 union all
select '2011-11-1 17:32',7 union all
select '2011-11-2 13:57',3 union all
select '2011-11-2 17:48',3 union all
select '2011-11-2 8:26',4 union all
select '2011-11-2 18:51',4 union all
select '2011-11-2 8:26',5 union all
select '2011-11-2 17:30',5 union all
select '2011-11-2 8:27',6 union