0

私はこの SQL クエリで絶対的な頭脳の故障を抱えています。誰かが私がすべきことを明らかにしてくれることを願っています。

次のテーブルがあり、それらは「anumber」でリンクされています。

athletes
--------
anumber     (id)
acountry    ('Australia','Japan')
aname

result_placing
--------------
result_id       id
anumber         (id linked to athletes)
result_placing  (Gold, Silver or null)

金、銀の結果が関連付けられている国のリストを取得したいと思います。次のように出力します。

country     | resul_placing | result_count
----------- | ------------- | ------------
Australia   | Gold          |       2
Australia   | Silver        |       1

私はこのようなことを試しました:

SELECT      acountry 
FROM        athletes 
INNER JOIN  result_placing 
ON          athletes.anumber = result_placing.anumber

どのショー

 Australia | Gold   |
 Australia | Silver |

それぞれの数を取得する方法がわかりません。count(distinct) を試しましたが、Access が気に入りません。

Count を次のように使用するとすぐに:

SELECT      a.acountry
        ,   r.placing
        ,   count(a.acountry) 
FROM        athlete         a 
INNER JOIN  result_place    r 
ON          a.anumber       = r.anumber

私は受け取ります:

You tried to execute a query that does not include the specified expression 'acountry' as part of an aggregate function

4

3 に答える 3

2

試す

SELECT      a.acountry
        ,   r.result_placing
        ,   sum(*) as cnt 
FROM        athletes        a 
RIGHT JOIN  result_placing  r 
ON          a.anumber       = r.anumber 
GROUP BY    a.acountry
        ,   r.result_placing 
ORDER BY    a.acountry
于 2012-05-03T02:20:11.540 に答える
1

LEFT OUTER JOINGROUP BYを使用してデータをクエリする 1 つの方法を次に示します。

Script:

SELECT              a.acountry
                ,   IIf(r.result_placing = '' 
                        OR IsNull(r.result_placing), 
                        'n/a', 
                        r.result_placing
                    ) AS medal
                ,   COUNT(r.result_id) AS medalcount
FROM                athletes a
LEFT OUTER JOIN     result_placing r
ON                  a.anumber = r.anumber
GROUP BY            a.acountry
                ,   r.result_placing

Table Data:

選手表データ

アスリート

results_placeing表データ

results_placeings

Output:

クエリ出力

クエリ

于 2012-05-03T02:37:20.407 に答える
1

これを試したことはありませんが、ここに行きます。group by を使用する必要があります。次のクエリを試してください

SELECT a.acountry, r.result_placing, count(a.acountry) FROM athletes a INNER JOIN     result_placing r ON a.anumber = r.anumber group by a.acountry, r.result_placing
于 2012-05-03T02:17:16.670 に答える