在sQL查询结果中包含标题

时间:2014-10-03 15:22:05

标签: sql oracle

我正在运行一个SQL查询来从表中选择数据。

我想将标题包含在查询结果的第一行。 标题将来自其他表或我们可以sqlhardcode值。

下面是我获取数据的查询。

 select invoice_type_code,
  shipper_short_code ,
  charge_type_code ,
  incurred_date  ,
  charge_amount  , 
  charge_description 
  from prepayment_charge_calc ;

我希望每个列的第一行数据上方有一个标题。这些标题不应该是列名

例如。

header1   header2   header3  header4   header5  header6  
 1          2           3          4        5        6

标题1到6来自其他表或者可以是带符号的值。在此标题下我们应该从" prepayment_charge_calc"中提取数据。 表.. 1,2,3,4是来自" prepayment_charge_calc"的数据。表

任何人都可以建议我查询。

3 个答案:

答案 0 :(得分:3)

假设使用Oracle DBMS,您可以使用DUAL表手动创建标题行,然后与实际数据结合使用。使用伪psuedo列(" rno"在下面的示例中)对数据进行排序。但是,您必须将任何其他数据类型转换为VARCHAR才能使其工作。想法如下所示:

select
   'header1',
   'header2',
   'header3',
   'header4',
   'header5',
   'header6',
   1 rno
from
   dual
union 
select 
  invoice_type_code,
  shipper_short_code ,
  charge_type_code ,
  incurred_date  , --convert this using to_char if date datatype
  charge_amount  , --convert this using to_char if numeric datatype
  charge_description,
  2 rno
from 
  prepayment_charge_calc
order by rno;

答案 1 :(得分:2)

您希望能够将各种不同的列名称硬编码到查询中。

在Oracle中,您可以使用引号:

执行此操作
 select 
  invoice_type_code as "1",
  shipper_short_code as "asdf",
  charge_type_code as "12353",
  incurred_date as "ddf",
  charge_amount as "234$", 
  charge_description as "header6"
 from 
  prepayment_charge_calc 

您可以看到example of that here

答案 2 :(得分:0)

我没有看到任何额外的努力让标题中的列名按照您想要的方式,只需按照您想要的方式提及ALIAS

例如:

select col1 as "header1", col2 as "header2".... from table

在任何GUI/non GUI based tool中,scroll pane会自动将table header放在resultset的顶部,这实际上是您需要的列标题。