473,406 Members | 2,620 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.

Find Unique Values ONE Column - Access SQL

This forum has been so helpful last time, thought I would give it another try!

Have gotten to the point where I can't see the forest for the trees as I have the feeling the solution is quite simple!

I have a table with the following columns: job, name of the workmen, days of the week, rate, adj, total and comments.

To give an idea of how, a part of, the table looks like, please see below:
Expand|Select|Wrap|Line Numbers
  1. Job   Workman
  2. 11    11 - PROJECT NAME
  3. 11    Workman 1
  4. 11    Workman 2
  5. 11    Workman 3
  6. 12    12 - PROJECT NAME
  7. 13    13 - PROJECT NAME
  8. 13    Workman 4
  9. 14    14 - PROJECT NAME
  10. 14    Workman 5
  11. 14    Workman 6
  12. 14    Workman 7
  13. 15    15 - PROJECT NAME
Now I am trying to delete the following records:
Expand|Select|Wrap|Line Numbers
  1. 12 12 - PROJECT NAME
  2. 15 15 - PROJECT NAME
Basically when there are no names under a project, it means no one has worked there and we don't need to see them.

The code I have at the moment is:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT [CISPAYE].Job 
  2. FROM [CISPAYE]
  3. GROUP BY Job
(I use this code to select the required records and then turn it into a delete query).

However, this just returns only ONE column with ALL distinct values (e.g. 11, 12, 13, 14, 15)

I would like to keep the fields when their value appears more than once basically (PLUS the other columns weekdays, rate, adj, etc.). I hope this makes sense!!

If anyone has any suggestions that would be greatly appreciated!

Many thanks,
emperial
Aug 6 '14 #1

✓ answered by NeoPa

Everything all the others have said about normalisation.

For your delete query, and this is certainly going to blow up in your face later as next week you will have similar records and the count will only ever be =1 again for jobs that have never had any work done on them, try the following simpler approach :
Expand|Select|Wrap|Line Numbers
  1. DELETE
  2. FROM   [Example]
  3. WHERE  ([Job] In(SELECT   [Job]
  4.                  FROM     [Example]
  5.                  GROUP BY [Job]
  6.                  HAVING   (Count(*)=1)))
NB. It will blow up for you whether you use this approach or any other as you have based the logic on a fallacy. The fallacy is that logic that works for a single week will continue to work when you have multiple weeks' worth of data in the table.

12 2887
twinnyfo
3,653 Expert Mod 2GB
First, your Table is definitely not normalized. I don't see how you can have a table with the name of the job and name of the workmen in the same column. I can't advise on this until you square away your tables first, which is my best advice.

Please see this link for advice on how to normalize your tables:

Database Normalization
Aug 6 '14 #2
Hi Twinnyfo,

Thanks for helping me out again!

Sorry if I didn't make it clear, the job number and workmen are two separate columns (I did try to insert some 'tabs' in the example table, but they got deleted after posting).

Does this help at all?
Aug 6 '14 #3
Please find attached a screenshot of the database and the records I would like to have deleted

Attached Images
File Type: jpg Example.jpg (30.2 KB, 700 views)
Aug 6 '14 #4
zmbd
5,501 Expert Mod 4TB
Hello,
First start with a select query to return only those records you want effected.
In this case it appears that you want
Expand|Select|Wrap|Line Numbers
  1. [job] = 12 or 15 
  2.    AND
  3.       [Workman] = "12 - PROJECT NAME" 
  4.          OR "15 - PROJECT NAME"
Try that first...

Once you have your select query working then convert to a delete action query.
Aug 6 '14 #5
Hi ZMBD,

Thank you very much for the above.

It does work indeed!

However, as the projects keep changing, say, at the moment no one is working at job "12" and "15", but next week it might be "14" and "13" that I would need to delete. So is there a way for Access to check whether a number occurs only once, and if so, deletes that record from the table?

Thank you very much again (also for moderating my post ^^)
Aug 6 '14 #6
zmbd
5,501 Expert Mod 4TB
Hmm,

Go to your select query,
Drag [Job] down twice to the table.
Click on the totals button
Set the first [JOB] to group by
set the second [JOB] to count
set the condition to "=1"

On the dataset in the OP you should only see: 12 and 15
Expand|Select|Wrap|Line Numbers
  1. [JOB]
  2.   12
  3.   15
