在SQL Server中合并两个表

时间:2019-02-11 09:55:45

标签: sql-server

我有m_peserta表。

no_test | name
-----------------------
11      | Mr. X 
21      | Mr. Y 
31      | Mr. Z 
41      | Mr. P 

我有结果表

no_test     | quest_cat | answer    
-------------------------------------------
11          | Type I-2  | 1 
11          | Type I-5  | 1 
11          | Type I-6  | 1 
11          | Type II-2 | My Answer is bla
11          | Type II-4 | My Answer is bla 
21          | Type I-5  | 1 
21          | Type I-6  | 1 
21          | Type II-3 | My Answer is bla
21          | Type II-1 | *note : this is a Null Answer 
21          | Type III-1| *note : this is a Null Answer 
21          | Type III-3| My Answer is bla

我有一个名为combin_table的空表,其列为no_test,名称,count_quest_I,count_quest_II,count_quest_III。

我需要从组合表m_peserta和结果中查询“ INSERT”,所以我有一个这样的combin_table

no_test | name  | count_quest_I | count_quest_II | count_quest_III 
-----------------------------------------------------
11      | Mr. X | 3             | 2              | 0        
21      | Mr. Y | 2             | 1              | 1   

请帮助,谢谢

2 个答案:

答案 0 :(得分:0)

与group by一起加入会有所帮助。

请参阅下面的查询

insert into combine_table 
   (a.no_test,
    a.name,
    count_quest_I,
    count_quest_II,
    count_quest_III)
select 
    a.no_test,
    a.name,
    count_quest_I = sum(case when quest_cat like 'Type I-%' then 1 else 0 end),
    count_quest_II = sum(case when quest_cat like 'Type II-%' then 1 else 0 end),
    count_quest_III = sum(case when quest_cat like 'Type III-%' then 1 else 0 end)
from
    m_peserta  a join result r
        on a.no_test=r.no_test
group by 
    a.no_test,a.name

答案 1 :(得分:0)

您可以尝试以下操作。

import Image, ImageTk   #Necesary to display pictures
import Tkinter

root = Tkinter.Tk()
img = ImageTk.PhotoImage(Image.open("Cat.jpg"))
panel = Tkinter.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")

root.mainloop()