34,590
社区成员
发帖
与我相关
我的任务
分享
select UserName , count(distinct ItemID) TotalItem , sum(Quantity) TotalQuantity , sum(UnitPrice * Quantity) TotalPrice , Date from a group by UserName , Date
-- =============================================
-- Author: T.O.P
-- Create date: 20091127
-- Version: SQL SERVER 2000
-- =============================================
declare @TB table([UserName] varchar(6),[ItemID] varchar(4),[UnitPrice] int,[Quantity] int,[TotalPrice] int,[Date] datetime)
insert @TB
select 'client','W123',20,1,20,'2007-12-5' union all
select 'client','W123',30,2,60,'2007-12-5' union all
select 'client','w18',25,1,25,'2007-12-5' union all
select 'client','w19',50,1,50,'2007-12-6' union all
select 'Bobo','w12',10,5,50,'2007-12-6' union all
select 'Bobo','W16',20,2,40,'2007-12-6'
select UserName, count(distinct ItemID) as TotalItem, sum(Quantity) as TotalQuantity, sum(TotalPrice) as TotalPrice,Date
from @TB
group by UserName, Date
order by username desc
--测试结果:
/*
UserName TotalItem TotalQuantity TotalPrice Date
-------- ----------- ------------- ----------- ------------------------------------------------------
client 2 4 105 2007-12-05 00:00:00.000
client 1 1 50 2007-12-06 00:00:00.000
Bobo 2 7 90 2007-12-06 00:00:00.000
(所影响的行数为 3 行)
*/
create table tt(username varchar(10),itemid varchar(10),unitprict int,quantity int, totalprict int,date datetime)
insert into tt select 'client','W123',20,1,20,'2007-12-5'
insert into tt select 'client','W123',30,2,60,'2007-12-5'
insert into tt select 'client','w18',25,1,25,'2007-12-5'
insert into tt select 'client','w19',50,1,50,'2007-12-6'
insert into tt select 'Bobo','w12',10,5,50,'2007-12-6'
insert into tt select 'Bobo','W16',20,2,40,'2007-12-6'
select username,count(distinct itemid)TotalItem,
sum(quantity)TotalQuantity,sum(totalprict)TotalPrice,
date from tt group by username,date
select UserName
, TotalItem=count(distinct ItemID)
, TotalQuantity=sum(Quantity)
, TotalPrice=sum(TotalPrice)
, date
from A
group by UserName, date
select UserName
, TotalItem=count(ItemID)
, TotalQuantity=sum(UnitPrice)
, TotalPrice=sum(TotalPrice)
, date
from A
group by UserName, date