我正在寻找一个解决方案,以从表Table 1和Table结果。请参阅下面的图像的详细信息加入两个表中的SQL
Table1.OrderID = Table2.OrderID
我不是寻找一个简单的连接查询。在输出Table1中值不重复。
我正在寻找一个解决方案,以从表Table 1和Table结果。请参阅下面的图像的详细信息加入两个表中的SQL
Table1.OrderID = Table2.OrderID
我不是寻找一个简单的连接查询。在输出Table1中值不重复。
,最后我得到了什么,我正好在寻找
的解决方案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
好哥们。你钉了它。好的:-) –
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
我不想查找连接查询。请重新检查我的结果。在输出Table1中值不重复。这将是空白 –
您可以通过使用ROW_NUMBER()OVER(PARTITION BY ORDER BY)实现这一结果 代码:
select
CASE WHEN ROW_NUMBER() OVER(PARTITION BY tb1.orderid ORDER BY tb1.orderid) = 1 THEN tb1.orderid ELSE null END AS orderid,
CASE WHEN ROW_NUMBER() OVER(PARTITION BY tb1.custname ORDER BY tb1.orderid) = 1 THEN tb1.custname ELSE '' END AS custname,
CASE WHEN ROW_NUMBER() OVER(PARTITION BY tb1.Descp ORDER BY tb1.orderid) = 1 THEN tb1.Descp ELSE '' END AS Descp,
tb2.item,tb2.quentity
from tb1 inner join tb2 on tb1.orderid=tb2.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
这是一个基本的JOIN。向我们展示您的查询尝试! – jarlh
请将脚本和数据作为文本发布,而不是图片。 – etsa
请做一些研究,这不是你需要在这里问的问题,你可以通过快速搜索找到答案。 – Tanner