我是 SSRS 的新手。我想按 customerid 对交易表进行分组,并计算每个 customerid 有多少交易。我能够做到这一点。
但是我想按那个计数排序,和/或按那个计数过滤。你是怎么做到的?
谢谢!
我是 SSRS 的新手。我想按 customerid 对交易表进行分组,并计算每个 customerid 有多少交易。我能够做到这一点。
但是我想按那个计数排序,和/或按那个计数过滤。你是怎么做到的?
谢谢!
要对行组设置排序和过滤,请右键单击行组。
您可以在此处访问组排序和过滤属性。它们都应该允许您根据计数列的名称设置规则。
如果您不需要在报告中显示事务,则应在查询中的数据库级别执行聚合,而不是通过 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()
usingOVER
子句 and 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
您将能够CustomerTransactionCount
在 SSRS 中的任何行/列组中用作过滤器和排序列。
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