Converting columns to single row
Hi,
I have scenario like this
bw_we account user_defined1 user_defined2 user_defined3
'abc' 1234 5678 6789 0987
abc 2424 64635 8977 789534
def 44382 4924929 9449 242424
def 42488 88488 888324 8888i need to get result set as
gl_account
1234
5678
6789
0987
2424
64635
8977
789534
.all the values in each row should be converted into one column..is there any oracle function do this?
In 10g you'll have to do something like the following to UNPIVOT the results:
SQL> WITH test_data AS
2 (
3 SELECT 'abc' AS BW_WE, 1234 AS ACCOUNT, 5678 AS USER_DEFINED1, 6789 AS USER_DEFINED2, 0987 AS USER_DEFINED3 FROM DUAL UNION ALL
4 SELECT 'abc' AS BW_WE, 2424 AS ACCOUNT, 64635 AS USER_DEFINED1, 8977 AS USER_DEFINED2, 789534 AS USER_DEFINED3 FROM DUAL UNION ALL
5 SELECT 'def' AS BW_WE, 44382 AS ACCOUNT, 4924929 AS USER_DEFINED1, 9449 AS USER_DEFINED2, 242424 AS USER_DEFINED3 FROM DUAL UNION ALL
6 SELECT 'def' AS BW_WE, 42488 AS ACCOUNT, 88488 AS USER_DEFINED1, 888324 AS USER_DEFINED2, 888 AS USER_DEFINED3 FROM DUAL
7 )
8 -- END SAMPLE DATA
9 SELECT DECODE
10 (
11 RN
12 , 1, ACCOUNT
13 , 2, USER_DEFINED1
14 , 3, USER_DEFINED2
15 , 4, USER_DEFINED3
16 ) AS COLLAPSED_COL
17 FROM TEST_DATA
18 CROSS JOIN (SELECT ROWNUM RN FROM DUAL CONNECT BY LEVEL <= 4)
19 /
COLLAPSED_COL
1234
2424
44382
42488
5678
64635
4924929
88488
6789
8977
9449
888324
987
789534
242424
888
16 rows selected.
Similar Messages
-
How to convert single column into single row
I need to convert single column into single row having n no.of.values in a column. without using case or decode. I need a query to display as below.
emp_id
100
101
102
102
103
200
I need output like 100,101,102,103,104.........200.I assume you want to convert 200 rows with one column into one row with 200 columns. If so, this is called pivot. If you know number of rows (max possible number of rows) and you are on 11G you can use PIVOT operator (on lower versions GROUP BY + CASE). Otherwise, if row number isn't known, you can use dynamic SQL or assemble select on clent side. Below is example emp_id = 1,2,..5 (to give you idea) and you are on 11G:
with emp as (
select level emp_id
from dual
connect by level <= 5
select *
from emp
pivot(
max(emp_id) for emp_id in (1 emp_id1,2 emp_id2,3 emp_id3,4 emp_id4,5 emp_id5)
EMP_ID1 EMP_ID2 EMP_ID3 EMP_ID4 EMP_ID5
1 2 3 4 5
SQL>
SY. -
Interactive report: Can I exclude a particular column from single row view?
Hi -- I posted on this yesterday (Possible to exclude interactive report column from single row display?
a bit anxious for suggestions, I guess!
I've added a column with edit-link functionality to my interactive report query (that is, the link
is not attached to the database data... it's an additional column and shows an icon). It's also
in addition to the default single row view link. (We need both.)
Unfortunately, the Edit link column shows up in the single row view. I've pared it down as much
as I can: the label is empty, and the null value shows as "-". But an extra row with "-" is pretty
ugly. Is there a way to always, completely exclude this column from the single row view?
I know I could put the edit link on a data column, but:
1) I want the link to always be to the left of the data (and the user can re-order columns)
2) when the user doesn't have edit privileges, the link will need to be disabled or just not
be displayed, and I think that would be a problem if the link were on the data. (true?)
Thanks,
CarolPlease disregard this thread, and the one it refers back to. I see a flaw in the design of what I was attempting to do! Creating the link for Editing as a column means the user could inadvertently not display it, or move it, or... any number of problematic scenarios.
Thanks,
Carol -
Hi All,
Can any one plzz help me out how to convert columns in to rows.
I am having data as follows:
Date1 Date2 Date3 Date4 Date5 Date6
I need the Out put as Below
Date
Date1
Date2
Date3
Date4
Date5
Date6
Thanks in Advance
Regards
Praveenyou can do this by using OTN's search engine:
http://forums.oracle.com/forums/search.jspa?objID=f75&q=converting+columns+into+raws -
Column values convert to a single row...
I have to return the column values to a single row seperated by comma.
If any null values in the column just ignore it without leaving a comma.
below is an eg. There are three values and two NULL values in the table
SQL> select ID from temp_fa;
ID
1
2
3
5
6 rows selected.
I am expecting an output as 1,2,3,5Please helpThere is always more than one way in the Oracle world ;)
You can use TRIM, for example (same setup as your example):
hoek&XE> create table t as select level col from dual connect by level <= 6;
Tabel is aangemaakt.
hoek&XE> update t set col = null where col in (1,3,5);
3 rijen zijn bijgewerkt.
hoek&XE> select * from t;
COL
2
4
6
6 rijen zijn geselecteerd.
hoek&XE> select ltrim(sys_connect_by_path(col, ','), ',') output
2 from ( select col
3 , row_number() over (order by col) rn
4 from t
5 )
6 where connect_by_isleaf=1
7 start with rn=1
8 connect by rn = prior rn+1;
OUTPUT
2,4,6,,,
1 rij is geselecteerd.
hoek&XE> select trim ( both ',' from sys_connect_by_path(col, ',')) output
2 from ( select col
3 , row_number() over (order by col) rn
4 from t
5 )
6 where connect_by_isleaf=1
7 start with rn=1
8 connect by rn = prior rn+1;
OUTPUT
2,4,6
1 rij is geselecteerd. -
Possible to exclude interactive report column from single row display?
hi -- I have an interactive report that I've added a column to (in addition to the table columns that are selected).
The added column is a link to a form for editing a single row. This column/link is in addition to the default link
that goes to a single row view. So, a row of the report has 1) the single row view link, 2) the Edit link,
3) the columns in the table.
The edit link column is named "Edit" (so Edit appears above the "pencil" link icon). Problem is that when the
user goes to the single row view, the Edit column is displayed. (I've set the label in the view to a blank space,
and the value is null (displayed as "-" in the single row view)... but it's generally ugly, and adds that nonsensical
line to the single row view.
Is there any way to never display that column in the single row view, but always display it in the report?
I've considered putting the edit link on the first column of the table... but I don't like that the link will move
if the user changes the column order. It seems it should always be at the left of the row, like the single row view
link.
Thanks,
CarolPlease disregard this thread. I see a flaw in the design of what I was attempting to do! Creating the link for Editing as a column means the user could inadvertently not display it, or move it, or... any number of problematic scenarios.
Thanks,
Carol -
Interactive Report's Column Definition - Single Row View Label Bug?
Hello,
I have an Interactive report that I've added a Blue background to certain column headings (done by giving Column Heading a background color), this works fine except for the Single Row view - it displays the <....> code I used to get the background color and the column heading. I've tried to change the Single Row View Label by unchecking the 'Use Same Text for Single Row View' on the Column Definition and giving a different label, it just ignores whatever I put in and shows the Column Heading with the < .... > code. Is this a Bug or am I doing something wrong?
Thanks,
AnnaHi Anna,
I believe that it is a bug.
If the 'Use Same Text for Single Row View' checkbox is unticked and some text is entered in the 'Single Row View Label' field it gets ignored completely when viewed in the single row view and the existing value in 'Column Heading' is used.
I wanted to do something similar to you, colouring some of the Interactive Report column headers, and it works fine in the IR report itself but shows the code (span style:color etc) in the single row query. I am using Apex 3.1.1 and the Sand Theme.
I created a simple application on apex.oracle.com using the red theme and in single row view it still ignores the 'Single Row View Label' but interestingly shows the Column Heading text without the code. This indicates that there could be a work-around by altering the theme.
Regards,
Chris -
Converting column store to row store
Hello Everyone,
I have a question related to the Column store to Row Store conversion:
Is it always necessary to perform the conversion when all connections to the system have been stopped from the application level?
what would be the loss if the conversion is performed with the applications or connections to the system still open?
Regards,
VinayHi Vinay,
Can you explain your questions more clearly? Do you mean convert a column table to a row table?
Best regards,
Wenjun -
Logic to show two rows of a column as Single row
Hi
I need output some thing like below
ind type amount
xxxx abc 123
bac 456
yyyy abc 123
bac 456
zzzzz abc 123
bac 456
two rows considered as one row .
Can any one please help me how to solve this.sample like
iin the first two the column ind should be show as single xxxx and also type with in same row
Edited by: varma on Apr 10, 2012 1:27 PM -
Converting Column Header to Rows Value in SQL Query
Dear Guys,find blew the table from A1_30 to YetToDue columns. I need from A1_30 to A356 columns convert into row values and that values column head name like Abstract. A1_30 A31_60 A61_90 A91_120 A121_180A181_365A365 BalArr NoOfLoanLoanOS YetToDue
0 0 0 101 0 0 0 909 66730 909 3637
0 40 0 0 0 0 0 1818 66681 1818 3637
0 56 0 0 0 0 0 1818 70210 1818 5455
0 0 66 0 0 0 0 909 73735 909 7273
0 35 0 0 0 0 0 2727 73327 2727 7273
0 0 66 0 0 0 0 909 73353 909 7273
0 40 0 0 0 0 0 909 66778 909 3637
0 40 0 0 0 0 0 1818 66744 1818 3637
0 0 0 101 0 0 0 909 66787 909 3637
0 40 0 0 0 0 0 1818 66671 1818 3637
0 56 0 0 0 0 0 1818 70201 1818 5455
0 56 0 0 0 0 0 1818 70204 1818 5455
0 0 66 0 0 0 0 909 73331 909 7273
0 40 0 0 0 0 0 1818 66726 1818 3637
0 40 0 0 0 0 0 2727 66798 2727 3637
0 40 0 0 0 0 0 909 66676 909 3637
0 40 0 0 0 0 0 1818 66685 1818 3637
0 56 0 0 0 0 0 1818 70209 1818 5455
0 57 0 0 0 0 0 1818 70226 1818 5455
0 35 0 0 0 0 0 2727 73762 2727 7273
0 0 66 0 0 0 0 909 73333 909 7273
0 40 0 0 0 0 0 1818 66759 1818 3637
0 40 0 0 0 0 0 882 66597 882 8826
0 40 0 0 0 0 0 2727 66789 2727 3637
0 40 0 0 0 0 0 2646 66609 2646 8826
0 40 0 0 0 0 0 2646 66641 2646 8826
0 57 0 0 0 0 0 1818 70193 1818 5455
0 56 0 0 0 0 0 1818 70202 1818 5455
0 35 0 0 0 0 0 2727 73757 2727 7273
0 0 66 0 0 0 0 909 73345 909
7273
I expecting the results:
Abstract BalArr NoOfLoan LoanOS YetToDue
A0_30 0 0 0 0
A31_60 45261 1579445 45261 122852
A61_90 4545 367097 4545 36365
A91_120 1818 133517 1818 7274
A121_180 0 0 0 0
A181_365 0 0 0 0
A365 0 0 0 0
pls help I really appreciate ..Dear Partrck Hurst.I have facing struggle with my store procedure , It work very slow performance when i pass the entryPlease go through my store procedure and let me advice if i wrote the script wrongly .ALTER PROCEDURE [dbo].[MFDB_GenerateDCBCollection]
@i_BranchId INT,
@i_SHGId INT,
@p_SHGMemberId INT,
@i_LoanNumber INT,
@i_DayOpenDate DATETIME,
@o_ErrorStatus INT Output
) AS
--WITH ENCRYPTION AS
DECLARE @p_DayOpenDate AS DATETIME
DECLARE @p_BranchId AS INT
DECLARE @D_FromDate AS DATE
DECLARE @D_ToDate AS DATE
DECLARE @E_ToDate AS DATE
DECLARE @p_RecordCounter AS INT
DECLARE @p_RecordCount AS INT
DECLARE @p_TempCollectionId AS INT
DECLARE @p_TempStateId AS INT
DECLARE @p_TempRegionId AS INT
DECLARE @p_TempBranchId AS INT
DECLARE @p_TempSHGId AS INT
DECLARE @p_TempSHGName AS VARCHAR(75)
DECLARE @p_TempSHGMemberId AS INT
DECLARE @p_TempMemberId AS INT
DECLARE @p_TempMemberName AS VARCHAR(75)
DECLARE @p_TempVillageName AS VARCHAR(100)
DECLARE @p_TempPanchayatName AS VARCHAR(100)
DECLARE @p_TempPanchayatId AS INT
DECLARE @p_TempVillageId AS INT
DECLARE @p_TempInstNumber AS INT
DECLARE @p_TempLoanNumber AS INT
DECLARE @p_TempDisbursedDate AS DATE
DECLARE @p_TempStatus AS INT
DECLARE @p_TempLoanSchemeId AS INT
DECLARE @p_TempLCategoryId AS INT
DECLARE @p_TempLActivityId AS INT
DECLARE @p_TempLActSpecId AS INT
DECLARE @p_TempLoanAmount AS NUMERIC(18,2)
DECLARE @p_TempApprovalStatus AS INT
DECLARE @p_TempFundingAgencyId AS INT
DECLARE @p_TempOpenBalPrin AS NUMERIC(18,2)
DECLARE @p_TempOpenBalInt AS NUMERIC(18,2)
DECLARE @p_TempStdPrin AS NUMERIC(18,2)
DECLARE @p_TempStdInt AS NUMERIC(18,2)
DECLARE @p_TempCollPrin AS NUMERIC(18,2)
DECLARE @p_TempCollInt AS NUMERIC(18,2)
DECLARE @p_TempAdvancePrin AS NUMERIC(18,2)
DECLARE @p_TempAdvanceInt AS NUMERIC(18,2)
DECLARE @p_TempArrearPrin AS NUMERIC(18,2)
DECLARE @p_TempArrearInt AS NUMERIC(18,2)
DECLARE @p_TempBalancePrin AS NUMERIC(18,2)
DECLARE @p_TempBalanceInt AS NUMERIC(18,2)
DECLARE @p_TempDemandDate AS DATE
DECLARE @p_TempCollectionDate AS DATE
DECLARE @p_TempLStatus AS INT
DECLARE @p_TempLSourceId AS INT
DECLARE @p_TempVoucherName AS VARCHAR(75)
DECLARE @p_TempInsurance AS NUMERIC(18,2)
DECLARE @p_TempUpFrontFess AS NUMERIC(18,2)
DECLARE @p_TempDocumentFees AS NUMERIC(18,2)
DECLARE @p_TotalInterest AS NUMERIC(18,2)
DECLARE @i_Month AS INT = 1
DECLARE @p_Year AS INT = 2015
DECLARE @i_SHGMemberId AS INT
BEGIN TRANSACTION DCBGenerated
BEGIN
Set @o_ErrorStatus = 0
SELECT @D_FromDate = DATEADD(MONTH, @i_Month - 1, DATEADD(YEAR, @p_Year - 1900, 0)) --as [START DATE]
SELECT @D_ToDate = DATEADD(MONTH, @i_Month, DATEADD(YEAR, @p_Year - 1900, -1)) --as [EMD DATE]
SELECT @i_SHGMemberId = MFDB_SHGMemberId FROM MFDB_SHGMEMBER_MASTER WHERE MFDB_SHGId = @i_SHGId AND MFDB_MemberId = @p_SHGMemberId
SELECT @p_TempVillageName = MFDB_VillageName,
@p_TempVillageId = MFDB_VillageId ,
@p_TempPanchayatId = MFDB_PanchayatId ,
@p_TempPanchayatName = MFDB_PanchayatName
FROM MFDB_SHG_MASTER WHERE MFDB_SHGId = @i_SHGId
/* Drop and Create TempCollection Table. This table has all installments for a given member */
CREATE TABLE #TEMPDCBCOLLECTION
[MFDB_LedgerNumber] [int] IDENTITY(1,1) NOT NULL,
[MFDB_StateId] [int] NOT NULL,
[MFDB_RegionId] [int] NOT NULL,
[MFDB_BranchId] [int] NOT NULL,
[MFDB_SHGId] [int] NOT NULL,
[MFDB_SHGName] [varchar](75) NOT NULL,
[MFDB_ShgMemberId] [int] NOT NULL,
[MFDB_MemberName] [varchar](50) NOT NULL,
[MFDB_LoanNumber] [int] NOT NULL,
[MFDB_LoanAmount] [int] NOT NULL
) ON [PRIMARY]
/* Drop and Create TempCollection Table. This table has all installments for a given member */
;WITH CTE_1 AS
SELECT CT.MFDB_StateId AS StateId,
CT.MFDB_RegionId AS RegionId,
CT.MFDB_BranchId AS BranchId,
CT.MFDB_SHGId AS ShgId,
SM.MFDB_SHGName AS ShgName,
CT.MFDB_SHGMemberId AS SHGMemberId,
SMM.MFDB_MemberName AS MemberName,
CT.MFDB_LoanNumber AS LoanNumber,
LM.MFDB_LoanAmount AS LoanAmount
FROM dbo.MFDB_COLLECTION_TXN CT
LEFT JOIN dbo.MFDB_COLLECTION_AUDIT CA
On CT.MFDB_SHGId = CA.MFDB_SHGId
And CT.MFDB_MemberId = CA.MFDB_MemberId
And CT.MFDB_LoanNumber = CA.MFDB_LoanNumber
And CT.MFDB_CollectionId = CA.MFDB_CollectionId
And CT.MFDB_LSourceId = CA.MFDB_LSourceId
INNER JOIN dbo.MFDB_SHG_MASTER SM
On CT.MFDB_SHGId = SM.MFDB_SHGId
INNER JOIN dbo.MFDB_SHGMEMBER_MASTER SMM
On CT.MFDB_SHGId = SMM.MFDB_SHGId
And CT.MFDB_SHGMemberId = SMM.MFDB_SHGMemberId
INNER JOIN dbo.MFDB_LOANAPPLICATION_MASTER LM
ON LM.MFDB_SHGId = CT.MFDB_SHGId
AND LM.MFDB_ShgMemberId = CT.MFDB_ShgMemberId
AND LM.MFDB_LoanNumber = CT.MFDB_LoanNumber
Where DATEDIFF(DAY,MFDB_CollectionDate,@i_DayOpenDate) = 0
And CT.MFDB_CollectionAmount > 0
And CT.MFDB_CollApprovedBy IS NULL
AND CT.MFDB_BranchId = @i_BranchId
AND CT.MFDB_SHGId = @i_SHGId
AND CT.MFDB_ShgMemberId = @i_SHGMemberId
AND CT.MFDB_LoanNumber = @i_LoanNumber
CTE_2 AS
SELECT DISTINCT
StateId,
RegionId,
BranchId,
ShgId,
ShgName,
SHGMemberId,
MemberName,
LoanNumber,
LoanAmount
FROM CTE_1
INSERT INTO #TEMPDCBCOLLECTION ([MFDB_StateId],[MFDB_RegionId],[MFDB_BranchId],[MFDB_SHGId],[MFDB_SHGName],[MFDB_ShgMemberId],[MFDB_MemberName],[MFDB_LoanNumber],[MFDB_LoanAmount])
SELECT StateId,
RegionId,
BranchId,
ShgId,
ShgName,
SHGMemberId,
MemberName,
LoanNumber,
LoanAmount
FROM CTE_2
DECLARE DCBMonthCursor CURSOR FOR
SELECT [MFDB_StateId],
[MFDB_RegionId],
[MFDB_BranchId],
[MFDB_SHGId],
[MFDB_SHGName],
[MFDB_ShgMemberId],
[MFDB_MemberName],
[MFDB_LoanNumber],
[MFDB_LoanAmount]
FROM #TEMPDCBCOLLECTION
OPEN DCBMonthCursor
FETCH NEXT FROM DCBMonthCursor
INTO @p_TempStateId ,
@p_TempRegionId ,
@p_TempBranchId ,
@p_TempSHGId ,
@p_TempSHGName ,
@p_TempSHGMemberId ,
@p_TempMemberName ,
@p_TempLoanNumber ,
@p_TempLoanAmount
WHILE (@@FETCH_STATUS = 0)
BEGIN
Set @p_TotalInterest = 0
Select @p_TotalInterest = SUM(MFDB_StdInterest) From MFDB_LOAN_DISBURSEMENT Where MFDB_SHGId = @p_TempSHGId And MFDB_ShgMemberId = @p_TempSHGMemberId And MFDB_LoanNumber = @p_TempLoanNumber
;With
CTE_1 AS
SELECT LM.MFDB_StateId AS StateId ,
LM.MFDB_RegionId AS RegionId,
LM.MFDB_BranchId AS BranchId,
LM.MFDB_SHGId AS SHGId,
SM.MFDB_SHGName AS SHGName,
LM.MFDB_ShgMemberId AS SHGMemId,
LM.MFDB_MemberName AS MemberName,
SM.MFDB_VillageId AS VillageId,
LM.MFDB_LoanNumber AS LoanNo,
LM.MFDB_LoanAmount AS LoanAmount,
LM.MFDB_DisburseDate AS DisbursedDate,
ISNULL((SELECT LD.MFDB_DemandDate
FROM dbo.MFDB_LOAN_DISBURSEMENT LD
Where LD.MFDB_SHGId = LM.MFDB_SHGId
And LD.MFDB_ShgMemberId = LM.MFDB_ShgMemberId
And LD.MFDB_LoanNumber = LM.MFDB_LoanNumber
And DATEDIFF(DAY,LD.MFDB_DemandDate,@i_DayOpenDate) = 0
And LD.MFDB_ApprovalStatus = 1 ),NULL) AS DemandDate,
ISNULL((SELECT TOP 1 CT.MFDB_CollectionDate
FROM dbo.MFDB_COLLECTION_TXN CT
Inner Join dbo.MFDB_COLLECTION_AUDIT CA
On CT.MFDB_SHGId = CA.MFDB_SHGId
And CT.MFDB_ShgMemberId = CA.MFDB_ShgMemberId
And CT.MFDB_LoanNumber = CA.MFDB_LoanNumber
And CT.MFDB_LSourceId = CA.MFDB_LSourceId
And CT.MFDB_CollectionId = CA.MFDB_CollectionId
WHERE CT.MFDB_SHGId = LM.MFDB_SHGId
AND CT.MFDB_ShgMemberId = LM.MFDB_ShgMemberId
AND CT.MFDB_LoanNumber = LM.MFDB_LoanNumber
And DATEDIFF(DAY,CT.MFDB_CollectionDate,@i_DayOpenDate) = 0
ORDER BY CA.MFDB_CollectionId DESC),NULL) AS CollectionDate,
ISNULL((SELECT TOP 1 CA.MFDB_InstallmentNumber
FROM dbo.MFDB_COLLECTION_AUDIT CA
Inner Join dbo.MFDB_COLLECTION_TXN CT
On CT.MFDB_SHGId = CA.MFDB_SHGId
And CT.MFDB_ShgMemberId = CA.MFDB_ShgMemberId
And CT.MFDB_LoanNumber = CA.MFDB_LoanNumber
And CT.MFDB_LSourceId = CA.MFDB_LSourceId
And CT.MFDB_CollectionId = CA.MFDB_CollectionId
WHERE CA.MFDB_SHGId = LM.MFDB_SHGId
AND CA.MFDB_ShgMemberId = LM.MFDB_ShgMemberId
AND CA.MFDB_LoanNumber = LM.MFDB_LoanNumber
And DATEDIFF(DAY,CT.MFDB_CollectionDate,@i_DayOpenDate) = 0
ORDER BY CA.MFDB_CollectionId DESC),
ISNULL((SELECT LD.MFDB_InstallmentNumber
FROM dbo.MFDB_LOAN_DISBURSEMENT LD
Where LD.MFDB_SHGId = LM.MFDB_SHGId
And LD.MFDB_ShgMemberId = LM.MFDB_ShgMemberId
And LD.MFDB_LoanNumber = LM.MFDB_LoanNumber
And DATEDIFF(DAY,LD.MFDB_DemandDate,@i_DayOpenDate) = 0
And LD.MFDB_ApprovalStatus = 1 ),0)) AS IntallNo,
ISNULL(CAST((SELECT TOP 1 NR.MFDB_BalancePrincipal
FROM dbo.MFDB_NEW_DCB_REPORT NR
WHERE NR.MFDB_SHGId = LM.MFDB_SHGId
AND NR.MFDB_ShgMemberId = LM.MFDB_ShgMemberId
AND NR.MFDB_LoanNumber = LM.MFDB_LoanNumber
ORDER BY NR.MFDB_LedgerNumber DESC) AS NUMERIC(18,0)),0) AS ArrPrincipal,
ISNULL(CAST((SELECT TOP 1 NR.MFDB_BalanceInterest
FROM dbo.MFDB_NEW_DCB_REPORT NR
WHERE NR.MFDB_SHGId = LM.MFDB_SHGId
AND NR.MFDB_ShgMemberId = LM.MFDB_ShgMemberId
AND NR.MFDB_LoanNumber = LM.MFDB_LoanNumber
ORDER BY NR.MFDB_LedgerNumber DESC) AS NUMERIC(18,0)),0) AS ArrInterest,
0 AS CurrDemandPrin,
0 AS CurrDemandInt,
ISNULL((SELECT CAST(SUM(ISNULL(CA.MFDB_CollectedPrincipal,0) +
ISNULL(CA.MFDB_AdvancePrincipal,0) +
ISNULL(CA.MFDB_ArrearPrincipal,0)) AS NUMERIC(18,0))
FROM dbo.MFDB_COLLECTION_AUDIT CA
Inner Join dbo.MFDB_COLLECTION_TXN CT
On CT.MFDB_SHGId = CA.MFDB_SHGId
And CT.MFDB_ShgMemberId = CA.MFDB_ShgMemberId
And CT.MFDB_LoanNumber = CA.MFDB_LoanNumber
And CT.MFDB_LSourceId = CA.MFDB_LSourceId
And CT.MFDB_CollectionId = CA.MFDB_CollectionId
WHERE CA.MFDB_SHGId = LM.MFDB_SHGId
AND CA.MFDB_ShgMemberId = LM.MFDB_ShgMemberId
AND CA.MFDB_LoanNumber = LM.MFDB_LoanNumber
And DATEDIFF(DAY,CT.MFDB_CollectionDate,@i_DayOpenDate) = 0
),0) AS CurrCollPri,
ISNULL((SELECT CAST(SUM(ISNULL(CA.MFDB_CollectedInterest,0) +
ISNULL(CA.MFDB_AdvanceInterest,0) +
ISNULL(CA.MFDB_ArrearInterest,0)) AS NUMERIC(18,0))
FROM dbo.MFDB_COLLECTION_AUDIT CA
Inner Join dbo.MFDB_COLLECTION_TXN CT
On CT.MFDB_SHGId = CA.MFDB_SHGId
And CT.MFDB_ShgMemberId = CA.MFDB_ShgMemberId
And CT.MFDB_LoanNumber = CA.MFDB_LoanNumber
And CT.MFDB_LSourceId = CA.MFDB_LSourceId
And CT.MFDB_CollectionId = CA.MFDB_CollectionId
WHERE CA.MFDB_SHGId = LM.MFDB_SHGId
AND CA.MFDB_ShgMemberId = LM.MFDB_ShgMemberId
AND CA.MFDB_LoanNumber = LM.MFDB_LoanNumber
And DATEDIFF(DAY,CT.MFDB_CollectionDate,@i_DayOpenDate) = 0
),0) AS CurrCollInt,
ISNULL(CAST((SELECT TOP 1 NR.MFDB_AdvCBPrincipal
FROM dbo.MFDB_NEW_DCB_REPORT NR
WHERE NR.MFDB_SHGId = LM.MFDB_SHGId
AND NR.MFDB_ShgMemberId = LM.MFDB_ShgMemberId
AND NR.MFDB_LoanNumber = LM.MFDB_LoanNumber
ORDER BY NR.MFDB_LedgerNumber DESC) AS NUMERIC(18,0)),0) AS AdvOBPri,
ISNULL(CAST((SELECT TOP 1 NR.MFDB_AdvCBInterest
FROM dbo.MFDB_NEW_DCB_REPORT NR
WHERE NR.MFDB_SHGId = LM.MFDB_SHGId
AND NR.MFDB_ShgMemberId = LM.MFDB_ShgMemberId
AND NR.MFDB_LoanNumber = LM.MFDB_LoanNumber
ORDER BY NR.MFDB_LedgerNumber DESC) AS NUMERIC(18,0)),0) AS AdvOBInt,
0 AS AdvCBPri,
0 AS AdvCBInt,
0 AS BalancePri,
0 AS BalanceInt,
ISNULL((SELECT CAST(SUM(ISNULL(CA.MFDB_CollectedPrincipal,0) +
ISNULL(CA.MFDB_AdvancePrincipal,0) +
ISNULL(CA.MFDB_ArrearPrincipal,0)) AS NUMERIC(18,0))
FROM dbo.MFDB_COLLECTION_AUDIT CA
Inner Join dbo.MFDB_COLLECTION_TXN CT
On CT.MFDB_SHGId = CA.MFDB_SHGId
And CT.MFDB_ShgMemberId = CA.MFDB_ShgMemberId
And CT.MFDB_LoanNumber = CA.MFDB_LoanNumber
And CT.MFDB_LSourceId = CA.MFDB_LSourceId
And CT.MFDB_CollectionId = CA.MFDB_CollectionId
WHERE CA.MFDB_SHGId = LM.MFDB_SHGId
AND CA.MFDB_ShgMemberId = LM.MFDB_ShgMemberId
AND CA.MFDB_LoanNumber = LM.MFDB_LoanNumber
And DATEDIFF(DAY,CT.MFDB_CollectionDate,@i_DayOpenDate) >= 0
),0) AS OS_Pri,
ISNULL((SELECT CAST(SUM(ISNULL(CA.MFDB_CollectedInterest,0) +
ISNULL(CA.MFDB_AdvanceInterest,0) +
ISNULL(CA.MFDB_ArrearInterest,0)) AS NUMERIC(18,0))
FROM dbo.MFDB_COLLECTION_AUDIT CA
Inner Join dbo.MFDB_COLLECTION_TXN CT
On CT.MFDB_SHGId = CA.MFDB_SHGId
And CT.MFDB_ShgMemberId = CA.MFDB_ShgMemberId
And CT.MFDB_LoanNumber = CA.MFDB_LoanNumber
And CT.MFDB_LSourceId = CA.MFDB_LSourceId
And CT.MFDB_CollectionId = CA.MFDB_CollectionId
WHERE CA.MFDB_SHGId = LM.MFDB_SHGId
AND CA.MFDB_ShgMemberId = LM.MFDB_ShgMemberId
AND CA.MFDB_LoanNumber = LM.MFDB_LoanNumber
And DATEDIFF(DAY,CT.MFDB_CollectionDate,@i_DayOpenDate) >= 0
),0) AS OS_Int,
0 AS RepaymentPercentage,
LM.MFDB_LoanSchemeId AS LoanProduct,
LM.MFDB_LActivityId AS LoanActivity,
LM.MFDB_LActSpecId AS LoanSubActivity,
LM.MFDB_FundingAgencyId AS FundAgencyId,
LM.MFDB_LSourceId AS SourceId
FROM dbo.MFDB_LOANAPPLICATION_MASTER LM
Left Join dbo.MFDB_SHG_MASTER SM
On SM.MFDB_ShgId = LM.MFDB_ShgId
WHERE LM.MFDB_ApprovalStatus= 3
AND LM.MFDB_DisburseDate IS NOT NULL
AND LM.MFDB_StateId = @p_TempStateId
AND LM.MFDB_RegionId = @p_TempRegionId
AND LM.MFDB_BranchId = @p_TempBranchId
AND LM.MFDB_SHGId = @p_TempSHGId
AND LM.MFDB_ShgMemberId = @p_TempSHGMemberId
AND LM.MFDB_LoanNumber = @p_TempLoanNumber
CTE_2 AS
SELECT StateId, RegionId, BranchId, SHGId, SHGName, SHGMemId, MemberName, VillageId , LoanNo, LoanAmount, DisbursedDate,DemandDate, CollectionDate, IntallNo,
ArrPrincipal,
ArrInterest,
CurrDemandPrin,
CurrDemandInt,
ISNULL(ISNULL(ArrPrincipal,0) + ISNULL(CurrDemandPrin,0),0) AS TotDemandPri,
ISNULL(ISNULL(ArrInterest,0) + ISNULL(CurrDemandInt,0),0) AS TotDemandInt,
AdvOBPri,
AdvOBInt,
CurrCollPri,
CurrCollInt,
AdvCBPri,
AdvCBInt,
BalancePri,
BalanceInt,
ISNULL(ISNULL(LoanAmount,0) - ISNULL(OS_Pri,0),0) AS OS_Pri,
ISNULL(ISNULL(@p_TotalInterest,0) - ISNULL(OS_Int,0),0) OS_Int,
CONVERT(INT,RepaymentPercentage) AS RepaymentPercentage,
LoanProduct,
LoanActivity,
LoanSubActivity,
FundAgencyId,
SourceId
FROM CTE_1
CTE_3 AS
SELECT StateId, RegionId, BranchId, SHGId, SHGName, SHGMemId, MemberName, VillageId , LoanNo, LoanAmount, DisbursedDate,DemandDate, CollectionDate, IntallNo,
ArrPrincipal,
ArrInterest,
CurrDemandPrin,
CurrDemandInt,
TotDemandPri,
TotDemandInt,
CASE WHEN AdvOBPri < 0 THEN AdvOBPri * -1 ELSE AdvOBPri END AS AdvOBPri,
CASE WHEN AdvOBInt < 0 THEN AdvOBInt * -1 ELSE AdvOBInt END AS AdvOBInt,
CurrCollPri,
CurrCollInt,
CASE WHEN ISNULL(TotDemandPri,0) = 0 THEN ISNULL((CurrCollPri + AdvOBPri),0)
WHEN ISNULL(TotDemandPri,0) > 0 AND ISNULL(CurrCollPri + AdvOBPri,0) > ISNULL(TotDemandPri,0) THEN ISNULL(ISNULL(CurrCollPri + AdvOBPri,0) - ISNULL(TotDemandPri,0),0)
WHEN ISNULL(TotDemandPri,0) > 0 AND ISNULL(CurrCollPri + AdvOBPri,0) < ISNULL(TotDemandPri,0) THEN 0
WHEN ISNULL(CurrCollPri + AdvOBPri,0) = ISNULL(TotDemandPri,0) THEN 0
ELSE 0 END AS AdvCBPri,
CASE WHEN ISNULL(TotDemandInt,0) = 0 THEN ISNULL((CurrCollInt + AdvOBInt),0)
WHEN ISNULL(TotDemandInt,0) > 0 AND ISNULL(CurrCollInt + AdvOBInt,0) > ISNULL(TotDemandInt,0) THEN ISNULL(ISNULL(CurrCollInt + AdvOBInt,0) - ISNULL(TotDemandInt,0),0)
WHEN ISNULL(TotDemandInt,0) > 0 AND ISNULL(CurrCollInt + AdvOBInt,0) < ISNULL(TotDemandInt,0) THEN 0
WHEN ISNULL(CurrCollInt + AdvOBInt,0) = ISNULL(TotDemandInt,0) THEN 0
ELSE 0 END AS AdvCBInt,
0 AS BalancePri,
0 AS BalanceInt,
OS_Pri,
OS_Int,
0 AS Repayment,
LoanProduct,
LoanActivity,
LoanSubActivity,
FundAgencyId,
SourceId,
CONVERT(CHAR(3), @i_DayOpenDate, 0) + ' - ' + RIGHT(CONVERT(CHAR(4), YEAR(@i_DayOpenDate)),2) AS Months
FROM CTE_2
INSERT INTO dbo.MFDB_NEW_DCB_REPORT
( [MFDB_TxnDate]
,[MFDB_StateId]
,[MFDB_RegionId]
,[MFDB_BranchId]
,[MFDB_SHGId]
,[MFDB_SHGName]
,[MFDB_ShgMemberId]
,[MFDB_MemberName]
,[MFDB_VillageId]
,[MFDB_LoanNumber]
,[MFDB_LoanAmount]
,[MFDB_DisbursedDate]
,[MFDB_DemandDate]
,[MFDB_CollectionDate]
,[MFDB_InstallmentNumber]
,[MFDB_ArrearPrincipal]
,[MFDB_ArrearInterest]
,[MFDB_DemandPrincipal]
,[MFDB_DemandInterest]
,[MFDB_TotDemandPrincipal]
,[MFDB_TotDemandInterest]
,[MFDB_AdvOBPrincipal]
,[MFDB_AdvOBInterest]
,[MFDB_CollectedPrincipal]
,[MFDB_CollectedInterest]
,[MFDB_AdvCBPrincipal]
,[MFDB_AdvCBInterest]
,[MFDB_BalancePrincipal]
,[MFDB_BalanceInterest]
,[MFDB_OS_Pri]
,[MFDB_OS_Int]
,[MFDB_RepaymentPercentage]
,[MFDB_LoanSchemeId]
,[MFDB_LActivityId]
,[MFDB_LActSpecId]
,[MFDB_FundAgencyId]
,[MFDB_LSourceId]
,[MFDB_Months]
,[MFDB_PanchayatId]
,[MFDB_PanchayatName]
,[MFDB_VillageName]
SELECT @i_DayOpenDate ,StateId, RegionId, BranchId, SHGId, SHGName, SHGMemId, MemberName, VillageId , LoanNo, LoanAmount, DisbursedDate,DemandDate,CollectionDate,IntallNo,
ArrPrincipal,ArrInterest,CurrDemandPrin,CurrDemandInt,TotDemandPri,TotDemandInt,AdvOBPri,AdvOBInt,CurrCollPri,CurrCollInt,
CASE WHEN AdvCBPri < 0 THEN 0 ELSE AdvCBPri END,
CASE WHEN AdvCBInt < 0 THEN 0 ELSE AdvCBInt END,
CASE WHEN ISNULL(TotDemandPri,0) = 0 THEN 0
WHEN ISNULL(TotDemandPri,0) > 0 AND ISNULL(CurrCollPri + AdvOBPri,0) > ISNULL(TotDemandPri,0) THEN 0
WHEN ISNULL(TotDemandPri,0) > 0 AND ISNULL(CurrCollPri + AdvOBPri,0) < ISNULL(TotDemandPri,0) THEN ABS(ISNULL(ISNULL(CurrCollPri + AdvOBPri,0) - ISNULL(TotDemandPri,0),0) * -1)
WHEN ISNULL(CurrCollPri + AdvOBPri,0) = ISNULL(TotDemandPri,0) THEN 0
ELSE 0 END AS BalancePri,
CASE WHEN ISNULL(TotDemandInt,0) = 0 THEN 0
WHEN ISNULL(TotDemandInt,0) > 0 AND ISNULL(CurrCollInt + AdvOBInt,0) > ISNULL(TotDemandInt,0) THEN 0
WHEN ISNULL(TotDemandInt,0) > 0 AND ISNULL(CurrCollInt + AdvOBInt,0) < ISNULL(TotDemandInt,0) THEN ABS(ISNULL(ISNULL(CurrCollInt + AdvOBInt,0) - ISNULL(TotDemandInt,0),0) * -1)
WHEN ISNULL(CurrCollInt + AdvOBInt,0) = ISNULL(TotDemandInt,0) THEN 0
ELSE 0 END AS BalanceInt,
OS_Pri,
OS_Int,
0 AS Repayment,
LoanProduct,LoanActivity,LoanSubActivity,FundAgencyId,SourceId,Months,@p_TempPanchayatId,@p_TempPanchayatName,@p_TempVillageName
FROM CTE_3
If @@Error != 0
Begin
Set @o_ErrorStatus = 1
ROLLBACK TRANSACTION DCBGenerated
RETURN
End
FETCH NEXT FROM DCBMonthCursor
INTO @p_TempStateId ,
@p_TempRegionId ,
@p_TempBranchId ,
@p_TempSHGId ,
@p_TempSHGName ,
@p_TempSHGMemberId ,
@p_TempMemberName ,
@p_TempLoanNumber ,
@p_TempLoanAmount
END
CLOSE DCBMonthCursor
DEALLOCATE DCBMonthCursor
DELETE FROM #TEMPDCBCOLLECTION
If @@Error != 0
Begin
Set @o_ErrorStatus = 1
ROLLBACK TRANSACTION DCBGenerated
RETURN
End
END
COMMIT TRANSACTION DCBGenerated -
Concat different rows column into single row field
Hi,
I have a table tblSite which has SiteID and SiteInvestigator. Can I concat different row base on siteID.
SiteID -- SiteInvestigator
1 -- x
1 -- y
2 -- z
2 -- x1
3 -- x2
Basicaly I want data look like this,
sitid --siteinvestigator
1 -- x,y
2 -- z,x1
3 -- x2
I want to use only sql query or create a view to get data like this. No stored procedure please.
Can somebody help?
Regards,
Vinaythis example might be of help.
SQL> select * from tab1;
COL1 COL2 COL3 COL4
Sofinummer 1 occupation 1
Sofinummer 1 occupation 2
Sofinummer 1 occupation 3
Sofinummer 2 occupation 1
SQL> select col1, col2,
2 substr(replace(max(substr(sys_connect_by_path (col3||' '||
3 col4, '-'),2)),'-',' '),1,60)
4 as col3
5 from tab1
6 start with col4 = 1
7 connect by col4 = prior col4 + 1
8 and prior col1 = col1
9 and prior col2 = col2
10 group by col1, col2;
COL1 COL2 COL3
Sofinummer 1 occupation 1 occupation 2 occupation 3
Sofinummer 2 occupation 1
SQL> -
Converting column data into rows in oracle 10g
sample data:
PATID NA2 NA3 NA4
1 3 4 5
1 34 45 56
1 134 245 356
2 134 245 356
2 334 275 56
2 4 275 56
2 4 5 56
how to display the above data like
PATID NA2 NA3 NA4
1 ID1 ID2 ID3 ID4 ID1 ID2 ID3 ID4 ID1 ID2 ID3 ID4
3 34 134 4 45 245 5 56 356
2 134 134 4 4 245 275 275 5 356 56 56 56Many examples are here:
http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php
(and you can do a search on this forum as well to find more)
edit
Your sample data is not very clear, by the way.
Please post a CREATE TABLE and some INSERT statements, just enough to put up the testcase.
Use the tag before and after posting examples, so formatting will be maintained.
See the [FAQ|http://forums.oracle.com/forums/help.jspa] for more information regarding tags (scroll a bit down)...
Edited by: hoek on Jan 26, 2010 5:23 PM -
Hello,
I am trying to build and SQL to convert columns from multiple rows to the all rows - see below test data and result expected:
CREATE TABLE XX_TEST(NAME VARCHAR2(10),A1 VARCHAR2(10),A2 VARCHAR2(10), A3 VARCHAR2(10),A4 VARCHAR2(10),A5 VARCHAR2(10));
INSERT INTO XX_TEST VALUES('LIST','A','B','C','D','E');
INSERT INTO XX_TEST VALUES('L1','1',NULL,'3',NULL,NULL);
INSERT INTO XX_TEST VALUES('L2','1','5','4',NULL,NULL);
COMMIT
SELECT * FROM XX_TEST;
Result expected:
NAME is Column from table XX_TEST but COLUMN and VALUE are the columns converted to rows-
NAME COLUMN VALUE
L1 A1 1
L1 A2 NULL
L1 A3 3
L1 A4 NULL
L1 A5 NULL
L2 A1 1
L2 A2 5
L2 A3 4
L2 A4 NULL
L2 A5 NULL
Thanks
BSHi,
Thanks for posting the sample data in such a useful form!
Whenever you post a question, you should always say what version of Oracle you're using, too.
Displaying multiple columns from one row as one column on multiple rows is called Unpivoting .
In Oracle 11, you can use the SELECT ... UNPIVOT feature to do that.
In any version of Oracle, you can cross-join your table to a Counter that has as many rows as your original table has columns to be unpivoted.
In this problem, we need a self-join of the unpivoted data, to join the rows with name='LIST' to every other row.
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 5
, unpivoted_xx_test AS
SELECT x.name
, 'A' || c.n AS col
, CASE c.n
WHEN 1 THEN x.a1
WHEN 2 THEN x.a2
WHEN 3 THEN x.a3
WHEN 4 THEN x.a4
WHEN 5 THEN x.a5
END AS val
FROM cntr c
CROSS JOIN xx_test x
SELECT u.name
, u.col
, l.val AS val1
, u.val AS val2
FROM unpivoted_xx_test l
JOIN unpivoted_xx_test u ON l.col = u.col
WHERE l.name = 'LIST'
AND u.name != 'LIST'
ORDER BY name
, col
;Output:
NAME COL VAL1 VAL2
L1 A1 A 1
L1 A2 B
L1 A3 C 3
L1 A4 D
L1 A5 E
L2 A1 A 1
L2 A2 B 5
L2 A3 C 4
L2 A4 D
L2 A5 EThe query above uses some features that were new in Oracle 9, but the basic strategy will work in earlier versions.
If your columns don't have such regular names (A1, A2, A3, ...) then you can use another CASE expression to derive unpivoted_xx_test.col. -
How to convert columns into rows using transpose function
Hi
anybody tell me how to convert columns values into rows using transpose function.Since BluShadow went to all the trouble to put it together, someone should use it.
See the post titled How do I convert rows to columns? here SQL and PL/SQL FAQ
John -
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
Maybe you are looking for
-
New Airport Express is not discoverable by Airport Utility
Hello, After searching the forums for a while and not finding any posts that really address the issue I'm having, I've finally decided to create my own post. I received a brand new Airport Express from my girlfriend for Christmas. I've finally gotten
-
I am using Outlook for Mac on my MacBook Pro. All was working well until I upgraded to OS X Mountain Lion (10.8.2). Since the upgrade, some (not all) of my incoming email messages are bounced back to the sender with the message: ....Status: 5.5.0..
-
Adding a new button in the PO search summary in SRM 3
Hello gurus, my requirement is to add a new button in the search summary for PO in SRM. Here's the task: every time a search is executed, the result is a list of purchase orders and every item has on the left two icons, "Show PO" and "Modify PO". The
-
Hi All, I face the following error message in the web browser when I try to run a simple page from JDeveloper: 500 Internal Server Error java.io.IOException The message is the same when I run the Hello World test from the Tutorials project. Here is t
-
Modification are not updating in the program
I had done some modification to the existing program and activated. When i came out from the program that modification are not appeared. Could you tell me the reason