SQL CASE ANOTHER COLUMN OF ANOTHER ROW

Dears

Im trying to case a column to use the sum of M_DR + M_CR as the CLS_BAL. E,g if the ID is 192000801 then it should have the sum of M_DR and M_CR of ID 1920008      as its CLS_BAL as I have made in bold below.

Attach is a sample of the data I am working with.

SHEET              CAT              H_ID      ID                      CHRT_ID              OPN_BAL            M_DR                    M_CR             CLS_BAL
BALANCE      ASSETS      10122      1920008              1920008              0.00                            3000.00      0.00                     0.00
BALANCE      ASSETS      10125      192000801      192000801      0.00                      0.00                            0.00                    3000.00
BALANCE      ASSETS      10117      1930002              1930002              20847369.45            0.00                            403436.43     21250805.88
BALANCE      ASSETS      10125      193000201      193000201      0.00                            0.00                            0.00                     0.00
EE-SAMPLE-0427.xlsx
barkomeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pratima PharandeCommented:
you want  M_DR + M_CR as the CLS_BA .. rite ?

try this


Select SHEET,      CAT,      H_ID,      ID,      CHRT_ID,      OPN_BAL,      M_DR,      M_CR, (M_DR + M_CR) as       n_CLS_BAL

from tablename
0
barkomeAuthor Commented:
Yes, but I want  ID 192000801       to have (M_DR + M_CR) of ID 1920008              
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
This relies on having only one matching row for each (shortened) ID:
Select SHEET, CAT, H_ID,  a.ID,  CHRT_ID, OPN_BAL, M_DR, M_CR,  n_CLS_BAL
from tbl a
left join (select ID, M_DR + M_CR as n_CLS_BAL from tbl) b
on left(a.ID, 7) = b.ID

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

barkomeAuthor Commented:
I was looking forward for something similar to this(syntax not correct though):

            CASE
            WHEN CHRT_ID ='193000201'      THEN  (RP_M_DR + RP_M_CR WHERE CHRT_ID ='1930002')
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You would have to use a subselect in THEN. But honestly, that does not make sense - to have fixed IDs you need to enumerate ..
  CASE
            WHEN CHRT_ID ='193000201'      THEN  (select RP_M_DR + RP_M_CR from tbl WHERE CHRT_ID ='1930002')

Open in new window

It is also ineffective if used often.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
barkomeAuthor Commented:
Thanks Qlemo, I had this in mind, but had difficulty referencing 'tbl' because it my tbl is a derived table from a subquery.

The ID's I also want to have fixed are just 3 in number, and wont change.
0
Dale FyeCommented:
To avoid line wrap, embed your data inside the Code brackets, it will be far easier to read:

SHEET        CAT         H_ID       ID            CHRT_ID          OPN_BAL        M_DR          M_CR          CLS_BAL
BALANCE      ASSETS      10122      1920008       1920008             0.00       3000.00         0.00            0.00
BALANCE      ASSETS      10125      192000801     192000801           0.00          0.00         0.00         3000.00
BALANCE      ASSETS      10117      1930002       1930002      20847369.45          0.00    403436.43     21250805.88
BALANCE      ASSETS      10125      193000201     193000201           0.00          0.00         0.00            0.00

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
If you generate your subquery data as in my first example, or with a CTE, you should have no difficulties to access the results as in my second example.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.