473,378 Members | 1,152 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,378 software developers and data experts.

Complicated Query by date

daniel aristidou
491 256MB
Hi guys,
i hope this question belongs here as i intend to work a solution in sql server which is my back end system rather than my front end system which is VB.

Im trying to create a query that performs a number of calculations on data and returns a table of workers and the amount they sold (in cash) between a certain period of time.

Well here are my relevant tables:

Worker 'Worker table holds direct information ie name & tel
Sale 'Holds information about which worker made the sale, and Which customer
SaleProduct 'This holds the information on which product and how many - it also contains the value of each individual product and how much discount

The operation therefore that needs to take place is:
For each worker:
Find the sales that they made between the dates specified (this is the problem)
Find the relevant SaleProduct for that sale
Multiply the amount of Products by the Price '(this is saved in the Saleproduct Table since prices change) And remove the discount
Add all of the totals up to get a total
Return a row with the workers details and the total sold.

Here is my query:
Expand|Select|Wrap|Line Numbers
  1. SELECT        Worker.WName, SUM((ProductSale.Amount * ProductSale.CRPrice) * ((100 - ProductSale.Discount) / 100)) AS SOLD, Worker.WMob, Worker.WID, 
  2.                          Worker.WTel
  3. FROM            Worker INNER JOIN
  4.                          Sale ON Worker.WID = Sale.WID INNER JOIN
  5.                          ProductSale ON Sale.SID = ProductSale.SID
  6. GROUP BY Worker.WName, Worker.WMob, Worker.WID, Worker.WTel
The field that needs to be filtered is Sale.SDate
Also the current query does not return workers that have not made any sales, is there any solution to this?

Any help is greatly appreciated
Jun 11 '09 #1
3 1604
ck9663
2,878 Expert 2GB
On your first question, read this. On your second question, use LEFT JOIN, not INNER.

Happy coding!


--- CK
Jun 11 '09 #2
daniel aristidou
491 256MB
Thanks CK Your help is really appreciated, The tips you gave me above helped me greatly.

But following up this question i have another problem i need to merge this table with a similar one. Here are the two codes
Expand|Select|Wrap|Line Numbers
  1. SELECT        Worker.WName, Worker.WMob, Worker.WID, Worker.WTel, SUM((ProductSale.Amount * ProductSale.CRPrice) * ((100 - ProductSale.Discount) / 100)) 
  2.                          AS SOLD
  3. FROM            Worker LEFT OUTER JOIN
  4.                          Sale ON Worker.WID = Sale.WID LEFT OUTER JOIN
  5.                          ProductSale ON Sale.SID = ProductSale.SID LEFT OUTER JOIN
  6.                          Reciept ON Worker.WID = Reciept.WID
  7. WHERE        (Sale.SDate BETWEEN @StartDate AND @EndDate)
  8. GROUP BY Worker.WName, Worker.WMob, Worker.WID, Worker.WTel
  9.  
  10.  
  11. And
  12. SELECT        Worker.WID, Worker.WName, Worker.WMob, Worker.WTel, SUM(Reciept.Amount) AS Collected
  13. FROM            Worker LEFT OUTER JOIN
  14.                          Reciept ON Worker.WID = Reciept.WID
  15. WHERE        (Reciept.RDATE BETWEEN @StartDate AND @EndDate)
  16. GROUP BY Worker.WID, Worker.WName, Worker.WMob, Worker.WTel
As you can see the only difference is the aggregate function and the Where statements (i know the where statements are not to blame because removing them produces the same results), however when i merge these two tables i get wrong results in the sums despite getting the correct results when they are run separately. I tried adding Distinct statements before the Reciept.Amount in the sum function but since multiplereciepts have the same amount paid it again resulted in wrong results.
This is my merged code
Expand|Select|Wrap|Line Numbers
  1. SELECT        Worker.WName, Worker.WMob, Worker.WID, Worker.WTel, SUM((ProductSale.Amount * ProductSale.CRPrice) * ((100 - ProductSale.Discount) / 100)) 
  2.                          AS SOLD, SUM(Reciept.Amount) AS Collected
  3. FROM            Worker LEFT OUTER JOIN
  4.                          Sale ON Worker.WID = Sale.WID LEFT OUTER JOIN
  5.                          ProductSale ON Sale.SID = ProductSale.SID LEFT OUTER JOIN
  6.                          Reciept ON Worker.WID = Reciept.WID
  7. WHERE        (Sale.SDate BETWEEN @StartDate AND @EndDate) AND (Reciept.RDATE BETWEEN @StartDate AND @EndDate)
  8. GROUP BY Worker.WName, Worker.WMob, Worker.WID, Worker.WTel, Sale.SDate
Thanks CK for the help :)
Jun 12 '09 #3
ck9663
2,878 Expert 2GB
I assume when you said "merge" you mean UNION or "concatenate" this two results.

1. Try to align your columns. On your first query, Name is the first column, on your second, it's ID. Align properly.

2. Use UNION ALL. UNION (without - ALL) filter out duplicates.

Happy coding!


--- CK
Jun 12 '09 #4

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

Similar topics

8
by: Adam Louis | last post by:
I would like help resolving this problem. I'm a novice who's been hired to query a hospital database and extract useful information, available to me only in a dynamically generated, downloadable...
5
by: Norma | last post by:
I am trying to make a query pull data from between the dates I enter in the parameter but also look back 'in time' to see where 2 other fields have null values, and only pull data into the query if...
1
by: longtim | last post by:
I have been having endless difficulty creating reports/queries that set any relevent parameters from controls in forms. I am creating an application under access 2003 but will target access...
5
by: ????? | last post by:
I have an access query which gets data from a number of different tables. Although the tables have primary key fields, the order in which the records are returned means that none of these are in...
2
by: tedhekman | last post by:
Hi there! I am pretty new to Access, have been loving learning it! I have a problem here I can't even begin to figure out. Here is what I need: Given 1 Date and 1 Store, retrieve the following...
11
by: Andy_Khosravi | last post by:
My problem: I'm having trouble with a query taking much too long to run; a query without any criteria evaluating only 650 records takes over 300 seconds to run (over the network. On local drive...
26
by: jshanman | last post by:
I am writing a timeline that uses Google Maps. I have a function that converts a date time to latitude coords. This function is used to draw the markers on the timeline. I need a reverse function...
0
by: Nick | last post by:
Hi, I have two tables Trade table and Cons table. Records are inserted in both the tables independent of each other. There are fields like Exc_Ref, Qty, Date in both the tables. I need to...
3
by: z.ghulam | last post by:
Hi, I am trying to create a query from a table which has an ID, Diary and Date fields. A particular ID may have several diary entries which all have their specific date. What I'm after is a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.