1001 2 2009-9-1 12:22:21
1002 3 2009-9-1 12:23:22
1001 4 2009-9-1 13:22:22
1002 2 2009-9-1 13:23:23
1001 2 2009-9-2 12:22:21
1002 3 2009-9-2 12:23:22
1001 4 2009-9-2 13:22:22
1002 2 2009-9-2 13:23:23
---------------------------------------------------
要变成 如下:
员工号 数量 日期
1001 6 2009-9-1
1002 5 2009-9-1
1001 6 2009-9-2
1002 5 2009-9-2
怎么写sql
5 个解决方案
#1
select 员工号,sum(数量) as 数量,convert(varchar(10),日期,120) as 日期
from tb
group by 员工号,convert(varchar(10),日期,120)
#2
--
-- create table ta (empyeeno varchar(10),qty int,date datetime)
-- insert ta select '1001','2','2009/9/1' union all
-- select '1002','3','2009/9/1' union all
-- select '1001','4','2009/9/1' union all
-- select '1002','2','2009/9/1' union all
-- select '1001','2','2009/9/2' union all
-- select '1002','3','2009/9/2' union all
-- select '1001','4','2009/9/2' union all
-- select '1002','2','2009/9/2'
select * from ta
select empyeeno,sum(qty),date from ta group by empyeeno,date
#3
select 员工号,sum(数量) as 数量,convert(varchar(10),日期,120) as 日期
from tb
group by 员工号,日期
#4
厉害!!!!!!
#5
/*
员工号 数量 日期
1001 2 2009-9-1 12:22:21
1002 3 2009-9-1 12:23:22
1001 4 2009-9-1 13:22:22
1002 2 2009-9-1 13:23:23
1001 2 2009-9-2 12:22:21
1002 3 2009-9-2 12:23:22
1001 4 2009-9-2 13:22:22
1002 2 2009-9-2 13:23:23
---------------------------------------------------
要变成 如下:
员工号 数量 日期
1001 6 2009-9-1
1002 5 2009-9-1
1001 6 2009-9-2
1002 5 2009-9-2
怎么写sql
*/
go
if object_id('tbl')is not null
drop table tbl
go
create table tbl(
员工号 varchar(5),
数量 int,
日期 datetime
)
go
insert tbl
select '1001',2,'2009-9-1 12:22:21' union all
select '1002',3,'2009-9-1 12:23:22' union all
select '1001',4,'2009-9-1 13:22:22' union all
select '1002',2,'2009-9-1 13:23:23' union all
select '1001',2,'2009-9-2 12:22:21' union all
select '1002',3,'2009-9-2 12:23:22' union all
select '1001',4,'2009-9-2 13:22:22' union all
select '1002',2,'2009-9-2 13:23:23'
select 员工号,SUM(数量)as 数量,convert(date,日期,120) as 日期
from tbl
group by 员工号,convert(date,日期,120)
/*
员工号 数量 日期
1001 6 2009-09-01
1002 5 2009-09-01
1001 6 2009-09-02
1002 5 2009-09-02
*/
员工号 数量 日期
1001 2 2009-9-1 12:22:21
1002 3 2009-9-1 12:23:22
1001 4 2009-9-1 13:22:22
1002 2 2009-9-1 13:23:23
1001 2 2009-9-2 12:22:21
1002 3 2009-9-2 12:23:22
1001 4 2009-9-2 13:22:22
1002 2 2009-9-2 13:23:23
---------------------------------------------------
要变成 如下:
员工号 数量 日期
1001 6 2009-9-1
1002 5 2009-9-1
1001 6 2009-9-2
1002 5 2009-9-2
怎么写sql
*/
go
if object_id('tbl')is not null
drop table tbl
go
create table tbl(
员工号 varchar(5),
数量 int,
日期 datetime
)
go
insert tbl
select '1001',2,'2009-9-1 12:22:21' union all
select '1002',3,'2009-9-1 12:23:22' union all
select '1001',4,'2009-9-1 13:22:22' union all
select '1002',2,'2009-9-1 13:23:23' union all
select '1001',2,'2009-9-2 12:22:21' union all
select '1002',3,'2009-9-2 12:23:22' union all
select '1001',4,'2009-9-2 13:22:22' union all
select '1002',2,'2009-9-2 13:23:23'
select 员工号,SUM(数量)as 数量,convert(date,日期,120) as 日期
from tbl
group by 员工号,convert(date,日期,120)
/*
员工号 数量 日期
1001 6 2009-09-01
1002 5 2009-09-01
1001 6 2009-09-02
1002 5 2009-09-02
*/
#1
select 员工号,sum(数量) as 数量,convert(varchar(10),日期,120) as 日期
from tb
group by 员工号,convert(varchar(10),日期,120)
#2
--
-- create table ta (empyeeno varchar(10),qty int,date datetime)
-- insert ta select '1001','2','2009/9/1' union all
-- select '1002','3','2009/9/1' union all
-- select '1001','4','2009/9/1' union all
-- select '1002','2','2009/9/1' union all
-- select '1001','2','2009/9/2' union all
-- select '1002','3','2009/9/2' union all
-- select '1001','4','2009/9/2' union all
-- select '1002','2','2009/9/2'
select * from ta
select empyeeno,sum(qty),date from ta group by empyeeno,date
#3
select 员工号,sum(数量) as 数量,convert(varchar(10),日期,120) as 日期
from tb
group by 员工号,日期
#4
厉害!!!!!!
#5
/*
员工号 数量 日期
1001 2 2009-9-1 12:22:21
1002 3 2009-9-1 12:23:22
1001 4 2009-9-1 13:22:22
1002 2 2009-9-1 13:23:23
1001 2 2009-9-2 12:22:21
1002 3 2009-9-2 12:23:22
1001 4 2009-9-2 13:22:22
1002 2 2009-9-2 13:23:23
---------------------------------------------------
要变成 如下:
员工号 数量 日期
1001 6 2009-9-1
1002 5 2009-9-1
1001 6 2009-9-2
1002 5 2009-9-2
怎么写sql
*/
go
if object_id('tbl')is not null
drop table tbl
go
create table tbl(
员工号 varchar(5),
数量 int,
日期 datetime
)
go
insert tbl
select '1001',2,'2009-9-1 12:22:21' union all
select '1002',3,'2009-9-1 12:23:22' union all
select '1001',4,'2009-9-1 13:22:22' union all
select '1002',2,'2009-9-1 13:23:23' union all
select '1001',2,'2009-9-2 12:22:21' union all
select '1002',3,'2009-9-2 12:23:22' union all
select '1001',4,'2009-9-2 13:22:22' union all
select '1002',2,'2009-9-2 13:23:23'
select 员工号,SUM(数量)as 数量,convert(date,日期,120) as 日期
from tbl
group by 员工号,convert(date,日期,120)
/*
员工号 数量 日期
1001 6 2009-09-01
1002 5 2009-09-01
1001 6 2009-09-02
1002 5 2009-09-02
*/
员工号 数量 日期
1001 2 2009-9-1 12:22:21
1002 3 2009-9-1 12:23:22
1001 4 2009-9-1 13:22:22
1002 2 2009-9-1 13:23:23
1001 2 2009-9-2 12:22:21
1002 3 2009-9-2 12:23:22
1001 4 2009-9-2 13:22:22
1002 2 2009-9-2 13:23:23
---------------------------------------------------
要变成 如下:
员工号 数量 日期
1001 6 2009-9-1
1002 5 2009-9-1
1001 6 2009-9-2
1002 5 2009-9-2
怎么写sql
*/
go
if object_id('tbl')is not null
drop table tbl
go
create table tbl(
员工号 varchar(5),
数量 int,
日期 datetime
)
go
insert tbl
select '1001',2,'2009-9-1 12:22:21' union all
select '1002',3,'2009-9-1 12:23:22' union all
select '1001',4,'2009-9-1 13:22:22' union all
select '1002',2,'2009-9-1 13:23:23' union all
select '1001',2,'2009-9-2 12:22:21' union all
select '1002',3,'2009-9-2 12:23:22' union all
select '1001',4,'2009-9-2 13:22:22' union all
select '1002',2,'2009-9-2 13:23:23'
select 员工号,SUM(数量)as 数量,convert(date,日期,120) as 日期
from tbl
group by 员工号,convert(date,日期,120)
/*
员工号 数量 日期
1001 6 2009-09-01
1002 5 2009-09-01
1001 6 2009-09-02
1002 5 2009-09-02
*/