需要在sql中不重复地联接两个表
表1 表2需要在sql中不重复地联接两个表,sql,postgresql,Sql,Postgresql,表1 表2 `s.no | name |total_price ------ | ------ |------------ 1 | kathir | 100' 我的问题是: `s.no | rent_name |rent_price| f_key ------ | ------ -----|----------|------ 1 | k1 | 10 | 1 2 | k2 | 20
`s.no | name |total_price
------ | ------ |------------
1 | kathir | 100'
我的问题是:
`s.no | rent_name |rent_price| f_key
------ | ------ -----|----------|------
1 | k1 | 10 | 1
2 | k2 | 20 | 1
结果:
select t1.name,t1.total_price,t1.rent_name,t1.rent_price
from table1 t1 left join table2 t1 ON t1.s_no=t2.f_key
`s.no | rent_name |rent_price| f_key
------ | ------ -----|----------|------
1 | k1 | 10 | 1
2 | k2 | 20 | 1
3 |k3 | 30 |2
4|k1 |10 |2
当我计算凯瑟的总价时,它给了我200英镑,但实际价格只有100英镑
预期结果:
`s.no | name |total_price| rent_name | rent_price
------| -------|-----------|-----------|------
1 | kathir | 100 | k1 | 10
2 |kathir | 100 | k2 |20
`s.no | rent_name |rent_price| f_key
------ | ------ -----|----------|------
1 | k1 | 10 | 1
2 | k2 | 20 | 1
3 |k3 | 30 |2
4|k1 |10 |2
如果我计算kathir的总价之和为100,租金名称为k1和k2,租金价格为10,20,总价为30。如何实现这一点请尝试我编辑的答案,如下所示
`s.no | name |total_price| rent_name | rent_price
------| -------|-----------|-----------|------
1 | kathir | 100 | k1 | 10
2 |kathir | 0 | k2 |20
表1
在表1中可以有许多记录
`s.no | name |total_price
------ | ------ |------------
1 | kathir | 100
------ | ------ |------------
2 |mani | 200
`s.no | name |total_price
------ | ------ |------------
1 | kathir | 100
------ | ------ |------------
2 |mani | 200
3 vinoth 300
4 dinesh 400
选择t1.名称,t1.总价,t1.租金名称,t1.租金价格
从表1 t1左连接表2 t1上的t1.s_no=t2.f_键
预期结果:
`s.no | name |total_price| rent_name | rent_price
------| -------|-----------|-----------|------
1 | kathir | 100 | k1 | 10
2 |kathir | 100 | k2 |20
`s.no | rent_name |rent_price| f_key
------ | ------ -----|----------|------
1 | k1 | 10 | 1
2 | k2 | 20 | 1
3 |k3 | 30 |2
4|k1 |10 |2
我的最新答案。它能按你的要求工作
`s.no | name |total_price| rent_name | rent_price
------| -------|-----------|-----------|------
1 | kathir | 100 | k1 | 10
2 |kathir | 0 | k2 |20
3 |mani |200 |k3 |30
4 |mani |0 |k1 |10
sqlfiddle的工作链接在表1中
select t2.s_no, t1.name,(case when ROW_NUMBER() OVER(PARTITION BY f_key ORDER BY t2.s_no asc) = 1 then t1.total_price else 0 end) as total_price,t2.rent_name,t2.rent_price
from table1 t1 left join table2 t2 ON t1.s_no=t2.f_key
select ROW_NUMBER() OVER() as sl_no, t1.name,(case when ROW_NUMBER() OVER(PARTITION BY f_key ORDER BY t2.s_no asc) = 1 or f_key is null then t1.total_price else 0 end) as total_price,t2.rent_name,t2.rent_price
from table1 t1 left join table2 t2 ON t1.s_no=t2.f_key
在表1中可以有许多记录
`s.no | name |total_price
------ | ------ |------------
1 | kathir | 100
------ | ------ |------------
2 |mani | 200
`s.no | name |total_price
------ | ------ |------------
1 | kathir | 100
------ | ------ |------------
2 |mani | 200
3 vinoth 300
4 dinesh 400
选择t2.s\U编号,t1.name,(当行号()超过(按f\U键顺序按t2.s\U编号asc划分)=1,然后选择t1.total\U price,否则0结束)作为总价,t2.rent\U名称,t2.rent\U price
从表1 t1左连接表2 t2在t1上。s_no=t2.f_键
结果:
select t1.name,t1.total_price,t1.rent_name,t1.rent_price
from table1 t1 left join table2 t1 ON t1.s_no=t2.f_key
`s.no | rent_name |rent_price| f_key
------ | ------ -----|----------|------
1 | k1 | 10 | 1
2 | k2 | 20 | 1
3 |k3 | 30 |2
4|k1 |10 |2
如果你没有在
选择中使用表2,为什么要加入表2?可能的@lightsouls复制更像是精确复制。第一个结果是有意义的。它使所有行价格都为0 sumit我只想要kathir一个记录价格是100,而不是全零这个答案与你的问题有什么不同?如果我的答案对你有帮助的话那么请接受我的回答。