【问题标题】:need to join two tables without duplication in sql需要在sql中连接两个没有重复的表
【发布时间】:2017-04-03 16:04:33
【问题描述】:

表1

  `s.no | name   |total_price
 ------ | ------ |------------
      1 | kathir |  100'

表2

  `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 | name   |total_price| rent_name | rent_price
    ------| -------|-----------|-----------|------
      1   | kathir |  100      | k1        |  10
      2   |kathir  |  100      | k2        |20

当我计算 kathir 总价的总和时,它给我 200 但实际只有 100 预期结果:

    `s.no | name   |total_price| rent_name | rent_price
    ------| -------|-----------|-----------|------
      1   | kathir |  100      | k1        |  10
      2   |kathir  |   0      | k2        |20

如果我计算 kathir 的总价格的总和是给我 100 和租金名称 k1 和 k2 和租金价格 10,20 和总和是 30..如何实现这一点

【问题讨论】:

标签: sql postgresql


【解决方案1】:

表1

  `s.no | name   |total_price
 ------ | ------ |------------
      1 | kathir |  100
 ------ | ------ |------------
      2 |mani    |  200

在表一中可以有许多记录

 `s.no | rent_name   |rent_price| f_key
 ------ | ------ -----|----------|------
      1 | k1          |  10      | 1
      2 | k2          | 20       | 1
      3 |k3           | 30       |2
       4|k1            |10        |2

选择 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 | 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

【讨论】:

  • 这个答案与您的问题有何不同?
【解决方案2】:

尝试我编辑的答案如下

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 

【讨论】:

  • 它使所有行价格为 0 sumit 我只想要 kathir 一个记录价格是 100,而不是全为零
【解决方案3】:

表1

  `s.no | name   |total_price
 ------ | ------ |------------
      1 | kathir |  100
 ------ | ------ |------------
      2 |mani    |  200
      3  vinoth    300
      4  dinesh    400

在表一中可以有许多记录

 `s.no | rent_name   |rent_price| f_key
 ------ | ------ -----|----------|------
      1 | k1          |  10      | 1
      2 | k2          | 20       | 1
      3 |k3           | 30       |2
       4|k1            |10        |2

选择 t2.s_no, t1.name,(当 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

结果:

    `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
       5     vinoth  | 300
       6     dinesh  |  0

【讨论】:

    【解决方案4】:

    我的最新答案。它根据您的要求工作

    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
    

    sqlfiddle 的工作链接是http://sqlfiddle.com/#!15/6cf9d

    【讨论】:

    • 如果我的回答对您有帮助,请接受我的回答。
    猜你喜欢
    • 2021-02-23
    • 1970-01-01
    • 2018-09-17
    • 2017-01-25
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 2015-12-06
    相关资源
    最近更新 更多