1. 我有一張表如下:
atttime(datetime) emp(nvarchar) dept(nvarchar)
2008-08-02 6:45 zhangsan a
2008-08-02 18:20 zhangsan a
2008-08-02 11:22 zhangsan a
2008-08-02 18:20 zhangsan a
2008-08-08 7:15 zhangsan a
2008-08-08 19:12 zhangsan a
2008-08-01 6:41 ww a
2008-08-01 8:41 ww a
2008-08-01 17:20 ww a
2008-08-06 7:11 ww a
2008-08-06 18:12 ww a
2008-08-04 8:13 lisi b
2.想要得到資料如下:
即得到指定dept(a)下的所有emp(zhangsan,ww)的atttime記錄,並按每個人分組顯示,求出每個人每天的最大時間和最小時間,
沒有記錄的日期用空的補全。
attdate minatttime maxatttime emp dept
2008-08-02 6:45 18:20 zhangsan a
2008-08-03 null null zhangsan a
2008-08-04 null null zhangsan a
2008-08-05 null null zhangsan a
2008-08-06 null null zhangsan a
2008-08-07 null null zhangsan a
2008-08-08 7:15 19:12 zhangsan a
2008-08-01 6:41 17:20 ww a
2008-08-02 null null ww a
2008-08-03 null null ww a
2008-08-04 null null ww a
2008-08-05 null null ww a
2008-08-06 7:11 18:12 ww a
3.答案如下:
-->
生成測試資料
: @attrecords
create
table attrecords(atttime datetime
,emp varchar
(8),dept varchar
(1))
insert
into attrecords
select
'2008-08-02 6:45'
,'zhangsan'
,'a'
union all
select
'2008-08-02 18:20'
,'zhangsan'
,'a'
union all
select
'2008-08-02 11:22'
,'zhangsan'
,'a'
union all
select
'2008-08-02 18:20'
,'zhangsan'
,'a'
union all
select
'2008-08-08 7:15'
,'zhangsan'
,'a'
union all
select
'2008-08-08 19:12'
,'zhangsan'
,'a'
union all
select
'2008-08-01 6:41'
,'ww'
,'a'
union all
select
'2008-08-01 8:41'
,'ww'
,'a'
union all
select
'2008-08-01 17:20'
,'ww'
,'a'
union all
select
'2008-08-06 7:11'
,'ww'
,'a'
union all
select
'2008-08-06 18:12'
,'ww'
,'a'
union all
select
'2008-08-04 8:13'
,'lisi'
,'b'
--sql
查詢如下:
--在實際中
.應該在資料庫中建立乙個序號表來代替
master.dbo.spt_values
gocreate
view dbo.v_test
asselect
a.emp,a.dept,
convert
(varchar
(10),a.atttime,120)
as atttime,
convert
(varchar
(10),
min(b.atttime),108)
as minatttime,
convert
(varchar
(10),
max(b.atttime),108)
as maxatttime
from
(select a.id,b.dept,b.emp,
dateadd
(day
,a.id,b.minatttime)
as atttime
from
(select number as id from master.dbo.spt_values where
type
='p'
)as a
cross
join
(select dept,emp,
min(atttime)
as minatttime,
max(atttime)
as maxatttime
from attrecords where dept =
'a'group
by dept,emp )
as b
where
dateadd
(day
,a.id,b.minatttime)
<= b.maxatttime)
as a
left
join attrecords as b
ondatediff
(day
,b.atttime,a.atttime)
= 0 and a.emp = b.emp and a.dept = b.dept
group
by a.dept,a.emp,
convert
(varchar
(10),a.atttime,120);go
select
*from dbo.v_test order
by dept,emp,atttime;
godrop
view dbo.v_test;
drop
table attrecords;
問下這個查詢如果用linqtosql直接對attrecords查詢如何才能得到我想要的資料呢?
乙個分組時間函式查詢語句
mysql資料查詢 t user表 資料如下 login name user user birthday admin 男 1970 01 07 eee 男 1970 01 07 rrr 女 1970 01 21 test 女 1970 01 21 ttt 男 1970 01 15 www 男 197...
關於乙個sql的寫法
需求是這樣的 有個學生表 create table student id int primary key,name varchar2 200 有個成績表 create table score id int 對應與student 的 id 僅為測試,表結構也沒好好設計 math int,eng int...
乙個 關於 case when的SQL
例 從stud grade表中查詢所有同學考試成績情況,凡成績為空者輸出 未考 小於60分輸出 不及格 60分至70分輸出 及格 70分至90分輸出 良好 大於或等於90分時輸出 優秀 select stud id,name,score case when grade is null then 未考...