2017-07-31 4 views
2

たとえば、学生がアクティビティを行うたびにテーブル'studentActivities'が記録されているテーブルがあるとします。これと同じように:2つの選択カウントステートメントの結合方法

activity   countryofbirth name event_date 
School Swimming USA    Bob  5/21/2017 12:50 
Park Swimming Australia   Sarah 2/11/2017 19:50 
Park Swimming Australia   Sarah 2/13/2017 16:50 
Park Running  USA    Bob  2/10/2017 11:50 
School Tennis USA    Bob  2/12/2017 11:50 
NULL    USA    Jane 8/4/2016 13:30 

私はそれがIn-School ActvityOut-of-School Activityであれば、学生が基づいん数の異なる活動をカウントしようとしています。

Student_Name Country_of_Birth In-School_Activities Out_of_School_Activities 
Bob   USA    2     1 
Sarah  Australia  0     1 
Jane   USA    0     0 

私が試してみました:

SELECT 
studentActivities.name AS [student_name], 
studentActivities.countryofbirth AS County_of_Birth], 
COUNT (DISTINCT activity) as [In-School Activities] 

FROM studentActivities 

WHERE studentActivities.activity IN ('School Swimming', 'School Running', 'School Soccer', 'School Tennis') 

GROUP BY studentActivities.name, studentActivities.countryofbirth 

UNION 

SELECT 
studentActivities.name AS [student_name], 
studentActivities.countryofbirth AS [County_of_Birth], 
COUNT (DISTINCT activity) as [Out_of_School Activities] 

FROM studentActivities 

WHERE studentActivities.activity NOT IN ('School Swimming', 'School Running', 'School Soccer', 'School Tennis') 

GROUP BY studentActivities.name, studentActivities.countryofbirth 
School Swimming, School Running, School Soccer, School Tennis

は、私は何を取得したいと思いするほどのテーブルです:

この

は経由 4で、学校の活動によって区別されます

しかし、これは私が望む結果を私に与えることはありません。私が望む結果を得るにはどうすればいいですか?

答えて

1

条件付き集約を使用してcount(distinct ...)

select 
    Name 
    , CountryOfBirth 
    , InSchoolActivies = count(distinct case when activity in ('School Swimming', 'School Running', 'School Soccer', 'School Tennis') then activity end) 
    , OutOfSchoolActivies = count(distinct case when activity not in ('School Swimming', 'School Running', 'School Soccer', 'School Tennis') then activity end) 
from StudentActivites 
group by Name, CountryOfBirth 

rextesterデモで:http://rextester.com/EMUWU73595

返品:(サンプルデータでBobの生年月日を修正した後)

+-------+----------------+------------------+---------------------+ 
| Name | CountryOfBirth | InSchoolActivies | OutOfSchoolActivies | 
+-------+----------------+------------------+---------------------+ 
| Bob | USA   |    2 |     1 | 
| Jane | USA   |    0 |     0 | 
| Sarah | Australia  |    0 |     1 | 
+-------+----------------+------------------+---------------------+ 
+0

ありがとうございます!ソリューションはうまくいきます! – Techno04335

+0

@ Techno04335お手伝いします! – SqlZim

1

あなたは

WITH InSchool AS (
SELECT 
studentActivities.name AS [student_name], 
studentActivities.countryofbirth , 
COUNT (DISTINCT activity) as [In-School Activities] 
FROM studentActivities 
WHERE studentActivities.activity IN ('School Swimming', 'School Running', 'School Soccer', 'School Tennis') 
GROUP BY studentActivities.name, studentActivities.countryofbirth 
) 
, OutOfSchool AS ( 
SELECT 
studentActivities.name AS [student_name], 
studentActivities.countryofbirth, 
COUNT (DISTINCT activity) as [Out_of_School Activities] 
FROM studentActivities 
WHERE studentActivities.activity NOT IN ('School Swimming', 'School Running', 'School Soccer', 'School Tennis') 
GROUP BY studentActivities.name, studentActivities.countryofbirth 
) 
SELECT 
    COALESCE(InSchool.student_name, OutOfSchool.student_name) [Student_Name], 
    COALESCE(InSchool.CountryOfBirth, OutOfSchool.CountryOfBirth) CountryOfBirth, 
    COALESCE(InSchool.[In-School Activities],0) [In-School Activities], 
    COALESCE(OutOfSchool.[Out_of_School Activities],0) [Out_of_School Activities] 
FROM InSchool 
FULL OUTER JOIN OutOfSchool 
ON InSchool.[student_name] = OutOfSchool.[Student_name] 
AND InSchool.CountryOfBirth = OutOfSchool.CountryOfBirth 
1

に参加し、この私の心に来るもの、私は確認していなかったが、これは、私はあなたが参加を使用する必要があるタスク

