[英]SQL Server Add X rows to results to always equal a 5 row output
I have code which returns the last 5 results for any given ID (including the ID given). 我有代码返回任何给定ID的最后5个结果(包括给定的ID)。 What i want is to return extra rows with the word
none
and 00
respectively if there aren't previous values to equal 5. 如果没有先前的值等于5,我想要的是分别返回带有单词
none
和00
额外行。
So if the code returns 3 results generate 2 extra rows, if the code returns 4 results generate 1 extra row. 因此,如果代码返回3结果,则生成2个额外行,如果代码返回4,则结果生成1个额外行。
Current Code: ( aaa.FS01 = '12345'
) is just an example of an ID 当前代码:(
aaa.FS01 = '12345'
)只是ID的一个示例
SELECT aaa.FS01,
CAST(COUNT(distinct bbb.MG) AS FLOAT)/ convert(varchar(10), DATEDIFF(second ,MIN(TP), MAX(TP))/3600.0) TPDIFF,
22 TAG
FROM TableA aaa
INNER JOIN TableB bbb ON bbb.tpl = aaa.FS01 OR bbb.fpl = aaa.FS01
INNER JOIN TableC ccc ON ccc.id = aaa.FS01
INNER JOIN TableD ddd ON aaa.VG = ddd.gkey
WHERE aaa.FS01 IN (
SELECT TOP 5 ccc.id
FROM TableA aaa
INNER JOIN TableC ccc ON ccc.id = aaa.FS01
INNER JOIN TableD v ON aaa.VG = ddd.gkey
WHERE ddd.name = (SELECT ddd.name FROM TableA aaa
INNER JOIN TableD v ON aaa.VG = ddd.gkey WHERE aaa.FS01 = '12345')
ORDER BY ccc.ata DESC)
GROUP BY aaa.FS01, ccc.ata
ORDER BY ccc.ata DESC
Current Output (For an FS01
that only returns 3 values): 电流输出(对于仅返回3个值的
FS01
):
FS01 TPDIFF TAG
12345 30 22
22222 50 22
45122 90 22
Expected Output (For an FS01
that only returns 3 values): 预期输出(对于仅返回3个值的
FS01
):
FS01 TPDIFF TAG
12345 30 22
22222 50 22
45122 90 22
none 00 22
none 00 22
Current Output (For an FS01
that only returns 1 value): 电流输出(对于仅返回1值的
FS01
):
FS01 TPDIFF TAG
74133 30 22
Expected Output (For an FS01
that only returns 1 value): 预期输出(对于仅返回1值的
FS01
):
FS01 TPDIFF TAG
74133 80 22
none 00 22
none 00 22
none 00 22
none 00 22
You can do this with a single query: 您可以使用单个查询执行此操作:
with t as (<your query here>),
nones as (
select 'none' as FS01, '00' as TPDIFF, '22' as TAG
from (values (1), (2), (3), (4), (5))
)
select top (5) tn.FS01, tn.TPDIFF, tn.TAG
from ((select t.* from t) union all
nones
) tn
order by (case when FS01 = 'none' then 1 else 2 end) as asc;
I would declare a table variable like this. 我会声明一个像这样的表变量。 Then use a while to append empty rows.
然后使用一段时间追加空行。
Something like this: 像这样的东西:
DECLARE @tbl AS TABLE(FS01 text, TPDIFF text, TAG Text);
INSERT INTO @tbl (FS01, TPDIFF, TAG)
--your select statement (copied)
SELECT aaa.FS01,
CAST(COUNT(distinct bbb.MG) AS FLOAT)/ convert(varchar(10), DATEDIFF(second ,MIN(TP), MAX(TP))/3600.0) TPDIFF,
22 TAG
FROM TableA aaa
INNER JOIN TableB bbb ON bbb.tpl = aaa.FS01 OR bbb.fpl = aaa.FS01
INNER JOIN TableC ccc ON ccc.id = aaa.FS01
INNER JOIN TableD ddd ON aaa.VG = ddd.gkey
WHERE aaa.FS01 IN (
SELECT TOP 5 ccc.id
FROM TableA aaa
INNER JOIN TableC ccc ON ccc.id = aaa.FS01
INNER JOIN TableD v ON aaa.VG = ddd.gkey
WHERE ddd.name = (SELECT ddd.name FROM TableA aaa
INNER JOIN TableD v ON aaa.VG = ddd.gkey WHERE aaa.FS01 = '12345')
ORDER BY ccc.ata DESC)
GROUP BY aaa.FS01, ccc.ata
ORDER BY ccc.ata DESC
declare @cnt as int;
SELECT @cnt = count(*) from @tbl
while @cnt < 5
BEGIN
insert into @tbl (FS01, TPDIFF, TAG) VALUES('none', '00', '22');
select @cnt = @cnt+1;
end
select top 5 * from @tbl;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.