【发布时间】:2013-09-11 19:39:49
【问题描述】:
我正在尝试查询我的数据库并返回同一字段中包含多个列的结果。
我的数据库中的数据可能如下所示:
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
我正在尝试编写一个查询来返回结果,该结果会给我多个列来表示每种颜色为“蓝色”或“红色”以及它们的 ID、价格、成本和描述,如下所示:
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 查询来完成此操作。
非常感谢任何帮助,如果我可以提供任何其他信息,请告诉我。
*
因此,在查看下面的 cmets 后,我想我最好只包含我现在正在使用的实际代码。
我的问题:我需要一个查询中两个 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
【问题讨论】:
-
是否有定义的、有限的颜色集?这是存储在单独的表中吗?没有一个,您将无法旋转它。
-
P.S.不用担心图片;最好使用原始文本,因为这意味着人们可以更轻松地使用与您相同的数据。如果你想画一张表Senseful Solutions have a great tool,但最好把一个示例表结构和少量实际数据放在SQL Fiddle 中,这样人们就可以玩了,这样他们更有可能回答你的问题!跨度>
-
会有两种颜色,颜色将在单独的表格中。
-
Ben 我从我一直在处理的实际语句中添加了一些代码。感谢您的意见!
-
您可能想尝试使用数据透视表 (@987654323@)
标签: sql oracle pivot multiple-columns