使用SQL将多个Crosstab查询结果合并到单独的Bins(范围)中

问题描述 投票:0回答:1

我有一个 Excel 工作表,显示 11 个 Bin(列范围)的计数(参见图片),这些计数来自一个 Access 数据集。 我想在 Access 中执行此操作,而不是从 Excel 进行链接。 我可以创建单独的查询 - 没问题。 我还为 11 个 Bin 中的每一个创建了一个交叉表查询。 该图像是 1 个 CT 查询的图像。 为了获得类似于 Excel 结果的结果,我必须使用 11 个 CT 结果设计第二个查询,然后将每个“行”添加为单独的字段。 我只知道在查询中运行嵌套查询不是正确的方法。 虽然它有效,但效率非常低并且消耗大量资源。 我确信有一个 SQL 解决方案 - 不是 UNION - 其中脚本将使用 Bin 标准执行 11 个交叉表中的每一个,然后创建数组。 我擅长 Excel,擅长 Access VBA,但在 SQL 方面经验不多。 请帮忙(并保持耐心)。Excel 11 BinsCrosstab designResult of Crosstab

sql ms-access
1个回答
0
投票

考虑条件聚合(可移植到其他数据库),方法是将

WHERE
计算的条件转换为计算的
SELECT
列,使用
IIF()
来标记数据,然后对数据进行求和以对标记进行计数。您还可以包含其他聚合,例如记录数。

SELECT 
    CURRENT_STATE AS State,
    COUNT(CREDID) AS Volume,
    SUM(IIF([Days Ac] BETWEEN 0 AND 8, 1, 0)) AS [0 - 8 Days Ac],
    SUM(IIF([Days Ac] BETWEEN 9 AND 15, 1, 0)) AS [9 - 15 Days Ac],
    SUM(IIF([Days Ac] BETWEEN 16 AND 20, 1, 0)) AS [16 - 20 Days Ac],
    SUM(IIF([Days Ac] BETWEEN 21 AND 25, 1, 0)) AS [21 - 25 Days Ac],
    SUM(IIF([Days Ac] BETWEEN 26 AND 30, 1, 0)) AS [26 - 30 Days Ac],
    SUM(IIF([Days Ac] BETWEEN 31 AND 40, 1, 0)) AS [31 - 40 Days Ac],
    SUM(IIF([Days Ac] BETWEEN 41 AND 60, 1, 0)) AS [41 - 60 Days Ac],
    SUM(IIF([Days Ac] BETWEEN 61 AND 90, 1, 0)) AS [61 - 90 Days Ac],
    SUM(IIF([Days Ac] BETWEEN 91 AND 110, 1, 0)) AS [91 - 110 Days Ac],
    SUM(IIF([Days Ac] BETWEEN 111 AND 130, 1, 0)) AS [111 - 130 Days Ac],
    SUM(IIF([Days Ac] > 130, 1, 0)) AS [>130 Days Ac]
    SUM(IIF([Days Ac] IS NOT NULL, 1, 0) AS [CheckValue]
FROM (
   SELECT CURRENT_STATE, CREDID, Date() - [last_state_changed] + [nettime] AS [Days Ac]
   FROM tblBreakOut
) sub
GROUP BY CURRENT_STATE 

但是,如果您使用嵌套提前计算垃圾箱,MS Access 的交叉表仍然可以工作

IIF()
:

TRANSFORM COUNT(CREDID) AS [Count]
SELECT 
   CURRENT_STATE AS [State],
   COUNT(CREDID) AS [Volume]
FROM (
   SELECT 
       CURRENT_STATE, 
       CREDID, 
       IIF(Date() - [last_state_changed] + [nettime] BETWEEN 0 AND 8, '0 - 8 Days Ac',
        IIF(Date() - [last_state_changed] + [nettime] BETWEEN 9 AND 15,'9 - 15 Days Ac',
         IIF(Date() - [last_state_changed] + [nettime] BETWEEN 16 AND 20, '16 - 20 Days Ac',   
          IIF(Date() - [last_state_changed] + [nettime] BETWEEN 21 AND 25, '21 - 25 Days Ac',        
           IIF(Date() - [last_state_changed] + [nettime] BETWEEN 26 AND 30, '26 - 30 Days Ac',          
            IIF(Date() - [last_state_changed] + [nettime] BETWEEN 31 AND 40, '31 - 40 Days Ac',
             IIF(Date() - [last_state_changed] + [nettime] BETWEEN 41 AND 60, '41 - 60 Days Ac',
              IIF(Date() - [last_state_changed] + [nettime] BETWEEN 61 AND 90, '61 - 90 Days Ac',
               IIF(Date() - [last_state_changed] + [nettime] BETWEEN 91 AND 110, '91 - 110 Days Ac',
                IIF(Date() - [last_state_changed] + [nettime] BETWEEN 111 AND 130, '111 - 130 Days Ac',
                 IIF(Date() - [last_state_changed] + [nettime] > 130, '>130 Days Ac',
                  IIF(Date() - [last_state_changed] + [nettime] IS NOT NULL, 'CheckValue', NULL)
                 )
                )
               )
              )
             )
            )
           )
          )
         )
        )  
       ) AS [Days Ac] 
   FROM tblBreakOut
) sub
GROUP BY CURRENT_STATE
PIVOT [Days Ac] IN (
    [0 - 8 Days Ac],
    [9 - 15 Days Ac],
    [16 - 20 Days Ac],
    [21 - 25 Days Ac],
    [26 - 30 Days Ac],
    [31 - 40 Days Ac],
    [41 - 60 Days Ac],
    [61 - 90 Days Ac],
    [91 - 110 Days Ac],
    [111 - 130 Days Ac],
    [>130 Days Ac],
    [CheckValue]
)

© www.soinside.com 2019 - 2024. All rights reserved.