SQL - How to retrieve the correct number

Viewed 27

I have written a personal pseudocode to help others understand what I am trying to achieve. I am a beginner in SQL but I know all the basics, just not experienced enough with the possiblilities.

I tried using SELECT CASE but it doesn't achieve what I need.

Here is my sample data

CREATE TABLE Records
    ([ColA] INTEGER, [ColB] INTEGER, [ColTotal] INTEGER)
;

INSERT INTO Records
    ([ColA], [ColB] )
VALUES
    ('3', '4'),
    ('4', '2’),
    ('1', '2'),
    ('3', '5'),
    ('3', '1'),
    ('2', '2')
;

Here is my PSEUDOCODE (I found out after accepted answer that my logic in the psuedo was incorrect too. This has been fixed)

SELECT COL A, COL B, COL TOTAL
IF COL A  >= COLB THEN
ADD COLB value to COL TOTAL
ELSE
USE Value from COLA and add to total
END IF

Here is my SQL

SELECT SUM(ColTotal)
FROM t
WHERE ColA >= ColB
1 Answers
Related