加入两个表并计算表2中的匹配列

时间:2016-02-22 17:53:07

标签: sql sql-server sql-server-2008 vb.net-2010

我有两个表,并希望基于两者的组合表。

表格原样....

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

下图显示了结果: enter image description here

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

4 个答案:

答案 0 :(得分:0)

请尝试使用此示例:

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 

答案 1 :(得分:0)

可能这个

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

答案 2 :(得分:0)

尝试此查询:

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

答案 3 :(得分:0)

thanx all, 如果有人想要的话,我会在你的帮助下解决它 这是:

    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