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
Similar Messages
-
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 -
How to get row number in the fetch row itself in Sql Query ?
Hi,
i am fetching some rows from a sql query . Is there any way to get row number as well in each row while all rows are fetched ?
like this :
RowNum data1 data2
1 abc ere
2 bnh ioiHello
Ofcourse you can get the rownum inside a query, just keep in mind that the rownum is the number of order in which the records were fetched from the table, so if you do an order by, the rownum will not be sequential, unless you query the information in a subquery first.
SELECT rown, col1, col1
FROM table
Or
SELECT rownum, col1, col2
FROM (SELECT col_1, col_2 FROM table ORDER BY col1)
Regards
Johan -
In JDBC Sender Adapter , the server is Microsoft SQL .I need to pass current date as the input column while Executing stored procedure, which will get me 10 Output Columns. Kindly suggest me the SQL Query String , for executing the Stored Procedure with Current date as the input .
Hi Srinath,
The below blog might be useful
http://scn.sap.com/community/pi-and-soa-middleware/blog/2013/03/06/executing-stored-procedure-from-sender-adapter-in-sap-pi-71
PI/XI: Sender JDBC adapter for Oracle stored procedures in 5 days
regards,
Harish -
Converting column header and a single record into rows
Lets suppose the table with only one record
col1 col2 col3-----Column Header
d1 d2 d3
i want it in following manner
column_name Column_data----Column Header
col1 d1
col2 d2
col3 d3
for this i have written the query as
select column_name,column_data from (select * from XXX where col1=123)
MODEL RETURN UPDATED ROWS
dimension by(col1)
measures (col1,0 column_name,0 column_data )
ignore nav
rules ITERATE(167)
column_name[iteration_number]=cast( iteration_number as varchar2(3)),
column_data[iteration_number]=cast( iteration_number as int)
here i am just assigning iteration_number but i want to assign name of the column and its value;
Note:--table contains nearly 200 columnsSomething like (using one of my own tables):
select column_name colname,
decode(column_name,'RUN_NO',to_char(run_no),
'SEARCH_ID',to_char(search_id),
'PROCESSING_START_DATE',to_char(processing_start_date,'DDMMYYYYHH24MISS'),
'PROCESSING_END_DATE',to_char(processing_end_date,'DDMMYYYYHH24MISS'),
'NO_OF_MATCHES',to_char(no_of_matches),
'ERROR_DETAILS',error_details,
'ADVREP_TIMESTAMP',to_char(advrep_timestamp,'DDMMYYYYHH24MISS')
) colvalue
from partial_search_history, (select rownum r, column_name
from user_tab_columns
where table_name = 'PARTIAL_SEARCH_HISTORY')
where run_no = 356You can build the decode by doing
SELECT ''''||COLUMN_NAME||''',TO_CHAR('||COLUMN_NAME||'),'
FROM user_tab_columns
where table_name = 'PARTIAL_SEARCH_HISTORY'A decode on DATA_TYPE field would allow you to add a format mask for DATE fields if necessary. -
How to merge three columns values to single row values in sql server 2008
Hi Frds.....
I have three quantity in my table.
Quantity1,quantity2,quantity3
this three quantity have different values
ex:
quantity1 = 1000,quantity2=2000,quantity3=3000
the three column combine 2 display in single row values. this values display in one by one.
ex: quantity
1000
2000
3000You will need to use the UNPIVOT operator:
DECLARE @example TABLE
Id int NOT NULL IDENTITY(1,1),
Quantity1 int,
Quantity2 int,
Quantity3 int
INSERT INTO @example VALUES (1000, 2000, 3000), (4000, 5000, 6000);
SELECT * FROM @example;
SELECT Id, Quantity, QuantityType
FROM @example
UNPIVOT
Quantity FOR QuantityType IN (Quantity1, Quantity2, Quantity3)
) AS u;
Output:
(2 row(s) affected)
Id Quantity1 Quantity2 Quantity3
1 1000 2000 3000
2 4000 5000 6000
(2 row(s) affected)
Id Quantity QuantityType
1 1000 Quantity1
1 2000 Quantity2
1 3000 Quantity3
2 4000 Quantity1
2 5000 Quantity2
2 6000 Quantity3
(6 row(s) affected) -
Populate columns with row values in Sql Server Reporting Services
I have got a dataset with 2 columns named Row and Title. There are 8 rows in this dataset and I want to display those 8 titles within columns in a table. So I create a table with 8 columns and set each column's expression to
=LookUp(Fields!Row.Value,1,Fields!Title.Value,"Titles")
=LookUp(Fields!Row.Value,2,Fields!Title.Value,"Titles")
=LookUp(Fields!Row.Value,3,Fields!Title.Value,"Titles")
However only the first column displays a title. The other 7 display nothing. Is my expression wrong?Hi,
Is the row datatype non-numeric? Maybe the lookup is failing for that reason. You coulr try adding a conversion to the field:
=LookUp(cint(Fields!Row.Value),1,Fields!Title.Value,"Titles")
Could you, perhaps, instead use a tablix table and put the Title column on the row group? That would pivot the data the way that you want it.
Mark -
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 -
Column sort gives "failed to parse SQL query"
Hi folks,
I have a page with a chart and 5 reports. All report columns are sortable.
When I first navigate to the page from a menu, the chart and all reports display correctly. Given certain query criteria, reports 2 and 4 correctly return no data, and show my polite 'no data found' message.
If I sort report 1 or 3 or 5 by clicking a column heading, the chart and reports 1, 3 and 5 display correctly, but in report regions 2 and 4 I then get
failed to parse SQL query:
ORA-06502: PL/SQL: numeric or value error: NULL index table key valueThese errors can be cleared by navigating to the page again from a menu.
What's wrong? How can I fix it?
I'm at 1.6.0.00.87, db 10.1.0.3.0.
All ideas gratefully received.
Thanks
John DOK, I now have a workspace.
I recreated the problem using page 1 of the Sample Application (app 15027).
1 - Copy region "My Top Orders" to "My Top Orders 2" on page 1 (same display point, column 2).
2 - Edit the SQL of "My Top Orders 2" to remove the ORDER BY clause.
3 - Edit the columns of "My Top Orders 2" to make all columns other than ORDER_ID sortable.
4 - Copy region "My Top Orders 2" to "My Top Orders 3" on page 1 (same display point, column 2)..
5 - Run page 1
6 - Click a column heading on "My Top Orders 2" and I got "failed to parse..." on "My Top Orders 3".
BUT it's intermittent. Navigate away from page 1 then return, and you may find the error doesn't recur.
So, what's up Doc?
Many thanks,
John -
Retrieving a single value from sql:query
Hi all, pretty new to jsp and tag libraries so appoligies if this isn't the correct forum or my question is a bit lame...
I've got a basic input form written in JSP and i'm trying to use AJAX to return a customers name when they input there customer IS. My AJAX interaction works ok and so far i have got it to return a string value to indicate if the customer ID is valid or invalid.
However, now i want to return the customers name based on a SELECT SQL statement. My sql works ok but i can't figure out how to return / access the single cname field that is returned. ALL the <sql:query> examples that i have seen use a <c:forEach> loop but this doesn't seem to work for me.
My AJAX / Jsp code looks like this:
<%@ page contentType="text/html" info="Customer Id Lookup" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<sql:setDataSource scope="application"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/dbawp"
user="*******"
password="********"
/>
<sql:query var="qryValidCustomer" >
SELECT cname FROM camper WHERE cid = ?;
<sql:param value="${param.data}"/>
</sql:query>
<c:choose>
<c:when test="${qryValidCustomer.rowCount == 0}">
unknown camper
</c:when>
<c:otherwise>
<c:forEach items="${qryValidCustomer.row}" var="row">
${fn:escapeXml(row.cname)} <- Doesn't seem to work - i get nothing returned
</c:forEach>
<%--good camper--%>
</c:otherwise>
</c:choose>Can anyone point me in the right direction?
Manythanks in advance,
Sam> my db fields contain a list of sections, " 1,2,3,4,"
When you find yourself storing delimited lists in a field,
that's a sign that you should
normalize
your data. Storing lists is difficult to query, error prone and bad
for performance.
A separate table should be used to represent a many-to-many
relationship.
Table - Columns
Advertisement - AdvertisementID, AdvertisementDescription,
...other columns
Type - TypeID, TypeTitle, ...
AdvertisementType - AdvertisementID, TypeID
Instead of storing the related types as a list
AdvertisementID | TypeID
1 | 1,2,3,4
Store them as separate records in the 3rd table
AdvertisementID, TypeID
1, 1 (ad # 1, type # 1)
1, 2 (ad # 1, type # 2)
1, 3 (ad # 1, type # 3)
1, 4 (ad # 1, type # 4) -
Setting a Default Value in SQL Query Report
Hello:
We are using a SQL Query Report to provide a mass update to a table. We are using the apex.collection and having it display a number of records in a SQL Query Report for mass update. We have 14 columns in the report, for which the first 11 are populated via the collection. The remaining 3 are open for input but the individual making the updates. We've were able to provide a default value for 2 of the remaining 3 columns using a named LOV's - however the fourth column we would like to default a sysdate - but we are not successful.
We've attempted many things but none seem to work, including adding that column to the collection and assigning it a default sysdate value. We've also tried changing the settings in the report attributes --> Tabular Form Elements by setting the Display as to: Datepicker, Default type to PL/SQL Expression and setting the default to sysdate. We've also tried caputuring a date on the previous page and loading it onto the report page and trying to default the date column to a page item default.
I'd appreciate any help.
Thanks
FYI - we are using version 3.2use as default
to_char(sysdate, 'dd/mm/yyy') where the format is your application or item date format -
Get a insert session value for SQL query at report
Hi friends
I created a global temp table and procedure to support web search form.
and a search result report. The procudure
gets search result from multip tables and
insert into temp table --recordsearch. I can get value from temp table by call procedure
at SQL*Plus.
However, I can not get this value by web report.
How can I get this insert session value and pass to SQL query for report?
Thanks,
Newweb
CREATE GLOBAL TEMPORARY TABLE recordsearch
(emp_id VARCHAR2(200),
ssn VARCHAR2(9),
fname VARCHAR2(200),
lname VARCHAR2(200),
m_name VARCHAR2(200)
) ON COMMIT PRESERVE ROWS;it possible that your web form does not have a persistent, dedicated connection. if you have connection pooling for example, multiple sessions will see the same instance of the GTT, so if one deletes it, then nobody sees it (or you can see others data). if the connections are not persistent, then they can disconnect between calls, deleting the GTT table.
-
How to capture a parameter value in SQL QUERY of scale marker using GO URL
Hi,
Can any one please tell me how to capture the parameter value from go url inside Where clause of Scale Marker.
I am trying to sift the position of scale marker based on SQL Query.
Thanks-Bhaskar Gouda.
Edited by: 961171 on Sep 25, 2012 12:33 AMSince this is a synchronous interface, where source is a soap(proxy) call and target is JDBC in the first mapping both of them are request scenarios.
Source Structure:
RootNode
Request 1...unbounded
No_of_Days 1.1 String
Target Structure:
RootNode
Statement
TableName
Action mapped to SQL_QUERY
Access - SELECT DISTINCT AL.EC_NO,DP.DATE_TO_FORMAL FROM T_APPLICATION_LIST AL,(SELECT DE.EC_NO AS "EC_NO", DE.PACKAGE_NO AS "PACKAGE_NO",PC.DATE_TO_FORMAL AS "DATE_TO_FORMAL" FROM DAICYO_ECNO DE,PACKAGECTL PC WHERE DE.PACKAGE_NO = PC.PACKAGE_NO AND PC.DATE_TO_FORMAL > (TRUNC(SYSDATE) - to_number('$No_Of_DAYS$'))) DP WHERE AL.EC_NO IN (SELECT EC_NO FROM DAICYO_ECNO WHERE PACKAGE_NO IN (SELECT PACKAGE_NO FROM PACKAGECTL WHERE DATE_TO_FORMAL > (TRUNC(SYSDATE) - to_number('$No_Of_DAYS$')))) AND (AL.FAMILY = '' or '' is null and AL.FAMILY is not null ) and DP.EC_NO = AL.EC_NO ORDER BY DATE_TO_FORMAL
Key
No_Of_Days 1..1 String
In Return I am expecting a JDBC response from the Oracle Database as:
Source Structure:
RootNode
STATEMENT_response 1...unbounded
row 0...undbounded
EC_NO 1..1 String
Target Structure:
RootNode
RESPONSE
row
EC_NO 1..1 String -
How-To populate SELECT LIST default value from SQL Query
OK, I've done my homework, and did not find my answer in the Forum, so here it is.
I have a Page that displays fields from a SQL Query. The Page also has below that radiogroups, checkboxes, and Select Lists to allow the user to change values in the fields that are displayed at the top of the Region. I am able to pre-populate the radiogroups and checkboxes by performing a SELECT in the 'Source value or expression' of the Source area of the Page Item.
However, I am unable to do the same for the Select List fields. Their default value ends up being the first value in the LOV ('-- None --' for NULL values). Source Used: Only when current value in session state is null. Source Type: SQL Query. Also, when I first go into the Page when running it, and check the Session State values, they are all null, so I don't understand why the field is not populated by the SQL Query statement as the radiogroups and checkboxes are.
Any suggestions??? I've tried all sorts of combinations.Thank you for the suggestion.
I had considered that, but cannot do so, because we are using Oracle Application Express more for development than for data containment.
We have all our data in an Oracle 10.g DB which is separate from the OAEX server, and the OAEX app will reference that data via VIEWs and DB LINKs. We are doing so primarily as an added data security layer, where the data is housed separate from the app, in case the Web site is ever compromised. The data that can be viewed is restricted to a subset of the actual data that is available.
Since the data is on a separate server, (I assume) we will not be able to set the source type to Database column, because (I asume) that is dependent upon the data being housed by the OAEX server.
That still leaves me with having to populate the field with a SELECT statement from the Oracle DB. This unfortuneately is almost a show-stopper for me because of down-stream processing data requirements. Have not been able to come up with contingencies yet. -
Hi
I'm trying to make a default value using Computations;
when I create computation,
I select
Computation item: PXX_TEST
computation type: SQL query
computation Point: I tried all
Computation: select X FROM Y WHERE X= '1'
This is the way to get Default Value ??
I need to display the value when the page is open
Thanks
MMMM,
You can use the default value attribute of the item to set a value if the item's source produces a null. A computation (before/after header) can work too, but it's possible that the item source and Source Used attribute (Always/Only) are interfering with your computation so you might want to try setting Source Used to 'Only...' if you have a computation setting the item.
Scott
Maybe you are looking for
-
Hello All. I am trying to install Oracle Application Server 10g release 2(1.2.0.2) on Red Hat Enterprise Linux ES Release 4 64 bits. But got the following errors. End output from spawned process. Exception thrown from action: make Exception Name: Mak
-
Windows vista does not recognize my mini
So I got a windows vista computer and tried to connect my mini but it comes up saying USB device not recognized. ANY help is appreciated
-
Solution for Power Saver error during install
Hello, i have read several topics about the powersaver error during install of this utilty. i had the same problem even with a clean install of windows XP and i have found the solution. The error seems to disapear and Powersaver works fine after inst
-
LMS 3.2 DFM Event Summary problem
Hi all, I have two issues, and one question: Issues: 1) In the DFM portlet, under DFM Evnets Summary I see a list stating that I have 2 devices that have Event Name as Duplicate. When I click on the number 2, it opens up Device Fault Manager - Alerts
-
Hi all, Can anyone pls tell me how to access a servlet method from a simple java file. I want to access a parameter from a servlet from a simple java file which is run by a process in linux and which is not a part of my web application. I tried to do