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 12»»

Monthly balance from two tables Expand / Collapse
Author
Message
Posted Wednesday, December 26, 2012 12:58 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 have following information with two tables

CREATE TABLE [dbo].[voucherCr](
[srno] [int] IDENTITY(1,1) NOT NULL,
[vouchertype] [nvarchar](50) NULL,
[voucherprefix] [nvarchar](50) NULL,
[voucherno] [nvarchar](50) NULL,
[crparty] [int] NULL,
[cramount] [float] NULL)

CREATE TABLE [dbo].[voucherDr](
[srno] [int] IDENTITY(1,1) NOT NULL,
[vouchertype] [nvarchar](50) NULL,
[voucherprefix] [nvarchar](50) NULL,
[voucherno] [nvarchar](50) NULL,
[drparty] [int] NULL,
[dramount] [float] NULL)

CREATE TABLE [dbo].[voucher](
[srno] [int] IDENTITY(1,1) NOT NULL,
[vouchertype] [nvarchar](50) NULL,
[voucherprefix] [nvarchar](50) NULL,
[voucherno] [nvarchar](50) NULL,
[dt] [date] NULL,
[details] [nvarchar](255) NULL,
[invoicetype] [nvarchar](50) NULL)

Here between all three tables, vouchertype,voucherprefix,voucherno columns are inter related.

Now I have following data for three tables

Voucher

srno	vouchertype	voucherprefix	voucherno	dt	           details	invoicetype
1 PURCHASE P 1 2012-12-25 RETAIL INVOICE
2 PAYMENT R 1 2012-12-25
3 PURCHASE P 2 2012-12-25 RETAIL INVOICE
4 PURCHASE P 3 2012-12-25 RETAIL INVOICE

VoucherCr

srno	vouchertype	voucherprefix	voucherno	crparty	cramount
1 PURCHASE P 1 2 55000
2 PAYMENT R 1 1 55000
3 PURCHASE P 2 2 28000
4 PURCHASE P 3 2 30550

VoucherDr

srno	vouchertype	voucherprefix	voucherno	drparty	dramount
1 PURCHASE P 1 4 55000
2 PAYMENT R 1 2 55000
3 PURCHASE P 2 4 28000
4 PURCHASE P 3 4 29000
5 PURCHASE P 3 97 1160
6 PURCHASE P 3 98 290
7 PURCHASE P 3 46 50
8 PURCHASE P 3 66 50

Now I want results as follows

Month Credit Debit
December 168550 168550


Post #1400173
Posted Wednesday, December 26, 2012 2:01 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, June 24, 2014 6:38 PM
Points: 1,371, Visits: 1,560
As per the requirements, here is my query:

SELECT Month(v.dt)      [Month],
Sum(vc.cramount) Credit,
Sum(vd.dramount) Debit
FROM voucher v
INNER JOIN voucherCr vc
ON v.voucherno = vc.voucherno
AND v.voucherprefix = vc.voucherprefix
AND v.vouchertype = vc.vouchertype
INNER JOIN voucherDr vd
ON v.voucherno = vd.voucherno
AND v.voucherprefix = vd.voucherprefix
AND v.vouchertype = vd.vouchertype
GROUP BY Month(v.dt)

This will yield below result, which is different from yours. Please post the complete requirement..
Month       Credit                 Debit
----------- ---------------------- ----------------------
12 290750 168550


~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter

Post #1400186
Posted Wednesday, December 26, 2012 2:32 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 8:48 AM
Points: 1,118, Visits: 1,582
This would do what you are looking for:

Select p.Month, SUM(p.cramount) As Credit, SUM(q.dramount) As Debit From 
(
Select b.voucherno, DATENAME(MM, a.dt) As Month, SUM(b.cramount) As cramount From voucher As a
JOIN voucherCr As b ON a.voucherno = b.voucherno AND a.vouchertype = b.vouchertype
Group By b.voucherno, DATENAME(MM, a.dt)
) As p
JOIN
(
Select b.voucherno, DATENAME(MM, a.dt) As Month, SUM(b.dramount) As dramount From voucher As a
RIGHT JOIN voucherDr As b ON a.voucherno = b.voucherno AND a.vouchertype = b.vouchertype
Group By b.voucherno, DATENAME(MM, a.dt)
) As q ON p.voucherno = q.voucherno
Group By p.Month



Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1400190
Posted Wednesday, December 26, 2012 3:04 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
Thanks, this worked
Post #1400192
Posted Wednesday, December 26, 2012 3:06 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, June 24, 2014 6:38 PM
Points: 1,371, Visits: 1,560
vinu512 (12/26/2012)
This would do what you are looking for:

Select p.Month, SUM(p.cramount) As Credit, SUM(q.dramount) As Debit From 
(
Select b.voucherno, DATENAME(MM, a.dt) As Month, SUM(b.cramount) As cramount From voucher As a
JOIN voucherCr As b ON a.voucherno = b.voucherno AND a.vouchertype = b.vouchertype
Group By b.voucherno, DATENAME(MM, a.dt)
) As p
JOIN
(
Select b.voucherno, DATENAME(MM, a.dt) As Month, SUM(b.dramount) As dramount From voucher As a
RIGHT JOIN voucherDr As b ON a.voucherno = b.voucherno AND a.vouchertype = b.vouchertype
Group By b.voucherno, DATENAME(MM, a.dt)
) As q ON p.voucherno = q.voucherno
Group By p.Month



Thanks Vinu. I got my mistake


~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter

Post #1400194
Posted Wednesday, December 26, 2012 3:34 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 8:48 AM
Points: 1,118, Visits: 1,582
hemal_301080 (12/26/2012)
Thanks, this worked


You're Welcome!!

Anytime Lokesh. It was just a matter of Grouping the data before joining.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1400198
Posted Wednesday, December 26, 2012 3:46 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 8:48 AM
Points: 1,118, Visits: 1,582
I'm sorry Hemal, there is a slight error in my query. If there is data for more than one year then the data would be grouped only according to month only and not by year. The following is the edited query:

Select p.Month, SUM(p.cramount) As Credit, SUM(q.dramount) As Debit From 
(
Select b.voucherno, DATENAME(YY, a.dt) As Year, DATENAME(MM, a.dt) As Month, SUM(b.cramount) As cramount From voucher As a
JOIN voucherCr As b ON a.voucherno = b.voucherno AND a.vouchertype = b.vouchertype
Group By b.voucherno, DATENAME(YY, a.dt), DATENAME(MM, a.dt)
) As p
JOIN
(
Select b.voucherno, DATENAME(YY, a.dt) As Year, DATENAME(MM, a.dt) As Month, SUM(b.dramount) As dramount From voucher As a
RIGHT JOIN voucherDr As b ON a.voucherno = b.voucherno AND a.vouchertype = b.vouchertype
Group By b.voucherno, DATENAME(YY, a.dt), DATENAME(MM, a.dt)
) As q ON p.voucherno = q.voucherno
Group By p.Month



Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1400201
Posted Wednesday, December 26, 2012 3:53 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

Thank Vinu Vijayan
Post #1400202
Posted Wednesday, December 26, 2012 4:14 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 8:48 AM
Points: 1,118, Visits: 1,582
hemal_301080 (12/26/2012)

Thank Vinu Vijayan


No probs Hemal....it was my bad.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1400207
Posted Friday, March 8, 2013 5:10 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 facing problem with one situation

If voucherDr table contains data of two months, i.e. March,April
and voucherCr table contains data of one month, March only


Then I am not getting expected result
Post #1428527
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse