我目前正面临一堵墙,我试图从两个不同的表中获取信息,让它们与按计数记录的信息并排显示。一个是第一个表中具有特定值的总量,第二个是不同列的值
表A.current与B.id相同
但是我想要的信息是当前表A中的总匹配数,并显示B.name信息而不是b.id
我已经尝试了很多事情,到目前为止,我仍然遇到一个问题,它说关于from是不正确的或组是不正确的...
select count(pk.id) as "Total",
lc.fullyqualifiedname as "Name"
from tsu pk,
location lc
where pk.locationid = lc.id
group by lc.id
having lc.id = :ID;
从上面的代码中,我收到错误00923
如果有人可以帮助我-我在哪里出错了?
我最终需要做的是
第1列-位置为lc.id的总命中数(count(*)) 第2列-正在显示lc.id表示为完全限定名称的名称。提供貂皮表中另一列的值。
编辑:
select count(pk.id) as "Total",
lc.fullyqualifiedname as "Name"
from tsu pk,
location lc
where pk.locationid = lc.id
group by lc.id
having lc.id = :ID;
此脚本有效,但是它显示了表lc中的第1列,我想从lc中获得列名。
-问题通过已回答的脚本解决。
答案 0 :(得分:0)
对于单个lc.id
,无需使用GROUP BY
:
select count(pk.id) as "Total",
min(lc.fullyqualifiedname) as "Name" -- here agg function
from tsu pk
join location lc -- join syntax is preferred
on pk.locationid = lc.id
where lc.id = :ID; -- filtering with where
或者:
select count(pk.id) as "Total",
min(lc.fullyqualifiedname) as "Name",
lc.id
from tsu pk
join location lc
on pk.locationid = lc.id
---where lc.id IN (...)
group by lc.id;
答案 1 :(得分:0)
问题是lc.fullyqualifiedname
出现在select
列表中,但没有出现在group by
子句中,因此出现在ORA-00979: not a GROUP BY expression
中。
select count(pk.id) as "Total",
lc.fullyqualifiedname as "Name"
from tsu pk,
location lc
where pk.locationid = lc.id
and lc.id = :id
group by lc.fullyqualifiedname;
或更妙的是
select count(*) as "Total",
lc.fullyqualifiedname as "Name"
from tsu pk
join location lc on lc.id = pk.locationid
where lc.id = 1
group by lc.fullyqualifiedname;
如果这不是您所需要的,请提供一些示例数据和预期结果。