Sql 结果中同一字段的多个列

Sql 结果中同一字段的多个列,sql,oracle,pivot,multiple-columns,Sql,Oracle,Pivot,Multiple Columns,我试图查询我的数据库并返回结果,这些结果将包含来自同一字段的多个列 我的数据库中的数据可能如下所示: id, price, cost, description, color -------------------------------------------------------------- 10, 99, 50, bicycle, blue

我试图查询我的数据库并返回结果,这些结果将包含来自同一字段的多个列

我的数据库中的数据可能如下所示:

  id,        price,       cost,       description,        color
  --------------------------------------------------------------        
  10,         99,          50,         bicycle,           blue
  15,         88,          45,         tricycle,          red
  18,         90,          48,         tricycle,          blue
  20,         95,          55,         bicycle,           red
Blue, id, price, cost, description, Red, id, price, cost, description
blue, 10, 99,    50,   bicycle,     red, 15, 88,    45,   tricycle
blue, 18, 90,    48,   tricycle,    red, 20, 95,    55,   bicycle
我正在尝试编写一个查询以返回结果,该查询将为我提供多个列,将每种颜色表示为“蓝色”或“红色”,以及它们的id、价格、成本和描述,如下所示:

  id,        price,       cost,       description,        color
  --------------------------------------------------------------        
  10,         99,          50,         bicycle,           blue
  15,         88,          45,         tricycle,          red
  18,         90,          48,         tricycle,          blue
  20,         95,          55,         bicycle,           red
Blue, id, price, cost, description, Red, id, price, cost, description
blue, 10, 99,    50,   bicycle,     red, 15, 88,    45,   tricycle
blue, 18, 90,    48,   tricycle,    red, 20, 95,    55,   bicycle
关键是要能够并排查看数据,一旦数据在Excel中,我可以在数据透视表中轻松做到这一点,但我们正试图通过SQL查询来实现这一点

非常感谢您的帮助,如果还有其他信息,请告诉我

*

因此,在回顾了下面的评论之后,我想我最好只包含我现在正在使用的acutal代码

我的问题是:我需要一个查询中两个select语句的结果,但我就是不知道怎么做。所以总共有7列

类别代码、损失工时索赔计数、损失工时发生损失、损失工时索赔平均值、仅医疗索赔计数、仅医疗已发生损失、仅医疗索赔平均值

select distinct cm.class_code, count(cm.class_code) as "Lost Time Claim Count",
   round(sum(cf.Incurred_Total),0) as "Lost Time Incurred Loss",
   round((sum(cf.Incurred_Total)/count(cm.class_code)),0) as "Lost Time Claim Average",
   cm.claim_type_group

from claim_master cm left outer join
 claim_financial_view cf on cm.claim_master_id = cf.Claim_Master_Id

where cm.accident_date > to_date('31-Dec-2007','dd-mm-yyyy') and
  cm.accident_date < to_date('01-Jan-2013','dd-mm-yyyy') and
  cm.claim_type_group = 'L'

group by cm.class_code,
     cm.claim_type_group

Order by cm.class_code

__

select distinct cm.class_code, count(cm.class_code) as "Medical Only Claim Count",
   round(sum(cf.Incurred_Total),0) as "Medical Only Incurred Loss",
   round((sum(cf.Incurred_Total)/count(cm.class_code)),0) as "Medical Only Claim      Average",
   cm.claim_type_group

from claim_master cm left outer join
 claim_financial_view cf on cm.claim_master_id = cf.Claim_Master_Id

where cm.accident_date > to_date('31-Dec-2007','dd-mm-yyyy') and
  cm.accident_date < to_date('01-Jan-2013','dd-mm-yyyy') and
  cm.claim_type_group = 'M'

group by cm.class_code,
     cm.Claim_Type_Group

Order by cm.class_code
选择不同的cm.class\u代码,计数(cm.class\u代码)作为“损失工时索赔计数”,
四舍五入(总和,0)为“损失时间损失”,
四舍五入((总和(参见已发生的费用总额)/计数(厘米级费用代码)),0)为“损失工时索赔平均值”,
cm.索赔类型组
从主控cm左外连接
索赔\u财务\u视图cf on cm.claim\u master\u id=cf.claim\u master\u id
其中cm.事故日期>至日期('2007年12月31日','dd-mm-yyyy'),以及
cm.事故日期<截止日期('2013年1月1日','dd-mm-yyyy')和
cm.claim_type_group='L'
按cm.class_代码分组,
cm.索赔类型组
按cm.class_代码订购
__
选择不同的cm.class_代码,计数(cm.class_代码)作为“仅医疗索赔计数”,
四舍五入(金额(参见已发生的金额),0)为“仅医疗已发生的损失”,
四舍五入((总金额(参见已发生金额)/计数(cm类别代码)),0)为“仅医疗索赔平均值”,
cm.索赔类型组
从主控cm左外连接
索赔\u财务\u视图cf on cm.claim\u master\u id=cf.claim\u master\u id
其中cm.事故日期>至日期('2007年12月31日','dd-mm-yyyy'),以及
cm.事故日期<截止日期('2013年1月1日','dd-mm-yyyy')和
cm.claim_type_group='M'
按cm.class_代码分组,
cm.索赔类型组
按cm.class_代码订购

由于数据之间没有关系,我认为创建一个数据是最简单的。这可能是许多可能的解决方案之一:

select a.color as blue, a.id as idb, a.price as priceb
     , a.cost as costb, a.description as descb
     , b.color as red, b.id as idr, b.price as pricer
     , b.cost as costr, b.description as descr
  from ( select x.*, row_number() over ( order by id ) as rn
           from my_table x
          where color = 'blue'
                ) a
  full outer join ( 
         select x.*, row_number() over ( order by id ) as rn
            from my_table x
           where color = 'red'
                 ) b
    on a.rn = b.rn

解析函数提供了一些可以连接的东西,通过使用完整的外部连接,一种颜色的行数是否多于另一种颜色并不重要



啊,现在您在代码中添加了不同的列名!原则应该完全相同;您当前的查询变成了我这里的子查询。

是否有定义的、有限的颜色集?这是否存储在单独的表中?如果没有一个,你将无法改变这一点。不要担心照片;最好使用原始文本,因为这意味着人们可以更轻松地使用与您相同的数据。如果你想画一张表,但最好是把一个示例表结构和少量的实际数据放在一个表格中,这样人们就可以玩了,这样他们就更有可能回答你的问题!将有两种颜色,颜色将在一个单独的表中。Ben我从我一直在处理的实际语句中添加了一些代码。谢谢你的意见!您可能需要尝试数据透视表()