473,513 Members | 11,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sum statement has no records

112 New Member
I have two tables. One is glaccounts and another is ITR(Inventory transaction)
Glaccounts contains the field glaccount.
glaccount are 121101, 121102, 121103,.......121110

ITR Table has information about the inventory transaction. ITR table also have itr_glacctnbr that link with glaccounts table.

I want to calculate amount i spend in a month respective accounts.

Here my query

Select dbo.Glaccounts.GlAccount,SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt)as Monthtotal
FROM dbo.ITR INNER JOIN
dbo.Glaccounts ON dbo.ITR.ITR_GLAcctNbr = dbo.Glaccounts.GlAccount
where itr_transdate between '2007-12-01' and '2007-12-31'
group by dbo.Glaccounts.GlAccount

This query display the result.
Glaccount Monthtotal
121101 -51794.238300000005
121103 -44021.349499999997
121104 -37982.490399999922
121105 -500813.61599999992
121107 -2700.0
121108 -671744.63323501509


For some GLaccount i dont have records for that month. If no records i need to display 0 .

I want result like this

Glaccount Monthtotal
121101 -51794.238300000005
121102 0.0
121103 -44021.349499999997
121104 -37982.490399999922
121105 -500813.61599999992
121106 0.0
121107 -2700.0
121108 -671744.63323501509
121109 0.0
121110 0.0

Please tell me the query to get this result

Thanks
Jan 24 '08 #1
3 1641
Delerna
1,134 Recognized Expert Top Contributor
LEFT JOIN the table that dosn't have records for every month to the table that does have records for every month. then you can use isnull(fieldname,0) to return a zero where the months data is missing
Jan 24 '08 #2
imtmub
112 New Member
LEFT JOIN the table that dosn't have records for every month to the table that does have records for every month. then you can use isnull(fieldname,0) to return a zero where the months data is missing
Still I am getting the same result.

I have modified the query

SELECT dbo.Glaccounts.GlAccount,
isnull(SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt),0.00) AS Monthtotal
FROM dbo.ITR LEFT JOIN
dbo.Glaccounts ON dbo.ITR.ITR_GLAcctNbr = dbo.Glaccounts.GlAccount
WHERE (dbo.ITR.ITR_TransDate BETWEEN '2007-12-01' AND '2007-12-31')
GROUP BY dbo.Glaccounts.GlAccount
Jan 24 '08 #3
deepuv04
227 Recognized Expert New Member
Still I am getting the same result.

I have modified the query

SELECT dbo.Glaccounts.GlAccount,
isnull(SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt),0.00) AS Monthtotal
FROM dbo.ITR LEFT JOIN
dbo.Glaccounts ON dbo.ITR.ITR_GLAcctNbr = dbo.Glaccounts.GlAccount
WHERE (dbo.ITR.ITR_TransDate BETWEEN '2007-12-01' AND '2007-12-31')
GROUP BY dbo.Glaccounts.GlAccount

USE THE FOLLOLWING

SELECT dbo.Glaccounts.GlAccount,
isnull(SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt),0.00) AS Monthtotal
FROM dbo.ITR RIGHT OUTER JOIN
dbo.Glaccounts ON dbo.ITR.ITR_GLAcctNbr = dbo.Glaccounts.GlAccount
WHERE (dbo.ITR.ITR_TransDate BETWEEN '2007-12-01' AND '2007-12-31')
GROUP BY dbo.Glaccounts.GlAccount
Jan 24 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
130044
by: Michael | last post by:
I have two tables with a 1-many relationship. I want to write a select statement that looks in the table w/many records and compares it to the records in the primary table to see if there are any...
2
2135
by: David | last post by:
Hi, I have part of my SQL statement in my asp page as follows: WHERE ((pcbforecast.ShipETA < '31/12/2005') and (products.BBProductName = ....... The problem I am having is that I want the...
2
2502
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an...
3
13809
by: Peter Phelps | last post by:
My problem is as follows: I need automatically iterate through a single field in a table and use the values in the field to create an in-statement. Currently, the character limitation in the...
7
11418
by: mark | last post by:
Access 2000: I creating a report that has a record source built by the user who selects the WHERE values. An example is: SELECT * FROM CHARGELOG WHERE STDATE Between #10/27/2003# And...
10
3375
by: Mike | last post by:
I know this sounds strange but I am at a loss. I am calling a simple funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement. private void...
8
3487
by: nano2k | last post by:
Hi Shortly, I keep invoices in a table. Occasionally, someone will fire the execution of a stored procedure (SP) that performs several UPDATEs against (potentially) all invoices OLDER than a...
2
2903
by: willwmagic | last post by:
Hi everyone, I feel as this is a very simple question, but I cannot find any information pertaining to it. I want to be able to update a set of records that were returned through a select...
5
17332
by: prakashwadhwani | last post by:
I have 2 columns in a continuous form. One called Balance_1 and the other Balance_2. I want to transfer the contents of Balance_2 to Balance_1 ... for all 2000 rows in the table. How to go...
10
3550
by: JohnO | last post by:
Hi All, This question is related to iSeries V5R4 and db2. I want to implement an AFTER DELETE trigger to save the deleted rows to an archive table, I initially defined it as a FOR EACH...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.