我是SSRS的新手。我想按customerid对事务表进行分组,并计算每个customerid的事务数。我能够做到这一点。
但后来我想根据那个数进行排序,和/或按该计数过滤。那你怎么样?
谢谢!
答案 0 :(得分:2)
要设置对行组的排序和过滤,请右键单击行组。
您可以在此处访问“组排序和过滤”属性。它们都应该允许您根据计数列的名称设置规则。
答案 1 :(得分:0)
如果您不需要在报告中显示事务,则应在查询中的数据库级别执行聚合,而不是通过SSRS执行聚合。你会得到以下好处:
ORDER BY
子句中来“预订”您的数据集。
HAVING
子句中使用
-- Will filter out any customers who have 2 or fewer transactions
DECLARE @Filter AS int = 2
;
SELECT
CustomerId
,COUNT(TransactionId)
FROM
Transactions
GROUP BY
CustomerId
HAVING
COUNT(TransactionId) > @Filter
如果您仍需要显示交易,请在查询中添加其他列,使用Count()
子句和OVER
执行PARTITION BY customerid
,如下所示:
COUNT(transactions) OVER (PARTITION BY customerid) AS CustomerTransactionCount
假设一个非常简单的表结构,你最终会得到一个像这样的查询结构:
SELECT
CustomerId
,TransactionId
,TransactionAttribute_1
,TransactionAttribute_2
,TransactionAttribute_3
.
.
.
,TransactionAttribute_n
,COUNT(TransactionId) OVER (PARTITION BY CustomerId) AS CustomerTransactionCount
FROM
Transactions
您可以在SSRS中的任何行/列组中使用CustomerTransactionCount
作为过滤器和排序列。
OVER (PARTITION BY...)
不能在HAVING
子句中使用,因为不使用GROUP BY
子句。这意味着任何过滤都必须由SSRS执行。--Filter variable
DECLARE @Filter AS int = 2
;
WITH DataSet_CTE AS
(
-- Build the data set with transaction information and the aggregate column
SELECT
CustomerId
,TransactionId
,TransactionAttribute_1
,TransactionAttribute_2
,TransactionAttribute_3
.
.
.
,TransactionAttribute_n
,COUNT(TransactionId) OVER (PARTITION BY CustomerId) AS CustomerTransationCount
FROM
Transactions
)
-- Filter and return data
SELECT *
FROM DataSet_CTE
WHERE CustomerTransationCount > @Filter
--Filter variable
DECLARE @Filter AS int = 2
;
SELECT
*
FROM
(
-- Build the data set with transaction information and the aggregate column
SELECT
CustomerId
,TransactionId
,TransactionAttribute_1
,TransactionAttribute_2
,TransactionAttribute_3
.
.
.
,TransactionAttribute_n
,COUNT(TransactionId) OVER (PARTITION BY CustomerId) AS CustomerTransationCount
FROM
Transactions
) AS DataSet
WHERE
DataSet.CustomerTransationCount > @Filter