Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接两个表和结果数据时,sql中是否应包含不重复的项?_Sql_Sql Server_Join - Fatal编程技术网

连接两个表和结果数据时,sql中是否应包含不重复的项?

连接两个表和结果数据时,sql中是否应包含不重复的项?,sql,sql-server,join,Sql,Sql Server,Join,我有两张像下面这样的桌子 学生1表: +---+--------+-----+ |SID|SSection|SRank| +---+--------+-----+ | a| 1| 1| | b| 2| 2| | d| 4| 2| | e| 4| 1| | c| 3| 4| +---+--------+-----+ 学生2表格: +---+--------+-----+ |SID|SSection

我有两张像下面这样的桌子

学生1表:

+---+--------+-----+
|SID|SSection|SRank|
+---+--------+-----+
|  a|       1|    1|
|  b|       2|    2|
|  d|       4|    2|
|  e|       4|    1|
|  c|       3|    4|
+---+--------+-----+
学生2表格:

+---+--------+-----+
|SID|SSection|SRank|
+---+--------+-----+
|  a|       2|    1|
|  b|       2|    3|
|  f|       4|    2|
|  e|       4|    1|
|  c|       3|    4|
+---+--------+-----+
我想写一个select查询,它应该给出如下结果:

+---+--------+----------+----------+
|SID|SSection|test1SRank|test2SRank|
+---+--------+----------+----------+
|  f|       4|         0|         2|
|  e|       4|         1|         1|
|  d|       4|         2|         0|
|  c|       3|         4|         4|
|  b|       2|         2|         3|
|  a|       1|         1|         0|
|  a|       2|         0|         1|
+---+--------+----------+----------+


相同查询的一种更简单的方法如下:使用第二个查询生成相同的输出

;with cte as (

    select
        SID, SSection, SRank as test1SRank, null as test2SRank
    from student1 s1

    union all

    select
        SID, SSection, null as test1SRank, SRank as test2SRank
    from student2 s2

)
select
    SID, SSection, 
    isnull(max(test1SRank),0) test1SRank, 
    isnull(max(test2SRank),0) test2SRank
from cte
group by 
    SID, SSection
请尝试使用稠密的_RANK()函数来实现以下SQL CTE表达式,好吗


相同查询的一种更简单的方法如下:使用第二个查询生成相同的输出

;with cte as (

    select
        SID, SSection, SRank as test1SRank, null as test2SRank
    from student1 s1

    union all

    select
        SID, SSection, null as test1SRank, SRank as test2SRank
    from student2 s2

)
select
    SID, SSection, 
    isnull(max(test1SRank),0) test1SRank, 
    isnull(max(test2SRank),0) test2SRank
from cte
group by 
    SID, SSection
请尝试使用稠密的_RANK()函数来实现以下SQL CTE表达式,好吗


联合asn CTE的另一种方式:

;WITH cte AS (
    SELECT  [SID],
            SSection,
            SRank as test1SRank,
            0 as test2SRank
    FROM student1 
    UNION ALL
    SELECT  [SID],
            SSection,
            0 as test1SRank,
            SRank as test2SRank
    FROM student2
)

SELECT  [SID],
        SSection,
        MAX(test1SRank) as test1SRank,
        MAX(test2SRank) as test2SRank
FROM cte
GROUP BY [SID],
        SSection
输出:

SID  SSection    test1SRank  test2SRank
---- ----------- ----------- -----------
a    1           1           0
a    2           0           1
b    2           2           3
c    3           4           4
d    4           2           0
e    4           1           1
f    4           0           2

联合asn CTE的另一种方式:

;WITH cte AS (
    SELECT  [SID],
            SSection,
            SRank as test1SRank,
            0 as test2SRank
    FROM student1 
    UNION ALL
    SELECT  [SID],
            SSection,
            0 as test1SRank,
            SRank as test2SRank
    FROM student2
)

SELECT  [SID],
        SSection,
        MAX(test1SRank) as test1SRank,
        MAX(test2SRank) as test2SRank
FROM cte
GROUP BY [SID],
        SSection
输出:

SID  SSection    test1SRank  test2SRank
---- ----------- ----------- -----------
a    1           1           0
a    2           0           1
b    2           2           3
c    3           4           4
d    4           2           0
e    4           1           1
f    4           0           2

你试过什么?显示您当前的查询尝试。为什么有两个类似的表?您尝试了什么?向我们展示您当前的查询尝试。为什么有两个类似的表?Hi@MatBailie,实际上输出需要两行SID=A,因为不同的S分区值(1和2)我得到了一个,1,1,1 Arow@Eralper抱歉,有一个输入错误(
和student2.SSection=student2.SSection
错误,但现已更正).是的,现在正如期而至!Hi@MatBailie,实际上输出需要两行SID=A,因为不同的S分区值(1和2)我得到了一个,1,1,1 Arow@Eralper抱歉,有一个输入错误(
和student2.SSection=student2.SSection
错误,但现在已更正)。是的,现在正如预期的那样!
densite\u RANK()
实现了什么?在我看来,如果不使用第二个CTE,只需按
SID,SSection
进行分组,
densite\u RANK()
实现了什么?在我看来,如果不使用第二个CTE,只需按
SID、SSection