アプリ版:「スタンプのみでお礼する」機能のリリースについて

下記のようなテーブルがあったとして、
テーブルAを基準に考えたいのですが、、、、
テーブルAに付加する形でテーブルBのデータのidカウント数が最も少なく、idが同じもの中でdate日が一番新しいデータを表示し、古いものから並べかえかつテーブルCにidがあるものは一番最後にくるという風に並べかえたいのですが、無理でしょうか?

現在下記のようなSQLを考えていますが、うまくいかないです。。
select * from A left join(select id,case when count(id)=0 then 0 else count(id) end AS cnt FROM B group by id order by date) as b on A.id=b.id left join(select id from C) as c on A.id=c.id order by c.id desc,b.cnt;
エラー↓
in the GROUP BY clause or be used in an aggregate function

テーブルA
id | name | comment|
1 あ   おい
2 い   えい
3 か   テス

テーブルB
id | event | date |
1 test1 2005/1/4
2 test1 2005/1/4
1 test2 2005/3/4
2 test2 2005/3/4
1 test3 2005/5/4

テーブルC
id |
1

理想の結合データ
id | name | comment| cnt | date |
3 か   テス 0   
2 い   えい 2  2005/3/4 
1 あ   おい 3 2005/5/4

A 回答 (1件)

内部結合で。


select A.id,A.name,A.comment,
count(B.id) as cnt,
max(B.date) as date,
'0' as CK
from A,B
where B.id=A.id and not exists(
select id from C where C.id=A.id
)
group by A.id,A.name,A.comment
UNION
select A.id,A.name,A.comment,
0 as cnt,
NULL as date,
'0' as CK
from A
where not exists(
select id from C where C.id=A.id
)
and not exists(
select id from B where B.id=A.id
)
group by A.id,A.name,A.comment
UNION
select A.id,A.name,A.comment,
count(B.id) as cnt,
max(B.date) as date,
'1' as CK
from A,B,C
where B.id=A.id and C.id=A.id
group by A.id,A.name,A.comment
UNION
select A.id,A.name,A.comment,
0 as cnt,
NULL as date,
'1' as CK
from A,C
where C.id=A.id and not exists(
select id from B where B.id=A.id
)
group by A.id,A.name,A.comment
order by CK,cnt;
    • good
    • 0
この回答へのお礼

こんなにいっぱい書いていただきありがとうございます。
でも、できませんでした。。なんででしょう。。
adding missing FROM-clause entry in subquery for table "A"

お礼日時:2006/02/03 18:10

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!