私はこのSOでこの質問をしていましたが、すでに解決策を得ましたが、そのSQLクエリは部分的な正しい結果しか生成しませんでした。私はそれを理解しようとしましたが、理解するのが複雑なようです。ですから、どうか私を助けてください。ありがとうございました。
テーブル tbl_sActivity の場合
act_id| Client_id | act_status| user_id | act_date
1 | 7 | warm | 1 | 19/7/12
2 | 7 | dealed | 1 | 30/7/12 <- lastest status of client(7)
3 | 8 | hot | 1 | 6/8/12 <- lastest status of client(8)
4 | 5 | cold | 22 | 7/8/12 <- lastest status of client(5)
5 | 6 | cold | 1 | 16/7/12
6 | 6 | warm | 1 | 18/7/12
7 | 6 | dealed | 1 | 7/8/12 <- lastest status of client(6)
8 | 9 | warm | 26 | 2/8/12
9 | 10 | warm | 26 | 2/8/12
10 | 9 | hot | 26 | 4/8/12 <- lastest status of client(9)
11 | 10 | hot | 26 | 4/8/12
12 | 10 | dealed | 26 | 10/8/12 <- lastest status of client(10)
13 | 13 | dealed | 26 | 8/8/12 <- lastest status of client(13)
14 | 11 | hot | 25 | 8/8/12
15 | 11 | dealed | 25 | 14/8/12 <- lastest status of client(11)
各ユーザーがクライアントをどのようにフォローアップしているかの最新の進捗状況を示すユーザー プログレッシブ レポートを作成したいと考えています。したがって、このレポートは、各ユーザーのクライアント グループの数を最新のステータスごとに表示する必要があります。
正しい出力は次のようになります。
user_id | act_status | Count(act_status)
1 | dealed | 2
1 | hot | 1
22 | cold | 1
25 | dealed | 1
26 | hot | 1
26 | dealed | 2
私が持っていたSQLクエリは以下の通りです:
select user_id, act_status, count(act_Status)
from your_table
where act_date in (
select max(act_date)
from your_table
group by Client_id
)
group by user_id, act_status
データベースにさらにトランザクションを追加すると、出力が次のように間違っていました..
user_id | act_status | Count(act_status)
1 | dealed | 2
1 | hot | 1
22 | cold | 1
25 | hot | 1 ** (this one shouldn't show up)
25 | dealed | 1
26 | hot | 2 ** (should be only '1')
26 | dealed | 2