Columns converted into rows.
Hello-
I have a table with 5 columns contains distinct rows.
Example:
COL_A COL_B COL_C COL_D COL_E
1051 25000 7/17/2009 7/20/2009 190409949A
1051 V4581 12/9/2009 12/11/2009 407829924A
1051 78659 7/21/2010 7/25/2010 403021811A
1051 72400 8/21/2009 8/31/2009 407381779M
1051 73300 2/6/2010 2/13/2010 403506467A
1051 5849 3/23/2010 3/30/2010 402442165A
1051 6827 9/16/2009 9/23/2009 400245360W
1051 4254 3/11/2009 3/19/2009 404824298A
1051 2768 10/16/2009 10/17/2009 401987971A
1051 53081 12/23/2009 12/27/2009 406889222Aso on
Now I want to convert this 10 rows into one column like :
1051 2009717250200972090409949 10512009129452009121190409949 1051201072178201072503021811 1051 200982120097002009 83107381779 ......so on
ie first 10 records will become one column and so on......
So for every 10 records I have to make it one column as above results. and there are two spaces after 1051(means first column and after last column col_E there are 6 spaces then second row will start.
Can you please help me out how can we do it.
Thanks in advance!
Thanks
Nick
Edited by: user12020842 on Apr 21, 2011 12:23 PM
Try this,
SELECT REPLACE ( MAX (SYS_CONNECT_BY_PATH ( str, '@#')), '@#')
FROM (SELECT col_a || ' ' || col_b || col_c || col_D || col_e || ' ' str,
CEIL (ROWNUM / 10) lvl,
ROW_NUMBER () OVER (PARTITION BY CEIL (ROWNUM / 10) ORDER BY col_a) rn
FROM Table_name)
START WITH rn = 1
CONNECT BY PRIOR rn + 1 = rn
AND PRIOR lvl = lvl
GROUP BY lvlG.
Similar Messages
-
Converting single column content into rows
Hi All,
I have a table containing data in the following format
SNO Content
1 a,ab,aab,b,c
2 a,aac,aab,c,ccb,ee
3 bb,b,c
I have a requirement to convert this into following format
SNO Content
1 a
1 ab
1 aab
1 b
1 c
2 a
3 bb
3 b
3 c
How to acheive this in 10g? Is it possible in 9i?
Please share your thoughts.
Thanks in advance
Regards,
Subbu Stest@ORA92>
test@ORA92> @ver
BANNER
Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
PL/SQL Release 9.2.0.1.0 - Production
CORE 9.2.0.1.0 Production
TNS for 32-bit Windows: Version 9.2.0.1.0 - Production
NLSRTL Version 9.2.0.1.0 - Production
5 rows selected.
test@ORA92>
test@ORA92> select * from t;
SNO CONTENT
1 a,ab,aab,b,c
2 a,aac,aab,c,ccb,ee
3 bb,b,c
3 rows selected.
test@ORA92>
test@ORA92> select sno,
2 rtrim(substr(lst,
3 instr(lst, ',', 1, iter.pos) + 1,
4 instr(lst, ',', 1, iter.pos + 1) - instr(lst, ',', 1, iter.pos)),
5 ',') content
6 from (select sno, ',' || content || ',' lst from t) csv,
7 (select rownum pos from all_objects) iter
8 where iter.pos <= ((length(csv.lst) - length(replace(csv.lst, ','))) / length(',')) - 1
9 order by sno;
SNO CONTENT
1 a
1 ab
1 aab
1 c
1 b
2 a
2 aab
2 ee
2 ccb
2 c
2 aac
3 bb
3 c
3 b
14 rows selected.
test@ORA92>
test@ORA92>Haven't tried this with a large number of rows, but I suspect it would be slow in that case. Regular expressions should make this simpler and more efficient in 10g.
Cheers,
pratz -
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 -
Comma separated column values into row values
Hi all,
i am selecting the data from TABLE A ( id number ,rights varchar2 ). result set as
ID RIGHTS
1 M,P,Y,N,C,P
4 N,E,A
10 N,C,R,P
but i want the output as
ID RIGHTS
1 M
1 P
1 Y
1 N
1 C
1 P
4 N
4 E
4 A
10 N
10 C
10 R
10 P
kindly share your idea's to get the desired results.
thanks in advance
Edited by: 887268 on Nov 30, 2012 11:12 PMTry this
SQL> WITH a(id, rights) AS
2 (
3 SELECT 1, 'M,P,Y,N,C,P' FROM dual UNION ALL
4 SELECT 4, 'N,E,A' FROM dual UNION ALL
5 SELECT 10, 'N,C,R,P' FROM dual)
6 SELECT id, regexp_substr(rights, '[^,]+',1,level) res
7 FROM a
8 CONNECT BY level - 1 <= regexp_count(rights,',') /* regexp_count:- 11g */
9 AND prior id = id
10 AND prior sys_guid() IS NOT NULL /* sys_guid:- 11g */
11 ORDER BY id
12 /
ID RES
1 M
1 P
1 Y
1 N
1 C
1 P
4 N
4 A
4 E
10 N
10 R
ID RES
10 C
10 P
13 rows selected.Thanks!
Edited by: Ashu_Neo on Dec 1, 2012 9:43 PM
-- Added comment -
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 -
Convert single column into rows
hi Gurus,
I have one table test colums are id and name.
id number
name varchar2
data is like
id name
1 xy
2 xyy
3 mm
4 pp
Now my requirement is to convert single column id into rows
i,e my output should be of singel rows like :- 1,2,3,4
How to achive this result .
I dont have any idea to do this query.
Please help guys.
Thanks in advance.
VijayWell,
As long as your code doesn't have to run in production, simplest way is:
WM_CONCAT (but it's not documented)
or use XMLAGG, it's simpler than a connect by:
MHO%xe> with t as (
2 select 1 col, 'xy' str from dual union all
3 select 2, 'xyy' from dual union all
4 select 3, 'mm'from dual union all
5 select 4, 'pp' from dual union all
6 select 8, 'pp' from dual union all
7 select 12, 'pp' from dual union all
8 select 40, 'pp' from dual
9 )-- actual query, based on id's generated above:
10 select rtrim(xmlagg(xmlelement(e,col||',')).extract('//text()'),',') col
11 from t;
COL
1,2,3,4,8,12,40
1 rij is geselecteerd. -
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 -
How to insert column values into database as rows
Hi,
I have 8 columns and some not null columns. Based on not null columns I want to insert into table as rows. The 8 columns may contain values or no value. If the first column contains data, then I have to insert into one row. if the second column contains data I have to insert a row and in second column. respectively...So How can I insert column values into rows. Can I write 8 insert statements. (OR) is it possible to insert data from columns using where clause.
Please help me out....
Thanks in AdvanceLines Table:
line_id, orcl_bank_account_num, product_type, service_type, lease_type,
funding_type, cpi, billing_frequency_unit_cd , annual_due_date ,
pricing_start_date, pricing_end_date, install_date, contract_end_date ,
prdct_replacement_cost_amt, cradle_replacement_amt, supranet_contract,
issuance_fee, board_inactive_date, header_id, creation_date, last_modified_date,
created_by_nam, modified_by_nam, activeinactive_flg, prdct_bill_amt_yr1,
prdct_bill_amt_yr2, prdct_bill_amt_yr3, prdct_bill_amt_yr4, prdct_bill_amt_yr5,
prdct_bill_amt_yr6, prdct_bill_amt_yr7, prdct_bill_amt_yr8, activation_fee_yr1,
activation_fee_yr2, activation_fee_yr3, activation_fee_yr4, activation_fee_yr5,
activation_fee_yr6, activation_fee_yr7, activation_fee_yr8,
In this table the columns structure is :
-- PRDCT_BILL_AMT_YR (1 to 8) NUMBER(14,4)
-- ACTIVATION_FEE_YR (1 to 8) NUMBER(8,2)
I have one more table:
PRDCT_INS_AMT NUMBER(14,4)
ACTIVATION_FEE_AMT NUMBER(14,4)
I want to insert prdct_bill_amt_yr (1 to 8) columns data into PRDCT_INS_AMT column. similarly activation_fee (1 to 8) columns data.
But the data should be inserted based product_type, service_type, lease_type columns values. (These 3 columns may contain upto 45 combinations). -
plz help me to print column values into row values.
for eg.
1
2
output will be 1,2Check this out.. might be useful
recursive select? -
Hi All,
I need help in building view which actually can show columns data as row.
e.g.
row is as follows
Name Age Salary
ABC 25 10000
BBC 28 12000
The above tables data I want to get as
Name ABC BBC
Age 25 28
Salary 10000 12000
Thanks in advance.Even if I don't really understand such requirement, I wrote some times ago such function to play around that :
Re: Converting Columns into rows
Nicolas. -
How to convert into columns rows
Hi all,
How to convert columns into rows.
thank you
regards
P PrakashWhats your DB Version. If its 11g and above you can check out PIVOT and UNPIVOT clause of SELECT statement. Its a new feature of 11g.
For previous version just search this forum you will get lots of examples. -
Dear all....I need to convert all columns into rows in a table. For example table has following columns:
Emp_Cod........Val1......Val2......Val3
1 a b c
Now I wish that each column should display as a value like:
Emp_Cod........Val1
1 a
1 b
1 c
Now the one way to solve this job is to write a union statement for each column but for this I'll have to write equal number of select statements as there are columns.
What I need that is there anyway to write minimum code for this job, is there any alternate way???SQL> with t as(select 1 emp_code, 'a' val1, 'b' val2, 'c' val3 from dual)
2 select*from t unpivot(v for c in(val1,val2,val3));
EMP_CODE C V
1 VAL1 a
1 VAL2 b
1 VAL3 c
SQL> col COLUMN_VALUE for a20
SQL> with t as(select 1 emp_code, 'a' val1, 'b' val2, 'c' val3 from dual)
2 select*from t,table(sys.odcivarchar2list(val1,val2,val3));
EMP_CODE V V V COLUMN_VALUE
1 a b c a
1 a b c b
1 a b c c -
How to convert a row into a column with the row headers displayed as column in javaFx?
How do in convert a row of data into column of data to display as shown below:
Column1|Column2|Column3|Column4
C1 | C2 | C3 | C4
C5 | C6 | C7 | C8
How to convert the above default behavior to as below
Column1| C1 | C5
Column2| C2 | C6
Column3| C3 | C7
Column4| C4 | C8.
-
Convert 1 row, multiple (identical) columns into 1 column, multiple rows
I have 10g, so unpivot is unavailable to me. I have a query that looks like:
with data as (
select 1,2,3 from dual
) select * from data
which returns 1,2,3. I need it to return:
1
2
3 and be able to name the output column. The results of this will be used in a sub-select in an IN clause. This is a trivial example. In reality the list of numbers will be varied and large - too many to either use bind variables or to put in the IN clause as a list. Is there a simple way I'm overlooking?Hi,
One way to unpivot is to join to a table that has as many rows as you need. Instead of an actual table, you can always generate a Counter Table (actually a result set), with exactly the right number of rows. Use CASE (or something similar) go get a different column for each row of the counter table.
For example, to get the 3 columns of the scott.dept table onto 3 separate rows:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 3
SELECT CASE c.n
WHEN 1 THEN TO_CHAR (d.deptno)
WHEN 2 THEN d.dname
WHEN 3 THEN d.loc
END AS data
FROM scott.dept d
CROSS JOIN cntr c
;Output:
DATA
10
20
30
40
ACCOUNTING
RESEARCH
SALES
OPERATIONS
NEW YORK
DALLAS
CHICAGO
BOSTON -
How to display the rows in to columns and columns into rows?
DES:- I know by using pivot and unpivot you can convert rows into columns and columns into rows but i don't know how to write the syntax?
Hi,
Besides the places Martin mentioned above, there are many examples in the forum FAQ:
https://community.oracle.com/message/9362005#9362005
For an example of both PIVOT and UNPIVOT in the same query, see
http://forums.oracle.com/forums/thread.jspa?threadID=920854&tstart=0
Maybe you are looking for
-
Adobe says photoshop elements serial is invalid
Bought adobe photoshop & premier element bundle from best buy and adobe says serial is invalid. Tried the retreiver and got another invalid from adobe. Took it back and exchanged for another and same thing happened. Really upset with adobe. The want
-
HT1923 My daughter disabled her ipod because she forgot her password. How do I fix it?
My daughter disabled her ipod because she forgot her password. How do I fix it?
-
Solaris process memory usage increase but not forever
On Solaris 10 I have a multithreaded process with a strange behaviour. It manages complicated C++ structures (RWTVal or RWPtr). These structures are built from data stored in a database (using Pro*C). Each hour the process looks for new informacion i
-
Hi all, I have done one UDF, for one of my interface , i need to use this for another interface , , i am not able to find the UDF in another namespace for my new interface please help me thanking you Sridhar
-
HT4859 Simple backup to iCloud
Can anyone point me in the right direction? I simply want iCloud to backup a single directory on my Mac, and that's it. I don't want automated backups of my entire machine ... just a single directory, simple. Can anyone tell me how that is to be done