我需要为表中的每个组ID创建一个用逗号分隔的值列表,并填写所有缺少的值,但是我不太确定如何进行操作。 我正在使用Server 2016。
我的桌子:
id | group_id | value
------+------------+---------
1 | 1 | 15
2 | 1 | 16
3 | 1 | 20
4 | 2 | 12
5 | 2 | 15
6 | 3 | 8
预期输出:
group_id | list
-----------+-----------------------
1 | '15,16,17,18,19,20'
2 | '12,13,14,15'
3 | '8'
答案 0 :(得分:0)
这是另一种方法。创建一个使用开始和结束数字并生成缺失数字的函数。
create function dbo.get_list(@start int, @end int)
returns varchar(1000)
as begin
declare @retval varchar(1000);
set @retval = cast(@start as varchar(1000));
set @start = @start + 1;
while @start <= @end
begin
set @retval = @retval + ',' + cast(@start as varchar(1000));
set @start = @start + 1;
end;
return @retval
end
完成后,编写以下SQL:
select group_id, dbo.get_list(min(value), max(value))
from test
group by group_id
结果
group_id list
-------- ------------------
1 15,16,17,18,19,20
2 12,13,14,15
3 8
答案 1 :(得分:0)
我认为递归CTE是一种好方法:
from threading import Thread
from queue import Queue
jobs = Queue() # buffer for sysinfo
output_list = [] # store ips
def get_sys_info(self, host_id, appliance):
sysinfo = self.hx_request("https://{}:3000//hx/api/v3/hosts/{}/sysinfo"
jobs.put(sysinfo) # add sysinfo to jobs queue
return sysinfo # comment if you don't need it
def get_ips_from_sysinfo(self):
"""it will run contineously untill finish all jobd"""
while True:
# get sysinfo from jobs queue
sysinfo = jobs.get() # it will wait here for new entry
if sysinfo == 'exit':
print('we are done here')
break
sysinfo = sysinfo["data"]
network_array = sysinfo.get("networkArray", {})
network_info = network_array.get("networkInfo", [])
ips = []
for ni in network_info:
ip_array = ni.get("ipArray", {})
ip_info = ip_array.get("ipInfo", [])
for i in ip_info:
ips.append(i)
output_list.append(ips)
if __name__ == "__main__":
# start our listner thread
Thread(target=rr.get_ips_from_sysinfo)
threads = []
for i in ids:
t = Thread(target=rr.get_sys_info, args=(i, appliance))
threads.append(t)
t.start()
# wait for threads to finish then terminate get_ips_from_sysinfo() by send 'exit' flag
for t in threads:
t.join()
jobs.put('exit')
Here是db <>小提琴。