简体   繁体   English

SQL Server查询联接两个表

[英]SQL Server query joining two tables

I have two tables here 我这里有两张桌子

Table 1 : 表1

NAME      |MED_TYPE |MED_STATUS  |DAYS
----------|---------|------------|--------
miyo      |1b       |APPROVED    |0.5
miya      |1b       |APPROVED    |1.0
jun       |3b       |APPROVED    |1.0
kite      |3b       |APPROVED    |1.0
hans      |1b       |APPROVED    |1.0
mark      |1b       |APPROVED    |2.0
jep       |1b       |APPROVED    |1.0
Gali      |2b       |APPROVED    |0.5
Hera      |1b       |APPROVED    |0.5
Zues      |2b       |APPROVED    |0.5

Table 2 : 表2

STUDENT |MED_TYPE|REMAINING_MED|ACTIVE
--------|--------|-------------|------
miko    |3b      |1.0          |1
kite    |1b      |6.0          |1
kite    |2b      |9.5          |1
kite    |2b      |1.0          |1
jun     |1b      |10.0         |1
arri    |1b      |8.5          |1
arri    |2b      |9.5          |1
arri    |3b      |1.0          |1
imko    |1b      |6.5          |1
miko    |2b      |8.0          |1

With this query: 使用此查询:

SELECT
    NAME,
    SUM(CASE
           WHEN MED_TYPE = '1b' AND MED_STATUS = 'APPROVED' 
              THEN DAYS
              ELSE 0
        END) AS USED_1b,
    SUM(CASE
           WHEN MED_TYPE = '2b' AND MED_STATUS = 'APPROVED' 
              THEN DAYS
              ELSE 0
        END) AS USED_2b
FROM 
    table1
GROUP BY 
    NAME

I get the following results: 我得到以下结果:

STUDENT   |USED_1b|USED_2b
----------|-------|-------
abe       |3.5    |5.0
arri      |1.5    |0.5
kiko      |0.0    |0.0
chen      |4.0    |0.5
heli      |0.5    |0.0
miyo      |6.5    |5.5
mika      |2.0    |1.0
jun       |3.0    |3.0
jake      |2.5    |2.5
zues      |3.5    |2.5

but I want to get the following: 但我想得到以下内容:

NAME | USED_1b | USED_2b | REMAINING_1b | REMAINING_2b

remaining_1b and remaining_2b will be coming from table 2 column remaining_med , they can be categorized with med type. remaining_1bremaining_2b将来自表2的remaining_med列,它们可以用med类型来分类。 What connects the two tables is the name and student. 连接两个表的是姓名和学生。 Also I want to check if the student is active or not. 我也想检查学生是否活跃。 If not active it won't be displayed in the result table. 如果未激活,它将不会显示在结果表中。

What will be the right query for this? 什么是正确的查询呢? I don't know what to use join or union. 我不知道该使用join或union。 I'm pretty new to SQL Server. 我是SQL Server的新手。

BTW, the tables 1 and 2 and the results are just partial of the whole table. 顺便说一句,表1和表2以及结果只是整个表的一部分。 It will be too long if I post all here. 如果我在这里张贴全部内容,将太长了。

You can find sums separately and then join. 您可以分别找到总和,然后加入。

Like this: 像这样:

select t1.name,
    t1.USED_1b,
    t1.USED_2b,
    t2.remaining_1b,
    t2.remaining_2b
from (
    select name,
        SUM(case when MED_TYPE = '1b'
                    and MED_STATUS = 'APPROVED' then DAYS else 0 end) as USED_1b,
        SUM(case when MED_TYPE = '2b'
                    and MED_STATUS = 'APPROVED' then DAYS else 0 end) as USED_2b
    from table1
    group by name
    ) t1
join (
    select student,
        SUM(case when MED_TYPE = '1b' then remaining_med else 0 end) as remaining_1b,
        SUM(case when MED_TYPE = '2b' then remaining_med else 0 end) as remaining_2b
    from table2
    group by student
    ) t2 on t1.name = t2.student;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM