【问题标题】:Can rows be pivoted into columns by using a SQL self join?可以使用 SQL 自联接将行转置为列吗?
【发布时间】:2020-12-20 22:31:30
【问题描述】:

对于下面提到的表说 datarate_tbl,我需要自联接方面的帮助

我需要比较两个日期的数据,例如同一张表中的 18 日和 20 日。 我需要查询数据以从同一个表中提取数据,以便将 file_size 和 rate 列的数据与第 18 个数据对齐

查询到的数据应该是这样的

我尝试使用自联接编写查询,但努力获取结果数据集。希望有任何帮助

我的查询给了我额外的两行而不是 5 行。问题发生在网络并行处。

 select a.date::date ,a.hostname,a.test_type,a.file_size,a.rate ,b.date::date ,b.file_size ,b.rate

from checkperf_allproduct_metrics a

left join(select * from checkperf_allproduct_metrics where date::date = '2020-12-18') b

on a.instance = b.instance

and a.hostname=b.hostname

and a.test_type =b.test_type

where a.instance = 'nytpsg00394'

and a.date::date = '2020-12-20'

;



date    |                        hostname                        |  test_type   | file_size  |    rate     |    date    | file_size  |    rate

------------+--------------------------------------------------------+--------------+------------+-------------+------------+------------+-------------

2020-12-20 | abc.com.in                             | disk write   | 1073741824 |      297.67 | 2020-12-18 | 1073741824 |      344.78

2020-12-20 | abc.com.in                             | disk read    | 1073741824 |     3413.33 | 2020-12-18 | 1073741824 |     2438.10

2020-12-20 | abc.com.in                             | stream       |          0 |    15887.52 | 2020-12-18 | 1073741824 |    15116.31

2020-12-20 | abc.com.in-> abc.com.in| Net Parallel |            | 3187.470000 | 2020-12-18 | 1073741824 | 3285.460000

2020-12-20 | abc.com.in-> abc.com.in| Net Parallel |            | 3187.470000 | 2020-12-18 | 1073741824 | 3215.270000

2020-12-20 | abc.com.in-> abc.com.in| Net Parallel |            | 3163.380000 | 2020-12-18 | 1073741824 | 3285.460000

2020-12-20 | abc.com.in-> abc.com.in| Net Parallel |            | 3163.380000 | 2020-12-18 | 1073741824 | 3215.270000

问题实际上发生在 Net Parallel 列中。如果你能帮我解决这个问题。

【问题讨论】:

    标签: sql postgresql pivot inner-join self-join


    【解决方案1】:

    我会推荐条件聚合:

    select hostname, test_type,
        max(file_size) filter(where date = date '2020-12-18') as file_size_18,
        max(rate) filter(where date = date '2020-12-18') as rate_18,
        max(file_size) filter(where date = date '2020-12-20') as file_size_20,
        max(rate) filter(where date = date '2020-12-20') as rate_20
    from mytable t
    where date in (date '2020-12-18', date '2020-12-20')
    group by hostname, test_type
    

    【讨论】:

    • 非常感谢 GMB 的回复。第二个非常接近,但我遇到了同样的问题,它给出了一些重复的行。那么第一个选项可能在 postgress 中不起作用。你能帮忙在postgress中提供查询吗
    • @Asrar:第二个查询不会为您的示例数据生成重复项。至于第一个,这是 Postgres 中支持的语法。试试看(我刚刚更新了id)
    • Dint 帮了我 GMB 第二个是给我额外的两行,我想避免。
    • 第一个力给出了预期的结果。如果您可以帮助我修复查询以获取 5 行发布在所需数据集中,我已完成编辑
    • @Asrar:使用第一个查询。我删除了另一个。我保证每个主机名和测试类型一行
    【解决方案2】:

    像这样在 sql 查询中尝试使用 max 和 filter 函数。

    select hostname, test_type,
    
        max(file_size) filter(where date::date = date '2020-12-18') as file_size_18,
    
        max(rate) filter(where date::date = date '2020-12-18') as rate_18,
    
        max(file_size) filter(where date::date = date '2020-12-20') as file_size_20,
    
        max(rate) filter(where date::date = date '2020-12-20') as rate_20
    
    from checkperf_allproduct_metrics t
    
    where date::date in (date '2020-12-18', date '2020-12-20')
    
    group by hostname, test_type;
    

    输出

                          hostname                        |  test_type   | file_size_18 |   rate_18   | file_size_20 |   rate_20
    
    --------------------------------------------------------+--------------+--------------+-------------+--------------+-------------
    
    abc.com.in                             | disk read    |              |             |   1073741824 |     2925.71
    
    abc.com.in                             | disk write   |              |             |   1073741824 |      449.12
    
    abc.com.in                             | stream       |              |             |            0 |    15354.96
    
    abc.com.in->abc.com.in| Net Parallel |              |             |              | 3164.820000
    
    abc.com.in                             | disk read    |   1073741824 |     2438.10 |   1073741824 |     3413.33
    
    abc.com.in                             | disk write   |   1073741824 |      344.78 |   1073741824 |      297.67
    
    abc.com.in                             | stream       |   1073741824 |    15116.31 |            0 |    15887.52
    
    abc.com.in-> abc.com.in| Net Parallel |   1073741824 | 3285.460000 |              | 3187.470000
    

    【讨论】:

    • 我认为您的主机名或类型有一些带空格的记录。尝试在 group by 和 select 中同时使用 trim。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多