473,406 Members | 2,847 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,406 software developers and data experts.

Getting Unique values with GROUP BY

Hi

I'm trying to create a view containing 3 fields, all from the same table.

Here's what I have:

Expand|Select|Wrap|Line Numbers
  1. SELECT MAX(dateField), field2, field3
  2. FROM table1
  3. GROUP BY field2, field3
Basically I want the highest dateField for each field2, and then just add the corresponding field3 next to it. But this query doesnt give me unique values for field2

If I leave the field3 out of the query as follows:
Expand|Select|Wrap|Line Numbers
  1. SELECT MAX(dateField), field2
  2. FROM table1
  3. GROUP BY field2
then I get unique field2 values, but i dont have the corresponding field3 value.

How should I rewrite this query to achieve what i'm trying to?

Thanks
Jan 22 '08 #1
8 3005
ck9663
2,878 Expert 2GB
Hi

I'm trying to create a view containing 3 fields, all from the same table.

Here's what I have:

Expand|Select|Wrap|Line Numbers
  1. SELECT MAX(dateField), field2, field3
  2. FROM table1
  3. GROUP BY field2, field3
Basically I want the highest dateField for each field2, and then just add the corresponding field3 next to it. But this query doesnt give me unique values for field2

If I leave the field3 out of the query as follows:
Expand|Select|Wrap|Line Numbers
  1. SELECT MAX(dateField), field2
  2. FROM table1
  3. GROUP BY field2
then I get unique field2 values, but i dont have the corresponding field3 value.

How should I rewrite this query to achieve what i'm trying to?

Thanks

will this work:

Expand|Select|Wrap|Line Numbers
  1. select a.maxdatefield, table1.field2, table1.field3 from
  2. (SELECT MAX(dateField) as maxdatefield, field2
  3. FROM table1
  4. GROUP BY field2) a
  5. inner join table1 on a.field2 = table1.field2 
-- ck
Jan 22 '08 #2
will this work:

Expand|Select|Wrap|Line Numbers
  1. select a.maxdatefield, table1.field2, table1.field3 from
  2. (SELECT MAX(dateField) as maxdatefield, field2
  3. FROM table1
  4. GROUP BY field2) a
  5. inner join table1 on a.field2 = table1.field2 
-- ck
No it doesn't work.
I get way too many results.
Jan 23 '08 #3
ck9663
2,878 Expert 2GB
No it doesn't work.
I get way too many results.

would you mind posting the name of these fields?

-- CK
Jan 23 '08 #4
would you mind posting the name of these fields?

-- CK
Yea sure.

They are:
period (datetime), personnel_number (nvarchar), ppaygrade(nvarchar)

The table serves as records of updates. I want to retrieve the latest (period) update for each personnel_number, with the updated ppaygrade value for that period
Jan 23 '08 #5
ck9663
2,878 Expert 2GB
Yea sure.

They are:
period (datetime), personnel_number (nvarchar), ppaygrade(nvarchar)

The table serves as records of updates. I want to retrieve the latest (period) update for each personnel_number, with the updated ppaygrade value for that period
requirement: personnel_number + period should be unique. otherwise, which one would you choose?

try:

Expand|Select|Wrap|Line Numbers
  1. select a.personal_number, a.period, b.ppaygrade
  2. (select personnel_number, max(period) as period from mytable group by personnel_number) A inner join
  3. mytable B on A.personnel_number = B.personnel_number and A.period = B.Period
  4.  
-- CK
Jan 23 '08 #6
requirement: personnel_number + period should be unique. otherwise, which one would you choose?

try:

Expand|Select|Wrap|Line Numbers
  1. select a.personal_number, a.period, b.ppaygrade
  2. (select personnel_number, max(period) as period from mytable group by personnel_number) A inner join
  3. mytable B on A.personnel_number = B.personnel_number and A.period = B.Period
  4.  
-- CK
Okay I see what you mean.

I tried your way above and got 6888 records as opposed to the 6420 unique personnel_numbers in the table.

Just as a test, if i do the following:

Expand|Select|Wrap|Line Numbers
  1. Select period, personnel_number FROM myTable 
  2. GROUP BY period, personnel_number
And I get fewer results than when i simply say:
Expand|Select|Wrap|Line Numbers
  1. Select period, personnel_number FROM myTable 
then it means I have duplicates for some of the period/personnel_number combinations, am I right?
Jan 23 '08 #7
ck9663
2,878 Expert 2GB
Okay I see what you mean.

I tried your way above and got 6888 records as opposed to the 6420 unique personnel_numbers in the table.

Just as a test, if i do the following:

Expand|Select|Wrap|Line Numbers
  1. Select period, personnel_number FROM myTable 
  2. GROUP BY period, personnel_number
And I get fewer results than when i simply say:
Expand|Select|Wrap|Line Numbers
  1. Select period, personnel_number FROM myTable 
then it means I have duplicates for some of the period/personnel_number combinations, am I right?

yep. that means you have duplicates...

this:

Expand|Select|Wrap|Line Numbers
  1. Select period, personnel_number, count(*) FROM myTable 
  2. GROUP BY period, personnel_number having count(*) > 1
will show you what are those duplicates and how many are there ...

-- ck
Jan 23 '08 #8
yep. that means you have duplicates...

this:

Expand|Select|Wrap|Line Numbers
  1. Select period, personnel_number, count(*) FROM myTable 
  2. GROUP BY period, personnel_number having count(*) > 1
will show you what are those duplicates and how many are there ...

-- ck
Ok thanks alot for the help.

Im off trying to create the compound query to eliminate these duplicates

cya
Jan 23 '08 #9

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

Similar topics

2
by: grist2mill | last post by:
I have a table 'Group2Operation' that stores many to many relations between the 'Group' table and the 'Operation' table (each group is has permission to perform one or more of the available...
4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
4
by: Sean Shanny | last post by:
To all, Running into an out of memory error on our data warehouse server. This occurs only with our data from the 'September' section of a large fact table. The exact same query running over...
9
by: Robert Mago | last post by:
Is there a way to create a 10 characthers or less, alph-numeric string which is unique. I can't use the guid since its longer then 10 characthers. Also i cannot use a random number, since being...
4
by: jim | last post by:
I've been searching and trying out SQL statements for hours and I'm still having a problem. I'm querying 3 tables... and I only want the first unique record returned. Currently, the data...
11
by: sqlservernewbie | last post by:
Hi Everyone, Here is a theoretical, and definition question for you. In databases, we have: Relation a table with columns and rows
6
by: shira | last post by:
Hi, Looking to see if someone might have an explanation for this behavior. Is it a bug? Corruption? I have been able to reproduce the problem with only 2 rows and 1 field. Here is the table:...
4
bugboy
by: bugboy | last post by:
I'm inserting a new word into table 'w' and a definition into table 'c' which are linked in table 's' which is the relation table for the many to many relationship between 'w' and 'c'. I've been...
2
by: srusskinyon | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
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: 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...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.