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

Complex SQL Query

hi,
here is the structure of db

CREATE TABLE [LOCATION] (
[Id] VARCHAR(38),
[ParentId] VARCHAR(38),
[Name] VARCHAR(50))
INSERT INTO [LOCATION] ([Id], [ParentId], [Name])
VALUES ('{000E12CF-0C0A-4991-8A38-6C1B1EA28A56}', '{35542804-DD2A-45EC-B7C9-1E3F358FEA23}', 'Singapore')
GO

INSERT INTO [LOCATION] ([Id], [ParentId], [Name])
VALUES ('{006C9BE7-E711-4DCB-8141-782A36F6DEFC}', '{A3BB1EBD-D17D-4465-BB9A-B3622E826707}', 'Nevada')
GO

INSERT INTO [LOCATION] ([Id], [ParentId], [Name])
VALUES ('{00B46FFB-091F-4DF2-8635-67056E60EC2D}', '{18EACAAA-178B-47BB-80C3-F4438ED490F6}', 'Qatar')
GO
and go on...
CREATE TABLE [SECTOR] (
[ID] VARCHAR(38),
[ParentId] VARCHAR(38),
[Name] VARCHAR(255))
GO
INSERT INTO [SECTOR] ([ID], [ParentId], [Name])
VALUES ('{04DD96C5-F838-49EA-8F32-610B926C6E9C}', '{B934848B-F2E0-4BCB-809C-65B312FAC066}', 'Library')
GO

INSERT INTO [SECTOR] ([ID], [ParentId], [Name])
VALUES ('{0636DAEF-8055-43E3-9053-4BF59E818829}', '{423ED5A4-5499-4115-8519-6D6D76FD084C}', 'Other Engineering')
GO
and ...

CREATE TABLE [Website] (
[ID] VARCHAR(38),
[HomePage] VARCHAR(512))
GO
INSERT INTO [Website] ([ID], [HomePage])
VALUES ('{16D04933-52D2-45F7-9FCB-1A1A7F5EB253}', 'www.yahoo.com')
GO

INSERT INTO [Website] ([ID], [HomePage])
VALUES ('{26D04433-52D2-45F7-9FCB-1A1A7F5EB123}', 'www.hotmail.com')
GO
....

CREATE TABLE [Web2Location] (
[Web_ID] VARCHAR(38),
[Location_ID] VARCHAR(38))
GO

INSERT INTO [Web2Location] ([Web_ID], [Location_ID])
VALUES ('{16D04933-52D2-45F7-9FCB-1A1A7F5EB253}', '{1B79459C-371D-4472-BBCF-578B6C950463}')
GO

INSERT INTO [Web2Location] ([Web_ID], [Location_ID])
VALUES ('{26D04433-52D2-45F7-9FCB-1A1A7F5EB123}', '{70476DF7-B4B8-417E-ACE8-735CEFB71197}')
GO
...

CREATE TABLE [Web2Sector] (
[Web_ID] VARCHAR(38),
[Sector_ID] VARCHAR(38))
GO
INSERT INTO [Web2Sector] ([Web_ID], [Sector_ID])
VALUES ('{26D04433-52D2-45F7-9FCB-1A1A7F5EB123}', '{0C020B48-580C-4389-B4E3-2EDBD88A4877}')
GO

INSERT INTO [Web2Sector] ([Web_ID], [Sector_ID])
VALUES ('{26D04433-52D2-45F7-9FCB-1A1A7F5EB123}', '{12150FF7-5323-4F26-8EE3-BBF25110C48D}')
GO

CREATE TABLE [WebAccount] (
[ID] VARCHAR(38),
[Web_ID] VARCHAR(38),
[Login] VARCHAR(255),
[Pass] VARCHAR(255))
GO
INSERT INTO [WebAccount] ([ID], [Web_ID], [Login], [Pass])
VALUES ('{00BB7207-1EB1-497E-9FFA-446531FA97CB}', '{817011C3-4E7F-4BD7-B2A6-B5C057A4A7C4}', 'Usman', 'pass')
GO

INSERT INTO [WebAccount] ([ID], [Web_ID], [Login], [Pass])
VALUES ('{4199B81B-AC1E-4DA9-9C5E-25482DDB3840}', '{26D04433-52D2-45F7-9FCB-1A1A7F5EB359}', 'test', 'test')
GO
=================End of query portion====================

Now what I want is, I have location and sector IDs and I want a list/records from WebAccount table that are relevent to given location and sector IDs matching from Web2Location and Web2Sector tables.

Please let me know if it make sense and thanks in advance.
Dec 14 '07 #1
4 1084
amitpatel66
2,367 Expert 2GB
hi,
here is the structure of db

