客户费用查询

发布于 2024-10-30 10:02:59 字数 98 浏览 1 评论 0原文

需要构建一个查询,显示客户每周、每月、每季度、6 个月和一年在每日在线游戏上花费了多少钱。当一位客户进入游戏时,有一个字段创建日期(日期时间)字段。

有人可以帮忙吗?

Need to build a query showing how much customers spend money per week, month, quarter, 6 months and a year on a daily on-line game. There is a field create date(datetime) field when one customer enters the game.

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油焖大侠 2024-11-06 10:02:59

也许是这样的。

declare @T table (CreatedDate datetime, MoneySpent int)

insert into @T values
('20101231', 5),
('20100101', 10),
('20100102', 20),
('20100103', 30),
('20110104', 40),
('20110105', 50),
('20110106', 60),
('20110107', 70)

-- per year
select
  sum(MoneySpent) Spent,
  year(CreatedDate) [Year]
from @T
group by year(CreatedDate)

-- per month
select
  sum(MoneySpent) Spent,
  year(CreatedDate) [Year],
  month(CreatedDate) [Month]
from @T
group by year(CreatedDate), month(CreatedDate)

-- per half year
select
  sum(MoneySpent) Spent,
  year(CreatedDate) [Year],
  case when month(CreatedDate) <= 6 then 'First' else 'Second' end [HalfYear]
from @T
group by year(CreatedDate), case when month(CreatedDate) <= 6 then 'First' else 'Second' end

-- per quarter
select
  sum(MoneySpent) Spent,
  year(CreatedDate) [Year],
  ((month(CreatedDate)-1) % 4)+1 [Quarter]
from @T
group by year(CreatedDate), ((month(CreatedDate)-1) % 4)+1

-- per week
select
  sum(MoneySpent) Spent,
  year(CreatedDate) [Year],
  datepart(iso_week, CreatedDate) [Week]
from @T
group by year(CreatedDate), datepart(iso_week, CreatedDate)

结果

Spent       Year
----------- -----------
65          2010
220         2011

Spent       Year        Month
----------- ----------- -----------
60          2010        1
220         2011        1
5           2010        12

Spent       Year        HalfYear
----------- ----------- --------
60          2010        First
220         2011        First
5           2010        Second

Spent       Year        Quarter
----------- ----------- -----------
60          2010        1
220         2011        1
5           2010        4

Spent       Year        Week
----------- ----------- -----------
220         2011        1
5           2010        52
60          2010        53

Perhaps something like this.

declare @T table (CreatedDate datetime, MoneySpent int)

insert into @T values
('20101231', 5),
('20100101', 10),
('20100102', 20),
('20100103', 30),
('20110104', 40),
('20110105', 50),
('20110106', 60),
('20110107', 70)

-- per year
select
  sum(MoneySpent) Spent,
  year(CreatedDate) [Year]
from @T
group by year(CreatedDate)

-- per month
select
  sum(MoneySpent) Spent,
  year(CreatedDate) [Year],
  month(CreatedDate) [Month]
from @T
group by year(CreatedDate), month(CreatedDate)

-- per half year
select
  sum(MoneySpent) Spent,
  year(CreatedDate) [Year],
  case when month(CreatedDate) <= 6 then 'First' else 'Second' end [HalfYear]
from @T
group by year(CreatedDate), case when month(CreatedDate) <= 6 then 'First' else 'Second' end

-- per quarter
select
  sum(MoneySpent) Spent,
  year(CreatedDate) [Year],
  ((month(CreatedDate)-1) % 4)+1 [Quarter]
from @T
group by year(CreatedDate), ((month(CreatedDate)-1) % 4)+1

-- per week
select
  sum(MoneySpent) Spent,
  year(CreatedDate) [Year],
  datepart(iso_week, CreatedDate) [Week]
from @T
group by year(CreatedDate), datepart(iso_week, CreatedDate)

Result

Spent       Year
----------- -----------
65          2010
220         2011

Spent       Year        Month
----------- ----------- -----------
60          2010        1
220         2011        1
5           2010        12

Spent       Year        HalfYear
----------- ----------- --------
60          2010        First
220         2011        First
5           2010        Second

Spent       Year        Quarter
----------- ----------- -----------
60          2010        1
220         2011        1
5           2010        4

Spent       Year        Week
----------- ----------- -----------
220         2011        1
5           2010        52
60          2010        53
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文