我正在尝试创建一个将获取数据列表的查询,并在每次符合条件时给我N行。
说我有以下数据:
ID | Type
1 | Vegetables
2 | Vegetables
3 | Vegetables
4 | Fruits
5 | Fruits
6 | Meats
7 | Dairy
8 | Dairy
9 | Dairy
10 | Dairy
我想要的是:
Type
Dairy
Dairy
Dairy
Fruits
Fruits
Meats
Meats
Vegetables
Vegetables
我的标准是,对于每种类型中的每2个,我将其视为"整体"值。如果有任何超过整数的值,则向上舍入到最接近的整数。因此,蔬菜类型从1.5行到2行,乳品类型保持在2行。
然后我想为每个不是集合中最后一个类型的Type添加一行(这就是为什么Vegetable只有两行),也许是另一个列面额显示它是添加的行。
答案 0 :(得分:0)
所以你想要计算记录,除以2,向上舍入然后加1。
--Create a temporary table with all numbers from 1 to 1024.
declare @Numbers table
(
MaxQty INT IDENTITY(1,1) PRIMARY KEY CLUSTERED
)
WHILE COALESCE(SCOPE_IDENTITY(), 0) <= 1024
BEGIN
INSERT @Numbers DEFAULT VALUES
END
--First get the count of records
SELECT [Type], Sum(1) as CNT
INTO #TMP1
FROM MyTable
Group By [Type]
--Now get the number of times the record should be repeated, based on this formula :
-- count the records, divide by 2, round up and then add 1
SELECT [Type], CNT, CEILING((CNT/2)+1) as TimesToRepeat
INTO #TMP2
FROM #TMP1
--Join the #TMP2 table with the @Numbers table so you can repeat your records the
-- required number of times
SELECT A.*
from #TMP2 as A
join @Numbers as B
on B.MaxQty <= A.TimesToRepeat
不漂亮,但应该有效。这仍然不能解释集合中的最后一个类型,我对这一部分感到有点困惑。
答案 1 :(得分:0)
此查询将返回每种类型以及必须重复的次数:
SELECT Type, tot+IIf(Type=(SELECT MAX(Type) FROM tablename),0,1) AS Rep
FROM (SELECT tablename.Type, -Int(-Count([tablename].[ID])/2) AS tot
FROM tablename
GROUP BY tablename.Type
) AS s;
然后我的想法是使用一个名为[times]的表,其中包含重复n次的每个数字:
n
---
1
2
2
3
3
3
...
然后您的查询可能是这样的:
SELECT s.*
FROM (
SELECT Type, tot+IIf(Type=(SELECT MAX(Type) FROM tablename),0,1) AS rep
FROM (SELECT tablename.Type, -Int(-Count([tablename].[ID])/2) AS tot
FROM tablename
GROUP BY tablename.Type
) AS s1) s INNER JOIN times ON s.rep=times.n