Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
first I'm using Access database and ole db conn.

I have table Booking :

+------------+--------------+
| no_booking | date_service |
+------------+--------------+
| AN02091701 | 02/09/2017 |
| AN02091702 | 02/09/2017 |
| AN02091703 | 02/09/2017 |
| AN02091703 | 02/09/2017 |
| AN03091701 | 03/09/2017 |
| AN03091702 | 03/09/2017 | ---? find AN03091702
| AN04091701 | 04/09/2017 |
| AN04091702 | 04/09/2017 |
| AN05091701 | 05/09/2017 |
| AN05091702 | 05/09/2017 |
| AN05091703 | 05/09/2017 |
| AN07091701 | 07/09/2017 |
+------------+--------------+

I want make bookingservice ..

i'm using date picker to select date as request member (exactly will be randomly)..

condition that i want --> in one day only have < 4 no_booking

next i try to find the date which have < 4 no_booking to create no_booking automatic.

assumtion that we don't now where the date have < 4 no_booking ...

how to find the top date that contain < 4 no_booking ( the date is 03/09/2017 because have 2 no_booking),

then select the lastindex of no_booking in 03/09/2017 ----> exactly AN03091702 . using fastest query ?

Hope I explain correctly,but sorry about my english ..

Thanks ....

What I have tried:

SQL
select no_booking,datebo from booking group by no_booking,datebo having count (datebo) < 3 
Posted
Updated 3-Sep-17 17:46pm
v6
Comments

1 solution

If you need to select only columns containing a specific value you must use a WHERE clause:
SQL
SELECT nobooking,datebo FROM booking WHERE nobooking='AN30081702' GROUP BY nobooking,datebo HAVING COUNT(datebo) < 3;

If the column name contains a space ("No Booking" instead of "nobooking") or a period, you have to enclose it by quotes or brackets. If you want to return all columns from a table, you can also use the * placeholder:
SQL
SELECT * FROM booking WHERE [No booking]='AN30081702' GROUP BY [No booking],[datebo.] HAVING COUNT([datebo.]) < 3;

As you can see it is a bad idea to uses spaces and dots in database table and column names. Avoid them (and SQL keywords too) as names if possible.
 
Share this answer
 
v2
Comments
Khabibb Mubarakk 30-Aug-17 19:11pm    
assumption that you don't how the no_boking,because will be so many data..
first select no booking where datebo having count < 3 :
so will be index like this :
--------------
+ No_booking +
--------------
+ AN30081701 + ---> this have date.bo (30/80/2017) top of date
+ AN30081702 + ---> (30/80/2017)
+ AN01091701 +
+ AN02091701 +
--------------
then select last index no_booking in the top of date
so will be select : AN30081702
I hope u understand my explanation.
thanks
Graeme_Grant 30-Aug-17 19:54pm    
Use a UNION ALL to join results from two queries: 1 for no bookings, one for bookings. See this solution from earlier today: Search for a single value in multiple tables[^] - the principle is the same.
Khabibb Mubarakk 3-Sep-17 23:54pm    
hay Grant ,,I want ask to you .
after i solved it using this query :
"select top 1 no_booking from booking where date_service in (select top 1 date_service from booking group by date_service having count (date_service) < 4) order by no_booking desc"

, i got new problem !
when the condition data like this :
+------------+--------------+
| no_booking | date_service |
+------------+--------------+
| AN02091701 | 02/09/2017 |
| AN02091702 | 02/09/2017 |
| AN02091703 | 02/09/2017 |
| AN02091704 | 02/09/2017 |
| AN03091701 | 03/09/2017 |
| AN03091702 | 03/09/2017 |
| AN03091703 | 03/09/2017 |
| AN03091704 | 03/09/2017 | --> date_service 04/09/2017 (is doesn't have no_booking)
| AN05091701 | 05/09/2017 |
| AN05091702 | 05/09/2017 |
| AN05091703 | 05/09/2017 |
| AN07091701 | 07/09/2017 |
+------------+--------------+
i know my query just select to AN05091703 on date 05/09/2017,and the next command i make next no_booking AN05091704 .
but i want too select or find date 04/09/2017,and then make new no_booking AN04091701 .
how to make correct query ?
Graeme_Grant 3-Sep-17 23:58pm    
Use a CASE Statement[^] for a custom field to identify if it has a booking or not.
Khabibb Mubarakk 4-Sep-17 0:02am    
but firstly i must find the date 04/09/2017 ???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900