473,396 Members | 1,864 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.

max() throwing up group by errors, please help :D

hello, i have rows of data with duplicates EXCEPT the id which is unique, i am try to select a row for each max date of each duplicate set: for example with the follwoing rows:

1----------08/10/2007----------Delivery----------Dorothy Haynes
2----------28/09/2007----------Delivery----------Dorothy Haynes
3----------08/10/2007----------Delivery----------Dorothy Kittrick
4----------28/09/2007----------Delivery----------Dorothy Kittrick
5----------01/10/2007----------Delivery----------Edith Gill
6----------08/10/2007----------Delivery----------Edith Gill
7----------28/09/2007----------Delivery----------Edith Gill

i need to return data like:

1----------08/10/2007----------Delivery----------Dorothy Haynes
3----------08/10/2007----------Delivery----------Dorothy Kittrick
6----------08/10/2007----------Delivery----------Edith Gill

cutting out duplicates using the latest date as the final result row.

i tried:

Expand|Select|Wrap|Line Numbers
  1. select vw_Requests.Request_ID, Pat_Fullname, vw_Requests.Request_Created 
  2. from vw_Requests
  3. WHERE vw_Requests.Request_Type_ID = 3 and vw_Requests.Shop_ID = 012 AND vw_Requests.Run_ID Is Null 
  4. group by vw_Requests.Pat_Fullname, vw_Requests.Request_Created
  5. having vw_Requests.Request_Created = max(vw_Requests.Request_Created)
  6. order by vw_Requests.Request_Created Desc
but i just get error:
Column 'vw_Requests.Request_ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
now i understand why the error occurs, but grouping by the id returns hundreds of rows and thus does not work, any ideas?

Best regards

HC
Oct 11 '07 #1
4 1360
iburyak
1,017 Expert 512MB
Try this:

Expand|Select|Wrap|Line Numbers
  1. select max(vw_Requests.Request_ID), Pat_Fullname, max(vw_Requests.Request_Created)
  2. from vw_Requests
  3. WHERE vw_Requests.Request_Type_ID = 3 and vw_Requests.Shop_ID = 012 AND vw_Requests.Run_ID Is Null 
  4. group by vw_Requests.Pat_Fullname
  5. order by vw_Requests.Request_Created Desc
Good Luck.
Oct 11 '07 #2
thankyou for your reply. However that has two issues,
  1. The order by clause throws up the error 'Column name 'vw_Requests.Request_Created' is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.'
  2. if i remove the order by clause the query executes, however the data returned is incorrect, as i will now explain:

note in the image below the data we are selecting from (in the excel book) and the actual selected data on the left, you will see it has not selected the row with the higher date?



any ideas?
Oct 12 '07 #3
UPDATE: whatever idiot wrote the view i am selecting from has declared the datetime field in the view as a varchar, so max() is maxing the string, not as data, which explains why is select the higher date based on the 2 at the beinging of the dd/mm/yyyy

So i'm guessing i will have to do max(cast(date as satetime) although the varchar only has dd/mm/yyyy so i may need to use convert().

best get reading up on my date convert codes :D
Oct 12 '07 #4
iburyak
1,017 Expert 512MB
You are right.
Just if you still want order by just do this:


Expand|Select|Wrap|Line Numbers
  1. Order by 3
Order by column positions....


Good Luck.
Oct 12 '07 #5

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

Similar topics

2
by: JaNE | last post by:
Maybe this isn't the most apropriate place to ask mysql related question, but I think it is close enought to most php users and I always got find answers on this group. well, I need to count how...
2
by: Eric | last post by:
please help to select these rows from these tables my tables are table1 table1Id groupId table2id price 1 1 1 10 2 1 3 1000 3 1 ...
3
by: Elvis | last post by:
Hi, i need to create a query on Access 2000 for select some records. Here is an example with records inserted on my two tables. TABLE ACQ: ID ARTICLE PERIOD DATE QTY RESERVE...
5
by: Bayla Frankl | last post by:
Hi all, I am a little stumped. I have a query I am trying to run to retrieve the last Progress Note record from the database for the current patient so that the therapists can see the last note...
3
by: MostlyH2O | last post by:
Hi Folks, I have a query that joins 3 tables. One of the tables (SalaryData) has data where there may be duplicate records with different dates. Of those duplicate records, I want the query to...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
8
by: nico3334 | last post by:
I have a database that has a Date column (1/1/2007) and a Data column that has numerical data. I am currently running a query to sum the Data column for a certain month and grouped by day (There is...
4
by: vincibleman | last post by:
Howdy all, Working my way into SQL from Access. Think I might have the hang of the basics, but would really appreciate a sanity check. The stored procedure listed below works, but I can't help...
1
by: jas2803 | last post by:
I have two tables, active clients and shipments. Shipments tables is like this client, date, amount the clients table is: client, state, city I use this to get the Max amount per client for a...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.