954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Query

select companyname,sum(sale) as TotalSale,sum(purchase) as TotalPurchase from invoice group by companyname Having companyname IN (select companyname from invoice)

It returns sum only for those column whose all sum are not blank, how can i show blank sum values

I mean there are 7 companies but only 4 records are shown. I want to display all seven.

Majestics
Practically a Master Poster
621 posts since Jul 2007
Reputation Points: 199
Solved Threads: 49
 

I think you do not need the HAVING part of the SQL.

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

Yes u are right as always.... But what about my problem....

Majestics
Practically a Master Poster
621 posts since Jul 2007
Reputation Points: 199
Solved Threads: 49
 

can u post some sample data.

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 
select companyname,sum(sale) as TotalSale,sum(purchase) as TotalPurchase from invoice group by companyname


I used the above query to get the total sale and purchase belong to following company

A , B ,C ,D ,E ,F ,G

only A,C,E,G have sale and purchase value other have no sale or purchase record.

so I got A 12 13 ,C 12 15 , E 15 89 , G 14 89 but i want rest company to show 0 if there sum of sale or purchase are not present.

Majestics
Practically a Master Poster
621 posts since Jul 2007
Reputation Points: 199
Solved Threads: 49
 

Following are the record
Company purchase sale
A 1 1
A 1 2
B 5 7

now the same above query will only show A and B company sale and purchase rest of companies values will be ignored and this is what i dont want.

Majestics
Practically a Master Poster
621 posts since Jul 2007
Reputation Points: 199
Solved Threads: 49
 

Let me clear it i m making report

Majestics
Practically a Master Poster
621 posts since Jul 2007
Reputation Points: 199
Solved Threads: 49
 

You will need to do an outer join select all from one table while getting the matches from another. Why are you joining on company name instead of an I'd value? Mark

web-guy
Newbie Poster
5 posts since Jul 2011
Reputation Points: 27
Solved Threads: 1
 

Please take my above query and place a outer join in correct manner....

Majestics
Practically a Master Poster
621 posts since Jul 2007
Reputation Points: 199
Solved Threads: 49
 

Use the nvl() function around the sums and remove the having portion of your query. I didn't realize that you were calling the same table twice which is is unnecessary for your query. The nvl function will prevent summing nulls which isn't possible. It will convert nulls trouble zero.

web-guy
Newbie Poster
5 posts since Jul 2011
Reputation Points: 27
Solved Threads: 1
 
select companyname,sum(sale) as TotalSale,sum(purchase) as TotalPurchase from invoice group by companyname


I have already modified by query....

Also Nvl dont work.....

Majestics
Practically a Master Poster
621 posts since Jul 2007
Reputation Points: 199
Solved Threads: 49
 

Is this oracle DB? Or someother flavor?

web-guy
Newbie Poster
5 posts since Jul 2011
Reputation Points: 27
Solved Threads: 1
 

100% Oracle Database...... :) I m making report which take all company name there purchase and sale SUMS and difference, some company can have no sale and purchase so atleast there name must be listed with 0 on both sale and purchase... Thats what all i want.

Majestics
Practically a Master Poster
621 posts since Jul 2007
Reputation Points: 199
Solved Threads: 49
 

Nvl(sales,0) and nvl(purchase,0) is where you need to change.

web-guy
Newbie Poster
5 posts since Jul 2011
Reputation Points: 27
Solved Threads: 1
 

NVL WORKED, but my problem isnt solved yet

Majestics
Practically a Master Poster
621 posts since Jul 2007
Reputation Points: 199
Solved Threads: 49
 

I agree with web-guy's earlier suggestion that you probably need to outer join your customer table to your invoice table for the sums. I assume that the customers who have no orders have no records in the invoice table. If they aren't in the table, they aren't going to appear in your group by results.

Try using [customer table] left outer join [invoice table] on the customer Ids.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Another way (older way) to join two oracle tables together with outer join syntax would be to do it the following way.
select c.companyname, sum(nvl(i.sales,0))
from customers c, invoices i
where c.companyname = i.companyname (+)
group by c.companyname

The (+) syntax goes where you expect the nulls to occur

web-guy
Newbie Poster
5 posts since Jul 2011
Reputation Points: 27
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You