【问题标题】:SQL Query pivot approach assistanceSQL 查询数据透视方法帮助
【发布时间】:2012-02-11 13:35:05
【问题描述】:

我真的在这个支点上苦苦挣扎,希望寻求帮助和启蒙可能会有所帮助。

假设我有下表.... 表A

type      actId date        rowSort   order value   value_char  colName
------------------------------------------------------------------------------------
checking  1003   2011-12-31  2          1   44      44          Amount
checking  1003   2011-12-31  2          2   55      55          Interest
checking  1003   2011-12-31  2          3   66      66          Change
checking  1003   2011-12-31  2          4   77      77          Target
checking  1003   2011-12-31  2          5   88      88          Spread
savings   23456  2011-12-31  1          1   999     999         Amount
savings   23456  2011-12-31  1          2   888     888         Interest
savings   23456  2011-12-31  1          3   777     777         Change
savings   23456  2011-12-31  1          4   666     666         Target
savings   23456  2011-12-31  1          5   555     555         Spread

我想转置到表 b

checking  chkId date        rowSort   order chkvalue  chkValchar  colName  savings   savId  savVal  savValChar
-------------------------------------------------------------------------------------------------------------------
checking  1003   2011-12-31  2          1   44        44          Amount    savings   23456  999          999           
checking  1003   2011-12-31  2          2   55        55          Interest  savings   23456  888          888           
checking  1003   2011-12-31  2          3   66        66          Change    savings   23456  777          777
checking  1003   2011-12-31  2          4   77        77          Target    savings   23456  666          666
checking  1003   2011-12-31  2          5   88        88          Spread    savings   23456  555          555

我承认目前这超出了我的能力范围。
我相信我需要使用 rowSort(识别储蓄与检查)以及使用 order 列进行排序来对这张表进行透视。这可能是错误的,这就是我在这里的原因。

支点是正确的方法吗?我是否正确假设我的支点是使用聚合 max(rowSort)?

【问题讨论】:

  • 这些数据集似乎缺少某些内容 - 特别是客户 ID。你怎么知道支票账户 1003 应该与储蓄账户 23456 相关联?另外,如果客户有多个支票和/或多个储蓄账户怎么办?
  • 您需要提供有关数据的更多详细信息...检查行与保存行的关系是基于顺序还是列名?日期因素吗?你需要在 rowSort 中看到什么?不清楚table1中的数据如何变成table2中的数据,需要提供更多细节
  • @looktheninjas:您似乎需要根据colNameorder 为每个客户端分组数据,在我看来,顺序似乎是唯一标识colName 的键。即 colName 为 Amount 的所有记录加上 Intereset 并不重要,它是检查或保存。所有这些分组将针对每个客户。你能澄清一下吗?

标签: sql sql-server-2005 pivot


【解决方案1】:

假设rowSort 来自`checking equal to rowSort+1 来自savings 并且行链接通过field value,应该这样做:

SELECT DISTINCT

  a.type as checking,
  a.actId as chkId,
  a.date,
  a.rowSort+1,
  a.order,
  a.value as chkvalue,
  a.value_char as chkValchar,
  a.colName,
  b.type as 'savings',
  a.actId as savId,
  b.value as savVal,
  b.value_char as savValChar
  FROM tablea a
  INNER JOIN tablea b ON b.rowSort = a.rowSort+1 and b.value = a.value

【讨论】:

  • 是的,你们两个都是对的,有一个客户 ID 列,它对应于客户将拥有的支票储蓄 mmarket 账户的数量。很抱歉造成混乱
  • aF 我回来了双双排。因此,我返回 25 行,而不是 5 行。
  • @looktheninjas 我已经明确表示了,现在呢?如果您还有 25 行,请在您的问题上发布结果。
【解决方案2】:

根据您提出的要求,您不会为此查询使用PIVOT,而是希望将您的表JOIN 分配给它自己。下面的查询应该为您提供所需的记录,而无需使用 DISTINCT

select c.type as checking
    , c.actId as chkid
    , c.date
    , c.rowsort
    , c.[order]
    , c.value as chkvalue
    , c.value_char as chkValchar
    , c.colName
    , s.type as savings
    , s.actId as savId
    , s.value as savVal
    , s.value_char as savValchar
from t1 c
inner join t1 s
    on c.rowsort = s.rowsort + 1
    and c.[order] = s.[order]

SQL Fiddle with Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 2018-05-04
    • 2011-06-18
    相关资源
    最近更新 更多