CREATE TABLE [LOCATION] (
[Id] VARCHAR(38),
[ParentId] VARCHAR(38),
[Name] VARCHAR(50))
INSERT INTO [LOCATION] ([Id], [ParentId], [Name])
VALUES ('{000E12CF-0C0A-4991-8A38-6C1B1EA28A56}', '{35542804-DD2A-45EC-B7C9-1E3F358FEA23}', 'Singapore')
GO

INSERT INTO [LOCATION] ([Id], [ParentId], [Name])
VALUES ('{006C9BE7-E711-4DCB-8141-782A36F6DEFC}', '{A3BB1EBD-D17D-4465-BB9A-B3622E826707}', 'Nevada')
GO

INSERT INTO [LOCATION] ([Id], [ParentId], [Name])
VALUES ('{00B46FFB-091F-4DF2-8635-67056E60EC2D}', '{18EACAAA-178B-47BB-80C3-F4438ED490F6}', 'Qatar')
GO
and go on...
CREATE TABLE [SECTOR] (
[ID] VARCHAR(38),
[ParentId] VARCHAR(38),
[Name] VARCHAR(255))
GO
INSERT INTO [SECTOR] ([ID], [ParentId], [Name])
VALUES ('{04DD96C5-F838-49EA-8F32-610B926C6E9C}', '{B934848B-F2E0-4BCB-809C-65B312FAC066}', 'Library')
GO

INSERT INTO [SECTOR] ([ID], [ParentId], [Name])
VALUES ('{0636DAEF-8055-43E3-9053-4BF59E818829}', '{423ED5A4-5499-4115-8519-6D6D76FD084C}', 'Other Engineering')
GO
and ...

CREATE TABLE [Website] (
[ID] VARCHAR(38),
[HomePage] VARCHAR(512))
GO
INSERT INTO [Website] ([ID], [HomePage])
VALUES ('{16D04933-52D2-45F7-9FCB-1A1A7F5EB253}', 'www.yahoo.com')
GO

INSERT INTO [Website] ([ID], [HomePage])
VALUES ('{26D04433-52D2-45F7-9FCB-1A1A7F5EB123}', 'www.hotmail.com')
GO
....

CREATE TABLE [Web2Location] (
[Web_ID] VARCHAR(38),
[Location_ID] VARCHAR(38))
GO

INSERT INTO [Web2Location] ([Web_ID], [Location_ID])
VALUES ('{16D04933-52D2-45F7-9FCB-1A1A7F5EB253}', '{1B79459C-371D-4472-BBCF-578B6C950463}')
GO

INSERT INTO [Web2Location] ([Web_ID], [Location_ID])
VALUES ('{26D04433-52D2-45F7-9FCB-1A1A7F5EB123}', '{70476DF7-B4B8-417E-ACE8-735CEFB71197}')
GO
...

CREATE TABLE [Web2Sector] (
[Web_ID] VARCHAR(38),
[Sector_ID] VARCHAR(38))
GO
INSERT INTO [Web2Sector] ([Web_ID], [Sector_ID])
VALUES ('{26D04433-52D2-45F7-9FCB-1A1A7F5EB123}', '{0C020B48-580C-4389-B4E3-2EDBD88A4877}')
GO

INSERT INTO [Web2Sector] ([Web_ID], [Sector_ID])
VALUES ('{26D04433-52D2-45F7-9FCB-1A1A7F5EB123}', '{12150FF7-5323-4F26-8EE3-BBF25110C48D}')
GO

CREATE TABLE [WebAccount] (
[ID] VARCHAR(38),
[Web_ID] VARCHAR(38),
[Login] VARCHAR(255),
[Pass] VARCHAR(255))
GO
INSERT INTO [WebAccount] ([ID], [Web_ID], [Login], [Pass])
VALUES ('{00BB7207-1EB1-497E-9FFA-446531FA97CB}', '{817011C3-4E7F-4BD7-B2A6-B5C057A4A7C4}', 'Usman', 'pass')
GO

INSERT INTO [WebAccount] ([ID], [Web_ID], [Login], [Pass])
VALUES ('{4199B81B-AC1E-4DA9-9C5E-25482DDB3840}', '{26D04433-52D2-45F7-9FCB-1A1A7F5EB359}', 'test', 'test')
GO
=================End of query portion====================

Now what I want is, I have location and sector IDs and I want a list/records from WebAccount table that are relevent to given location and sector IDs matching from Web2Location and Web2Sector tables.

