繁体   English   中英

访问SQL-为每10行创建一行,并在组中找到剩余的行

[英]Access SQL - Create Row For Every 10 Rows And Remainder Found In Group

我无法概念化如何使用SQL专门执行此操作。

说我有一个查询,它可能是该问题产生的解决方案中的子查询:

Color     |  Count
Brown     |  25
Yellow    |  5
Blue      |  35

使用以上结果集,我想查询以下内容:

Color
Brown
Brown
Brown
Yellow
Blue
Blue
Blue
Blue

另一种解决方案是进行计数并执行以下操作:

SELECT -Int((-Count(*)/10)) AS Expr1, Color
FROM ColorTable
group by test.Source_City

使用上述数据将产生以下结果:

Color     |  Count
Brown     |  3
Yellow    |  1
Blue      |  4

一个解决方案是获取Count并为每个1写一行。

因此,我们将测试数据存储在名为[InitialCounts]的表中

Color   Count
------  -----
Blue       35
Brown      25
Yellow      5

和一个名为[Numbers]的“数字表”,其中包含

   n
----
   1
   2
   3
...
9999

(或根据需要达到的高度,基于下面我们希望为每种颜色得出的最大行数)。

查询

SELECT 
    Color, 
    Int(CDbl(Count)/10 + 0.5) AS NewCount
FROM InitialCounts

回报

Color   NewCount
------  --------
Blue           4
Brown          3
Yellow         1

如果我们想为每种颜色产生重复的行,我们可以做

SELECT NewCounts.Color
FROM
    Numbers,
    (
        SELECT 
            Color, 
            Int(CDbl(Count)/10 + 0.5) AS NewCount
        FROM InitialCounts
    ) AS NewCounts
WHERE Numbers.n <= NewCounts.NewCount
ORDER BY NewCounts.Color

回国

Color 
------
Blue  
Blue  
Blue  
Blue  
Brown 
Brown 
Brown 
Yellow

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM