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

Query sorting by date

374 256MB
Hi all,

Something which I feel would have been relatively easy seems to not be coming out the way I wish.

I am using the following sql to query how many nonconformances have occured by month and at what cost.

Expand|Select|Wrap|Line Numbers
  1. SELECT Count(tbllog.NCC_ID) AS NCCS, Sum(tblCosts.CostFig) AS SumOfCostFig, Format([DteOccur],"mmmm" & "yyyy") AS TimeLine
  2. FROM tbllog LEFT JOIN tblCosts ON tbllog.NCC_ID = tblCosts.NCC_ID
  3. GROUP BY Format([DteOccur],"mmmm" & "yyyy")
  4. ORDER BY Format([DteOccur],"mmmm" & "yyyy");
  5.  
I am trying to order the dates by oldest to newest. However I get the following output (I threw in feb1986 just to prove to myself it was wrong):
Expand|Select|Wrap|Line Numbers
  1. NCCS    SumOfCostFig    TimeLine
  2. 5    £33,234.00    August2010
  3. 10        February1986
  4. 1    £30,000.00    February2010
  5. 4    £3,790.00    July2010
  6. 7    £985.00    June2010
  7. 1    £50,000.00    March2010
  8. 2    £5,700.00    May2010
As you can see the timeline is not in the correct order. I can see the sql doesnt look quite right as there isn't an actual order to say go ascending/descending so I assume it is how I have tried to format DteOccur which is a Short Date to get it to month/year.

Anyone able to clarify how this should be done correctly.

Thanks,

Chris
Aug 11 '10 #1
1 1729
munkee
374 256MB
I have got further with this and now producing:


Expand|Select|Wrap|Line Numbers
  1. SumOfCostFig    Dateofevent    CountOfNCC_ID
  2. £30,000.00    February 2010    1
  3. £50,000.00    March 2010    1
  4. £5,700.00    May 2010    2
  5. £985.00    June 2010    7
  6. £3,790.00    July 2010    4
  7. £33,234.00    August 2010    5
However if I remove the "Count" on NCC_ID I get the true data:
Expand|Select|Wrap|Line Numbers
  1. SumOfCostFig    Dateofevent    NCC_ID
  2. £30,000.00    February 2010    585
  3. £50,000.00    March 2010    586
  4. £5,700.00    May 2010    472
  5. £125.00    June 2010    522
  6. £400.00    June 2010    524
  7. £80.00    June 2010    525
  8. £40.00    June 2010    536
  9. £340.00    June 2010    580
  10. £1,260.00    July 2010    521
  11. £2,000.00    July 2010    578
  12. £30.00    July 2010    579
  13. £500.00    July 2010    602
  14.     August 2010    650
  15.     August 2010    656
  16. £33,234.00    August 2010    657
  17.     August 2010    670
As you can see in June shows 5 records to be added to the NCC ID Count. However 7 are shown in the first table when I do a Count.

I think this is because for some reason the count is counting the underlying cost fig table data. Where each ID number has multiple costs assigned to it. My query has summated these costs and grouped by them. However the query Count on NCC ID looks to be counting the ID values in the cost table.

How do I make my query count UNIQUE/DISTINCT as this will then only count the ID once.


Current sql:
Expand|Select|Wrap|Line Numbers
  1. SELECT Sum(tblCosts.CostFig) AS SumOfCostFig, Format([DteOccur],"mmmm yyyy") AS Dateofevent, Count(tbllog.NCC_ID) AS Test
  2. FROM tbllog LEFT JOIN tblCosts ON tbllog.NCC_ID = tblCosts.NCC_ID
  3. GROUP BY Format([DteOccur],"mmmm yyyy"), Month([DteOccur])
  4. ORDER BY Month([DteOccur]);
  5.  
Aug 11 '10 #2

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

Similar topics

7
by: vnl | last post by:
I'm trying to run a SQL query but can't find any records when trying to select a certain date. Here's the sql: SELECT field 1, field2, date_and_time, FROM table1 WHERE date_and_time =...
5
by: Dani | last post by:
Hi, I´m no superuser when it comes to MS Access. So I use MS Query to pull out info from an SQL database to Excel. I have a tabel containing different "titles" or "colums". One contains a date....
2
by: Julie Wardlow | last post by:
Help! I am calculating a future date using the DateAdd function in a query (the calculation also involves an IIf statement), and have managed to get this formula to produce the required result....
1
by: schmud | last post by:
i'm having trouble correctly sorting dates in my dataview. It will sort the column but it just sorts according to the month number. I need to display the data on my companies site with the most...
3
by: Scotter | last post by:
Hi, I've been trying to make a report that will show the deliverys to be made between 2 dates, or the deliverys to be made after a certian date, or the deliveries that were made before a certian...
4
Sandboxer
by: Sandboxer | last post by:
I want to be able to program Access to provide for me, by individual day, what my contract obligations are to my customers. Will Access recognize all the individual days in between a date range...
7
by: nhkam | last post by:
I am using MS Access 2007 I have a transaction table which holds all records with posting date on each of them. I used a make table query to find out the max date hold in the transaction table and...
0
by: Martin Ruprecht | last post by:
Hi, I do have a big problem: We are creating a reporting tool for logistic solutions using Crystal Reports. I actually am programming a history report. There are millions of lines in the...
1
by: Donna Wiley | last post by:
I have a date field that could be formatted m/d/yyyy, mm/d/yyyy, m/dd/yyyy, or mm/dd/yyyy. I need to be able to enter a start date and an end date in a parameter query, i.e. Between (date) and...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.