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

SQL query to return 1 record from multiple similar records

Hi,

I’ve setup a table which contains records with multiple duplicated time fields (tblTime_Slots)
i.e.
ID Time_Slot
1 08:00
2 08:00
3 09:00
4 09:00
etc…

I want to bring back the Time_Slots in a combo box, but rather than bringing back each Time_slot twice I want to write an SQL query that will only display each Time_Slot once.
i.e.
ID Time_Slot
1 08:00
3 09:00

Therefore if the user allocates 08:00 to a booking then the next time the come to allocate a time to a new booking it returns the following in the combo box: -

ID Time_Slot
2 08:00
3 09:00

With the 08:00 Time_Slot now showing the record with ID 2 rather than ID 1 (which has been allocated to the previous booking).

If the time slot 08:00 with ID 2 was to then be allocated to a booking then the next time the query was run then the combo box wouldn’t show a 08:00 Time_Slot as they have all been allocated to previous bookings.

I’ve tried writing the following query: -
Expand|Select|Wrap|Line Numbers
  1. SELECT tbltime.ID, tbltime.Time
  2. FROM tbltime
  3. WHERE tbltime.Time in(SELECT DISTINCT tbltime.Time
  4. FROM tblTime
  5. GROUP BY tbltime.Time
  6. HAVING count(*) <=2);
  7.  
But it simply returns all the Time_Slots even the duplicated ones twice

Could the answer be to instead possibly return the MIN ID for the DISTINCT Time_Slots? Therefore for the 08:00 Time_Slot it would first of all return ID 1, then after that had been allocated to a booking it would return ID 2 for the 08:00 Time_Slot, then after that has been allocated to a booking it wouldn’t show a 08:00 Time_Slot.

Any help with this would be much appreciated as I’ve spent ages trying to figure it out. I appreciate that the query is in two parts, the first which displays only one of each of the repeated Time_Slot, and the next part of the query only returning Time_Slots that haven’t previously been allocated to a booking. I’m having that much trouble with the first part of the query I haven’t even got onto attempting the second part of the query.

Thanks
May 28 '08 #1
7 3720
JustJim
407 Expert 256MB
Hi Wildster,
You have got yourself into a tangle haven't you? The first warning bell rings as soon as you say that you have multiple duplicated data in a table. This is a sure indication that your database is not close to being 'Normalized'. I suggest that you take a step back for a moment (it will save you a lot of time in the future) and have a read of Mary's bestselling, classic article here . I know that it is quite involved, but have a go and see how you get on.

We're willing to help, but it's a lot easier if we're working from a solid foundation.

Jim
May 29 '08 #2
NeoPa
32,556 Expert Mod 16PB
You are going to have a lot of trouble with this one as you don't seem to have the question sorted out properly in your mind yet.

What you are ACTUALLY looking for (if my mind-reading hat is working properly) is to show ALL time slots which are not fully used up, and with each slot you want to show the first available booking. There are two bookings (numbered 1 & 2) per time slot.

If this is a fair representation of what you really want and you would like some help getting there then let us know.
May 29 '08 #3
JustJim
407 Expert 256MB
No, no; there's no need for thanks. It's a pleasure to help you.

Jim
Jun 18 '08 #4
NeoPa
32,556 Expert Mod 16PB
I'm afraid that this is something you'll have to get used to Jim when working with the general public (and even members of Bytes as that is basically open to all anyway).

Although a good proportion are well mannered and may even be suitable to date our daughters, a sizeable minority have never been exposed to good manners for various reasons.

At the end of the day, we simply have to deal with them from time-to-time, but they are stuck with the situation (and the results thereof) for their whole lives (or until they decide to change).

This may be little consolation, but at least we know they suffer from it much more than we ever will.

PS. It's good to see you're still sticking around :)
Jun 18 '08 #5
JustJim
407 Expert 256MB
Hi NeoPa

I'm not losing any sleep over it, I was just cleaning up my subscriptions page and thought I'd give some of them a last chance before I deleted them.

Jim
Jun 19 '08 #6
NeoPa
32,556 Expert Mod 16PB
Clean up subscriptions! What an idea!

I have about 4,000, and know the most recent posts are always on the front page (I make sure to include 100 per page mind you). I think they drop off automatically after a certain period anyway (Check account options for details).
Jun 19 '08 #7
JustJim
407 Expert 256MB
Clean up subscriptions! What an idea!

I have about 4,000, and know the most recent posts are always on the front page (I make sure to include 100 per page mind you). I think they drop off automatically after a certain period anyway (Check account options for details).
Yeah mate but not all of us do nearly 16 posts per day! For me housekeeping is a little feather duster around the edges (with the aforementioned grumpy sniping), for you it must involve a bulldozer!

You do great work though - keep it up.

Jim
ETA Wildster, come back - we can help!
Jun 19 '08 #8

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

Similar topics

2
by: Hammy Hammy | last post by:
Hi all, I have an invoice table query that returns 10 records. ie. there are 10 invoices. When I try to join a subjects table to retrieve the subject name associated with an invoice it returns...
3
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record...
3
by: Carlos Nunes-Ueno | last post by:
Hello all, I'm building a database for a high school academic convention and the big task for this DB is to sort out the contest winners. Here's the tables (slightly simplified): ...
8
by: Bill | last post by:
Hello out there; This may be a challenge but I'm certain it's possible but I can't seem to figure out how. I have a table that has several date fields, e.g., Date1, Date2, Date3, Date4 ......
2
by: Venk | last post by:
hi all, I saw one reply to arun on the subject "Dynamic Query in Ms-Access" by one Mr Rick I found it very useful. Now to extend this solution forward I have the following situation. I...
3
by: shorti | last post by:
db2 v 8.2 on AIX 5.3 I will try to explain as brief as I can what it is I need. I am building a function that will be called multiple times where I will need to return x amount of records each...
2
by: jennk | last post by:
i am working in Access 97, our database tables are linked from ODBCsqlsvr (not even sure what that means). i have a table where each record has a unique customer and their order information. there...
1
by: tjm0713 | last post by:
Not sure this can be done but here is my current situation. I have a table containing millions of records. Each record has 28 fields. One of the fields I am trying to return the single record for...
13
by: angi35 | last post by:
Hi - working in Access 2000... my goal is to combine fields from two tables in a query: Table 1 has ItemNumber and ItemDescription. There's only one record per ItemNumber. Table 2 has ItemAlias....
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.