如何使用带有内连接的list命令

时间:2014-07-23 10:14:33

标签: sql firebird2.5

Table 1
Doccode  |  Doc_status
CM0001          1
CM0002          0
CM0003          1

Table 2
Doccode  |  Vehicle
CM0001      bike-001
CM0001      car-003
CM0002      bike-028
CM0003      car-011
CM0003      car-777

我只在table2上使用list命令选择数据

select t2.doccode, list(vehicle, ', ')
from table2 t2
group by t2.doccode

以上命令确定!!

但是......我不能用它与内连接下面的命令有什么问题?

select
      t2.doccode, list(vehicle, ', '),
      t1.doc_status
from
      table2 t2 inner join table1 t1
      on t2.doccode = t1.doccode
group by
     t2.doccode

2 个答案:

答案 0 :(得分:1)

在这种情况下GROUP BY使用带有汇总功能的LIST()应该包含GROUP BY中的所有字段。因此,如果您不需要 doc_status 将其删除,例如:

select
  t2.doccode, list(vehicle, ', ')
from
  table2 t2 inner join table1 t1
  on t2.doccode = t1.doccode
group by
t2.doccode

如果您确实需要 doc_status ,请尝试:

select t1.doccode, VehicleList, doc_status
FROM table1 t1
LEFT JOIN (
select
  doccode, list(vehicle, ', ') as VehicleList
from  table2 
group by
doccode ) t2
ON t1.doccode = t2.doccode

答案 1 :(得分:1)

我认为您可以将其添加到group by

select t2.doccode, list(vehicle, ', '),
       t1.doc_status
from table2 t2 inner join
      table1 t1
     on t2.doccode = t1.doccode
group by t2.doccode, t1.doc_status;