这可能是一个愚蠢的问题,但SQL不是我的强项。
我有一些已经聚合的数据,我用它来显示为饼图,并根据ASPxDashBoardViewer上的参数向下钻取。我的问题是,有时数据可能会显示一些值为0%的图表项,然后无法点击。
存储过程返回的表如下所示:
DECLARE @Responses TABLE
(
Code varchar(10),
Description varchar(255),
[Status Code Count] int,
[Month] varchar(255),
[Category] varchar(255)
)
我复制了以下数据的示例。可能有多个具有相同类别和不同状态代码计数的项目,我在饼图中使用此值,状态代码计数值(Sum)以及类别,代码和描述的参数。这样可以从类别开始向下钻取饼图。 (当您深入查看某个类别时,您会看到构成该类别的所有代码/说明。)
所以我想做的是,在不改变主要过程的情况下,这是非常不稳定的,是改变选择聚合数据的最终选择语句。我想排除状态代码总和的任何内容,对于具有相同类别的所有行,如果该类别的总和状态代码计数的百分比,总计数将产生一个百分比,当转换时将舍入为零到int。
proc结束时的select语句如下所示:
SELECT
Code, Description, SUM([Status Code Count]) AS [Status Code Count],
[Month], Category
FROM @Responses
GROUP BY code, [Description], [Month], Category
数据如下所示:
+------+---------------------------------------------------------------------------------------------+-------------------+---------------+--------------------------+
| Code | Description | Status Code Count | Month | Category |
+------+---------------------------------------------------------------------------------------------+-------------------+---------------+--------------------------+
| 10 | Account in Sequestration | 1 | October, 2014 | Other |
| 12 | ACCOUNT CLOSED | 1788 | October, 2014 | Other |
| 18 | Account Holder Deceased | 54 | October, 2014 | Other |
| 2 | Not Provided For | 59556 | October, 2014 | 2 Not Provided For |
| 22 | Account effects not cleared | 1 | October, 2014 | Other |
| 26 | NO SUCH ACCOUNT | 1176 | October, 2014 | Other |
| 3 | DEBITS / CREDITS NOT ALLOWED TO ACCOUNT | 61 | October, 2014 | Other |
| 3 | Debits not allowed for this account | 134 | October, 2014 | Other |
| 30 | CLIENT DID NOT AUTHORISE DEBIT | 697 | October, 2014 | Other |
| 30 | No authority to debit | 2097 | October, 2014 | 30 No authority to debit |
| 32 | DEBIT CONTRAVENES CLIENT'S AUTHORITY | 39 | October, 2014 | Other |
| 32 | Debit in contravention of payer's authority | 238 | October, 2014 | Other |
| 32 | Upfront Reply *** WARNING ONLY *** HOMING BRANCH INVALID - TRANSACTION REJECTED | 47 | October, 2014 | Upfront Rejection |
| 34 | AUTHORISATION CANCELLED | 203 | October, 2014 | Other |
| 34 | Upfront Reply *** WARNING ONLY ** NAEDO TXN NOT ALLOWED-TXN REJ | 345 | October, 2014 | Upfront Rejection |
| 34 | Upfront Reply *** WARNING ONLY *** HOMING ACCOUNT INVALID - TRANSACTION REJECTED | 283 | October, 2014 | Upfront Rejection |
| 34 | Upfront Reply *** WARNING ONLY *** NAEDO TRANSACTION NOT ALLOWED - TRANSACTION REJECTED *** | 4 | October, 2014 | Upfront Rejection |
| 35 | Upfront Reply *** WARNING ONLY ** ACCOUNT TYPE INVALID-TXN REJ | 2 | October, 2014 | Upfront Rejection |
| 36 | PREVIOUSLY STOPPED AS STOP PAYMENT | 11 | October, 2014 | Other |
| 36 | Previously stopped via stop payment advice | 86 | October, 2014 | Other |
| 4 | PAYMENT STOPPED | 865 | October, 2014 | Other |
| 4 | Payment Stopped by Account Holder | 213 | October, 2014 | Other |
| 5 | Account Dormant | 70 | October, 2014 | Other |
| 56 | Not FICA Compliant | 4 | October, 2014 | Other |
| 6 | ACCOUNT FROZEN | 655 | October, 2014 | Other |
| 899 | DISTRIBUTION UPFRONT REJECTION | 18 | October, 2014 | Other |
| 908 | EXCEPTIONS ERROR | 1 | October, 2014 | Other |
+------+---------------------------------------------------------------------------------------------+-------------------+---------------+--------------------------+
答案 0 :(得分:2)
SELECT
Code, Description, SUM([Status Code Count]) AS [Status Code Count],
[Month], a.Category
FROM @Responses a
JOIN (SELECT Category, SUM([Status Code Count]) AS Total FROM @Responses GROUP BY Category) b ON a.Category = b.Category
GROUP BY code, [Description], [Month], a.Category
HAVING CONVERT(INT, 100 * SUM([Status Code Count]) / Total) >= 1
答案 1 :(得分:0)
尽管我愿意接受CrimsonKing的回答,但它并没有给我我期望的结果。 (我会尝试修改它以使其工作,然后如果可以的话接受它。它比这简单得多......)
这是我的解决方案,但有点难看。
将表格定义更改为:
DECLARE @Responses TABLE
(
Code varchar(10),
Description varchar(255),
[Status Code Count] int,
[Month] varchar(255),
[Category] varchar(255),
Percentage float
)
然后使用像这样的游标而不是最后的选择:
DECLARE @Count float
SELECT @Count = SUM([Status Code Count]) from @Responses
DECLARE @Category varchar(255)
DECLARE totals_cursor CURSOR FOR
SELECT Category FROM @Responses
OPEN totals_cursor
FETCH NEXT FROM totals_cursor INTO @Category
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @Percentage float
SELECT @Percentage=SUM([Status Code Count])/@Count*100 FROM @Responses WHERE Category = @Category
UPDATE @Responses SET Percentage = @Percentage WHERE Category = @Category
FETCH NEXT FROM totals_cursor INTO @Category
END
CLOSE totals_cursor;
DEALLOCATE totals_cursor;
SELECT Code, Description, SUM([Status Code Count]) AS [Status Code Count], [Month], Category
FROM @Responses
WHERE CAST(percentage as int) > 0
GROUP BY code, [Description], [Month], Category