Click here to Skip to main content
15,891,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have two table
1) Company
SQL
Company_id Int
Name       Varchar
Address    Varchar


2) Meeting
SQL
Meeting_Id Int
Company_id Int
Start_date datetime
End_date   datetime
Country_id int
state_id   int

my sql is like below
SQL
SELECT *
FROM   Company C
WHERE  c.name LIKE 'a%'
    AND EXISTS
        (
            SELECT 1
            FROM   Meeting M
            WHERE  c.Company_id = M.Company_id
                AND M.Country_id = 2
                AND M.State_id = 10
        )
    AND EXISTS
        (
            SELECT 1
            FROM   Meeting M
            WHERE  C.Company_id = M.Company_id
                AND M.start_date>= '2011-01-01'
                AND m.end_date<= '2011-12-31'
        )



Issue is when i run this query result will not match with below query's result data.

SQL
SELECT *
FROM   Company C
WHERE  c.name LIKE 'a%'
    AND EXISTS
        (
            SELECT 1
            FROM   Meeting M
            WHERE  c.Company_id = M.Company_id
                AND M.Country_id = 2
                AND M.State_id = 10
                AND M.start_date>= '2011-01-01'
                AND m.end_date<= '2011-12-31'
        )


I just want to know that i miss something in this query?
Posted

1 solution

If you take the following meetings for two companies:
MeetingID CompanyID Startdate  EndDate    CountryID StateID

M1        C1        2010-02-01 2010-02-01 2         10
M2        C1        2011-08-07 2011-08-07 20        5
M3        C2        2011-10-09 2011-10-09 2         10

For the first select statement the meetings M1 and M3 meet the criteria of the first EXISTS statement and meetings M2 and M3 meet the criteria of the second EXISTS statement. So companies C1 and C2 meet both EXISTS statements and are shown in the result.

Now in the second select statement meetings M1 and M2 do not meet the criteria of the single EXISTS statement and company C1 is not shown. Because meeting M3 does meet the criteria company C2 is shown in the result.
 
Share this answer
 
v2

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