Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.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_Sql Server 2008 - Fatal编程技术网

在SQL中连接两个表

在SQL中连接两个表,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我正在寻找一个从表1和表2生成结果的解决方案。有关详细信息,请参见下图 Table1.OrderID=Table2.OrderID 我不是在寻找一个简单的连接查询。在输出表1中,值不重复 select Table1.*, Table2.Items, Table2.Quantity -- List the columns you want. I've specified the table name to avoid ambiguous column errors, and * means al

我正在寻找一个从表1和表2生成结果的解决方案。有关详细信息,请参见下图

Table1.OrderID=Table2.OrderID

我不是在寻找一个简单的连接查询。在输出表1中,值不重复

select Table1.*, Table2.Items, Table2.Quantity -- List the columns you want. I've specified the table name to avoid ambiguous column errors, and * means all columns
from Table1
inner join Table2 -- This is a join (inner gets only the records that exist in both tables)
on Table1.OrderID = Table2.OrderID -- This is the join condition, you define which columns are the same between the tables
对于空白位,因为您缺乏在显示层中处理此问题的意识:

with CTE as
(
select Table1.*, 
       Table2.Items, 
       Table2.Quantity,
       row_number() over(partition by Table1.OrderID order by Items ) as rn
    from Table1
    inner join Table2 
    on Table1.OrderID = Table2.OrderID 
)

select case when rn = 1 then OrderID end as OrderID,
       case when rn = 1 then CustomerName end as CustomerName ,
       case when rn = 1 then CTE.Desc end as Desc,
       Items,
       Quantity
from CTE

最后,我找到了一个解决方案,解决了我一直在寻找的问题

WITH MasterTable as (SELECT ROW_NUMBER() OVER(PARTITION BY OrderID ORDER BY OrderID ASC) AS SlNo,* FROM Table1),
DetailTable as (SELECT ROW_NUMBER() OVER(PARTITION BY OrderID ORDER BY OrderID ASC) AS SlNo,* FROM Table2)

SELECT * FROM MasterTable A FULL JOIN DetailTable B
ON A.OrderID = B.OrderID AND A.SlNo=B.SlNo
ORDER BY B.OrderID

您可以通过在(按顺序划分)上使用行数()来实现此结果。 代码:

输出:

orderid custname    Descp   item    quentity
   1    Ccustomer1  1item   Pen     1
   2    Ccustomer2  2item   Ball    2
                             Bat    1
   3    Ccustomer3  3item   Book    2
                            Box     1
                            Bag     1
                            Pen     2

这是一个基本的连接。显示您的查询尝试!请将脚本和数据发布为文本,而不是图像。请进行一些研究,这不是您需要在此处提出的问题,您可以通过快速搜索找到答案。示例一:@Vinoth_S,right join-为什么?我不是在寻找join查询。请重新检查我的结果。在输出表1中,值不重复。这将是一个好朋友。你搞定了。好:——)
orderid custname    Descp   item    quentity
   1    Ccustomer1  1item   Pen     1
   2    Ccustomer2  2item   Ball    2
                             Bat    1
   3    Ccustomer3  3item   Book    2
                            Box     1
                            Bag     1
                            Pen     2