我需要一个支点才能做到这一点,但无法弄明白。
我有两个表accesslog
和employee
RCID EmployeeID LogDate LogTime IN/OUT
559 050098 8/18/2015 15:53:14 3
586 050098 8/19/2015 07:14:09 0
587 050098 8/19/2015 07:14:19 0
640 050098 8/19/2015 12:32:31 1
643 050098 8/19/2015 12:33:48 2
661 050098 8/19/2015 17:17:00 3
678 050098 8/20/2015 07:12:41 0
723 050098 8/20/2015 12:31:35 1
724 050098 8/20/2015 12:34:28 2
736 050098 8/20/2015 16:56:04 3
863 050098 8/25/2015 07:47:45 0
927 050098 8/25/2015 12:48:57 1
928 050098 8/25/2015 12:53:23 2
952 050098 8/25/2015 17:20:05 3
972 050098 8/26/2015 07:41:54 0
1016 050098 8/26/2015 12:14:11 1
1017 050098 8/26/2015 12:17:19 2
1051 050098 8/26/2015 17:07:16 3
1084 050098 8/27/2015 07:39:04 0
1119 050098 8/27/2015 12:20:56 1
1123 050098 8/27/2015 12:23:34 2
1153 050098 8/27/2015 17:25:57 3
1270 050098 9/1/2015 07:41:44 0
1292 050098 9/1/2015 12:14:06 1
1294 050098 9/1/2015 12:15:34 2
1324 050098 9/1/2015 17:19:00 3
1355 050098 9/3/2015 07:39:25 0
1399 050098 9/3/2015 12:17:29 1
1401 050098 9/3/2015 12:21:41 2
1419 050098 9/3/2015 17:09:23 3
1445 050098 9/4/2015 07:24:23 0
1510 050098 9/4/2015 12:58:15 1
1511 050098 9/4/2015 12:59:36 2
1536 050098 9/4/2015 17:40:01 3
1562 050098 9/7/2015 07:03:31 0
1637 050098 9/7/2015 17:34:07 3
1649 050098 9/8/2015 07:11:26 0
1709 050098 9/8/2015 17:13:09 3
1734 050098 9/9/2015 07:44:49 0
1755 050098 9/9/2015 12:09:06 1
1760 050098 9/9/2015 12:12:30 2
员工表;
employee_id firtsname lastname
050098 Alex Nomam
050057 Mandy Hampton
我可以使用Pivot这样显示此表吗?
姓名:Alex Nomam 月:2015年8月
AM PM
In Out In Out
1
2
3
4
5
6
7
8
9
10
11.
12
13
14
15
16
17
18 07:14:19
19 07:14:19 12:32:31 12:33:48 17:17:00
20
21
22
23
24
25
26
27
28
29
30
我使用php和mssql请帮助我,我是php和sql的新手
查看屏幕代表here
答案 0 :(得分:0)
<强>查询:强>
DECLARE @firstname varchar(30), @lastname varchar(30), @Month date
SET @firstname='Alex'
SET @lastname='Nomam'
SET @Month= '08/01/2015'
DECLARE @endDay int
SET @endDay=
DAY( DATEADD( MONTH, 1 + DATEDIFF(MONTH,0,
CAST(YEAR(@Month) * 10000 + MONTH(@Month) * 100 + 01 AS NVARCHAR(20))),-1))
select
[day],
[0] as [AM In],
[1] as [AM Out],
[2] as [PM In],
[3] as [PM out]
from
daysOfMonth d left join
(
select
LogDate, LogTime ,[In/Out]
from
accesslog a left join employee e
on a.EmployeeID=e.Employee_id
where
firstname=@firstname and
lastname=@lastname and
MONTH(LogDate)=MONTH(@Month) and
YEAR(LogDate)=YEAR(@Month)
) s
PIVOT
(
MAX(LogTime) for [In/Out] in ([0],[1],[2],[3])
)p
on d.[day]=DAY(Logdate)
where [day]<=@endDay
<强>解释强>
所以这实际上有两个问题。 一个是旋转和计算日期的时间,另一个是以月的所有日期的方式显示它。
为了计算月份的日期,我假设了一个表daysofmonth
,其在int列中有1-31天。
我说明您已通过@firstname
,@lastname
和@month
个参数进行查询。
DECLARE @firstname varchar(30), @lastname varchar(30), @Month date
使用@month
参数,我能够计算在显示每月某一天的最终值时在where子句中提供的月份的最后日期
SET @endDay = DAY( DATEADD(
MONTH,
1 + DATEDIFF(
MONTH,
0,
CAST(
YEAR(@Month) * 10000
+ MONTH(@Month) * 100
+ 01
AS NVARCHAR(20)
)
),
-1
)
)
枢轴计算也可以表示为
select *
from
(
select
LogDate, LogTime ,[In/Out]
from
accesslog a left join employee e
on a.EmployeeID=e.Employee_id
where
firstname=@firstname and
lastname=@lastname and
MONTH(LogDate)=MONTH(@Month) and
YEAR(LogDate)=YEAR(@Month)
) s
PIVOT
(
MAX(LogTime) for [In/Out] in ([0],[1],[2],[3])
)p
现在我们只需left join
将此pivot
输出到daysofmonth
表,并在@endDay
条件中添加where
子句以获得所需的输出。
演示SQL小提琴链接: http://sqlfiddle.com/#!3/b2d23/11