| Author |
Topic |
|
neutcomp
Posting Yak Master
111 Posts |
Posted - 2003-07-15 : 03:31:44
|
Hello I have a table tbl_availableid_houseyearweek1week2...week52id_house year week1 week2 week3 week434 2003 free free taken free34 2004 free free free free35 2003 taken taken free free Now I have a selection box where the can give the weeks when the want to go on a holliday. Lets say between week1 and week2. So I want as result id_house 34. And if I want between week 3 and week 4 I get as result also 34 and 35.ThanxxBjornEdited by - neutcomp on 07/15/2003 03:41:34 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-07-15 : 05:03:12
|
| declare @w1 int, @w2 int, @s varchar(1000)set @s=''set @w1=2 set @w2=7 --- start & end weekswhile @w1<=@w2beginset @s=@s+'week'+cast(@w1 as varchar(2))+'=''free'' and 'set @w1=@w1+1endset @s=left(@s, len(@s)-4)set @s='select id_house from tbl_available where '+@sexec (@s)- Vit |
 |
|
|
neutcomp
Posting Yak Master
111 Posts |
Posted - 2003-07-15 : 05:13:01
|
quote: declare @w1 int, @w2 int, @s varchar(1000)set @s=''set @w1=2 set @w2=7 --- start & end weekswhile @w1<=@w2beginset @s=@s+'week'+cast(@w1 as varchar(2))+'=''free'' and 'set @w1=@w1+1endset @s=left(@s, len(@s)-4)set @s='select id_house from tbl_available where '+@sexec (@s)- Vit
Oke is it possible that access does not understand this code?Its telling me:Invalid SQL statement: Expected 'Delete','Insert','Procedure','Select' or 'Update'And its selecting the "declare" in the SQL.ThanxxBjorn |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-07-15 : 05:29:59
|
| Mama mia!!It has to be done for pure Access?- Vit |
 |
|
|
neutcomp
Posting Yak Master
111 Posts |
Posted - 2003-07-15 : 05:32:47
|
Yes I work with VB its going to be an webbased search machine.I hope you now an other way. Is this query going to work with mysql?CyaBjorn |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-07-15 : 05:49:09
|
| I can't say nothing about MySQL, never saw it...Again, I can't guess where it is going to run?I can't produce an universal solution. :)- Vit |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-07-15 : 07:05:15
|
| Paste this function into a standard module:Function AvailableHouses(ByVal w1, ByVal w2, ByVal y)If Not (w1 = "" Or w2 = "" Or y = "") ThenDim s, ss, rs As DAO.Recordset, w11s = "": ss = ""w11 = CByte(w1)While w11 <= CByte(w2)s = s & "week" & w1 & "=" & Chr(34) & "free" & Chr(34) & " and "w11 = w11 + 1Wends = s & "year=" & ys = "select id_house from tbl_available where " & sSet rs = CurrentDb.OpenRecordset(s)While Not rs.EOFss = ss & rs(0) & ", "rs.MoveNextWendIf ss = "" ThenAvailableHouses = "No available houses at the time!"Elsess = Left(ss, Len(ss) - 2)AvailableHouses = ssEnd IfElseAvailableHouses = ""End IfEnd FunctionThen place on your form 4 textboxes, named:1) w1 - for start week2) w2 - for end week3) y - for year4) AnyName with DataSource property=AvailableHouses([Forms]![ff]![w1],[Forms]![ff]![w2],[Forms]![ff]![y])Replace here 'ff' with your form name. In this 4th textbox will be shownavailable houses IDs separated by comma.- Vit |
 |
|
|
neutcomp
Posting Yak Master
111 Posts |
Posted - 2003-07-15 : 07:16:24
|
Great I am going to try this!!CyaBjorn |
 |
