Solved

SQL Statment

Posted on 2009-07-07
16
313 Views
Last Modified: 2012-05-07
I have 2 Tabels , one showing the clinc data and oher there is amout of invoice I want to run report shwoing all records of clnc table and the total amount of invoice even null value during period time ... pls check my code which is correct but it showing only the records have figures and the clnc which have nill values it not appearing ... pls help us
SELECT a.clnna, a.clnc, SUM(b.total) AS total
FROM clnc a, invmaster b
WHERE a.clnc = b.clnc  AND 
    b.inv_date BETWEEN ? AND ?
GROUP BY a.clnna, a.clnc

Open in new window

0
Comment
Question by:ezaldeen
16 Comments
 
LVL 6

Expert Comment

by:jwenting
ID: 24792225
It doesn't show those records because they don't have any child records.
The where clause excludes any records in clnc for which no records exist in invmaster (assuming no records exist in invmaster for which the clnc column is NULL, which would be a logical assumption).

You'd have to use something like a union on clnc to get records there with no child records as well.
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 24792238
what database?
SELECT a.clnna, a.clnc, SUM(b.total) AS total
FROM clnc a
LEFT JOIN invmaster b
  ON a.clnc = b.clnc  AND 
 AND b.inv_date BETWEEN ? AND ?
GROUP BY a.clnna, a.clnc

Open in new window

0
 
LVL 4

Assisted Solution

by:JonasMalmsten
JonasMalmsten earned 20 total points
ID: 24792239
You need to do an "outer left join", not sure which database you use, but syntax for oracle would be:

SELECT a.clnna, a.clnc, SUM(b.total) AS total
FROM clnc a, invmaster b
WHERE a.clnc = b.clnc(+)  AND
    (b.clnc is null or b.inv_date BETWEEN ? AND ?)
GROUP BY a.clnna, a.clnc
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 6

Assisted Solution

by:divyeshhdoshi
divyeshhdoshi earned 20 total points
ID: 24792247
SELECT a.clnna, a.clnc, SUM(b.total) AS total
FROM clnc a, invmaster b
WHERE a.clnc = b.clnc(+)  AND
    b.inv_date BETWEEN ? AND ?
GROUP BY a.clnna, a.clnc
0
 

Author Comment

by:ezaldeen
ID: 24792479
Thanks for multi answers ...
This Access Database ....
I try to run SQl Statment which is given by JONAS & DIVYE.. but it not work fine
I use dataenvirment tool with VB6 ...
can any one give best statment work fine ..
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 24792508
ms access:
SELECT a.clnna, a.clnc, SUM(b.total) AS total
FROM clnc a
LEFT OUTER JOIN invmaster b
  ON ( a.clnc = b.clnc  AND 
 AND b.inv_date BETWEEN ? AND ? )
GROUP BY a.clnna, a.clnc

Open in new window

0
 

Author Comment

by:ezaldeen
ID: 24792801
Thanks ange....
It's work fine , but the field of (Total) showing NULL if there is no data , I need to show it as (0) Zero
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 24792850
SELECT a.clnna, a.clnc, ISNULL(SUM(b.total),0) AS total
FROM clnc a
LEFT OUTER JOIN invmaster b
  ON ( a.clnc = b.clnc  AND
 AND b.inv_date BETWEEN ? AND ? )
GROUP BY a.clnna, a.clnc
0
 

Author Comment

by:ezaldeen
ID: 24793233
Thanks
But still not solve it , it showing error , pls see the attachment
untitled.bmp
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 24793317
sorry. ..
SELECT a.clnna, a.clnc, IF(ISNULL(SUM(b.total),0,SUM(b.total)) AS total
FROM clnc a
LEFT OUTER JOIN invmaster b
  ON ( a.clnc = b.clnc  AND 
 AND b.inv_date BETWEEN ? AND ? )
GROUP BY a.clnna, a.clnc

Open in new window

0
 

Author Comment

by:ezaldeen
ID: 24793419
Dear ...
I use Dataenvirment with VB6 ...
your statment to work with this >>> I try many times still there is a problem ... still error coming
the IF statment not work into select statment with dataenvirment in VB6 ... pls search another way
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 24793519
sorry. ms access it's iif and not if in the query:
http://www.techonthenet.com/access/functions/advanced/iif.php
SELECT a.clnna, a.clnc, IIF(ISNULL(SUM(b.total),0,SUM(b.total)) AS total
FROM clnc a
LEFT OUTER JOIN invmaster b
  ON ( a.clnc = b.clnc  AND 
 AND b.inv_date BETWEEN ? AND ? )
GROUP BY a.clnna, a.clnc

Open in new window

0
 

Author Comment

by:ezaldeen
ID: 24793596
Thanks ...
I think this will work into access application but not with data envirment in VB6 ... I take it copy and paste on our vb6 application but not work and still there is an error
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 24793658
I never worked with dataenvironment, so I don't know what the problem could be, at least not without seeing the EXACT ERROR !
remember, we are not sitting in front of your screen. I don't have any crystal balls ...
0
 

Accepted Solution

by:
ezaldeen earned 0 total points
ID: 24793677
Thanks Alot ...
I found this and it's work fine Now :
***********************************************************
SELECT a.clnna, a.clnc, SUM(IIf(isnull(b.total), 0, b.total))
    AS total
FROM clnc a LEFT OUTER JOIN
    invmaster b ON (a.clnc = b.clnc AND b.inv_date BETWEEN
    ? AND ?)
GROUP BY a.clnna, a.clnc
***********************************************************************
Thanks ...
0
 

Author Comment

by:ezaldeen
ID: 25003483
fine
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now