Please let me know if it make sense and thanks in advance.
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT a.* FROM WebAccount a, Web2Location l
  3. WHERE a.web_id=l.web_id
  4. AND l.location_id = '{70476DF7-B4B8-417E-ACE8-735CEFB71197}'
  5. UNION ALL
  6. SELECT a.* FROM WebAccount a, Web2Sector S
  7. WHERE a.web_id=s.web_id
  8. AND s.sector_id = '{12150FF7-5323-4F26-8EE3-BBF25110C48D}'
  9.  
  10.  
Dec 14 '07 #2
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT a.* FROM WebAccount a, Web2Location l
  3. WHERE a.web_id=l.web_id
  4. AND l.location_id = '{70476DF7-B4B8-417E-ACE8-735CEFB71197}'
  5. UNION ALL
  6. SELECT a.* FROM WebAccount a, Web2Sector S
  7. WHERE a.web_id=s.web_id
  8. AND s.sector_id = '{12150FF7-5323-4F26-8EE3-BBF25110C48D}'
  9.  
  10.  
thanks amitpatel66, this help me a lot
Dec 14 '07 #3
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT a.* FROM WebAccount a, Web2Location l
  3. WHERE a.web_id=l.web_id
  4. AND l.location_id = '{70476DF7-B4B8-417E-ACE8-735CEFB71197}'
  5. UNION ALL
  6. SELECT a.* FROM WebAccount a, Web2Sector S
  7. WHERE a.web_id=s.web_id
  8. AND s.sector_id = '{12150FF7-5323-4F26-8EE3-BBF25110C48D}'
  9.  
  10.  
hi once again, I was going to implement this when i realize it doesn't solve the issue. how? lets see
suppose i select location India and sector IT(IDs) so it means query should return only accounts of sector IT that are located in India. But as it is now, it returns all accounts from IT sector located anywhere and combine them with India location accounts.
will you plz help me to solve this issue
Dec 15 '07 #4
amitpatel66
2,367 Expert 2GB
hi once again, I was going to implement this when i realize it doesn't solve the issue. how? lets see
suppose i select location India and sector IT(IDs) so it means query should return only accounts of sector IT that are located in India. But as it is now, it returns all accounts from IT sector located anywhere and combine them with India location accounts.
will you plz help me to solve this issue
Tryh this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT a.* FROM WebAccount a, Web2Location l,Web2Sector S
  3. WHERE a.web_id=l.web_id
  4. AND a.web_id = s.web_id
  5. AND l.location_id = '{70476DF7-B4B8-417E-ACE8-735CEFB71197}'
  6. AND s.sector_id = '{12150FF7-5323-4F26-8EE3-BBF25110C48D}'
  7.  
  8.  
Dec 17 '07 #5

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

Similar topics

4
by: Starbuck | last post by:
OK, first let me say that I am no DB person. But I have a user here who keeps getting this error whenever she does, whatever it is she does, with databases... A google search takes me to...
2
by: Mikel | last post by:
I am trying to get around the problem "The expression you have entered is too complex" for a select query. (The example below is not the expression that is giving me headaches.) So I am thinking...
4
by: ED | last post by:
I am attempting to to write a query that has a numerous nested IIf statements. The problem that I am having is that it is to long of a query to be built in design mode and when I build it in sql...
8
by: Matt | last post by:
Hi all, Thank you for taking the time. I have a database with 45 tables on it. 44 tables are linked to a main table through a one to one relationship. My question is, is there no way i can...
2
by: Ben de Vette | last post by:
Hi, I'm using the querybuilder when updating a record in a table (Access). However, I get a "Query is too complex" message. The Primary key is autonumbered. Why is it making such a complex...
1
by: arun | last post by:
Query is too complex -------------------------------------------------------------------------------- Hi, I was trying to solve this problem since last two days but couldn't find any solution. ...
1
by: Randy Volkart | last post by:
I'm trying to fix a glitch in a complex access database, and have a fairly complex problem... unless there's some obscure easy fix I don't know being fairly new with Access. Basically, the area...
19
by: kawaks40 | last post by:
Hi everyone :) I just recently started using access/sql. and right away I ran into this problem "SQL expression too complex" I google'd a lot on what it means, and the only workaround I've...
3
by: Eric Davidson | last post by:
DB2 9.5 I keep geting the message. SQL0101N The statement is too long or too complex. SQLSTATE=54001 When one of my sql statements takes over 60 seconds to compile the sql statement. Is...
0
crystal2005
by: crystal2005 | last post by:
Hi, I am having trouble with some complex SQL queries. I’ve got winestore database, taken from Web Database Application with PHP and MySQL book. And some question about queries as the following ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.