Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Monthly balance from two tables


Monthly balance from two tables

Author
Message
hemal_301080
hemal_301080
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 109
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
Lokesh Vij
Lokesh Vij
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1572 Visits: 1599
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


vinu512
vinu512
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1155 Visits: 1618
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 ;-)
hemal_301080
hemal_301080
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 109
Thanks, this worked
Lokesh Vij
Lokesh Vij
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1572 Visits: 1599
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


vinu512
vinu512
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1155 Visits: 1618
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 ;-)
vinu512
vinu512
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1155 Visits: 1618
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 ;-)
hemal_301080
hemal_301080
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 109
:-)
Thank Vinu Vijayan
vinu512
vinu512
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1155 Visits: 1618
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 ;-)
hemal_301080
hemal_301080
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 109
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search