连接两个 Table 并计算 table 2 中的匹配列

Join Two Table and count matching columns from table 2

我有两个 table 并且想要基于两者的组合 table。

原样table....

Combine table last column
show count
from Table 2 columns (Subject1, Subject2, Subject3, Subject4, Subject5, Subject6)
where table1.course = table2.course match

结果如下图所示:

我正在使用 MS SQL 服务器 2008。我想创建一个 SQl 查询并将其保存为视图,以便我可以在 vb.net 中使用它windows申请并通过RDLC报告展示......

试试这个例子:

SELECT     dbo.table1.Edate, dbo.table1.Course, dbo.table1.Subject AS SubjectTitle, COUNT(dbo.table2.Subject1) AS Subject1, COUNT(dbo.table2.Subject2) AS Subject2, COUNT(dbo.table2.Subject3) 
                      AS Subject3, COUNT(dbo.table2.Subject4) AS Subject4, COUNT(dbo.table2.Subject5) AS Subject5, COUNT(dbo.table2.Subject6) AS Subject6
FROM         dbo.table1 CROSS JOIN
                      dbo.table2
GROUP BY dbo.table1.Edate, dbo.table1.Course, dbo.table1.Subject 

大概是这个

WITH subjectList AS
(
  SELECT course, Subject1 as subject 
  FROM TABLE2
  UNION ALL
  SELECT course, Subject2 as subject 
  FROM TABLE2
  UNION ALL
  SELECT course, Subject3 as subject 
  FROM TABLE2
  UNION ALL
  SELECT course, Subject4 as subject 
  FROM TABLE2
  UNION ALL
  SELECT course, Subject5 as subject 
  FROM TABLE2
  UNION ALL
  SELECT course, Subject6 as subject 
  FROM TABLE2
)
SELECT T1.Edate, T1.course, sl.subject, count(*) as subject_count
FROM TABLE1 T1
JOIN subjectList sl on T1.Course = sl.course and T1.Subject = sl.subject
GROUP BY T1.Edate, T1.course, sl.subject

可能是这个

WITH subjectList AS
(
  SELECT course, Subject1 as subject 
  FROM TABLE2
  UNION ALL
  SELECT course, Subject2 as subject 
  FROM TABLE2
  UNION ALL
  SELECT course, Subject3 as subject 
  FROM TABLE2
  UNION ALL
  SELECT course, Subject4 as subject 
  FROM TABLE2
  UNION ALL
  SELECT course, Subject5 as subject 
  FROM TABLE2
  UNION ALL
  SELECT course, Subject6 as subject 
  FROM TABLE2
)
SELECT T1.Edate, T1.course, sl.subject, 
       count(*) OVER(PARTITION BY T1.course, sl.subject) as subject_by_course_count,
       count(*) OVER(PARTITION BY sl.subject) as subject_count
FROM TABLE1 T1
JOIN subjectList sl on T1.Course = sl.course and T1.Subject = sl.subject
GROUP BY T1.Edate, T1.course, sl.subject

试试这个查询:

select t.Edate,
t.Course,
t.Subject,
sum(case when t.Subject = t2.Subject1 or t.Subject = t2.Subject2 or t.Subject = t2.Subject3 or t.Subject = t2.Subject4 or t.Subject = t2.Subject5 or t.Subject = t2.Subject6 then 1 else 0 end) [Count]
from Table1 t
join Table2 t2 on t.Course = t2.Course
group by t.Edate, t.Course, t.Subject
order by t.Edate, t.Course

SQLFiddle

谢谢大家, 我在你的帮助下解决了它,如果有人想要这个 这里是:

    select t.Edate,t.Etime,
t.Course,
t.Subject,t.Paper,t.Code,
sum(case when t2.adcat = 'Regular' AND  t.Subject = S.Subject then 1 else 0 end) [Reg],
sum(case when t2.adcat = 'Private' AND  t.Subject = S.Subject then 1 else 0 end) [Pri],
sum(case when t2.adcat = 'Ex.' AND  t.Subject = S.Subject then 1 else 0 end) [Ex],
sum(case when t2.adcat = 'Back Paper' AND  t.Subject = S.Subject then 1 else 0 end) [BP],
sum(case when t2.adcat = 'Single Subject' AND  t.Subject = S.Subject then 1 else 0 end) [SS],
sum(case when t2.adcat = 'Improvement' AND  t.Subject = S.Subject then 1 else 0 end) [Imp]
from EXAMscheme t
join tblstudetail t2 CROSS APPLY(VALUES (subjectI), (subjectII), (subjectIII), (subjectIV), (subjectV), (subjectVI)) AS S(subject)
on t.Course = t2.Course and
 t.session = t.session and
  t.session = '2015-16' and 
  t2.session = '2015-16' AND
  t2.EXstatus = 'OK'
group by t.Edate,t.Etime,t.Paper,t.Code, t.Course, t.Subject
order by t.Edate, t.Course