Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Query balance of account from two or more tables Expand / Collapse
Author
Message
Posted Tuesday, December 25, 2012 5:14 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, September 15, 2013 9:46 PM
Points: 29, Visits: 79
I am having two tables as follows

voucherCr

srno vouchertype voucherprefix voucherno crparty cramount
1 PURCHASE P 1 2 55000
2 PAYMENT R 1 1 55000
voucherDr

srno vouchertype voucherprefix voucherno drparty dramount
1 PURCHASE P 1 4 54000
2 PURCHASE P 1 5 1000
3 PAYMENT R 1 2 55000
Here, in PURCHASE voucher P/1, I am purchasing goods from party 2 worth of 55000 and in PAYMENT voucher R/1, I am paying 55000 rupees to party 2.

Now If i query about party 2, I want to display information in following format

VTYPE VPRE VNO AGAINSTPARTY CREDIT DEBIT
PURCHASE P 1 4 55000 NULL
PAYMENT R 1 1 NULL 55000
So the closing balance for party 2 will be zero, as I have paid them 55000 against the purchase of 55000

Can you help me?

Post #1400047
Posted Tuesday, December 25, 2012 6:45 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 9:53 PM
Points: 3,438, Visits: 5,390
Normally, most people won't answer your question because you didn't take the time to provide DDL and sample data in consumable form. There are many good links to how to do this, but since it is Christmas and I'm feeling charitable I will demonstrate and explain with comments in the code.

-- Next time - provide DDL
CREATE TABLE #voucherCr
(srno INT
,vouchertype VARCHAR(20)
,voucherprefix CHAR(1)
,voucherno INT
,crparty INT
,cramount MONEY)

CREATE TABLE #voucherDr
(srno INT
,vouchertype VARCHAR(20)
,voucherprefix CHAR(1)
,voucherno INT
,drparty INT
,dramount MONEY)

-- And sample data in consumable form
INSERT INTO #voucherCr
SELECT 1,'PURCHASE','P',1,2,55000
UNION ALL SELECT 2,'PAYMENT','R',1,1,55000

INSERT INTO #voucherDr
SELECT 1,'PURCHASE','P',1,4,54000
UNION ALL SELECT 2,'PURCHASE','P',1,5,1000
UNION ALL SELECT 3,'PAYMENT','R',1,2,55000


SELECT VTYPE=vouchertype, VPRE=voucherprefix, VNO=voucherno
,AGAINSTPARTY=crparty
,CREDIT=cramount, DEBIT=NULL
FROM #voucherCr
WHERE crparty = 2
UNION ALL
SELECT vouchertype, voucherprefix, voucherno, drparty, NULL, dramount
FROM #voucherDr
WHERE drparty = 2

-- Always use temporary tables and don't forget to drop them
DROP TABLE #voucherCr
DROP TABLE #voucherDr


This query is only meant to get you started. It does not produce the expected results in the AGAINSTPARTY column, because frankly I couldn't see a pattern for how you came up with 1 and 4.

Play around with it and see if you can figure out how to make it work to get exactly the results you were expecting.

Merry Christmas.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1400115
Posted Tuesday, December 25, 2012 9:24 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, September 15, 2013 9:46 PM
Points: 29, Visits: 79
Thanks, dwain

Next time I will be providing DDL also, Sorry for my question

Again thanks for your answer. I will test it. And let you know
Post #1400134
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse