Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
muzaffar_ali99
Starting Member
33 Posts |
Posted - 2006-03-24 : 05:24:00
|
| I have a ZipCode,state and country.ZipCode='638011' and state='Al' and country='USA'I have a table 'tbl_shops' which containShop_Id,Program_Id,Location_IdI have a table 'tbl_program_matching_criteria' which containProgram_Id,Same_ZipCode of bit type,Same_Country bit type and Same State of bit type.I have a table 'tbl_location' which contain Location_Id,Zip_Code,State,CountryNow i need to select all the shops who match with location's zip_code,state,county if it is set to true in 'tbl_program_matching_criteria'Currently i am using a cursor which is doing the things fine but making my stored procedure very slow.Here is code"Declare Cur_SameZipCode cursor forward_only for select distinct Location_Id, Program_Id from @tbl_shops_Loop Open Cur_SameZipCode fetch next from Cur_SameZipCode into @Location_Id, @Program_id While @@Fetch_Status=0 Begin Select @Match_Zip_Code=Zip_Code,@Match_State=State,@Match_Country=Country from tbl_client_locations where location_Id=@Location_Id Select @Same_Zip_Code=Same_Zip_Code,@Same_State=Same_State,@Same_Country=Same_Country from tbl_program_matching_criteria where Program_id=@Program_id print @Location_Id if @Same_Zip_Code=1 if @Match_Zip_Code<>@Shopper_Zip_Code Begin Delete from @tbl_shops where location_id=@Location_id and program_id=@Program_Id End if @Same_State=1 Begin if @Match_State<>@Shopper_State Begin Delete from @tbl_shops where location_id=@Location_id and program_id=@Program_Id End End if @Same_Country=1 Begin if @Match_Country<>@Shopper_Country Begin Delete from @tbl_shops where location_id=@Location_id and program_id=@Program_Id End End fetch next from Cur_SameZipCode into @Location_ID,@Program_Id Endclose Cur_SameZipCode"I want state the example data of my table too.tbl_shopsshop_id location_id program_id1 1 12 2 13 3 2tbl_locationsLocation_id ZipCode State Country1 333332 AL USA2 638431 AL USAtbl_program_matching_criteriaProgram_Id same_ZipCode Same_Country Same_State1 1 1 02 0 1 13 1 0 0Now I want is the query which can select "shops" from tbl_shops that matches the zip_code,state,country of locations if bit of relative zip_code,state,country is set to 1. |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-03-24 : 10:22:22
|
| So, what would your output look like for your example data?Ryan Randallwww.monsoonmalabar.comIdeas are easy. Choosing between them is the hard part. |
 |
|
|
muzaffar_ali99
Starting Member
33 Posts |
Posted - 2006-03-24 : 10:39:28
|
| My Output would like all 'shops' from 'tbl_shops' to whom the zipcode,country,state has matched if required in program matching criteria like thisOutputshop_id location_id program_id1 1 12 1 2I mean the shops will be filtered that don't match the ZipCode='638011' and state='Al' and country='USA' if in program matching criteria their bits are set on Regards |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-03-24 : 10:58:07
|
| In that case, you got me. Why does the location_id of shop_id 2 change from '2' in the data to '1' in the output?Ryan Randallwww.monsoonmalabar.comIdeas are easy. Choosing between them is the hard part. |
 |
