【发布时间】:2015-07-07 17:46:58
【问题描述】:
这是我正在阅读的表格:
sk_calendar_week | ClientId | Amount
------------------+-----------+-------
2014001 | 1 | 550
2014002 | 2 | 900
2014003 | 3 | 389
2014004 | 4 | 300
这是我正在使用的查询:
declare @IniDate as int = 20140610, @EndDate as int = 20150425
select
COUNT(distinct sk_calendar_week) WeekQ,
COUNT(distinct sk_calendar_Month) MonthQ
from
(select
sk_date, sk_calendar_week, sk_calendar_Month,
ClientId, Amount
from
TableA
where
Sk_Date between @IniDate and @EndDate) q1
此查询返回:
WeekQ | MonthQ
------+-------
4 | 1
如何将WeekQ 中的 4 除以金额(550/4;900/4;389/4...),以获得这样的结果?
sk_calendar_week | ClientId | Amount | Division
-------------------+----------+--------+---------
2014001 | 1 | 550 | 137.5
2014002 | 2 | 900 | 225
2014003 | 3 | 389 | 97.25
2014004 | 4 | 300 | 75
【问题讨论】:
-
sk_date来自哪里?它是否也存储为int? -
@MartinSmith 是的,错字,我已经修复了
-
@APH 是的,sk_date 来自同一张表,是一个int。
标签: sql-server count subquery division