|
|
neutcomp
Posting Yak Master
111 Posts |
Posted - 2003-07-15 : 08:21:00
|
It works Thanxxx, thanxx thanxxx  cyaBjorn |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-07-15 : 09:52:43
|
| Try normalization your database at some point, and queries like this become trivial.tbl_available---------id_houseWeek[Available?]primary key of (id_house, Week) and [Available?] can be a Yes/No checkbox.then, you can also store much more data about that particular id_house/week combination, like who has it booked or whatever else you want, which you can't do with your current table structure.and to query the table and make the results LOOK like you are storing them the old way, just use a cross-tab query.- Jeff |
 |
|
|
neutcomp
Posting Yak Master
111 Posts |
Posted - 2003-07-15 : 12:15:49
|
| Yes I can, because I have more tables.tbl_house tbl_reservationetc.So tbl_available is only a related table from tbl_house.In your way where can you see then if the week 13 was free or not? You cant because you only have 1 colum week!CyaBjorn |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-07-15 : 13:58:54
|
| There is a field called "Week" on every row, which tells you which week that row is for. Actually, you would need a YEAR field as well, i forgot about that one, that would be part of the primary key.Then, the data you posted :id_house year week1 week2 week3 week434 2003 free free taken free34 2004 free free free free35 2003 taken taken free freewould be stored like this:id_house, year, Week, [available?]34,2003,1,yes34,2003,2,yes34,2003,3,no34,2003,4,yes34,2004,1,yes34,2004,2,yes34,2004,3,yes34,2004,4,yes35,2003,1,no35,2003,2,no35,2003,3,yes35,2003,4,yesthis returns all free weeks for a house_id and year:select * fromtbl_availablewhere[available?] = Truehouse_id = 34 andyear = 2003This returns all weeks that are available for all houses, by year:select id_house, year, count(*)fromtbl_available[available?] = Truegroup by id_house, yearall houses that are available on week 12, year 2003:select * fromtbl_availablewhere Week = 12 andyear = 2003[Available?]=True# of available houses per week:select week, year, count(*)from tbl_availablewhere [available?]= truegroup by week, yearetc..."My way" is the standard way of using relational databases. It's called normalization ... do some google searches for more information.hopefully you can see it's a LITTLE more easier to get some data back if you structure the tables in a normalized fashion.- JeffEdited by - jsmith8858 on 07/15/2003 14:08:41 |
 |
|
|
neutcomp
Posting Yak Master
111 Posts |
Posted - 2003-07-15 : 16:12:51
|
Your way is indeed mutch easier.Thanxx for the tip, I think I am going to change the structure.CyaBjorn P.s. if you have 100 houses you have 100 * 53 weeks = 5300 record. Is that not a problem? My way you have only 100 record!Edited by - neutcomp on 07/15/2003 16:16:29 |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-07-15 : 19:57:30
|
5300 is nothing, a very small count.it is better to have a "long skinny" table with many rows and few columns, rather than a "short fat" table with many columns and few rows, in general.you can also change your design even further, and create a table of "bookings" which has information about each booking -- who took the order, for how much, when it was done, who is booking it, etc ...Bookings--------Booking_id (primary key)Booked_ByDateTimeAmountNotesOrder_Taken_By...etc...then a table of "booking details", which is the actual dates this booking is for:Booking_Details----------*House_Id*Year*WeekBooking_Id(* indicates the primary key fields.)thus, if there is no row in the bookings_Details table for a house_id/year/week, then it is open. if there is an entry in the tabel, it is booked -- and you can link to the "bookings" table for information on who booked it, when, for how much, etc.For reporting, all you need are the following helper tables:Weeks-----*WeekYears-----*Yearwhere Weeks has week numbers from 1-52 in it, and Years has years from 2003 to whenever (say, 2099).then, to show all house_ids for all weeks and all years:select house_id, week, yearfrom houses, weeks, yearssave that as a query, called "AllHousesWeeksYears". notice there are NO joins between the tables!! very unusual, this is called a "cross join" (note my title here in these forums ). that causes EVERY possible combination of all 3 tables to be produced in the output, which is what we want -- all possible combinations.then, to see what is booked and what is open:SELECT a.house_id, a.week, a.year, b.Booking_IDFROMAllHousesWeeksYears aLEFT OUTER JOINBooking_Details bona.house_id = b.house_Id anda.week = b.week anda.year = b.yearWHERE Year = [some year]this is a little advanced, but play around -- you can learn lots of cool techniques.- JeffEdited by - jsmith8858 on 07/15/2003 19:59:50 |
 |
