这看起来像一团糟。我有一个请求将多个表输出到一个电子表格中。我还是新手,之前只使用过内连接。这是我的SQL的样子:
select distinct (o.objectnumber), g.locale, g.locus, g.excavation, g.mapreferencenumber,
case when t.texttypeid like '9' then t.textentry end,
case when f.userfieldid like '25' then f.fieldvalue end
from objects o
inner join TextEntries t on t.id = o.objectid
inner join ObjGeography g on g.ObjectID = o.objectid
inner join userfieldxrefs f on f.id = o.objectid
inner join PackageList pl on o.objectID = pl.ID
inner join Packages p on pl.PackageID = p.PackageID
where p.packageid like '8502'
order by g.mapreferencenumber asc
我知道,这是一团糟吗?它给了我正确的输出,但也创建了具有相同数据的多行。我已经对此进行了一些谷歌搜索,并看到了有关外连接的一些评论,但我不确定如何正确地将此应用于我的陈述。有什么想法吗?
抱歉;我被叫走了,刚才有机会检查这个。这是我的第一个问题,我想我并不真正了解你们需要看到什么才能理解我想要的东西。看起来按功能分组效果最好,我在研究声明时已经看过这个,但是没有理解如何正确实现它。谢谢大家!
答案 0 :(得分:0)
最好将GROUP BY用于结果集中所需的列
select o.objectnumber,
g.locale,
g.locus,
g.excavation,
g.mapreferencenumber,
case when t.texttypeid like '9' then t.textentry end,
case when f.userfieldid like '25' then f.fieldvalue end
from objects o
inner join TextEntries t on t.id = o.objectid
inner join ObjGeography g on g.ObjectID = o.objectid
inner join userfieldxrefs f on f.id = o.objectid
inner join PackageList pl on o.objectID = pl.ID
inner join Packages p on pl.PackageID = p.PackageID
where p.packageid like '8502'
group by o.objectnumber,
g.locale,
g.locus,
g.excavation,
g.mapreferencenumber,
case when t.texttypeid like '9' then t.textentry end,
case when f.userfieldid like '25' then f.fieldvalue end
order by g.mapreferencenumber asc