1

私はこの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
4

3 に答える 3

1

試してテストしました:

SELECT S.user_id, S.act_status, Count(S.act_status) AS CountOfact_status
FROM tbl_sActivity AS S INNER JOIN [SELECT A.client_id, Max(A.act_date) AS max_act
FROM tbl_sActivity AS A
GROUP BY A.client_id]. AS S2 ON (S.act_date = S2.max_act) AND (S.client_id = S2.client_id)
GROUP BY S.user_id, S.act_status

昨日の回答と同様のリクエストです。私のクエリを読んで、それを使用してみてください。これが理解に役立つことを願っており、学習する唯一の方法です:)

于 2012-08-17T08:30:05.563 に答える
0

日付列を使用する代わりに、主キー列act_idを使用する方がよいのではないでしょうか。

したがって、最大主キーを取得しますか?

私が意味したのは、これの代わりに:

where act_date in (
    select  max(act_date)
    from your_table
    group by Client_id
)

このようなもの:

  where act_id in (
select max(act_id)-- for each user, can't think of syntax now, but this could point you into the right direction
    )

BugFinderの答えは良く見えます。

于 2012-08-17T07:13:06.723 に答える
0
select user_id, act_status, count(act_Status) 
from your_table left join 
(select act_date in ( 
    select  max(act_date) 
    from your_table 
    group by Client_id 
) as t where t.client_id = your_table.client_id and t.act_date = your_table.act_date
group by user_id, act_status

あなたが望むものをあなたに与えるべきです

たとえば、クライアントごとに最大日付のテーブルを作成し、正確に両方のフィールドを使用してテーブルにリンクします。

于 2012-08-17T07:13:57.690 に答える