Sql server 同一行返回多次
目前,我在一个交换机中使用了三个端口,我正试图用下面的SQL监控流量Sql server 同一行返回多次,sql-server,inner-join,union,Sql Server,Inner Join,Union,目前,我在一个交换机中使用了三个端口,我正试图用下面的SQL监控流量 select a.interface, a.utilization_in, b.utilization_out from (select z.source as interface, y.samplevalue as utilization_in from #TABLE1 z join #TABLE2 y on z.table_id = y.table_id where z.source = 'switch1' and z.p
select a.interface, a.utilization_in, b.utilization_out from
(select z.source as interface, y.samplevalue as utilization_in from #TABLE1 z
join #TABLE2 y
on z.table_id = y.table_id
where z.source = 'switch1' and z.port like 'port_in%') a
INNER JOIN
(select x.source as interface, w.samplevalue as utilization_out from #TABLE1 x
join #TABLE2 w
on x.table_id = w.table_id
where x.source = 'switch1' and x.port like 'port_out%') b
on a.interface=b.interface
这会得到结果,但每行都会重复几次,3*3=9。我一直在四处搜索,发现有些人使用union运算符。但我不知道该把它放在哪里。感谢联合意味着您有两个单独的SELECT查询,它们具有相同的列数,并且您可以将这两个查询的结果作为一个结果集显示 所以基本上是这样的:
SELECT ... FROM ... WHERE ...
UNION
SELECT ... FROM ... WHERE ...
请记住,
UNION
也将执行distinct
,因此它不会返回重复的行。如果不想使用不同的,请使用UNION ALL。UNION表示您有两个单独的SELECT查询,它们具有相同的列数,并且将这两个查询的结果显示为一个结果集
select z.source as interface, y.samplevalue as utilization_in from #TABLE1 z
join #TABLE2 y
on z.table_id = y.table_id
where z.source = 'switch1' and z.port like 'port_in%'
UNION ALL -- or use just UNION if there are could be duplicates in both parts
select x.source as interface, w.samplevalue as utilization_out from #TABLE1 x
join #TABLE2 w
on x.table_id = w.table_id
where x.source = 'switch1' and x.port like 'port_out%'
所以基本上是这样的:
SELECT ... FROM ... WHERE ...
UNION
SELECT ... FROM ... WHERE ...
请记住,UNION
也将执行distinct
,因此它不会返回重复的行。如果您不想区分,请使用UNION ALL
select z.source as interface, y.samplevalue as utilization_in from #TABLE1 z
join #TABLE2 y
on z.table_id = y.table_id
where z.source = 'switch1' and z.port like 'port_in%'
UNION ALL -- or use just UNION if there are could be duplicates in both parts
select x.source as interface, w.samplevalue as utilization_out from #TABLE1 x
join #TABLE2 w
on x.table_id = w.table_id
where x.source = 'switch1' and x.port like 'port_out%'
但由于您具有相同的连接条件,并且唯一的区别在于端口,因此您可以使用以下查询:
select z.source as interface, y.samplevalue as utilization, CASE WHEN z.port like 'port_in%' THEN 'IN' ELSE 'OUT' END as in_or_out
from #TABLE1 z
join #TABLE2 y
on z.table_id = y.table_id
where z.source = 'switch1'
但由于您具有相同的连接条件,并且唯一的区别在于端口,因此您可以使用以下查询:
select z.source as interface, y.samplevalue as utilization, CASE WHEN z.port like 'port_in%' THEN 'IN' ELSE 'OUT' END as in_or_out
from #TABLE1 z
join #TABLE2 y
on z.table_id = y.table_id
where z.source = 'switch1'
只需使用条件聚合,这样您只需从表中选择一次:
select x.source as interface,
MAX(CASE WHEN x.port like 'port_out%' THEN w.samplevalue END) as utilization_out,
MAX(CASE WHEN x.port like 'port_in%' THEN w.samplevalue END) as utilization_in
from #TABLE1 x
join #TABLE2 w
on x.table_id = w.table_id
where x.source = 'switch1'
GROUP BY x.source
只需使用条件聚合,这样您只需从表中选择一次:
select x.source as interface,
MAX(CASE WHEN x.port like 'port_out%' THEN w.samplevalue END) as utilization_out,
MAX(CASE WHEN x.port like 'port_in%' THEN w.samplevalue END) as utilization_in
from #TABLE1 x
join #TABLE2 w
on x.table_id = w.table_id
where x.source = 'switch1'
GROUP BY x.source
但这并不能满足他的需求<代码>接口| uti_in | uti_out它通过提供新的列来实现(至少是没有联合的查询),但这并不能提供他想要的
Interface | uti_in | uti_out
它通过提供新的列来实现(至少是没有联合的查询),而这些列不会满足他的需求,也不会满足他的需求。