473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Query help needed

I am pretty new to SQL and database concepts. Heres my problem: I have a sales info. table and an expenses info table with invoice number being the field relating the two tables (1:N). The sales table has details about each job done for a specific client, ie. a breakdown of the income recieved from each particular job. Expenses Info. table has several expenses incurred for each job, that is to say, some jobs done require outsourcing which creates a job-specific expense. Some invoices have 1 or 2 expenses related to them while most have none because most of the work is done in-house. Now, i have created a query to calculate the profits realised per job but it is only calculating profits for the jobs with an expense attached to it. For the other jobs which didnt incurr any expenses, the profit is supposed to equal the sales but instead it is coming up blank. What can i do to get the calculations right? I am trying to avoid entering Zero manually into the records coz it'll be tedious (theyre are simply thousands of transactions) and it will take up memory unnecessarily right?
Nov 1 '06 #1
6 1600
NeoPa
32,556 Expert Mod 16PB
Without the code you've used to check, I can only guess that joins between tables need looking at.
Perhaps you use an INNER JOIN somewhere (against the Expenses table) which should really be a LEFT JOIN.
Nov 1 '06 #2
Used the wizard to create this :
Expand|Select|Wrap|Line Numbers
  1. SELECT SALES.DATE, SALES.[INVOICE NUMBER], CLIENTS.NAME, SALES.SUBTOTAL AS INCOME, [EXPENSE TOTALS PER INVOICE].[Sum Of TOTAL (KES)] AS EXPENSES, SALES!SUBTOTAL-[JOB EXPENSES]![TOTAL (KES)] AS PROFITS
  2. FROM (CLIENTS INNER JOIN SALES ON CLIENTS.NAME = SALES.CLIENT) LEFT JOIN ([EXPENSE TOTALS PER INVOICE] RIGHT JOIN [JOB EXPENSES] ON [EXPENSE TOTALS PER INVOICE].[INVOICE #] = [JOB EXPENSES].[INVOICE #]) ON SALES.[INVOICE NUMBER] = [JOB EXPENSES].[INVOICE #]
  3. GROUP BY SALES.DATE, SALES.[INVOICE NUMBER], CLIENTS.NAME, SALES.SUBTOTAL, [EXPENSE TOTALS PER INVOICE].[Sum Of TOTAL (KES)], SALES!SUBTOTAL-[JOB EXPENSES]![TOTAL (KES)]
  4. ORDER BY SALES.DATE;
  5.  
Nov 1 '06 #3
Heres the code so you can help me correct it:
Expand|Select|Wrap|Line Numbers
  1. SELECT SALES.DATE, SALES.[INVOICE NUMBER], CLIENTS.NAME, SALES.SUBTOTAL AS INCOME, [EXPENSE TOTALS PER INVOICE].[Sum Of TOTAL (KES)] AS EXPENSES, SALES!SUBTOTAL-[JOB EXPENSES]![TOTAL (KES)] AS PROFITS
  2. FROM (CLIENTS INNER JOIN SALES ON CLIENTS.NAME = SALES.CLIENT) LEFT JOIN ([EXPENSE TOTALS PER INVOICE] RIGHT JOIN [JOB EXPENSES] ON [EXPENSE TOTALS PER INVOICE].[INVOICE #] = [JOB EXPENSES].[INVOICE #]) ON SALES.[INVOICE NUMBER] = [JOB EXPENSES].[INVOICE #]
  3. GROUP BY SALES.DATE, SALES.[INVOICE NUMBER], CLIENTS.NAME, SALES.SUBTOTAL, [EXPENSE TOTALS PER INVOICE].[Sum Of TOTAL (KES)], SALES!SUBTOTAL-[JOB EXPENSES]![TOTAL (KES)]
  4. ORDER BY SALES.DATE;
  5.  
Nov 1 '06 #4
NeoPa
32,556 Expert Mod 16PB
Mamusonda,

Unfortunately, there are more tables in here that you haven't included in any explanation.
I'm gussing you aren't able to strip down the unnecessary code in this SQL so you'd better explain the table structure more fully instead - particularly how [EXPENSE TOTALS PER INVOICE] and [JOB EXPENSES] fit in.

Before you do all that though, try this (I may have had lucky guesses as to what's going on) :-
Expand|Select|Wrap|Line Numbers
  1. SELECT SALES.DATE, SALES.[INVOICE NUMBER],
  2.     CLIENTS.NAME, SALES.SUBTOTAL AS INCOME,
  3.     [EXPENSE TOTALS PER INVOICE].[Sum Of TOTAL (KES)] AS EXPENSES,
  4.     SALES!SUBTOTAL-[JOB EXPENSES]![TOTAL (KES)] AS PROFITS
  5. FROM (CLIENTS INNER JOIN SALES ON CLIENTS.NAME = SALES.CLIENT) 
  6.     LEFT JOIN ([EXPENSE TOTALS PER INVOICE] LEFT JOIN [JOB EXPENSES] ON 
  7.     [EXPENSE TOTALS PER INVOICE].[INVOICE #] = [JOB EXPENSES].[INVOICE #]) ON
  8.     SALES.[INVOICE NUMBER] = [JOB EXPENSES].[INVOICE #]
  9. GROUP BY SALES.DATE, SALES.[INVOICE NUMBER], CLIENTS.NAME,
  10.     SALES.SUBTOTAL, [EXPENSE TOTALS PER INVOICE].[Sum Of TOTAL (KES)],
  11.     SALES!SUBTOTAL-[JOB EXPENSES]![TOTAL (KES)]
  12. ORDER BY SALES.DATE;
Nov 1 '06 #5
VALIS
21
I am pretty new to SQL and database concepts. Heres my problem: I have a sales info. table and an expenses info table with invoice number being the field relating the two tables (1:N). The sales table has details about each job done for a specific client, ie. a breakdown of the income recieved from each particular job. Expenses Info. table has several expenses incurred for each job, that is to say, some jobs done require outsourcing which creates a job-specific expense. Some invoices have 1 or 2 expenses related to them while most have none because most of the work is done in-house. Now, i have created a query to calculate the profits realised per job but it is only calculating profits for the jobs with an expense attached to it. For the other jobs which didnt incurr any expenses, the profit is supposed to equal the sales but instead it is coming up blank. What can i do to get the calculations right? I am trying to avoid entering Zero manually into the records coz it'll be tedious (theyre are simply thousands of transactions) and it will take up memory unnecessarily right?
Does your query contain a formula driven field? I.E [PROFIT]:[SALES]-[EXPENSES]
If the value returned from the expenses field is null the profit result will be null.
Work around this by re-writing the formula
[PROFIT]:[SALES]-IIF([ISNULL([EXPENSES]),0,[EXPENSES])
This should return values for all entries
Nov 1 '06 #6
Thanx everyone for the help. Tried using the IIF statement suggestion although it was giving me errors. However, finally managed to get it working by rearranging the statement this way:

PROFITS: [SALES]![SUBTOTAL]-IIf(([EXPENSE TOTALS PER INVOICE]![Sum Of TOTAL (KES)]) Is Null,0,([EXPENSE TOTALS PER INVOICE]![Sum Of TOTAL (KES)]))
Nov 2 '06 #7

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

Similar topics

10
by: Irlan agous | last post by:
$query = "update reactie,form set 'reactie.persemail' = 'form.email' where 'reactie.persid' = 'form.id' AND reactie.persemail='' "; How can i get this query to work? Irlan
20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
6
by: paii | last post by:
I have a table that stores job milestone dates. The 2 milestones I am interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18. All jobs have TypeID 1 only some jobs have TypeID 18....
3
by: Eagle | last post by:
Hi all, This one's drivin' me nuts. Any help would be appreciated. (Access 2000). I have 3 tables: tblTools: having the basic data of a tool and a field (txt) showing yes or no with regard to...
3
by: cover | last post by:
I have a table with 50 fields that receive input depending on whether that input came in from a 'shaker' form or a 'conveyor' form. Input from the 'conveyor' form might populate 25 fields while...
3
by: faceman28208 | last post by:
Over the past few years I have consulted on six large projects that all independently arrived at the same moronic design desision: The use of SQL query classes. No, I don't mean a class...
0
by: Chuck36963 | last post by:
Hi all, I've been working on a listing problem and I can't figure out how to work it out. I have looked far and wide on the web to find answers, but I'd like other peoples input on my project in...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
2
by: sara | last post by:
I am helping a non-profit track their elder clients and care given to the clients. The organization would like a report that shows various info on ALL clients, such as: # in each town, # and...
20
by: exipnakias | last post by:
Hello Guys. In a form I created a listbox which looks up the values of a table. I want: 1) ..to create a query where a parameter will be needed in order to be loaded. But I do not want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.