我现在又一张表,
----订单表
create table Orders
(
O_ID int primary key identity(1,1) not null,--订单ID
Userid int null, --推广用户编号(联盟ID)
O_UserName nvarchar(50) null, --购买订单人用户名(BKW用户)
O_Name nvarchar(50) null, --所购订单物品(课程)名
O_AllMoney money null, --实付总金额
O_Time datetime default getdate(), --购课时间
RegTime datetime null
)
insert into Orders values(10000001,'huangqianjob','证券市场基础知识','688.0000','2010-7-24 10:25:45','2010-7-20 7:25:45')
我现在需要一条满足下列条件是:查找推荐ID是10000001且是最近6个月的推荐的购买了课程的用户按月消费的每个月的总值的查询语句: 讲条件拆分来讲就是:
1、查找用户是10000001推荐的用户购买课程的消费记录
2、查找出的记录是最近6个月的记录(最近的几个月份不定,是带的参数)
3、查找出的记录是按月进行分组统计的,即查找的是每月各个用户推荐的总和
4,要根据年月进行降序排列
我现在写的语句是如下:
select month(O_Time) as everyMonth,sum(O_AllMoney) as sumMoney from
(select O_AllMoney,o_id,O_Time from Orders where Userid=10000001 and DateDiff(m,O_Time,getdate())<7) as temp_table group by month(O_Time) order by month(O_Time) desc显示的信息是:
everyMonth sumMoney
8 2442.00
7 688.00
5 688.00
6月份没有数据,所以没显示,,这个语句有个问题,我想如果查找时有是2009年的12份和2010年的月份的话,肯定2009年的12月份排在前面去了,另外就是如果查找的月份超过12个月,这个语句就没用了。而我另外的一个语句是:
select month(O_Time),convert(varchar(7),O_Time,120) as everyMonth,sum(O_AllMoney) as sumMoney from
(select O_AllMoney,o_id,O_Time from Orders where Userid=10000001 and DateDiff(m,O_Time,getdate())<7) as temp_table group by month(O_Time),O_Time order by month(O_Time) desc
显示出来的是:
8 2010-08 1376.00
8 2010-08 688.00
8 2010-08 0.00
8 2010-08 0.00
8 2010-08 378.00
7 2010-07 688.00
5 2010-05 688.00
这个也不正确,各位大侠们可不可以给个正确的SQL 查询语句。
正确的显示效果应该是(要注意两个年份之间的月份排行,越近的月份越靠前):
2010-8 2442.00
2010-7 688.00
2010-5 688.00
----订单表
create table Orders
(
O_ID int primary key identity(1,1) not null,--订单ID
Userid int null, --推广用户编号(联盟ID)
O_UserName nvarchar(50) null, --购买订单人用户名(BKW用户)
O_Name nvarchar(50) null, --所购订单物品(课程)名
O_AllMoney money null, --实付总金额
O_Time datetime default getdate(), --购课时间
RegTime datetime null
)
insert into Orders values(10000001,'huangqianjob','证券市场基础知识','688.0000','2010-7-24 10:25:45','2010-7-20 7:25:45')
我现在需要一条满足下列条件是:查找推荐ID是10000001且是最近6个月的推荐的购买了课程的用户按月消费的每个月的总值的查询语句: 讲条件拆分来讲就是:
1、查找用户是10000001推荐的用户购买课程的消费记录
2、查找出的记录是最近6个月的记录(最近的几个月份不定,是带的参数)
3、查找出的记录是按月进行分组统计的,即查找的是每月各个用户推荐的总和
4,要根据年月进行降序排列
我现在写的语句是如下:
select month(O_Time) as everyMonth,sum(O_AllMoney) as sumMoney from
(select O_AllMoney,o_id,O_Time from Orders where Userid=10000001 and DateDiff(m,O_Time,getdate())<7) as temp_table group by month(O_Time) order by month(O_Time) desc显示的信息是:
everyMonth sumMoney
8 2442.00
7 688.00
5 688.00
6月份没有数据,所以没显示,,这个语句有个问题,我想如果查找时有是2009年的12份和2010年的月份的话,肯定2009年的12月份排在前面去了,另外就是如果查找的月份超过12个月,这个语句就没用了。而我另外的一个语句是:
select month(O_Time),convert(varchar(7),O_Time,120) as everyMonth,sum(O_AllMoney) as sumMoney from
(select O_AllMoney,o_id,O_Time from Orders where Userid=10000001 and DateDiff(m,O_Time,getdate())<7) as temp_table group by month(O_Time),O_Time order by month(O_Time) desc
显示出来的是:
8 2010-08 1376.00
8 2010-08 688.00
8 2010-08 0.00
8 2010-08 0.00
8 2010-08 378.00
7 2010-07 688.00
5 2010-05 688.00
这个也不正确,各位大侠们可不可以给个正确的SQL 查询语句。
正确的显示效果应该是(要注意两个年份之间的月份排行,越近的月份越靠前):
2010-8 2442.00
2010-7 688.00
2010-5 688.00
select
convert(varchar(7),O_Time,120) as everyMonth,
sum(O_AllMoney) as sumMoney
from
(select O_AllMoney,o_id,O_Time
from Orders
where Userid=10000001
and DateDiff(m,O_Time,getdate())<7
) as temp_table
group by convert(varchar(7),O_Time,120)
order by convert(varchar(7),O_Time,120) desc
2010-8 2442.00
2010-7 688.00
2010-5 688.00就分组汇总啊,修改 了下,注意GROUP BY select month(O_Time),convert(varchar(7),O_Time,120) as everyMonth,sum(O_AllMoney) as sumMoney from
(select O_AllMoney,o_id,O_Time from Orders where Userid=10000001 and DateDiff(m,O_Time,getdate())<7) as temp_table group by month(O_Time),convert(varchar(7),O_Time,120) order by month(O_Time) desc
FROM
(SELECT O_AllMoney, O_ID, O_Time
FROM Orders
WHERE (Userid = 10000001) AND (DATEDIFF(m, O_Time, GETDATE()) < 7)
) AS temp_table
GROUP BY O_Time
ORDER BY O_Time DESC
convert(varchar(7),O_Time,120) as everyMonth,
sum(O_AllMoney) as sumMoney
from
(select O_AllMoney,o_id,O_Time
from Orders
where Userid=10000001
and DateDiff(m,O_Time,getdate())<7
) as temp_table
group by convert(varchar(7),O_Time,120)
order by convert(varchar(7),O_Time,120) desc
(select O_AllMoney,o_id,O_Time from Orders where Userid=10000001 and DateDiff(m,O_Time,getdate())<7) as temp_table
group by month(O_Time),year(O_Time) order by everyMonth desc
select str(year(O_Time),4,0) +'年'+ str(month(O_Time),2,0)+'月' as everyMonth,sum(O_AllMoney) as sumMoney from
(select O_AllMoney,o_id,O_Time from Orders where Userid=10000001 and DateDiff(m,O_Time,getdate())<7) as temp_table
group by month(O_Time),year(O_Time) order by everyMonth desc
这个也是行,不过比你们两个的那个貌似麻烦点,我这个显示的是:
2010年 9月 756.00
2010年 8月 2442.00
2010年 7月 688.00
2010年 5月 688.00
这种,你们显示的是这种:
2010-09 756.00
2010-08 2442.00
2010-07 688.00
2010-05 688.00
呵呵,搞定了,谢谢哦!!