查询出来的结果按星期一,二,三。七统计,就是星期一显示星期一所有消费的和,以此类推
查询结果如下:
周数 价格1 价格2
星期一 10 20
星期二 20 20
星期三 30 20
星期四 40 20
星期五 50 20
星期六 60 20
星期日 70 20
总计 280 140或者提供点思路也行 不尽感激啊~~~!!
查询结果如下:
周数 价格1 价格2
星期一 10 20
星期二 20 20
星期三 30 20
星期四 40 20
星期五 50 20
星期六 60 20
星期日 70 20
总计 280 140或者提供点思路也行 不尽感激啊~~~!!
sum(case when 周数='星期一' then 价格1 else 0 end)+sum(case when 周数='星期一' then 价格2 else 0 end) '星期一',
sum(case when 周数='星期二' then 价格1 else 0 end)+sum(case when 周数='星期二' then 价格2 else 0 end) '星期二',
sum(case when 周数='星期二' then 价格1 else 0 end)+sum(case when 周数='星期二' then 价格2 else 0 end) '星期二',
sum(case when 周数='星期三' then 价格1 else 0 end)+sum(case when 周数='星期三' then 价格2 else 0 end) '星期三',
sum(case when 周数='星期四' then 价格1 else 0 end)+sum(case when 周数='星期四' then 价格2 else 0 end) '星期四',
sum(case when 周数='星期五' then 价格1 else 0 end)+sum(case when 周数='星期五' then 价格2 else 0 end) '星期五',
sum(case when 周数='星期六' then 价格1 else 0 end)+sum(case when 周数='星期六' then 价格2 else 0 end) '星期六',
sum(case when 周数='星期七' then 价格1 else 0 end)+sum(case when 周数='星期七' then 价格2 else 0 end) '星期七'
from tb
create table #TT
(
周数 nvarchar(20),
价格1 int,
价格2 int
)
insert into #TT select '星期一',10,20
insert into #TT select '星期二',20,20
insert into #TT select '星期三',30,20
insert into #TT select '星期四',40,20
insert into #TT select '星期五',50,20
insert into #TT select '星期六',60,20
insert into #TT select '星期七',70,20select
sum(case when 周数='星期一' then 价格1 else 0 end)+sum(case when 周数='星期一' then 价格2 else 0 end) '星期一',
sum(case when 周数='星期二' then 价格1 else 0 end)+sum(case when 周数='星期二' then 价格2 else 0 end) '星期二',
sum(case when 周数='星期二' then 价格1 else 0 end)+sum(case when 周数='星期二' then 价格2 else 0 end) '星期二',
sum(case when 周数='星期三' then 价格1 else 0 end)+sum(case when 周数='星期三' then 价格2 else 0 end) '星期三',
sum(case when 周数='星期四' then 价格1 else 0 end)+sum(case when 周数='星期四' then 价格2 else 0 end) '星期四',
sum(case when 周数='星期五' then 价格1 else 0 end)+sum(case when 周数='星期五' then 价格2 else 0 end) '星期五',
sum(case when 周数='星期六' then 价格1 else 0 end)+sum(case when 周数='星期六' then 价格2 else 0 end) '星期六',
sum(case when 周数='星期七' then 价格1 else 0 end)+sum(case when 周数='星期七' then 价格2 else 0 end) '星期七'
from #TT星期一 星期二 星期二 星期三 星期四 星期五 星期六 星期七
----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
30 40 40 50 60 70 80 90(1 行受影响)
WITH ROLLUP
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-01-06 19:23:10
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([date] datetime,[价格] int)
insert [tb]
select '2009-09-01',10 union all
select '2009-09-02',10 union all
select '2009-09-03',10 union all
select '2009-09-04',10 union all
select '2009-09-05',10 union all
select '2009-09-06',10 union all
select '2009-09-07',10 union all
select '2009-09-08',10 union all
select '2009-09-09',20 union all
select '2009-09-10',20 union all
select '2009-09-11',20 union all
select '2009-09-12',20 union all
select '2009-09-13',20 union all
select '2009-09-14',20 union all
select '2009-09-15',20 union all
select '2009-09-16',30 union all
select '2009-09-17',30 union all
select '2009-09-18',30 union all
select '2009-09-19',30 union all
select '2009-09-20',30
--------------开始查询--------------------------
select
isnull(DATENAME(WEEKDAY,date),'合计') as 周数,sum(价格) as 价格
from
[tb]
group by
DATENAME(WEEKDAY,date)
WITH ROLLUP----------------结果----------------------------
/* 周数 价格
------------------------------ -----------
星期二 40
星期六 60
星期日 60
星期三 60
星期四 60
星期五 60
星期一 30
合计 370(8 行受影响)*/
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
select isnull(datename(weekday , 时间字段),'总计') 星期几,
sum(价格1) 价格1 ,
sum(价格2) 价格2
from tb
group by datename(weekday , 时间字段)
with rollup--查某年某周的
select isnull(datename(weekday , 时间字段),'总计') 星期几,
sum(价格1) 价格1 ,
sum(价格2) 价格2
from tb
where year(时间字段) = 某年 and datepart(week , 时间字段) = 某周
group by datename(weekday , 时间字段)
with rollup