Reply to topic  [ 9 posts ] 
Quick SQL help 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Hello all :D

I know there are some SQL gurus on here and a friend of mine is trying to get a query to work but can't and I have tried to help but gave up.

The table is...

Account - Division - Hire Rev - Sales Rev - Loss Rev - Damage Rev

the table contains lots of lines (seriously lots) and each line represents one invoice.

The lines could be thus...

JOHN - A - 0.00 - 100.00 - 10.00 - 0.00
JOHN - B - 150.00 - 0.00 - 0.00 - 0.00
ADAM - A - 15.00 - 0.00 - 0.00 - 0.00
and so on...

What we would like to see as an output is this...

Account - A - B - C - D - E - ... and so on through all the divisions.
JOHN - 110.00 - 150.00 - 0.00 - 0.00 - ... etc...
ADAM - 15.00 - 0.00 - 0.00 - 0.00 - ... etc...

i.e. each line of the query will show one account and the total sum of all the revenue types for each division separately.

I hope that makes sense.

(A, B, C, D, E, ... are all divisions in this example).

Any help is appreciated.

TVM!

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Nov 19, 2009 4:59 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Table name is RI and the fields are all as I have written above but with no spaces (i.e. HireRev).

Thanks again.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Nov 19, 2009 5:00 pm
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
Which value is used for the sum? Is there a separate table where the divisions are listed?

That makes the query a bit different, otherwise, how many divisions are you dealing with?

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Thu Nov 19, 2009 5:34 pm
Profile ICQ
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
big_D wrote:
Which value is used for the sum? Is there a separate table where the divisions are listed?

That makes the query a bit different, otherwise, how many divisions are you dealing with?
There is no field in the table that stores the sum of the different revenue types and it's definitely possible to make a table containing all the division if one doesn't exist and it makes this easier.

Say for example we created a table called Division with the fields Division (to match the RI table) and Name. (For simplicity).

I guess that would make it easier?

Thanks!

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Nov 19, 2009 6:02 pm
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
SELECT DISTINCT Account, Division, SUM('Hire Rev') + SUM('Sales Rev') + SUM('Loss Rev') + SUM('Damage Rev')
FROM RI
ORDER BY Account, Division
GROUP BY Account, Division


That won't create a horizontal table, but it will give you one line per Account/Division combination.

Still thinking about horizontal summing, but it isn't straight forward...

Didn't need the division table for that, but it would be a part of basic database design for such a set-up.

It seems a very strange set-up for an invoice though... :?

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Thu Nov 19, 2009 6:54 pm
Profile ICQ
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Thanks!

I'll let the guy know.

big_D wrote:
It seems a very strange set-up for an invoice though... :?


The SQL database is an export that has slowly evolved on from a CSV file into something ... err ... bigger.

It's used by the audit team where I work.

The actual working database looks nothing like this though.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Nov 19, 2009 7:56 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Ah, I understand your first statement now.

The query you have will do this...

Account - Division - Revenue
ADAM - A - xx.xx
JOHN - A - xx.xx
JOHN - B - xx.xx

Am I right?

If so I think that would be sufficient for the purposes that the guy needs it for!

Thanks!

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Nov 19, 2009 7:59 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
Fogmeister wrote:
Account - Division - Revenue
ADAM - A - xx.xx
JOHN - A - xx.xx
JOHN - B - xx.xx

Am I right?

Yes.

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Thu Nov 19, 2009 8:00 pm
Profile
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
^^ Wot 'e sed! ;)

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Thu Nov 19, 2009 8:24 pm
Profile ICQ
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.