2014-02-17 58 views
9

我有一個select語句顯示行值作爲列標題

SELECT * 
    FROM TABLENAME 
WHERE WORKERNAME = 'A' 
    AND DATE = '12/17/2014' 

的輸出將是:

FREE | USED | DATE  | WORKERNAME 
------------------------------------ 
    1 | 0 |12/17/2014 | A 
    1 | 0 |12/17/2014 | A  
    1 | 0 |12/17/2014 | A  

我需要有一個輸出,其中爲DATEWORKERNAME輸出將是列標題看起來像:

A 
---------- 
12/17/2014 
---------- 
FREE | USED 
---------- 
1 | 0 
1 | 0 
1 | 0 

有人可以建議指出如何使用oracle SQL或PL/SQL來實現這一點?

+2

什麼是多行標題名稱的邏輯是什麼?問題結果如何有4行問題只有3? – TechDo

+0

什麼版本的Oracle?是它9i中,10G,11G(R1或R2),或圖12C – SriniV

+0

我編輯的問題,並去除第4行。這是我需要顯示的輸出。 – QKWS

回答

3

在使用純SQL或甚至PL/SQL之後,生成輸出結果並不會那麼優雅。如果你讓客戶做這項工作會更好。根據您希望如何將最終輸出呈現給最終用戶,您的選擇範圍從簡單的SQL * PLUS到更復雜的報告工具。下面是如何使用SQL * PLUS產生輸出一個簡單的例子:

clear screen; 

column workername new_value worker_name; 
column date1 new_value d1; 
column workername noprint; 
column date1 noprint; 
set linesize 15; 
column free format a7; 
column used format a7; 
ttitle center worker_name skip 1 - 
center '------------' skip 1 - 
center d1 skip 1 - 
center '------------' skip 1; 
set colsep '|' 

/* sample of data from your question */ 
with t1(free, used, date1, workername) as(
    select 1, 0, date '2014-12-17', 'A' from dual union all 
    select 1, 0, date '2014-12-17', 'A' from dual union all 
    select 1, 0, date '2014-12-17', 'A' from dual 
) 
select to_char(free) as free 
    , to_char(used) as used 
    , to_char(date1, 'mm/dd/yyyy') as date1 
    , workername 
    from t1 
where workername = 'A' 
    and date1 = date '2014-12-17'; 

結果:

 A  
    ------------ 
    12/17/2014 
    ------------ 
FREE |USED 
-------|------- 
1  |0  
1  |0  
1  |0 

如果有必要產生一個報告,其中包括不同workernames和/或不同date,所述break on SQL * PLUS命令可用於打破上的特定列或多列的組合的報告。例如:

column workername new_value worker_name; 
column date1 new_value d1; 
column workername noprint; 
column date1 noprint; 
set linesize 15; 
column free format a7; 
column used format a7; 
ttitle center worker_name skip 1 - 
center '------------' skip 1 - 
center d1 skip 1 - 
center '------------' skip 1; 
set colsep '|' 
break on worker_name skip page on date1 skip page; 

/* sample of data */ 
with t1(free, used, date1, workername) as(
    select 1, 0, date '2014-12-17', 'A' from dual union all 
    select 1, 0, date '2014-11-17', 'A' from dual union all 
    select 1, 0, date '2014-12-17', 'A' from dual union all 
    select 1, 0, date '2014-11-17', 'B' from dual 
) 
select to_char(free) as free 
    , to_char(used) as used 
    , to_char(date1, 'mm/dd/yyyy') as date1 
    , workername 
    from t1 
order by workername, date1; 

結果:

 A  
    ------------ 
    11/17/2014 
    ------------ 
FREE |USED 
-------|------- 
1  |0  

     A  
    ------------ 
    12/17/2014 
    ------------ 
FREE |USED 
-------|------- 
1  |0  
1  |0  

     B  
    ------------ 
    11/17/2014 
    ------------ 
FREE |USED 
-------|------- 
1  |0  

這裏是SQL*PLUS user's guide在這裏您可以找到對在上面的例子中所用的任何命令的詳細信息。

+0

+1不錯的工作! –

0
TTITLE LEFT 'Manager: ' MGRVAR SKIP 2 
BREAK ON MANAGER_ID SKIP PAGE 
BTITLE OFF 


SELECT MANAGER_ID, DEPARTMENT_ID, LAST_NAME, SALARY 
FROM EMP_DETAILS_VIEW 
WHERE MANAGER_ID IN (101, 201) 
ORDER BY MANAGER_ID, DEPARTMENT_ID; 


Manager:  101 

DEPARTMENT_ID LAST_NAME      SALARY 
------------- ------------------------- ---------- 
      10 Whalen       4400 
      40 Mavris       6500 
      70 Baer       10000 
      100 Greenberg      12000 
      110 Higgins      12000 

Manager:  201 

DEPARTMENT_ID LAST_NAME      SALARY 
------------- ------------------------- ---------- 
      20 Fay        6000 

6 rows selected. 

看看這樣的事情是否有助於你。

+1

-1 ... Downvoting,因爲它既具有任何有用的片段,也有他駕駛室內的顯示屏日期在格式,其中有經理簡單 – SriniV

+0

。 – user2094357

0

也許使用支點將是對您有用。

DECLARE @cols AS NVARCHAR(MAX), 
@colsName AS NVARCHAR(MAX), 
@query AS NVARCHAR(MAX) 

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(WORKERNAME +'_'+convert(varchar(50),[DATE]) +'_'+c.col) 
        from TRY 
        cross apply 
        (
        select 'FREE' col 
        union all 
        select 'USED' 
       ) c 
     FOR XML PATH(''), TYPE 
     ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'') 
select @colsName 
= STUFF((SELECT distinct ', ' + QUOTENAME(WORKERNAME +'_'+convert(varchar(50),[DATE])+'_'+c.col) 
      +' as [' 
      + WORKERNAME + case when c.col = 'FREE' then '_'+convert(varchar(50),[DATE])+'_FREE]' else '_'+convert(varchar(50),[DATE])+'_USED]' end 
     from TRY 
     cross apply 
     (
      select 'FREE' col 
      union all 
      select 'USED' 

     ) c 
     FOR XML PATH(''), TYPE 
     ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'') 

set @query = 'SELECT ID, ' + @colsName + ' 
from 
(
    select 
    ID, 
    WORKERNAME +''_''+convert(varchar(50),[DATE]) +''_''+col col, 
    value 
    from 
    (
    select ID, 
     WORKERNAME, 
     date, 
    cast(FREE as numeric(10, 2)) FREE, 
    cast(USED as numeric(10, 2)) USED 
    from TRY 
) src 
    unpivot 
    (
    value 
    for col in (FREE, USED) 
) unpiv 
) s 
pivot 
(
    sum(value) 
    for col in (' + @cols + ') 
) p 
order by ID' 

execute(@query) 
+0

PIVOT在10G沒有出臺任何解釋 – SriniV