【问题标题】:How to create SQL query results in columns from rows?如何在行中的列中创建 SQL 查询结果?
【发布时间】:2012-10-24 05:10:15
【问题描述】:

来自下表:

  • store(store_id, ...) {1 ..., 2 ...} 两个商店
  • 电影(film_id, ...)
  • 库存(inventory_id、film_id、store_id)
  • rental(rental_id、rental_date、inventory_id、return_date, ...)
  • 类别(category_id, name)
  • film_category(film_id, category_id)
  • 电影(film_id、rental_duration、rental_rate, ...)
  • 客户(customer_id, store_id, ...)

我可以在表上得到如下查询结果吗?

CategoryName | "Store 1: avaliable items" | "Store 1: unavaliable items" | "Store 2: avaliable items" | "Store 2: unavaliable items"

请注意,结果表中的存储文件位于不同的列中,而不是在同一列中。

我不是在寻找结果查询,我在寻找如何让它们并排存储 1、存储 2。

案例 SQL 语句可以在这里提供帮助吗?

【问题讨论】:

    标签: sql database oracle


    【解决方案1】:

    以以下形式获取您的数据(可能您必须合并多个选择)

    category item             count
    -------- ---------        -----
    cat 1    store1-avail     a1
    cat 1    store1-unavail   b1
    cat 1    store2-avail     c1
    cat 1    store2-unavail   d1
    cat 2    store1-avail     a2
    cat 2    store1-unavail   b2
    

    然后使用pivot function 将其转换为您想要的布局

    category  store1-avail store1-unavail store2-avail ...
    --------- ------------ -------------- ------------
    cat 1     a1           b1             c1
    cat 2     a2           b2             c2
    ...
    

    【讨论】:

      猜你喜欢
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 2014-10-15
      • 2020-02-07
      • 2017-02-07
      • 1970-01-01
      相关资源
      最近更新 更多