•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 423,085 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,379 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums
Views: 622 | Replies: 7
![]() |
•
•
Join Date: Jul 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
Hi there,
I have a master detail set of tables with a one-to-many relationship. Let's call the tables Projects (master) and Markets (details).
I need to allow users to query the database and be able to select projects by markets. In other words, users should be able to "say": "List all projects where Market is Architecture and Construction"
The query is to be created dynamically (this is not the problem here) and there are lots of projects and lots of markets.
Can anyone guide me on how to create the query (conceptually).
Below is a simplified version of the two tables. Now, I would like to query: "list all projects that is offices and hospitality"; with this sample data I should get project 001 only
Projects:
----------
PrjId PrjName... etc
001 myProject
...
Markets:
---------
PrjId MrktId Mrkt1 Mrkt2 Mrkt3 ... etc
001 001 offices single-family NULL
001 002 hospitality NULL NULL
001 003 retail NULL Something
002 004 offices NULL NULL
003 005 hospitality NULL NULL
I have a master detail set of tables with a one-to-many relationship. Let's call the tables Projects (master) and Markets (details).
I need to allow users to query the database and be able to select projects by markets. In other words, users should be able to "say": "List all projects where Market is Architecture and Construction"
The query is to be created dynamically (this is not the problem here) and there are lots of projects and lots of markets.
Can anyone guide me on how to create the query (conceptually).
Below is a simplified version of the two tables. Now, I would like to query: "list all projects that is offices and hospitality"; with this sample data I should get project 001 only
Projects:
----------
PrjId PrjName... etc
001 myProject
...
Markets:
---------
PrjId MrktId Mrkt1 Mrkt2 Mrkt3 ... etc
001 001 offices single-family NULL
001 002 hospitality NULL NULL
001 003 retail NULL Something
002 004 offices NULL NULL
003 005 hospitality NULL NULL
•
•
Join Date: Apr 2008
Posts: 295
Reputation:
Rep Power: 1
Solved Threads: 41
hi
If you were able to redesign table Markets like that:
(PrjID, MarkedID, Market), where at least all three attributes be part of markets' primary key, the query you are looking for and generating where clause at runtime would really be simple:
select distinct p.PrjID, PrjName from projects p inner join Markets m
where Market IN ('architecture', 'offices', 'hospitality', ... )
(This select requires that all primary and foreign keys are properly defined. If not, you must extend join Markets m by ON p.PrjID = m.PrjID)
With new table Markets
PrjID MrktId Market
001 001 offices
001 001 single-family
001 002 hospitality
001 003 retail
001 003 Something
002 004 offices
003 005 hospitality
and Projects the result set would be
PrjID PrjName
001 myProject
Unfortunately, current table Market is rather poorly designed. Because of the repeating group Mrkt1, Mrkt2 etc it even violates first normal form. I am afraid, it isn't in your power to simply redesign Markets.
There is a further advantage of new table Markets: no matter how many markets you want to insert you never need to change the structure (schema) of that table.
Btw, how many Mrkt columns has your current table Markets?
krs,
tesu
If you were able to redesign table Markets like that:
(PrjID, MarkedID, Market), where at least all three attributes be part of markets' primary key, the query you are looking for and generating where clause at runtime would really be simple:
select distinct p.PrjID, PrjName from projects p inner join Markets m
where Market IN ('architecture', 'offices', 'hospitality', ... )
(This select requires that all primary and foreign keys are properly defined. If not, you must extend join Markets m by ON p.PrjID = m.PrjID)
With new table Markets
PrjID MrktId Market
001 001 offices
001 001 single-family
001 002 hospitality
001 003 retail
001 003 Something
002 004 offices
003 005 hospitality
and Projects the result set would be
PrjID PrjName
001 myProject
Unfortunately, current table Market is rather poorly designed. Because of the repeating group Mrkt1, Mrkt2 etc it even violates first normal form. I am afraid, it isn't in your power to simply redesign Markets.
There is a further advantage of new table Markets: no matter how many markets you want to insert you never need to change the structure (schema) of that table.
Btw, how many Mrkt columns has your current table Markets?
krs,
tesu
Last edited by tesuji : Jul 1st, 2008 at 7:57 pm.
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
•
•
Join Date: Apr 2008
Posts: 295
Reputation:
Rep Power: 1
Solved Threads: 41
Hi again
I have got this idea of selecting the appropriate data from table Markets:
I haven't test it so far. But I believe it must be this sort of select statement which can also be dynamically generated during runtime.
Try it an tell me your result.
krs,
tesu
I have got this idea of selecting the appropriate data from table Markets:
sql Syntax (Toggle Plain Text)
SELECT DISTINCT PrjID, PrjName FROM projects WHERE PrjID IN ( SELECT PrjID FROM markets WHERE Mrkt1 IN ('architecture', 'offices', 'hospitality') union SELECT PrjID FROM markets WHERE Mrkt2 IN ('architecture', 'offices', 'hospitality') union SELECT PrjID FROM markets WHERE Mrkt3 IN ('architecture', 'offices', 'hospitality') )
I haven't test it so far. But I believe it must be this sort of select statement which can also be dynamically generated during runtime.
Try it an tell me your result.
krs,
tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
•
•
Join Date: Jul 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
Hi Tesuji,
As you imagine correctly, I have no power to re-design the Markets Table. I am only trying to query the database of a system that is being used in the company that am doing the work for. It is never that easy is it?
I have to find a solution to work with the setup that I described above. Sorry I did not mentioned this before.
Thanks for your reply.
LAM.
As you imagine correctly, I have no power to re-design the Markets Table. I am only trying to query the database of a system that is being used in the company that am doing the work for. It is never that easy is it?
I have to find a solution to work with the setup that I described above. Sorry I did not mentioned this before.
Thanks for your reply.
LAM.
•
•
•
•
hi
If you were able to redesign table Markets like that:
(PrjID, MarkedID, Market), where at least all three attributes be part of markets' primary key, the query you are looking for and generating where clause at runtime would really be simple:
select distinct p.PrjID, PrjName from projects p inner join Markets m
where Market IN ('architecture', 'offices', 'hospitality', ... )
(This select requires that all primary and foreign keys are properly defined. If not, you must extend join Markets m by ON p.PrjID = m.PrjID)
With new table Markets
PrjID MrktId Market
001 001 offices
001 001 single-family
001 002 hospitality
001 003 retail
001 003 Something
002 004 offices
003 005 hospitality
and Projects the result set would be
PrjID PrjName
001 myProject
Unfortunately, current table Market is rather poorly designed. Because of the repeating group Mrkt1, Mrkt2 etc it even violates first normal form. I am afraid, it isn't in your power to simply redesign Markets.
There is a further advantage of new table Markets: no matter how many markets you want to insert you never need to change the structure (schema) of that table.
Btw, how many Mrkt columns has your current table Markets?
krs,
tesu
•
•
Join Date: Jul 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
Let me add something else that might have been misinterpreted.
Columns Mrkt1, Mrkt2, MrktN do not contain the same information. To make this clearer, let me share some real data. The following data would be lookup data for each column in the Markets table.
Aviation (Mrket1)
Terminals
Lounges
...
Maritime (Mrket2)
Ports
Cruise Terminals
...
Education (Mrket3)
Pre-k
High-Schools
...
Also, to answer your question, They have only 8 markets at the moment, and it is not expected to grow considerably. Maybe a couple more.
Thanks again.
LAM.
Columns Mrkt1, Mrkt2, MrktN do not contain the same information. To make this clearer, let me share some real data. The following data would be lookup data for each column in the Markets table.
Aviation (Mrket1)
Terminals
Lounges
...
Maritime (Mrket2)
Ports
Cruise Terminals
...
Education (Mrket3)
Pre-k
High-Schools
...
Also, to answer your question, They have only 8 markets at the moment, and it is not expected to grow considerably. Maybe a couple more.
Thanks again.
LAM.
•
•
•
•
hi
If you were able to redesign table Markets like that:
(PrjID, MarkedID, Market), where at least all three attributes be part of markets' primary key, the query you are looking for and generating where clause at runtime would really be simple:
select distinct p.PrjID, PrjName from projects p inner join Markets m
where Market IN ('architecture', 'offices', 'hospitality', ... )
(This select requires that all primary and foreign keys are properly defined. If not, you must extend join Markets m by ON p.PrjID = m.PrjID)
With new table Markets
PrjID MrktId Market
001 001 offices
001 001 single-family
001 002 hospitality
001 003 retail
001 003 Something
002 004 offices
003 005 hospitality
and Projects the result set would be
PrjID PrjName
001 myProject
Unfortunately, current table Market is rather poorly designed. Because of the repeating group Mrkt1, Mrkt2 etc it even violates first normal form. I am afraid, it isn't in your power to simply redesign Markets.
There is a further advantage of new table Markets: no matter how many markets you want to insert you never need to change the structure (schema) of that table.
Btw, how many Mrkt columns has your current table Markets?
krs,
tesu
•
•
Join Date: Apr 2008
Posts: 295
Reputation:
Rep Power: 1
Solved Threads: 41
hi,
you should consider my posting #3:
You can freely change the IN list
WHERE Mrkt2 IN ('architecture', 'offices', 'hospitality')
for example
WHERE Maritime IN ('Cruise Terminals', 'ports')
So you need 8 constructions like:
UNION
SELECT PrjID FROM markets WHERE Mrktxx IN (List of items)
This can easily be generated at runtime.
tesu
you should consider my posting #3:
You can freely change the IN list
WHERE Mrkt2 IN ('architecture', 'offices', 'hospitality')
for example
WHERE Maritime IN ('Cruise Terminals', 'ports')
So you need 8 constructions like:
UNION
SELECT PrjID FROM markets WHERE Mrktxx IN (List of items)
This can easily be generated at runtime.
tesu
Last edited by tesuji : Jul 2nd, 2008 at 12:14 pm.
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
•
•
Join Date: Jul 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
Hi there,
I did the following test and it did not returned the expected results. Let me share it.
The statement:
select distinct prjId, Mrkt1 from markets
where Mrkt1 in ('Retail', 'Offices' )
The Idea:
To get all projects that are both, Retail and Offices
The data:
Mrkt Table
prjid Mrkt1
0100 Hospitality
0100 Retail
0200 Retail
0200 Offices
The EXPECTED result:
prjid = 0200
The ACTUAL result:
prjId = 0100, 0200
As far as I am concerned, "where Mrkt1 in ('Retail', 'Offices' )" is working as an OR
LAM.
I did the following test and it did not returned the expected results. Let me share it.
The statement:
select distinct prjId, Mrkt1 from markets
where Mrkt1 in ('Retail', 'Offices' )
The Idea:
To get all projects that are both, Retail and Offices
The data:
Mrkt Table
prjid Mrkt1
0100 Hospitality
0100 Retail
0200 Retail
0200 Offices
The EXPECTED result:
prjid = 0200
The ACTUAL result:
prjId = 0100, 0200
As far as I am concerned, "where Mrkt1 in ('Retail', 'Offices' )" is working as an OR
LAM.
•
•
•
•
Hi again
I have got this idea of selecting the appropriate data from table Markets:
sql Syntax (Toggle Plain Text)
SELECT DISTINCT PrjID, PrjName FROM projects WHERE PrjID IN ( SELECT PrjID FROM markets WHERE Mrkt1 IN ('architecture', 'offices', 'hospitality') union SELECT PrjID FROM markets WHERE Mrkt2 IN ('architecture', 'offices', 'hospitality') union SELECT PrjID FROM markets WHERE Mrkt3 IN ('architecture', 'offices', 'hospitality') )
I haven't test it so far. But I believe it must be this sort of select statement which can also be dynamically generated during runtime.
Try it an tell me your result.
krs,
tesu
•
•
Join Date: Apr 2008
Posts: 295
Reputation:
Rep Power: 1
Solved Threads: 41
Hello
That is true, because the IN list is kind of set, all records will be fetched that contain one of the list's element. So you can say the records are "ored" together in result set.
I assume that you can't determine whether a project, for example Terminals, entered by a user would be found in market Aviation only. If not, one has consider almost all permutations to be made of a set, for example given user entries (offices, single-family, retail, hospitality) would then dwindle into (offices, single-family, retail, hospitality), (single-family, offices, retail, hospitality), (retail, single-family,offices , hospitality) ...
So what to do? To get to create effective sql selects, I see two ways to proceed: 1. Re-structuring Markets table temporarily by means of temporary views, stored procedures (transact SQL programs), or applying new WITH clause.
2. Doing some c++, java, or c# programming using ado, odbc, jdbc db-interfaces.
Maybe the latter way is also the more appropriate one, if the some-day selected data are further processed by c++/java etc programs written by yourself. Can you tell me what you want to do with that data?
I will give some thoughts to these problems.
-----
tesu
That is true, because the IN list is kind of set, all records will be fetched that contain one of the list's element. So you can say the records are "ored" together in result set.
I assume that you can't determine whether a project, for example Terminals, entered by a user would be found in market Aviation only. If not, one has consider almost all permutations to be made of a set, for example given user entries (offices, single-family, retail, hospitality) would then dwindle into (offices, single-family, retail, hospitality), (single-family, offices, retail, hospitality), (retail, single-family,offices , hospitality) ...
So what to do? To get to create effective sql selects, I see two ways to proceed: 1. Re-structuring Markets table temporarily by means of temporary views, stored procedures (transact SQL programs), or applying new WITH clause.
2. Doing some c++, java, or c# programming using ado, odbc, jdbc db-interfaces.
Maybe the latter way is also the more appropriate one, if the some-day selected data are further processed by c++/java etc programs written by yourself. Can you tell me what you want to do with that data?
I will give some thoughts to these problems.
-----
tesu
Last edited by tesuji : Jul 2nd, 2008 at 5:26 pm.
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
![]() |
•
•
•
•
•
•
•
•
DaniWeb MS SQL Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- DNS Config Problems Help (Networking Hardware Configuration)
- DNSCache does not appear to flush? (Networking Hardware Configuration)
- Lookup tables - how to perform a switch using a string (C++)
- Need for DNS? (Windows NT / 2000 / XP / 2003)
- How to set a static IP (Networking Hardware Configuration)
- IP address lookup.... (Network Security)
- Blocking Brute-Force Attacks (ASP.NET)
- win 2003 NS (Windows Servers and IIS)
- Finding an IP address using Java (was: do u know?) (Java)
Other Threads in the MS SQL Forum
- Previous Thread: How to remove duplicate records
- Next Thread: Foreign and primary key


Linear Mode