anyone please help me i have procedure in oracle when the procedure is run table Rekon its get data from another table to create data, but the problem is when the data is null(empty) in another table in Table Rekon it's null but i want still have data in my Rekon table, example for template:
NO_ACC|ttrx|Amount
1111 |USD |2200
|SGD |5410
|total|7610
but when the data in another table is null in where clause substr(NARATION,1,3)='SGD'
the table its like:
NO_ACC|ttrx|Amount
1111 |USD |2200
|total|2200
it's my query:
insert into datamart.rekon_801_ugm_sm
select to_char(no_acc) no_acc, 'USD' ttrx, sum(amount) amount from (select
* from rekon_801_ugm where no_acc = 1111 and substr(narasi,9,4)='1112'
or substr(narasi,37,4)='1112') group by no_acc
union all
select to_char(no_acc) no_acc, 'SGD' as ttrx, sum(amount) amount from (select
* from rekon_801_ugm where no_acc = 8010000487 and substr(narasi,9,2)='88'
or substr(narasi,37,2)='88') group by no_acc
union all
select ' ' as no_acc, 'TOTAL TRANSAKSI H2H' as ttrx, sum(amount) amount from (select
* from rekon_801_ugm where no_acc = 1111 and debet_kredit = 'K') group by no_acc
You are using acc_no with a constant condition in all sub queries
if you want to have null values you must replace this condition with something like
(acc_no=8010000487 or acc_no is null)
Hi i'm trying to show the name of all Customers who have only one account with a balance of more than $1000 in any branch of the bank and one or more loan with an amount of more than $4000 in any branch in Edina
select account.cname,
from Account
where account.bal > 1000
UNION
(select loan.cname
from loan
where loan.amt>4000 AND
l.bname )in (
select bname
from branch
where lower(bcity)= 'edina'
);
does it look right?
You have extra comma in first select.
your in clause doe not have a column to compare with
Maybe something like:
select account.cname
from Account
where account.bal > 1000
UNION
select loan.cname
from loan
where loan.amt>4000 AND l.bname
and loan.bname in ( select bname from branch where lower(bcity)= 'edina' )
I've been trying for days to solve this problem to no solution.
I want to get the lowest running balance in a group.
Here is a sample data
The running balance is imaginary and is not part of the table.
the running balance is also computed dynamically.
the problem is I want to get the lowest running balance in a Specific month (January)
so the output should be 150 for memberid 10001 and 175 for memberid 10002 as highlighted in the image.
my desired out put should be
memberid | balance
10001 | 150
10002 | 175
Is that possible using sql query only?
PS. Using c# to compute lowest running balance is very slow since I have more than 600,000 records in my table.
I've updated the question.
The answer provided by Mihir Shah gave me the idea how solve my problem.
His answer takes to much time to process making it as slow as my computation on my c# program because his code loops on every record.
Here is my answer to get the minimum lowest value in a specific group (specific month) with a running value or running total without sacrificing a lot of performance.
with IniMonth1 as
(
select a.memberid, a.iniDeposit, a.iniWithdrawal,
(cast(a.iniDeposit as decimal(10,2)) - cast(a.iniWithdrawal as decimal(10,2))) as RunningTotal
from
(
select b.memberid, sum(b.depositamt) as iniDeposit, sum(b.withdrawalamt) as iniWithdrawal
from savings b
where trdate < '01/01/2016'
group by b.memberid
) a /*gets all the memberid, sum of deposit amount and withdrawal amt from the beginning of the savings before the specific month */
where cast(a.iniDeposit as decimal(10,2)) - cast(a.iniWithdrawal as decimal(10,2)) > 0 /*filters zero savings */
)
,DetailMonth1 as
(
select a.memberid, a.depositamt,a.withdrawalamt,
(cast(a.depositamt as decimal(10,2)) - cast(a.withdrawalamt as decimal(10,2))) as totalBal,
Row_Number() Over(Partition By a.memberid Order By a.trdate Asc) RowID
from savings a
where
a.trdate >= '01/01/2016'
and
a.trdate <= '01/31/2016'
and (a.depositamt<>0 or a.withdrawalamt<>0)
) /* gets all record within the specific month and gives a no of row as an id for the running value in the next procedure*/
,ComputedDetailMonth1 as
(
select a.memberid, min(a.runningbalance) as MinRunningBal
from
(
select a.rowid, a.memberid, a.totalbal,
(
sum(b.totalbal) +
(case
when c.runningtotal is null then 0
else c.runningtotal
end)
)as runningbalance , c.runningtotal as oldbalance
from DetailMonth1 a
inner join DetailMonth1 b
on b.rowid<=a.rowid
and a.memberid=b.memberid
left join IniMonth1 c
on a.memberid=c.memberid
group by a.rowid,a.memberid,a.totalbal,c.runningtotal
) a
group by a.memberid
) /* the loop is only for the records of the specific month only making it much faster */
/* this gets the running balance of specific month ONLY and ADD the sum total of IniMonth1 using join to get the running balance from the beginning of savings to the specific month */
/* I then get the minimum of the output using the min function*/
, OldBalanceWithNoNewSavingsMonth1 as
(
select a.memberid,a.RunningTotal
from
IniMonth1 a
left join
DetailMonth1 b
on a.memberid = b.memberid
where b.totalbal is null
)/*this gets all the savings that is not zero and has no transaction in the specific month making and this will become the default value as the lowest value if the member has no transaction in the specific month. */
,finalComputedMonth1 as
(
select a.memberid,a.runningTotal as MinRunTotal from OldBalanceWithNoNewSavingsMonth1 a
union
select b.memberid,b.MinRunningBal from ComputedDetailMonth1 b
)/*the record with minimum running total with clients that has a transaction in the specific month Unions with the members with no current transaction in the specific month*/
select * from finalComputedMonth1 order by memberid /* display the final output */
I have more than 600k savings record on my savings table
Surprisingly the performance of this code is very efficient.
It takes almost 2hr using my c# program to manually compute every record of all the members.
This code makes only 2 secs and at most 9 secs just to compute everything.
i Just display to c# for another 2secs.
The output of this code was tested and compared with my computation using my c# program.
May be below one is help you
Set Nocount On;
Declare #CashFlow Table
(
savingsid Varchar(50)
,memberid Int
,trdate Date
,deposit Decimal(18,2)
,withdrawal Decimal(18,2)
)
Insert Into #CashFlow(savingsid,memberid,trdate,deposit,withdrawal) Values
('10001-0002',10001,'01/01/2015',1000,0)
,('10001-0003',10001,'01/07/2015',25,0)
,('10001-0004',10001,'01/13/2015',25,0)
,('10001-0005',10001,'01/19/2015',0,900)
,('10001-0006',10001,'01/25/2015',25,0)
,('10001-0007',10001,'01/31/2015',25,0)
,('10001-0008',10001,'02/06/2015',25,0)
,('10001-0009',10001,'02/12/2015',25,0)
,('10001-0010',10001,'02/18/2015',0,200)
,('10002-0001',10002,'01/01/2015',500,0)
,('10002-0002',10002,'01/07/2015',25,0)
,('10002-0003',10002,'01/13/2015',0,200)
,('10002-0004',10002,'01/19/2015',25,0)
,('10002-0005',10002,'01/25/2015',25,0)
,('10002-0006',10002,'01/31/2015',0,200)
,('10002-0007',10002,'02/06/2015',25,0)
,('10002-0008',10002,'02/12/2015',25,0)
,('10002-0009',10002,'02/12/2015',0,200)
;With TrialBalance As
(
Select Row_Number() Over(Partition By cf.memberid Order By cf.trdate Asc) RowNum
,cf.memberid
,cf.deposit
,cf.withdrawal
,cf.trdate
From #CashFlow As cf
)
,RunningBalance As
(
Select tb.RowNum
,tb.memberid
,tb.deposit
,tb.withdrawal
,tb.trdate
From TrialBalance As tb
Where tb.RowNum = 1
Union All
Select tb.RowNum
,rb.memberid
,Cast((rb.deposit + tb.deposit - tb.withdrawal) As Decimal(18,2))
,rb.withdrawal
,tb.trdate
From TrialBalance As tb
Join RunningBalance As rb On tb.RowNum = (rb.Rownum + 1) And tb.memberid = rb.memberid
)
Select rb.memberid
,Min(rb.deposit) As runningBalance
From RunningBalance As rb
Where Year(rb.trdate) = 2015
And Month(rb.trdate) = 1
Group By rb.memberid
I have two tables called Recovery and Installments.
Recovery ([pk] RID, emp_id, amount, duration, type]
Installments ([pk] ID, [fk] recovery_id, recovered_amount, date)
When an employee is granted a loan, that information is stored in Recovery table. This loan is then recovered installment basis, from monthly salary. (installment is [amount /duration]).
Once recovered, details about the installment recovered should be stored in Installments table. Since an loan may be recovered in several installments the relation ship of Recovery to Installment is 1:M.
Now let's say I just want to find out all recovery details that should be deducted from Salary. I have written this query in this regard. Here I take sum of all paid installment and then deduct it from the loan amount and if the balance is greater than 0 then the installment is calculated. Expected result is Emp_DI | Installment
WITH Summary (RID, emp_id, amount, duration, installment)
AS(
SELECT Recovery.RID, Recovery.emp_id, Recovery.amount, Recovery.duration, SUM(Installment.recovered_installment) AS Installments
FROM Recovery LEFT OUTER JOIN
Installment ON Recovery.RID = Installment.recovery_id
WHERE Recovery.type = 'Loan'
GROUP BY Recovery.duration, Recovery.amount, Recovery.emp_id, Recovery.RID
)
SELECT emp_id, SUM(amount / duration) AS INS FROM Summary
WHERE ((CASE WHEN installment != NULL THEN (amount-installment)
ELSE(amount) END)> 0 )
GROUP BY emp_id
This query runs fine give the desired out put with one exception. I.e. it seems that there is an issue in WHERE clause.
WHERE ((CASE WHEN installment != NULL THEN (amount-installment)
ELSE(amount) END)> 0 )
This does not filter out the records with balance zero as expected. I couldn't understand the reason for this. So could you please help me? Your help is really appreciated!
The criteria installment != NULL needs to be replaced with installment is not null.
All comparisons to NULL return FALSE.
For the specific comparison you are making, this WHERE clause might be easier on the eyes:
WHERE (amount - COALESCE(installment,0)) > 0
You need is not null:
WHERE ((CASE WHEN installment IS NOT NULL THEN (amount-installment)
ELSE(amount) END)> 0 )
Almost any comparison to NULL, including = NULL and <> NULL returns NULL. And, NULL is treated as false. Use IS NULL and IS NOT NULL.
WHERE ( installment IS NOT NULL AND (amount-installment) > 0)
OR ( installment IS NULL AND AMOUNT >0)
In Sql Server NULL is an Unknown value, so you cant possibly compare an unknown value to any other value. therefore you use IS NULL or IS NOT NULL when checking for nulls in sql server.
I think you meant to put it in the having clause? Does this work?
WITH Summary as
(SELECT Recovery.RID,
Recovery.emp_id,
Recovery.amount,
Recovery.duration,
SUM(Installment.recovered_installment) AS Installments
FROM Recovery
LEFT OUTER JOIN Installment
ON Recovery.RID = Installment.recovery_id
WHERE Recovery.type = 'Loan'
GROUP BY Recovery.duration,
Recovery.amount,
Recovery.emp_id,
Recovery.RID)
SELECT emp_id, SUM(amount / duration) AS INS
FROM Summary
GROUP BY emp_id
having sum (CASE WHEN installments is not NULL
THEN(amount - installments) ELSE(amount) END) > 0
As others have pointed out, "is not null" is the correct way of saying not equal to null, as well.
I'm trying to find all duplicates in a Table and change one of their values.
Now i use:
SELECT Amount
FROM Bids
GROUP BY Amount, AuctionID
HAVING ( COUNT(Amount) > 1 ) AND (AuctionID=1)
The problem that it returns only
Amount
23.6500
41.8800
42.3500
And not
Amount
23.6500
23.6500
41.8800
41.8800
42.3500
42.3500
So I can't UPDATE all the rows.
How can I get it the way I showed?
Thanks,
Dan
Just wrap it inside an IN query:
SELECT Amount
FROM Bids
WHERE Amount IN (
SELECT Amount
FROM Bids
GROUP BY Amount, AuctionID
HAVING ( COUNT(Amount) > 1 ) AND (AuctionID=1)
)
UPDATE: added UPDATE statement
UPDATE Bids
SET Burned = 1
WHERE Amount IN (
SELECT Amount
FROM Bids
GROUP BY Amount, AuctionID
HAVING ( COUNT(Amount) > 1 ) AND (AuctionID=1)
)
Assume that you have Id in Bids table:
SELECT Amount
FROM Bids b1
WHERE AcutionId = 1
AND EXISTS (Select 1 from Bids b2
WHERE b2.AuctionID = b1.AuctionId
AND b1.Amount = b2.Amount
AND b1.Id <> b2.Id)
I'm curious to know why your original select doesn't satisfy your requirement. If for every member within a set of duplicates you're only selecting one of them, then you have one to update. It should be informative to add AuctionId to the select provided by Frank Schmitt to see what distinguishes these rows.