SELECT 
name AS [student_name], 
countryofbirth AS [Manufacturer], 
sum (activity IN ('School Swimming', 'School Running', 'School Soccer', 'School Tennis')) as [In-School Activities], 
sum (activity NOT IN ('School Swimming', 'School Running', 'School Soccer', 'School Tennis')) as [Out_of_School Activities] 
FROM (select distinct studentActivities.name, studentActivities.countryofbirth, activity) x 
GROUP BY name, countryofbirth; 
+1

ここで2番目のエイリアスを取得しましたか? – Eli

1

を得た方法で完全外部を使用する必要があります私はCaseステートメントを使用します

select a.[student_name], a.[student_name], a.[In-School Activities], b.[Out_of_School Activities] 
from ( 
    SELECT 
    studentActivities.name AS [student_name], 
    studentActivities.countryofbirth AS [Manufacturer], 
    COUNT (DISTINCT activity) as [In-School Activities] 
    FROM studentActivities 
    WHERE studentActivities.activity IN ('School Swimming', 'School Running', 'School Soccer', 'School Tennis') 
    GROUP BY studentActivities.name, studentActivities.countryofbirth 
) a 
left join (
    SELECT 
    studentActivities.name AS [student_name], 
    studentActivities.countryofbirth AS [Manufacturer], 
    COUNT (DISTINCT activity) as [Out_of_School Activities] 
    FROM studentActivities 
    WHERE studentActivities.activity NOT IN ('School Swimming', 'School Running', 'School Soccer', 'School Tennis') 
    GROUP BY studentActivities.name, studentActivities.countryofbirth 
) b on a.[student_name] = b.[student_name] and a.[Manufacturer] = b.[Manufacturer] 
1

(とない組合):

SELECT 
Sa.name AS [student_name], 
Sa.countryofbirth, 

Sum(Case when sa.activity IN ('School Swimming', 'School Running', 'School Soccer', 'School Tennis') 
then 1 else 0 end) as In_school_Activities, 

Sum(Case when sa.activity IN ('School Swimming', 'School Running', 'School Soccer', 'School Tennis') 
then 0 else 1 end) as Out_of_school_Activities 


FROM (select distinct student_name, country _of_birth, activity from studentActivities) sa 

GROUP BY sa.name, sa.countryofbirth 

重要なアプリケーションでは、名前は一意であるとは限りません。あなたが識別子を持っているなら(そしてあなたが名前を持っている時はいつでも)、それをグループ化するほうがいいです。

1

ピボットがこれを解決します。私はあなたが提供したデータでそれをテストし、それは期待通りに機能しました。

SELECT 
    Name 
    ,countryofbirth 
    , [In-School Activity] 
    ,[Out-of-School Activity] 
FROM(
    SELECT 
     name 
     ,countryofbirth 
     ,CASE Activity 
      WHEN 'School Swimming' THEN 'In-School Activity' 
      WHEN 'School Running' THEN 'In-School Activity' 
      WHEN 'School Soccer' THEN 'In-School Activity' 
      WHEN 'School Tennis' THEN 'In-School Activity'   
      ELSE 'Out-of-School Activity' 
     END class 
    FROM studentactivities 
    WHERE Activity IS NOT NULL 
)AS base 
PIVOT(
    COUNT (CLASS) 
    FOR CLASS IN ([In-School Activity],[Out-of-School Activity]) 
)AS Pivoted 
1

ボブには2つの生殖拠点があります。それは2つの別々のボブですか?それともエラーですか?それは100%正確ではないかもしれませんので、私はこれをテストしていないが、私は、共通テーブル式を好む:

;WITH cte 
AS (
SELECT DISTINCT NAME AS [student_name] 
    ,countryofbirth 
    ,activity 
    ,CASE 
     WHEN activity IN (
       'School Swimming' 
       ,'School Running' 
       ,'School Soccer' 
       ,'School Tennis' 
       ) 
      THEN 1 
     ELSE 0 
     END AS [In-school_activity_count] 
    ,CASE 
     WHEN activity IS NOT NULL 
      AND activity NOT IN (
       'School Swimming' 
       ,'School Running' 
       ,'School Soccer' 
       ,'School Tennis' 
       ) 
      THEN 1 
     ELSE 0 
     END AS [Out-of-school_activity_count] 
FROM studentActivities 
) 
SELECT student_name 
,countryofbirth 
,SUM([In-school_activity_count]) AS [In-School_Activities] 
,SUM([Out-of-school_activity_count]) AS [Out_of_School_Activities] 
FROM cte 
GROUP BY student_name 
,countryofbirth 
1

以下は簡単で、動作します:

select name,countryofbirth,sum(schoolflag) as 
In_School_Activity,sum(nonschool) Out_Of_School_Activity 
from 
(
select activity,name,countryofbirth, 
case when activity like 'School%' then 1 else 0 end as schoolflag , 
case when activity not like 'School%' then 1 else 0 end as nonschool 
from schoolactivity 
)a 
group by name,countryofbirth 
関連する問題