需要帮助编写SQL查询

时间:2015-03-06 08:20:19

标签: sql

我需要帮助编写SQL查询。假设以下表为输入

表#1:hostname

host_id     host_name
1          420000-PC001
2          420000-PC002

表#2:asset_id

asset_id    asset_type  asset_serialno  host_id
1            cpu          CPU10001        1
2            cpu          CPU10002        2
3            monitor      MON10001        1
4            monitor      MON10002       NULL
5            printer      PRN10001        2

两个表之间的关系位于host_id列。

我希望输出表格式如下

host_name      cpu_serialno   monitor_serialno   printer_serialno
420000-PC001    CPU10001        MON10001           NULL
420000-PC002    CPU10002        NULL               PRN10001

请帮我写这个SQL查询...我写了一个查询,但我无法显示空值......

4 个答案:

答案 0 :(得分:0)

为每种产品类型执行左连接:

select host_name , cpu_serialno, monitor_serialno, printer_serialno
from hostname
  left join (select asset_id, asset_serialno  as cpu_serialno
             from asset_id
             where asset_type = 'cpu') cpu_table
     ON hostname.host_id = cpu_table.hostname
  left join (select asset_id, asset_serialno  as monitor_serialno
             from asset_id
             where asset_type = 'monitor') monitor_table
     ON hostname.host_id = monitor_table.hostname
  left join (select asset_id, asset_serialno  as printer_serialno
             from asset_id
            where asset_type = 'printer') printer_table
     ON hostname.host_id = printer_table.hostname

答案 1 :(得分:0)

您应该使用LEFT JOINS子查询,类似:

SELECT host_name, 
       cpu_serialno, 
       monitor_serialno, 
       printer_serialno
FROM hostname AS t1
LEFT JOIN (SELECT  asset_id, 
                   asset_serialno AS cpu_serialno
             FROM  asset_id
             WHERE asset_type = 'cpu') t2
ON t1.host_id = t2.hostname
LEFT JOIN (SELECT  asset_id, 
                   asset_serialno  AS monitor_serialno
             FROM  asset_id
             WHERE asset_type = 'monitor') t3
ON t1.host_id = t3.hostname
LEFT JOIN (SELECT  asset_id, 
                   asset_serialno  AS printer_serialno
             FROM  asset_id
             WHERE asset_type = 'printer') t4
ON t1.host_id = t4.hostname

答案 2 :(得分:0)

另一种选择可以是SELECT语句

select
HOST_NAME,
max(cpu_serialno) cpu_serialno,
max(monitor_serialno) monitor_serialno,
max(printer_serialno) printer_serialno
from (
select 
    h.host_name,
    cpu_serialno = case when asset_type = 'cpu' then asset_serialno else null end,
    monitor_serialno = case when asset_type = 'monitor' then asset_serialno else null end,
    printer_serialno = case when asset_type = 'printer' then asset_serialno else null end
from hostname h
inner join asset_id a on h.host_id = a.host_id
) t
group by host_name

答案 3 :(得分:0)

谢谢大家回复了我的疑问。根据您的建议,我创建了一个查询,如下解决了我的问题

select h.host_name, cpu.asset_serialno, mon.asset_serialno, prn.asset_serialno from hostname h 
left join (select * from asset_id where asset_type='cpu') cpu on h.host_id = cpu.host_id 
left join (select * from asset_id where asset_type='monitor') mon on h.host_id = mon.host_id 
left join (select * from asset_id where asset_type='printer') prn on h.host_id = prn.host_id