We help IT Professionals succeed at work.
Get Started

How do i setup this SQL query?

302 Views
Last Modified: 2021-04-21
The table I have looks similar to the following:

Sequence Number (Primary Key)
Cheque Amount
2ndChequeRequestedSEQ   (Null able)

If 2ndChequeRequested is not null, that means there is another record (Where Sequence Number = 2ndChequeRequested). For Example:
SEQ   CKAMT         2ndChequeRequested
1        $100.00         2
2        $400.00         Null
3        $50.00           Null
4        $500.00         Null
5        $150.00         6
6        $250.00         Null

What I need the select statement to do is give me the following Results
$500               (ckamt for 1 + 2)
$50                 (ckamt for 3)
$500               (ckamt for 4)
$400               (ckamt for 5 + 6)

Sequence Type is Varchar while Ckamt is Decimal.
Any help would be greatly appreciated as I tried a few select case statements with zero luck. I am for some reason not able to figure out a way to code in SQL..

if 2ndChequeRequested is not null then
Ckamt = Select Sum(Ckamt + Select ckamt from table where SEQ = 2ndChequeRequested)
else
Select Ckamt from table
end if
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE