Single row output
Create table test_table ( metric_name varchar2(30), dt date, value number) ;
insert into test_table values ('METRIC_1', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 10) ;
insert into test_table values ('METRIC_1', to_date('12/31/2013 09:00:00','mm/dd/yyyy hh24:mi:ss'), 20) ;
insert into test_table values ('METRIC_1', to_date('12/31/2013 10:00:00','mm/dd/yyyy hh24:mi:ss'), 30) ;
insert into test_table values ('METRIC_2', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 11) ;
insert into test_table values ('METRIC_2', to_date('12/31/2013 09:00:00','mm/dd/yyyy hh24:mi:ss'), 22) ;
insert into test_table values ('METRIC_2', to_date('12/31/2013 10:00:00','mm/dd/yyyy hh24:mi:ss'), 33) ;
commit;
SQL> alter session set nls_date_format='mm/dd/yyyy hh24:mi:ss' ;
Session altered.
SQL> select * from test_table ;
METRIC_NAME DT VALUE
METRIC_1 12/31/2013 08:00:00 10
METRIC_1 12/31/2013 09:00:00 20
METRIC_1 12/31/2013 10:00:00 30
METRIC_2 12/31/2013 08:00:00 11
METRIC_2 12/31/2013 09:00:00 22
METRIC_2 12/31/2013 10:00:00 33
My intentended output is as follows:
METRIC_NAME 12/31/2013 08:00:00 12/31/2013 08:00:00 12/31/2013 08:00:00
METRIC_1 10 20 30
METRIC_2 11 22 33
The date columns will be more 24 for a day with 1 hour interval. So i want to print all 24 values in a single row for each metric.
Any help is much appreciate.
-Thanks
You could also use just the hours for the headers ...
with w_data as (
select metric_name, value,
(dt - trunc(dt))*24 dt_hour
from test_table
select *
from w_data
pivot(
sum(value)
for dt_hour in (6,7,8,9,10,11,12,13,14,15)
order by metric_name
METRIC_NAME 6 7 8 9 10 11 12 13 14 15
METRIC_1 10 20 30
METRIC_2 11 22 33
That removes some of the "have to know the date" that Solomon points out
(I just listed 6-15 . but you could list full 1-24 if you needed it)
Similar Messages
-
Oracle query - Merging multiple rows into a single row output
Hi All,
I have to have a multiple row output to be converted into a single row output.My current output looks as follows:
ID YR INC_CODE OFFN SCHOOLNO
8006 2002 00175 SC03 12
8006 2002 00175 DC06 12
8006 2002 00175 DC03 12
8006 2002 00175 DC02 12
ID,INCIDENT CODE,OFFENSE are all Primary keys
So I need the output as follows:(IN ONE ROW)
ID YR INC_CODE OFFN1 OFFN2 OFFN3 OFFN4 SCHOOLNO
8006 2002 00175 SC03 DC06 DC03 DC02 12
Can you help me on this since have been spinning the wheel and this has to be a query since will have couple of tables join to produce a materialized view.
Thanks in advanceHi Nigel,
Thanks for the reply I tested out the portion having the decode and I get the output as follows:
ID YR INC_CODE OFFN1 OFFN2 OFFN3 OFFN4 OFFN5
8982 2002 2175 DOC01 -----------------------
8982 2002 2175 DOC02-------------------
8982 2002 2175 DOC03------------
8982 2002 2175 DOC06-------
8982 2002 2175 SCV03
There is no value as max for OFFN and each INC_CODE MAY HAVE UP TO A MAX OF 5 OFFN.My query is as follows:
select distinct STU_STUDENT_ID, INC_BEG_SCH_YR,INC_INCIDENT_CODE
, decode(rank() over (partition by INC_CODE order by OFFN),1,OFFN,null) as offn1
, decode(rank() over (partition by INC_CODE order by OFFN),2,OFFN,null) as offn2
, decode(rank() over (partition by INC_CODE order by OFFN),3,OFFN,null) as offn3
, decode(rank() over (partition by INC_CODE order by OFFN),4,OFFN,null) as offn4
, decode(rank() over (partition by INC_CODE order by OFFN),5,OFFN,null) as offn5
from stu_offn where
stu_offn.ID = '8982' and stu_offn.INC_CODE = '2175'
(****Where clause is just given to just check a value)
So as you know I need to just have all the OFFN in a single row ie as follows:
ID YR INC_CODE OFFN1 OFFN2 OFFN3 OFFN4 OFFN5
8982 2002 2175 DOC01 DOC02 DOC03 DOC06 SCV03
Can you just give me a step by step procedure to go through this and the table in this case is just 'STU_OFFN'
Thanks for the earlier reply appreciate it!
****Sending this again to show the exact way the output is coming -
SQL Query to convert multiple rows to a single row output
I have the following sql query;
SELECT B.ClientID,
B.BillNo, B.[BillDate],
B.CurrencyCode,
B.BAmount
FROM tblCompanyDetails CD
CROSS
APPLY (
SELECT TOP 5 B1.ClientID,
B1.[BillNumber],
B1.[BillDate], B1.[CurrencyCode] AS CCY,
B1.[BillAmount]
FROM tblBills B1
WHERE ROUND(B1.[BillAmount],2)<>0
AND B1.ClientID=CD.CompanyID
AS B
WHERE CD.ContactId=235405
ORDER
BY B.ClientID,B.BillNo
DESC
This gives me the following row output;
ClientID
BillNo
BillDate
CurrencyCode
BAmount
94466
425616
2015-04-07
GBP
5000
94466
424517
2015-01-15
GBP
6000
94466
424455
2014-10-15
GBP
4000
However, I would like the ouput to appear like this;
ClientID
BillNo1
BillDate
CCY
BAmount
BillNo2
BillDate
CCY
BAmount
BillNo3
BillDate
CCY
BAmount
94466
425616
2015-04-07
GBP
5000
424517
2015-01-15
GBP
6000
424455
2014-10-15
GBP
4000I have tried to create a sample based on your question
CREATE TABLE MyTable
ClientID INT ,
BillNo INT,
BillDate DATE,
CurrencyCode varchar(5),
BAmount INT
INSERT INTO MyTable
VALUES (94466,425616,'2015-04-07','GBP',5000),
(94466,424517,'2015-01-15','GBP',6000),
(94466,424455,'2014-10-15','GBP',4000)
Then I have used another table to put ranking in that, you can actually tweak your statement to include rank also
CREATE TABLE RowTable
ClientID INT ,
BillNo INT,
BillDate DATE,
CurrencyCode varchar(5),
BAmount INT,
Ranking INT
INSERT INTO RowTable
SELECT * , ROW_NUMBER() OVER (ORDER BY ClientID) as row
FROM MyTable
Once your data is ready then execute below dynamic query
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(col+cast(Ranking as varchar(10)))
from RowTable
cross apply
select 'BillNo', 1 union all
select 'BillDate', 2 union all
select 'CurrencyCode', 3 union all
select 'BAmount' , 4
) c (col, so)
group by col, so, Ranking
order by Ranking, so
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT ClientId,' + @cols + '
from
select clientid, col+cast(Ranking as varchar(10)) col, value
from RowTable
cross apply
SELECT ''BillNo'', cast(BillNo as varchar(20)) union all
SELECT ''BillDate'', cast(BillDate as varchar(20)) union all
SELECT ''CurrencyCode'', CurrencyCode union all
SELECT ''BAmount'', CAST(BAmount as varchar(10))
) c (col, value)
) x
pivot
max(value)
for col in (' + @cols + ')
) p '
execute sp_executesql @query;
Output:
Hope this will help. -
SIngle row output for a multiple values
Create table test_table ( metric_name varchar2(30), dt date, value number) ;
insert into test_table values ('METRIC_1', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 10) ;
insert into test_table values ('METRIC_1', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 20) ;
insert into test_table values ('METRIC_1', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 30) ;
insert into test_table values ('METRIC_2', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 11) ;
insert into test_table values ('METRIC_2', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 22) ;
insert into test_table values ('METRIC_2', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 33) ;
commit;
alter session set nls_date_format='mm/dd/yyyy hh24:mi:ss' ;
SQL> select * from test_table ;
METRIC_NAME DT VALUE
METRIC_1 12/31/2013 08:00:00 10
METRIC_1 12/31/2013 09:00:00 20
METRIC_1 12/31/2013 10:00:00 30
METRIC_2 12/31/2013 08:00:00 11
METRIC_2 12/31/2013 09:00:00 22
METRIC_2 12/31/2013 10:00:00 33
But I need output in following format:
METRIC_NAME 12/31/2013 08:00:00 12/31/2013 09:00:00 12/31/2013 10:00:00
METRIC_1 10 20 30
METRIC_2 11 22 33
-ThanksHi,
943750 wrote:
Create table test_table ( metric_name varchar2(30), dt date, value number) ;
insert into test_table values ('METRIC_1', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 10) ;
insert into test_table values ('METRIC_1', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 20) ;
insert into test_table values ('METRIC_1', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 30) ;
insert into test_table values ('METRIC_2', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 11) ;
insert into test_table values ('METRIC_2', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 22) ;
insert into test_table values ('METRIC_2', to_date('12/31/2013 08:00:00','mm/dd/yyyy hh24:mi:ss'), 33) ;
commit;
alter session set nls_date_format='mm/dd/yyyy hh24:mi:ss' ;
SQL> select * from test_table ;
METRIC_NAME DT VALUE
METRIC_1 12/31/2013 08:00:00 10
METRIC_1 12/31/2013 09:00:00 20
METRIC_1 12/31/2013 10:00:00 30
METRIC_2 12/31/2013 08:00:00 11
METRIC_2 12/31/2013 09:00:00 22
METRIC_2 12/31/2013 10:00:00 33
But I need output in following format:
METRIC_NAME 12/31/2013 08:00:00 12/31/2013 09:00:00 12/31/2013 10:00:00
METRIC_1 10 20 30
METRIC_2 11 22 33
-Thanks
All the dts INSERTed were 08:00:00; why are some displayed as 09:00:00 or 10:00:00? I assume there are typos in the INSERT statements, and the results above are correct.
Taking data from 1 column on N rows, and displaying it as N columns on 1 row is called Pivoting. Search for "pivot" for details.
If you know the exact dts for which you want to check, you can do something like this:
SELECT *
FROM test_table
PIVOT ( MIN (value)
FOR dt IN ( TO_DATE ('12/31/2013 08:00:00', 'mm/dd/yyyy hh24:mi:ss') AS hr_08
, TO_DATE ('12/31/2013 09:00:00', 'mm/dd/yyyy hh24:mi:ss') AS hr_09
, TO_DATE ('12/31/2013 10:00:00', 'mm/dd/yyyy hh24:mi:ss') AS hr_10 )
ORDER BY metric_name
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Point out where the query above is giving the wrong results, and explain, using specific examples, how you get the correct results from the given data in those places. If you changed the query at all, post your code.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ: https://forums.oracle.com/message/9362002 -
Display spool output i.e a single row in a single line
hello All,
I have report that outputs many columns is single row , when run in the background.
The problem that I am facing that the the single row of columns is wrapped into second line when certain limit of chararcters is reached. That is the single row of data is placed into two lines.
While running the job in the background I choose the LOCL or LP01 printer with format X_65_255.
Also note that I have other SAP systems but in that systems when I use the same procedure(printer and format also the same) for the same report the outputf for single row is not warpped into 2 lines but appears in the same line.
Please provide your input. Points will be rewrded for helpful answers
regards
SachinNote that this solution is for downloading wide formats only - not printing...
Here is what I did:
To create format Y_1024_1023 (New name!)
1) Transaction SPAD
2) Click "Full Administration" button
3) Click on "Device Types" tab
4) Click "Display" button next to "Format Types" field
5) Place cursor on X_65_255
6) Click in menu Format --> Create using template (F5)
7) Change Number of rows to 1023 (if set to 65, you will have more pages and hence more headers)
9) Change Number of Columns to 1023 (as wide as can be displayed in SP01)
8)Give new name to format e.g. Y_1023_1023
To create spool device YSPOOL:
1) Transaction SPAD
2) Click on "Device/Servers" tab (first tab!)
3) Click 'Display" button next to Output Devices field
4) Click "Create" button (Shift-F1)
5) Give output device long name (e.g.YSPOOL) and short name (e.g.YSPO)
6) Device attributes tab --> Device type = ASCIIPRI (Some generic ASCII printer) - I actually used a ZU_ASCII created in our system but ASCIIPRI should also work.
7) Access method tab --> Host printer = some value (e.g. outfile2)
8) Save
Now assign Y_1024_1023 to Device type:
1) Transaction SPAD
2) Click on Device types tab
3) Enter "ASCIIPRI" in "Device types" field and click display button
4) Click on "Formats" button (F6)
5) Click on "Create' button (Shift-F1)
6) In popup, type Y_1023_1023 (or use drop-down to choose from)
7) Save
Per OSS Note 186603 --> Call SPAD and choose Settings -> Spool System -> Other. Select 'SP01: Number of Lines for List Display from Format'. You may have to clear cache via SPAD->Goto->Cache control -->Click "Reset Cache Settings".
Now, when you schedule your background job, choose YSPOOL as your output device and choose the format as Y_1024_1023. The spool generated from this job will be 1023 columns wide and 1024 lines long (less headers!). Note that opening such a large spool (long & wide) will consume lots of app server memory so exit out as soon as download to presentation server is complete. -
Output as a single row with values separated by comas
I have a query that returns one column as:
Col1
a
b
c
I want the output as a single row with values separated by comas:
Col1
a,b,c
How do I do it in SQL?Or... build your own aggregation function :
SQL> create or replace type AggregateStr as object
2 (
3 tag VARCHAR2(1),
4 str VARCHAR2(4000), -- concatenation string
5 static function ODCIAggregateInitialize(sctx IN OUT AggregateStr) return number,
6 member function ODCIAggregateIterate(self IN OUT AggregateStr, value IN VARCHAR2) return number,
7 member function ODCIAggregateTerminate(self IN AggregateStr, returnValue OUT VARCHAR2, flags IN number) return number,
8 member function ODCIAggregateMerge(self IN OUT AggregateStr, ctx2 IN AggregateStr) return number
9 );
10 /
Type created.
Elapsed: 00:00:00.00
SQL>
SQL> create or replace type body AggregateStr is
2 static function ODCIAggregateInitialize(sctx IN OUT AggregateStr) return number is
3 begin
4 sctx := AggregateStr(',','');
5 return ODCIConst.Success;
6 end;
7
8 member function ODCIAggregateIterate(self IN OUT AggregateStr, value IN VARCHAR2) return number is
9 begin
10 self.str:=self.str||value||self.tag;
11 return ODCIConst.Success;
12 end;
13
14 member function ODCIAggregateTerminate(self IN AggregateStr, returnValue OUT VARCHAR2, flags IN number) return number is
15 begin
16 returnValue := rtrim(self.str,',');
17 return ODCIConst.Success;
18 end;
19
20 member function ODCIAggregateMerge(self IN OUT AggregateStr, ctx2 IN AggregateStr) return number is
21 begin
22 self.str := ctx2.str;
23 return ODCIConst.Success;
24 end;
25 end;
26 /
Type body created.
Elapsed: 00:00:00.00
SQL>
SQL> CREATE OR REPLACE FUNCTION ConcateStr (input VARCHAR2) RETURN VARCHAR2
2 PARALLEL_ENABLE AGGREGATE USING AggregateStr;
3 /
Function created.
Elapsed: 00:00:00.00
SQL>
SQL> select max(concate_string) keep (dense_rank last order by length(concate_string)) as concate_string
2 from (select ConcateStr(ename) over (order by ename) concate_string
3 from emp);
CONCATE_STRING
ADAMS,ALLEN,BLAKE,CLARK,FORD,JAMES,JONES,KING,MARTIN,MILLER,SCOTT,SMITH,TURNER,WARD
Elapsed: 00:00:00.00Or if you doesn't matter of the order :
SQL> select ConcateStr(ename)
2 from emp;
CONCATESTR(ENAME)
SMITH,ALLEN,WARD,JONES,MARTIN,BLAKE,CLARK,SCOTT,KING,TURNER,ADAMS,JAMES,FORD,MILLERNicolas.
adding the second query.
Message was edited by:
N. Gasparotto -
Need a query to merge output in a single row?
Hi All,
I need a query to merge output in a single row.
Query :
Select dname from dept.
Actual output is :
Dname
EDP
ACCOUNT
GR
Desired Output is:
Dname
EDP ACCOUNT GR
Please provide me the solution
Thanks
Amitselect max(sys_connect_by_path (t.name,' ')) from ( select id,
name,
group_id,
row_number() over (partition by group_id order by id) rn
from ( select 1 id, 'test' name, 1 group_id from dual
union
select 2 id, 'test1' name, 1 group_id from dual
union
select 3 id, 'test2' name, 1 group_id from dual
union
select 4 id, 'test3' name, 1 group_id from dual) ) t
start with t.rn = 1 and id = 1
connect by t.rn = prior t.rn + 1
group by t.group_id -
Multiple rows Converge to Single row and join
Hi Folks,
I am facing a tricky challenge to join a table with multiple rows and converge into a single row (based on ID, period) and join with another table to get a single row. Let me explain.
Table 1: DTL_TABLE (id, period, course, names, title, type)
1 2010 mat john null null
1 2010 mat jim null null
1 2010 cam null officer null
1 2010 cam null Prof null
1 2010 phy null null Inclass
1 2010 phy null null Online
Join with
Table 2: ID_TABLE(id, period, Loc, Dept, Code)
1 2010 nj 101 CC.
Output format (id, period, course, names, title, type, Loc, Dept, Code)
result : 1 2010 mat,cam,phy john,jim officer,prof inclass,online nj 101 CC
I have created all the DDLs and DMLs if that help. Kindly let me know if a Join SQL query is possible.
Thank you,
Aj
CREATE TABLE DTL_TABLE
ids VARCHAR2 (10),
period VARCHAR2 (10),
course VARCHAR2 (10),
names VARCHAR2 (10),
title VARCHAR2 (10),
TYPE VARCHAR2 (10)
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'mat',
'jim',
NULL,
NULL);
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'mat',
'john',
NULL,
NULL);
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'mat',
'kale',
NULL,
NULL);
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'cam',
NULL,
'officer',
NULL);
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'cam',
NULL,
'prof',
NULL);
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'phy',
NULL,
NULL,
'inclass');
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'phy',
NULL,
NULL,
'online');
COMMIT;
CREATE TABLE id_table
ids VARCHAR2 (10),
period VARCHAR2 (10),
loc VARCHAR2 (10),
dept VARCHAR2 (10),
code VARCHAR2 (10)
INSERT INTO id_table
VALUES ('1',
'2010',
'nj',
'101',
'cc');
COMMIT;Aj09 wrote:
Hi Folks,
I am facing a tricky challenge to join a table with multiple rows and converge into a single row (based on ID, period) and join with another table to get a single row. Let me explain.
Table 1: DTL_TABLE (id, period, course, names, title, type)
1 2010 mat john null null
1 2010 mat jim null null
1 2010 cam null officer null
1 2010 cam null Prof null
1 2010 phy null null Inclass
1 2010 phy null null Online
Join with
Table 2: ID_TABLE(id, period, Loc, Dept, Code)
1 2010 nj 101 CC.
Output format (id, period, course, names, title, type, Loc, Dept, Code)
result : 1 2010 mat,cam,phy john,jim officer,prof inclass,online nj 101 CC
I have created all the DDLs and DMLs if that help. Kindly let me know if a Join SQL query is possible.
SELECT TBL.id,
DTLperiod,
course,
names,
title,
TYPE,
Loc,
Dept,
Code
FROM ID_TABLE TBL,
DTL_TABLE DTL
WHERE TBL.ID = DTL.ID
AND TBL.PERIOD = DTL.PERIOD; it not good to use RESERVED WORDS like "ID" or "TYPE" as column names -
thanks
Edited by: 786304 on May 25, 2011 12:22 PMBluShadow .
I agree your opinion.
I resurrect it from google cache.
Hi All,
I have two queries as follows
Select
Xrc.Profile_Nbr Var_Profile_Nbr,
xsr.Profile_Nbr Var_Profile_Nbr,
Nvl(Ppa.First_Name,' ') Var_First_Name,
Nvl(Ppa.Last_Name,' ') Var_Last_Nmae,
Nvl(Ppa.Last_Name2,' ') Var_Last_Name2,
Nvl(Ppa.Middle_Name,' ') Var_Middle_Name,
Nvl(Pfl.Display_Name,' ') Var_Display_Name,
Nvl(Addr.Street1,' ') Var_Street1,
Nvl(Addr.Street2,' ') Var_Street2,
Nvl(Addr.City,' ') Var_City ,
Nvl(Addr.State_Name,' ') Var_State_Name,
Addr.Post_Code Var_Post_Code,
Nvl(Cnt.Cntry_Name,' ') Var_Country,
Phn.Phone Var_Phone_Nbr,
Nvl(To_Char(Ppa.Birthdate,'MON/DD/YYYY'),' ') Var_Birthdate,
Nvl(Ppa.Occupation,' ') Var_Occupation,
Nvl(Ppa.Country_Of_Nationality,1) Var_Country_Of_Nationality,
Nvl(Ppa2.First_Name,' ') Var_First_Name,
Nvl(Ppa2.Last_Name,' ') Var_Last_Nmae,
Nvl(Ppa2.Last_Name2,' ') Var_Last_Name2,
Nvl(Ppa2.Middle_Name,' ') Var_Middle_Name,
Nvl(Pfl2.Display_Name,' ') Var_Display_Name,
Nvl(Addr2.Street1,' ') Var_Street1,
Nvl(Addr2.Street2,' ') Var_Street2,
Nvl(Addr2.City,' ') Var_City ,
Nvl(Addr2.State_Name,' ') Var_State_Name,
Addr2.Post_Code Var_Post_Code,
Nvl(Cnt2.Cntry_Name,' ') Var_Country,
Phn2.Phone Var_Phone_Nbr,
Nvl(To_Char(Ppa2.Birthdate,'MON/DD/YYYY'),' ') Var_Birthdate,
Nvl(Ppa2.Occupation,' ') Var_Occupation,
Nvl(ppa2.COUNTRY_OF_NATIONALITY,1) var_COUNTRY_OF_NATIONALITY
From
V_Xact_Receiver Xrc, V_Person_Profile Ppa, V_Profile Pfl,V_Phone Phn,V_Address Addr,V_Country_Lu Cnt
,V_Xact_sender xsr, V_Person_Profile Ppa2, V_Profile Pfl2,V_Phone Phn2,V_Address Addr2,V_Country_Lu Cnt2
Where Xrc.Xact_Hdr_Nbr=71138142
and xsr.Xact_Hdr_Nbr=71138142
And xrc.Profile_Nbr=Ppa.Profile_Nbr
And Ppa.Profile_Nbr=Pfl.Profile_Nbr
And Pfl.Profile_Nbr=Phn.Profile_Nbr
And Pfl.Profile_Nbr=Addr.Profile_Nbr
And Phn.Country_Nbr=Cnt.Country_Nbr
And Xsr.Profile_Nbr=Ppa2.Profile_Nbr
And Ppa2.Profile_Nbr=Pfl2.Profile_Nbr
And Pfl2.Profile_Nbr=Phn2.Profile_Nbr
And Pfl2.Profile_Nbr=Addr2.Profile_Nbr
And Phn2.Country_Nbr=Cnt2.Country_Nbr;
and
SELECT
Nvl(Dlu.Id_Type_Desc,' ') Var_Ident_Type_Desc,
Nvl(D.Id_Number,' ') Var_Id_Number,
Nvl(To_Char(D.Id_Issue_Date,'MON/DD/YYYY'),' ') Dte_Id_Issue,
Nvl(To_Char(D.Id_Expiration_Date,'MON/DD/YYYY'),' ') Dte_Id_Expiration,
Nvl(To_Char(Id_Expiration_Date,'MON/DD/YYYY'),' ') Dte_Id_Expiration,
Nvl(D.Id_Issue_Place,' ') Var_Issue_Loc,
Nvl(Pfl.Occupation,' ') Var_Occupation,
Nvl(To_Char(Pfl.Birthdate,'MON/DD/YYYY'),' ') Dte_Birth,
Nvl(To_Char(Pfl.Country_Of_Nationality,'MON/DD/YYYY'),' ') Var_Country_Of_Nationality
FROM V_PROFILE_IDENT D,
V_ID_TYPE_LU DLU,
V_Country_Lu Clu,
V_Xact_sender Xsr,
v_person_profile pfl
Where Xsr.Xact_Hdr_Nbr='71138142'
And D.Profile_Nbr = Xsr.Profile_Nbr
and D.Profile_Nbr=pfl.Profile_Nbr
And D.Id_Type_Nbr = Dlu.Id_Type_Nbr
And D.Country_Nbr = Clu.Country_Nbr(+)
And D.Is_Valid = 'Y'
and rownum<=3;
The first query it returns the result in a single row
The second query it returns the output in 3 rows
while i was club these two it gives the result in 3 rows
but have to get the result in a single row with all appropriate values side by side if the values are same get the values one only
and the query club as
Select
Xrc.Profile_Nbr Var_Profile_Nbr,
xsr.Profile_Nbr Var_Profile_Nbr,
Nvl(Ppa.First_Name,' ') Var_First_Name,
Nvl(Ppa.Last_Name,' ') Var_Last_Nmae,
Nvl(Ppa.Last_Name2,' ') Var_Last_Name2,
Nvl(Ppa.Middle_Name,' ') Var_Middle_Name,
Nvl(Pfl.Display_Name,' ') Var_Display_Name,
Nvl(Addr.Street1,' ') Var_Street1,
Nvl(Addr.Street2,' ') Var_Street2,
Nvl(Addr.City,' ') Var_City ,
Nvl(Addr.State_Name,' ') Var_State_Name,
Addr.Post_Code Var_Post_Code,
Nvl(Cnt.Cntry_Name,' ') Var_Country,
Phn.Phone Var_Phone_Nbr,
Nvl(To_Char(Ppa.Birthdate,'MON/DD/YYYY'),' ') Var_Birthdate,
Nvl(Ppa.Occupation,' ') Var_Occupation,
Nvl(Ppa.Country_Of_Nationality,1) Var_Country_Of_Nationality,
Nvl(Ppa2.First_Name,' ') Var_First_Name,
Nvl(Ppa2.Last_Name,' ') Var_Last_Nmae,
Nvl(Ppa2.Last_Name2,' ') Var_Last_Name2,
Nvl(Ppa2.Middle_Name,' ') Var_Middle_Name,
Nvl(Pfl2.Display_Name,' ') Var_Display_Name,
Nvl(Addr2.Street1,' ') Var_Street1,
Nvl(Addr2.Street2,' ') Var_Street2,
Nvl(Addr2.City,' ') Var_City ,
Nvl(Addr2.State_Name,' ') Var_State_Name,
Addr2.Post_Code Var_Post_Code,
Nvl(Cnt2.Cntry_Name,' ') Var_Country,
Phn2.Phone Var_Phone_Nbr,
Nvl(To_Char(Ppa2.Birthdate,'MON/DD/YYYY'),' ') Var_Birthdate,
Nvl(Ppa2.Occupation,' ') Var_Occupation,
Nvl(Ppa2.Country_Of_Nationality,1) Var_Country_Of_Nationality,
Nvl(Dlu.Id_Type_Desc,' ') Var_Ident_Type_Desc,
Nvl(D.Id_Number,' ') Var_Id_Number,
Nvl(To_Char(D.Id_Issue_Date,'MON/DD/YYYY'),' ') Dte_Id_Issue,
Nvl(To_Char(D.Id_Expiration_Date,'MON/DD/YYYY'),' ') Dte_Id_Expiration,
Nvl(To_Char(Id_Expiration_Date,'MON/DD/YYYY'),' ') Dte_Id_Expiration,
Nvl(D.Id_Issue_Place,' ') Var_Issue_Loc,
Nvl(Pfl3.Occupation,' ') Var_Occupation,
Nvl(To_Char(Pfl3.Birthdate,'MON/DD/YYYY'),' ') Dte_Birth,
Nvl(To_Char(Pfl3.Country_Of_Nationality,'MON/DD/YYYY'),' ') Var_Country_Of_Nationality
From
V_Xact_Receiver Xrc, V_Person_Profile Ppa, V_Profile Pfl,V_Phone Phn,V_Address Addr,V_Country_Lu Cnt
,V_Xact_sender xsr, V_Person_Profile Ppa2, V_Profile Pfl2,V_Phone Phn2,V_Address Addr2,V_Country_Lu Cnt2
V_Profile_Ident D,
V_Id_Type_Lu Dlu,
V_Country_Lu Clu,
V_Xact_receiver Xrc2,
v_person_profile pfl3
Where Xrc.Xact_Hdr_Nbr=71138142
And Xsr.Xact_Hdr_Nbr=71138142
and xrc2.Xact_Hdr_Nbr=71138142
And xrc.Profile_Nbr=Ppa.Profile_Nbr
And Ppa.Profile_Nbr=Pfl.Profile_Nbr
And Pfl.Profile_Nbr=Phn.Profile_Nbr
And Pfl.Profile_Nbr=Addr.Profile_Nbr
And Phn.Country_Nbr=Cnt.Country_Nbr
And Xsr.Profile_Nbr=Ppa2.Profile_Nbr
And Ppa2.Profile_Nbr=Pfl2.Profile_Nbr
And Pfl2.Profile_Nbr=Phn2.Profile_Nbr
And Pfl2.Profile_Nbr=Addr2.Profile_Nbr
And Phn2.Country_Nbr=Cnt2.Country_Nbr
And D.Profile_Nbr = Xrc2.Profile_Nbr
and D.Profile_Nbr=pfl3.Profile_Nbr
And D.Id_Type_Nbr = Dlu.Id_Type_Nbr
And D.Country_Nbr = Clu.Country_Nbr(+)
And D.Is_Valid = 'Y'
and rownum<=3;
regards, -
SQL Update a Single Row Multiple Times Using 2 Data Sets
I'm working in tsql and have an issue where I need to do multiple updates to a single row based on multiple conditions.
By Rank_
If the column is NULL I need it to update no matter what the Rank is.
If the Ranks are the same I need it to update in order of T2_ID.
And I need it to use the last updated output.
I've tried using the update statement below but it only does the first update and the rest are ignored. Here is an example of the data sets i'm working w/ and the Desired results. Thanks in advance!
update a
set Middle = case when a.Rank_> b.Rank_ OR a.Middle IS NULL then ISNULL(b.Middle,a.Middle) end,
LName = case when a.Rank_> b.Rank_ OR a.Lname IS NULL then ISNULL(b.LName,a.LName) end,
Rank_ = case when a.Rank_> b.Rank_ then b.Rank_ end
from #temp1 a
inner join #temp2 b on a.fname = b.fname
where b.T2_ID in (select top 100% T2_ID from #temp2 order by T2_ID asc)The Merge clause actually errors because it attempt to update the same record. I think this CTE statement is the closest I've come but I'm still working through it as I'm not too familiar w/ them. It returns multiple rows which I will have to
insert into a temp table to update since the resulting row I need is the last in the table.
;WITH cteRowNumber
AS(
Select DISTINCT
Row_Number() OVER(PARTITION BY a.LName ORDER BY a.LName ASC, a.Rank_ DESC,b.T2ID ASC) AS RowNumber
,a.FName
,a.LName
,b.LName as xLname
,a.MName
,b.MName AS xMName
,a.Rank_
,b.Rank_ AS xRank
,b.T2ID
FROM #temp1 a
inner join #temp2 b
ON a.fname = b.fname
), cteCursor
AS(
Select a.RowNumber,
a.Fname
,a.LName
,a.xLname
,a.MName
,a.xMName
,a.xRank
,a.T2ID
,CASE WHEN a.Rank_ >= a.xRank THEN ISNULL(a.xRank,a.Rank_) else ISNULL(a.Rank_,a.xRank) end AS Alt_Rank_
,CASE WHEN a.Rank_ >= a.xRank THEN ISNULL(a.xMName,a.MName) else ISNULL(a.MName,a.xMName) end AS Alt_MName
,CASE WHEN a.Rank_ >= a.xRank THEN ISNULL(a.xLName,a.lname) else ISNULL(a.LName,a.xlname) end as Alt_Lname
FROM cteRowNumber a
where a.RowNumber = 1
UNION ALL
Select crt.RowNumber
,crt.FName
,crt.LName
,crt.xLname
,crt.MName
,crt.xMName
,crt.xRank
,crt.T2ID
,CASE WHEN Prev.Alt_Rank_ >= crt.xRank THEN ISNULL(crt.xRank,Prev.Alt_Rank_) else ISNULL(Prev.Alt_Rank_,crt.xRank) end AS Alt_Rank
,CASE WHEN Prev.Alt_Rank_ >= crt.xRank THEN ISNULL(crt.xMName,Prev.Alt_MName) else ISNULL(Prev.Alt_MName,crt.xMName) end AS Alt_MName
,CASE WHEN Prev.Alt_Rank_ >= crt.xRank THEN ISNULL(crt.xLName,Prev.Alt_Lname) else ISNULL(Prev.Alt_Lname,crt.xLName) end as Alt_Lname
FROM cteCursor prev
inner join cteRowNumber crt
on prev.fname = crt.fname and prev.RowNumber + 1 = crt.RowNumber
SELECT cte.*
FROM cteCursor cte -
Hi,
I have three records in table can I put three records in a single row in alv report,
Please suggest,
rewarded highly
thanks in advance
cheersHi Ray
Why don't you create an internal table with fields of the tables (lfa1, lfb1 and lfm1) you need to read and display?
A vendor has to have the company data if he used by accounting, if he has only org data, It should mean that vendor is used only as partner.
Anyway every org can belong to only one company code and every vendor can belong to several companies (I don't know your organization) so the output table should be:
DATA: BEGIN OF T_OUTPUT OCCURS 0,
<LFA1 FIELDS> LIKE LFA1-.....
<LFB1 FIELDS> LIKE LFB1-.....
<LFM1 FIELDS> LIKE LFM1-.....
END OF T_OUTPUT.
LOOP AT T_LFA1.
MOVE-CORRESPONDING T_LFA1 TO T_OUTPUT.
You should append one record for every org
LOOP AT T_LFM1 WHERE LIFNR = T_LFB1-LIFNR.
Check if this org. belong to company vendor
READ TABLE T_T024E WITH KEY EKORG = T_LFM1-EKORG.
IF NOT T_T024E-BUKRS IS INITIAL.
LOOP AT T_LFB1 WHERE LIFR = LFA1-LIFNR.
AND BUKRS = T_T024E-BUKRS.
You should append one record for every company
MOVE-CORRESPONDING T_LFB1 TO T_OUTPUT,
T_LFM1 TO T_OUTPUT.
APPEND T_OUTPUT.
DELETE: T_LFB1, T_LFM1.
ENDLOP.
ENDIF.
ENDLOOP.
Now you elaborate the org without company:
LOOP AT T_LFM1 WHERE LIFNR = LFA1-LIFNR.
and here you can elaborate the company with out org:
LOOP AT T_LFB1 WHERE LIFNR = LFA1-LIFNR.
MOVE-CORRESPONDING T_LFB1 TO T_OUTPUT,
T_LFM1 TO T_OUTPUT.
APPEND T_OUTPUT.
DELETE: T_LFB1, T_LFM1.
ENDLOOP.
and at the end you can elaborate the last vendor
companies:
LOOP AT T_LFB1 WHERE LIFNR.
MOVE-CORRESPONDING T_LFB1 TO T_OUTPUT.
ENDLOOP.
ENDLOOP.
Max
Check if org. belong to company
IF SY-SUBRC = 0.
MOVE-CORRESPONDING T_LFM1 TO T_OUTPUT.
APPEND T_LFM1.
DELETE T_LFM1
ENDIF.
If vendor haven't company data
ENDLOOP.
ENDLOOP.
Max -
Multiple row value coming in a single row(nclob)
hi ,
i have a requirement where i have to work on a column on datatype nclob, now here the value of 2 rows coming into a single column. like this:
select distinct extractvalue(xmltype(details),'/Anything/invoiceNumber') as invoices ,
actinguserid as user_id ,createdt
from bchistevent where bucket = 201301
and upper(type) = 'COM.AVOLENT.PRESENTATION.EVENT.INVOICEDOWNLOADEVENT'
--and bchistevent.bucket = to_char (add_months (sysdate, -1),'YYYYMM')
395452969-000-20130103 1.46388193452398E37 1/8/2013 3:05:42 AM
300000590-000-20090723 1.46388193452398E37 1/11/2013 8:11:45 AM
300000590-000-20090723 1.46388193452398E37 1/11/2013 8:12:50 AM
395453127-000-20130103 1.46388193452398E37 1/14/2013 4:44:26 AM
*300084670-000-20120906, 300084671-000-20120906* 1.46388193452398E37 1/7/2013 12:45:19 AM
395452626-000-20130103 1.46388193452398E37 1/8/2013 3:03:57 AM
300084679-000-20120906 1.46388193452398E37 1/11/2013 8:10:47 AM
300000728-000-20090731 1.46388193452398E37 1/11/2013 8:19:19 AM
300084679-000-20120906 1.46388193452398E37 1/14/2013 12:31:48 AM
300000590-000-20090723 1.46388193452398E37 1/14/2013 4:13:19 AM
395452718-000-20130103 1.46388193452398E37 1/8/2013 7:10:19 AM
300084679-000-20120906 1.46388193452398E37 1/23/2013 6:54:11 AM
300084679-000-20120906 1.46388193452398E37 1/22/2013 3:11:54 AM
300000590-000-20090723 1.46388193452398E37 1/11/2013 8:14:02 AM
395453127-000-20130103 1.46388193452398E37 1/14/2013 4:33:12 AM
300084679-000-20120906 1.46388193452398E37 1/22/2013 3:03:36 AM
300084679-000-20120906 1.46388193452398E37 1/14/2013 12:34:13 AM
395452997-000-20130103 1.46388193452398E37 1/7/2013 3:31:38 AM
395452391-000-20121027 1.46388193452398E37 1/3/2013 4:40:05 AM
and the value of bold highlighted row is coming in a single row, plzz help how to break this in 2 rows??
Edited by: user1175303 on Mar 13, 2013 5:43 AMuser1175303 wrote:
the column value which is in question is <Anything><invoiceNumber>300084670-000-20120906, 300084671-000-20120906</invoiceNumber></Anything> here i am getting 2 values in a single column and because of this i am unable to get desired output...So you have XML issue but trying to resolve it in Oracle??? Why <invoiceNumber> holds two invoice numbers? Anyway:
with t as (
select distinct extractvalue(xmltype(details),'/Anything/invoiceNumber') as invoices,
actinguserid as user_id,
createdt
from bchistevent
where bucket = 201301
and upper(type) = 'COM.AVOLENT.PRESENTATION.EVENT.INVOICEDOWNLOADEVENT'
select regexp_substr(invoices,'[^,]+',1,column_value) invoices,
user_id,
createdt
from t,
table(
cast(
multiset(
select level
from dual
connect by level <= length(regexp_replace(invoices,'[^,]')) + 1
as sys.OdciNumberList
/SY. -
How to display mutliple rows of database data in a single row
We are using XML Publisher 5.6.2. We have a requirement, where we need to display a multiple database rows for a single column in a single row.
Basically say a database column has 5 rows
A1
A2
A3
A4
A5
We want to display in the output in a single cell as
A1 A2 A3 A4 A5
Thanks,
- Vasu -Look at this blog,
http://blogs.oracle.com/xmlpublisher/2007/05/24#a325 -
Merging multiple rows in to a single row (when rows meet merging criteria)
Hi
I have a scenario to merge multiple rows in to a single rows when the data in those rows fall in merge criteria .Below is how my data is
Now the merging logic for the above rows is , we need to combine multiple rows in to a single row when the data in those rows permits us to do in that way. Simply saying , its like sandwich where we combine multiple things to a single piece.The output for
the above rows should be
Here we combined Row 1 ,2, 3 in to a single row as the data in those rows permits to merge in to single row. But the row 4 cannot be combined to any of those rows because the data in those rows doesn't permits us do a merge ( As the value of the column
JobSource for the row 4 is different from the other rows ) .
My original data has 56 columns , but for readability i kept only 9 columns.
can you please throw some idea on how to achieve this scenario. I know we need to use CTE for achieving this, but i am not able succeed in doing an iteration on multiple rows.
Appreciate your response .Thanks for your reply .
Rule for merging is simple . First of all there is no unique row identifier for each row , the fact table is not having an identity column in the database . I called row 1 , row 2 etc in my post above only to make better explanation of my scenario.
The rule for merge is below.
1) we can combine only when the data in a column for one row is null & the data in same column for the other row is not null . It should also satisfy the condition where the data in other columns should conflict each other.
2) Data for all columns for the merging rows should not be conflicting (i.e. we should not merge the rows when the data in a column is not equal to the same column in the other row
,considering not null value)
Steps in merging the above source data :
1) Consider the case of row 1 and row 2 in the source, we can combine these rows as the data is satisfying the rule 1 for columns (Jobsource,Flight, Package,Update,Iscancelled
,Result, Severity) and columns (JobID and RuleName ) fall under rule 2. we merge these two rows in to a single row and keep in that in the table.
2) Then the resulting row is again merged with the second row which is present above by applying the rule 1 and rule 2 . Below would be output of merge operation.
Now there would be only two rows in the output . But these rows cannot be merged as the data doesn't satisfy the merge rules 2 . As Jobsource for the row 1 in the above output is "PresubmissionSource" which is not equal
to "PostSubmission" jobSource which is in row 2. So these two rows are left unmerged .So the above two rows would be the output of merge operation on my source data.
This process has to be repeated for all the rows in the table. As of now my table as 92 Million rows with 56 columns which need to be considered for merging rows. I replicated my scenario in 9 columns and 4 rows to understand better. -
Data Populated in a single row??
Hi,
I have a requirement as follows:
Suppose there is a column "A". There many be "n" no. of records for that column "A"
I want the code logic for this to sum up all the "n" no. of records,which is of type amount and to be populated in a single row.
Ex :
Column A
7867.68
5658.65
566765.44
n
like this "n" no. of records would be populated (at the runtime).
I need the output as :
Column A
SUM(7867.68 + 5658.65 + 566765.44 + .......n) in a single row.
Immediate reply would be greatly appreciated.
Thanks in advance,
Regards,
Vamc
Message was edited by: Manish Kumar : Do not ask for urgent/immediate replyHi vamshi,
Both Collect and AT END OF.... END AT, we can use for the above requirement.
Collect Statement:
LOOP AT IT_TABLE into WA_TABLE.
COLLECT WA_TABLE TO IT_FINAL.
ENDLOOP.
Control Break Statement (AT END OF... END AT):
If you are using Control break statement, before LOOP Statement, sort the table as shown below.
"Data Declarations
DATA: L_END TYPE I VALUE 0,
L_COUNT TYPE I VALUE 0.
"Sort the Internal Table
SORT IT_TABLE BY MATNR.
LOOP AT IT_TABLE INTO WA_TABLE.
L_COUNT = L_COUNT + WA_TABLE-VALUE.
AT END OF MATNR.
L_END = 1.
ENDAT.
IF L_END = 1.
MOVE CORRESPONDING WA_TABLE to WA_FINAL.
"Assign Count value
WA_FINAL-TOTAL = L_COUNT.
"Append the value to Internal Table
APPEND WA_FINAL TO IT_FINAL.
"Clear WA & Count
CLEAR: WA_FINAL,
L_COUNT,
L_END.
ENDIF.
ENDLOOP.
Regards
Rajkumar Narasimman
Maybe you are looking for
-
How can I stop iTunes installing Dupes?
I am cleansing and consolidating my entire MP3 collection to one external hard drive. All was going well until I had about 75GB of MP3s transferring from one drive into iTunes when it quit at about 90% for no apparent reason. So I now do not know wha
-
Massive assignation of attachments to fixed assets
Hi there, I have this situation, we have loaded fixed assets in the R3 system, I loaded the data using LSMW, but we need the fixed assets to have a picture from the legacy system. I know that trx AS02 allows you to attach a file to a fixed asset. I a
-
Could you please add an option to program search for "recently searched programs" with (for example) a 7-day history? It's annoying to have to keep typing the same program titles week after week.
-
Customer complaints unable to download
We're getting increase numbers of complaints from customers unable to download one or more editions. The download start but after a while they get the message that a problem occurred on the connection to the server. What can we do?
-
Installing the mavericks has been stopped--.it is saying that some error has been occurred
installing the mavericks has been stopped--.it is saying that some error has been occurred and asking to check in purchase section.But again after resuming download it is showing the same problem....my update has been installed upto 1.5 GB and stoppe