|
|
neutcomp
Posting Yak Master
111 Posts |
Posted - 2003-07-30 : 04:17:11
|
I hope you can help me out AGAIN.I have:id week year status active39 0 2003 aangemaakt 039 0 2004 aangemaakt 038 0 2003 aangemaakt 137 0 2003 aangemaakt 137 0 2004 aangemaakt 138 1 2003 aanvraag 038 2 2003 aanvraag 037 1 2003 aanvraag 037 2 2003 aanvraag 037 3 2003 bezet 037 4 2003 bezet 0 id = id_houseNow if someone search for a available house in week 1 and 2 year 2003.The result must be notting!aangemaakt = 'this house exist with this year'aanvraag = 'someone has made a request to rent this house in this week.bezet = 'We accepted his request so its taken'So i want the id from the house, where the week 1 and 2 are available in the year 2003 and the active of the house must me 1.If i switch on39 0 2003 aangemaakt 0To39 0 2003 aangemaakt 1The result must me 39.I hope you understand and can help me out.ThanxxBjornone more thing: In ASP I make an variable addSQL where this is going to standAND week <> 1 AND week <> 2Or if someone search for week 10 till 12AND week <> 10 AND week <> 11 AND week <> 12 |
 |
|
|
neutcomp
Posting Yak Master
111 Posts |
Posted - 2003-07-30 : 04:53:53
|
I am allready close to the solution I think:SELECT DISTINCT id_huisFROM tbl_beschikbaarheidWHERE id_huis NOT IN( SELECT c.id_huis FROM tbl_beschikbaarheid AS c WHERE active = 1 OR week = 1 OR week = 2) But the problem is.If a house have year 2003 and 2004. And week 1 and 2 are still free in year 2004 the house is still not selected because its not available in 2003.Hope i am allready at the finish line |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-07-30 : 05:16:48
|
| select t.id from twhere(select count(*) from t tt where tt.year=@year andtt.week >= @week1 and tt.week <= @week2 andactive = 1) = @week2 - @week1 + 1- Vit |
 |
|
|
neutcomp
Posting Yak Master
111 Posts |
Posted - 2003-07-30 : 05:35:11
|
Its a pitty that I cannot work with variables in Access.But If I write your code like this:select id_huis from tbl_beschikbaarheidwhere( select count(*) from tbl_beschikbaarheid where jaar = 2003 and week >= 1 and week <= 2 and active = 1) = (2 - 1) + 1 Its not going to work. And what about if the year doesn't mather?ThanxxBjorn |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-07-30 : 06:03:10
|
| Yes... I forgot a 'main' condition:select t.id_huis from tbl_beschikbaarheid twhere( select count(*) from tbl_beschikbaarheid tt where tt.jaar = 2003 and tt.week >= 1 and tt.week <= 2 and tt.active = 1 and tt.id_huis = t.id_huis) = (2 - 1) + 1- Vit |
 |
|
|
neutcomp
Posting Yak Master
111 Posts |
Posted - 2003-07-30 : 06:15:34
|
| [code]select DISTINCT a.id_huis from tbl_beschikbaarheid AS awhere( select count(*) from tbl_beschikbaarheid As b where b.jaar = 2003 and b.week >= 5 and b.week <= 6 and b.active = 1 and a.id_huis = b.id_huis)= (6-5) + 1[/code]This is for week 5 till 6. The result must be everything. But in your query the result is notthing.I think you are forgetting that in the tbl there are only the booking weeks. and not every week from 1 till 52 (or 53).Because your sub query gives 0 as result.Bjorn |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-07-30 : 06:35:48
|
| Your guess is right... I couldn't know this fact... :(- Vit |
 |
|
|
Next Page
|