2013-10-30 113 views
0

我有2個表。從表中獲取資源的名稱取決於編號

交易(ID,RESOURCEID,時間戳)

資源(ID,姓名)

我想得到這樣的輸出(resource.name,一天,算(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 

輸出是否有人可以幫我嗎?

編輯

資源1​​僅僅是例子。我不想在數字1之前連接資源。我想要與ID = 1關聯的實際名稱。

回答

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); 
+0

@Micheal - 謝謝。我做了你的建議。它的工作。 – CamelCase