so if in the OP the entries on lines 3 - 5 were deleted then you would have:
Expand|Select|Wrap|Line Numbers
  1. [JOB]
  2.   11
  3.   12
  4.   15
BACK UP YOUR DATABASE!
You can now convert to a delete query.

The issue here is that you shouldn't be doing this at all... your database doesn't appear to be normalized.
Aug 6 '14 #7
Hi ZMBD,

Thanks very much for the suggestion! Am going to give this a try.

Unfortunately I am not the end user which makes it quite difficult (and frustrating!) to set up a normalized db.

Thanks again and I'll let you know how I get on.

@Twinnyfo, apologies, I ready your first reply wrong. I thought it said 'job number and name of the workmen'. I do understand it's unfortunately not normalized but if I get this to work the db does everything it should be doing.
Aug 6 '14 #8
Hi all,

Just in case anyone is interested, this is probably against all rules, but I have used the following steps to get what I wanted:
- Used Job Count (and Group) to show records that would need to be deleted
- Appended these jobnumbers to a table with an Append query
- Used 'Find Unmatched Query Wizard' (so all records that didn't match the jobnumbers from the appended table would show)
- Saved as a query

@ZMDB, the Count worked in the query, however I couldn't turn it into a Delete query, hence the above steps. Probably wouldn't have been unable to figure it out if it wasn't for your suggestion. Thanks a bunch!
Aug 6 '14 #9
NeoPa
32,556 Expert Mod 16PB
Everything all the others have said about normalisation.

For your delete query, and this is certainly going to blow up in your face later as next week you will have similar records and the count will only ever be =1 again for jobs that have never had any work done on them, try the following simpler approach :
Expand|Select|Wrap|Line Numbers
  1. DELETE
  2. FROM   [Example]
  3. WHERE  ([Job] In(SELECT   [Job]
  4.                  FROM     [Example]
  5.                  GROUP BY [Job]
  6.                  HAVING   (Count(*)=1)))
NB. It will blow up for you whether you use this approach or any other as you have based the logic on a fallacy. The fallacy is that logic that works for a single week will continue to work when you have multiple weeks' worth of data in the table.
Aug 6 '14 #10
zmbd
5,501 Expert Mod 4TB
OK, should have taken this one step further...

Lets take simple table
[Table1]![pk],[Table1]![Field1]

Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.Field1
  2. FROM Table1
  3. GROUP BY Table1.Field1
  4. HAVING (((Count(Table1.Field1))=1));
Ok, what this does is the same thing I did in the prior post.
However, as you discovered this doesn't directly convert to the delete.

With the append query you almost have it...
What I had hoped was that you would have noticed when creating the agregate query that you could then create a second select query and use the agregate as a conditional to feed to the select query...

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.    Table1.Field1 AS F1
  3. FROM 
  4.    Table1
  5. WHERE 
  6.    (((Table1.Field1) 
  7.       In 
  8.       (SELECT [Field1] 
  9.        FROM [Table1] 
  10.          As Tmp 
  11.        GROUP BY [Field1] 
  12.        HAVING Count(*)=1 )));
This will then convert to your delete query...

I'll leave it to you to replace finish the SQL...

Note: This is only a short term solution... you need to normalize the database... enduser be flogged!

(Rabbit will most likely have a much more elegant solution, He's my SQL hero - I'm often a bit of an SQL hack!)
Aug 6 '14 #11
Thank you both for the suggestions!

That is indeed much more simple and have got it working now.

I used NeoPa's code.

I have only started using Access about 2 months ago, so I'm afraid I know what it needs to do but not sure what the best way (read: according to db rules!) is of getting it done!

Haha thanks ZMBD for your comment regarding the endusers. I'll make sure to let them know ;)

Thank you both very much for your time looking into this, very happy Bytes.com user once again!!
Aug 7 '14 #12
NeoPa
32,556 Expert Mod 16PB
Emperial:
very happy Bytes.com user once again!!
That's what we like to see :-)
Aug 7 '14 #13

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

Similar topics

5
by: jdwyer05 | last post by:
Hello, I am trying to populate a combo box with only unique values. Currently I am using an access database and VB6 Enterprise. The program populates the combo box fine however, there are several...
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:...
9
by: Dawid Zolkiewicz | last post by:
Hello I'm new here, so at the beginning I'd like to say hello for everybody. First I'll describe my problem. There is db2 database :) with about 0.5 mln people. Every person has three...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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.