|
|
muzaffar_ali99
Starting Member
33 Posts |
Posted - 2006-03-24 : 11:10:01
|
| Sorry that is just the example of data. The main purpose is to filter the data that i described above. There is no change in location_id.If You want to see that then it should likeShop_Id Location_Id Program_Id1 1 12 2 1 |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-03-24 : 11:21:12
|
| Can you give an example that works? That's surely very easy for you, and will make it so much easier for us to help you. As far as I can work out, the example you've given should result in no rows - not 2 rows. Is that not correct?Ryan Randallwww.monsoonmalabar.comIdeas are easy. Choosing between them is the hard part. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-03-24 : 11:26:07
|
I should wait for a proper example, but I've taken a stab without one, so this may not be correct...--datadeclare @tbl_shops table (shop_id int, location_id int, program_id int)insert @tbl_shops select 1, 1, 2 --note changed to 2union all select 2, 2, 2 --note changed to 2union all select 3, 3, 2declare @tbl_locations table (Location_id int, ZipCode varchar(10), State varchar(2), Country varchar(10))insert @tbl_locations select 1, '333332', 'AL', 'USA'union all select 2, '638431', 'AL', 'USA'declare @tbl_program_matching_criteria table (Program_Id int, same_ZipCode bit, Same_Country bit, Same_State bit)insert @tbl_program_matching_criteria select 1, 1, 1, 0union all select 2, 0, 1, 1union all select 3, 1, 0, 0--inputsdeclare @ZipCode varchar(10)declare @State varchar(2)declare @Country varchar(10)set @ZipCode = '638011'set @State = 'AL'set @Country = 'USA'--select the required outputselect s.*from @tbl_shops s inner join @tbl_program_matching_criteria c on s.program_id = c.program_id left outer join @tbl_locations l on s.Location_id = l.Location_idwhere (same_ZipCode = 0 or (same_ZipCode = 1 and ZipCode = @ZipCode)) and (Same_Country = 0 or (Same_Country = 1 and Country = @Country)) and (Same_State = 0 or (Same_State = 1 and State = @State )) Ryan Randallwww.monsoonmalabar.comIdeas are easy. Choosing between them is the hard part. |
 |
|
|
muzaffar_ali99
Starting Member
33 Posts |
Posted - 2006-03-24 : 11:33:10
|
| Ok see the exampletbl_client_locationslocation_id Zip_Code State Country1 33332 AL USA2 63108 NY USAtbl_program_matching_criteriaProgram_Id Same_ZipCode Same_State Same_Country1 1 1 1 tbl_shopsShop_Id Location_Id Program_Id1 1 12 2 1Now matching criterias are match zipcode,country and stateif I have my zipcode='33332' and State='AL' and Counry='USA'then this shop will show to me the shop_id 2 will not show to me.Result istbl_shopsShop_Id Location_Id Program_Id1 1 1If My Matching Critieria of Changestbl_program_matching_criteriaProgram_Id Same_ZipCode Same_State Same_Country1 1 0 0Then Only Same_ZipCode of the location will be checkedand if i have my zipcode='33332' and State='NY' and Counry='UK'now state and country does not matter to him.If all three bits are zero then location's zip_code,country and state would not be checked. |
 |
|
|
muzaffar_ali99
Starting Member
33 Posts |
Posted - 2006-03-24 : 11:37:45
|
| I am checking your code.......... |
 |
|
|
muzaffar_ali99
Starting Member
33 Posts |
Posted - 2006-03-24 : 13:21:02
|
| Thanx "RyanRandall" Thank you very much you have solved my great problem.But I request you to solve my another problem.I have another fields in "tbl_program_matching_criteria" too.For Example i have height field which can contain (>5,<5,>=5,=5,<=5,<5) here 5 is variable.and i have my height as shopper_height=5if my length is 5 and matching criteria it resides >5 then shop_id does not qualify. if resides=5 then shop_id qualifies.Tbl_Program_Matching_CriteriaProgram_Id Height 1 >5-2 after "-" height is in inches. both have to be matched.If any confusion please ask.Thanks agin.Regards |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-03-24 : 13:30:14
|
Thanks muzaffar You should know by now what I'm going to ask for before I try to solve anything for you... Can you give a full (i.e. with all the relevant tables, and with the outputs you'd want) example illustrating what you want to do.Ryan Randallwww.monsoonmalabar.comIdeas are easy. Choosing between them is the hard part. |
 |
|
|
muzaffar_ali99
Starting Member
33 Posts |
Posted - 2006-03-24 : 14:21:18
|
| I have table "tbl_program_matching_criteria" which have fieldsProgram_Id Same_Zip_Code Same_State Same_Country Height Weight Shopper_Income_Id 1 1 1 1 >5-2 50 1 2 0 0 0 null 1in the first program_id i will need to filter the shops with same city,state and zip_code that i have done now i need to check height,weight and shopper_income_id for the shops to show the shopper whose height,weight and incom_id i have in variables like @shopper_height,@shopper_weight,@shopper_income_idNow i can check icome_id with the query you wrote above but i think i will face problem in matching height that has >,>=,<,<=,= and then height in feet-inches >=5-3 for example.Now if shopper's height is 5-2 that i have a variables shopper_height='5-2' and this shopper is not >= to 5-3. But if SHopper_Height='5-4' and it is greater than '5-3' that of matching criteria then he qualifies to the shop.Thanks for your replies.Regrads |
 |
|
|
|
|
|
|
|