根据id获取表中资源的名称

时间:2013-10-30 23:05:48

标签: sql oracle oracle11g

我有2张桌子。

交易(id,resourceid,timestamp)

资源(id,名称)

我想得到一个像(resource.name,day,count(transaction.id))

这样的输出

我使用以下查询

select  EXTRACT(DAY from timestamp), resourceid, count(id) from transactions
where timestamp between '01-OCT-13' AND '10-OCT-13'
and resourceid in (select id from resource)
group by resourceid, EXTRACT(DAY from timestamp) \
order by EXTRACT(DAY from timestamp);

输出:

1,1,13
1,3,45
1,6,76
2,1,14
2,2,46

我希望看到像

这样的输出
1,resource1,13
1,resource3,45
1,resource6,76
2,resource1,14
2,resource2,46

有人可以帮帮我吗?

修改

resource1就是一个例子。我不想在数字1之前连接资源。我想要与id = 1相关联的实际名称。

1 个答案:

答案 0 :(得分:1)

如果您加入了交易和资源表,那么您可以使用recource.name字段。这可能是这样的:

select  EXTRACT(DAY from timestamp), resource.name, count(transactions.id) 
from transactions inner join resource 
    on transactions.resourceid = resource.id
where timestamp between '01-OCT-13' AND '10-OCT-13'
group by resource.name, EXTRACT(DAY from timestamp)
order by EXTRACT(DAY from timestamp);