Hi
I'm trying to create a view containing 3 fields, all from the same table.
Here's what I have: - SELECT MAX(dateField), field2, field3
-
FROM table1
-
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: - SELECT MAX(dateField), field2
-
FROM table1
-
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
8 3005
Hi
I'm trying to create a view containing 3 fields, all from the same table.
Here's what I have: - SELECT MAX(dateField), field2, field3
-
FROM table1
-
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: - SELECT MAX(dateField), field2
-
FROM table1
-
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: - select a.maxdatefield, table1.field2, table1.field3 from
-
(SELECT MAX(dateField) as maxdatefield, field2
-
FROM table1
-
GROUP BY field2) a
-
inner join table1 on a.field2 = table1.field2
-- ck
will this work: - select a.maxdatefield, table1.field2, table1.field3 from
-
(SELECT MAX(dateField) as maxdatefield, field2
-
FROM table1
-
GROUP BY field2) a
-
inner join table1 on a.field2 = table1.field2
-- ck
No it doesn't work.
I get way too many results.
No it doesn't work.
I get way too many results.
would you mind posting the name of these fields?
-- CK
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
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: - select a.personal_number, a.period, b.ppaygrade
-
(select personnel_number, max(period) as period from mytable group by personnel_number) A inner join
-
mytable B on A.personnel_number = B.personnel_number and A.period = B.Period
-
-- CK
requirement: personnel_number + period should be unique. otherwise, which one would you choose?
try: - select a.personal_number, a.period, b.ppaygrade
-
(select personnel_number, max(period) as period from mytable group by personnel_number) A inner join
-
mytable B on A.personnel_number = B.personnel_number and A.period = B.Period
-
-- 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: - Select period, personnel_number FROM myTable
-
GROUP BY period, personnel_number
And I get fewer results than when i simply say: - Select period, personnel_number FROM myTable
then it means I have duplicates for some of the period/personnel_number combinations, am I right?
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: - Select period, personnel_number FROM myTable
-
GROUP BY period, personnel_number
And I get fewer results than when i simply say: - 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: - Select period, personnel_number, count(*) FROM myTable
-
GROUP BY period, personnel_number having count(*) > 1
will show you what are those duplicates and how many are there ...
-- ck
yep. that means you have duplicates...
this: - Select period, personnel_number, count(*) FROM myTable
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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:...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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: 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,...
|
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: 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...
|
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: 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...
| |