Results 1 to 14 of 14

Thread: SQL Statement - Need Help

  1. #1

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565

    Question

    I've got 4 tables I need to create a SQL statement from which are Cheque_History, Employees, Productions and Employers.

    Cheque_History is joined to Employees via Employee_Id.
    Employees is joined to Productions via Production_Id.
    Productions is joined to Employers via Employer_Id.

    I need all values from Cheque_History, FirstName and Surname form Employees, Title from Productions and Name from Employers.

    My problem is that not all cheques in Cheque_History have an Employee_Id (as they may have been cancelled), therefore I need a Right Join between Cheque_History and Employees to get all of the cheques.

    Can anybody help please.

  2. #2
    Hyperactive Member
    Join Date
    Apr 2000
    Location
    Isle of Man
    Posts
    276
    i would normally use the query bulder in access, to be honest.

    but, I am a cheat!

  3. #3

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    Tried that but it won't run the query because of the Right Join.

    Kinda the point of my question.

  4. #4
    Hyperactive Member
    Join Date
    Apr 2000
    Location
    Isle of Man
    Posts
    276
    what, access won't run the query or your app won't run the query???

  5. #5

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    Access.

    Access won't run it because two of the tables have a right join, but the other tables are joined normally.

  6. #6
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    Originally posted by Stevie
    I've got 4 tables I need to create a SQL statement from which are Cheque_History, Employees, Productions and Employers.

    Cheque_History is joined to Employees via Employee_Id.
    Employees is joined to Productions via Production_Id.
    Productions is joined to Employers via Employer_Id.

    I need all values from Cheque_History, FirstName and Surname form Employees, Title from Productions and Name from Employers.

    My problem is that not all cheques in Cheque_History have an Employee_Id (as they may have been cancelled), therefore I need a Right Join between Cheque_History and Employees to get all of the cheques.

    Can anybody help please.
    Old style join would be as follows:

    select a.field, b.field, c.field, d.field
    from Cheque_History a, Employees b, Productions c, Employers d
    where d.Employer_Id = c.Employer_Id and
    c.Productions = b.Productions and
    b.Employee_Id =* a.Employee_Id

  7. #7

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    Sorry that won't work (it's how I had it originally), as I will not get the cancelled cheques as they don't have an Employee_Id.

    That is why I need a Right Join so that I get all records from Cheque_History, and all records from Employees where Employee_Id matches.

  8. #8
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    then create a temporary table with the elements from the three that use the inner join and, then, right join the temp table to your problem table

    --create temporary table, drop temp_table first if it exists
    select a.field, b.field, c.field
    into temp_table
    from Employees a, Productions b, Employers c
    where c.Employer_Id = b.Employer_Id and
    b.Productions = a.Productions

    --now right join
    select a.field1, a.field2, a.field3, b.field
    from temp_table a, Cheque_History b
    where a.Employee_Id =* b.Employee_Id


  9. #9

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    Cheers, I didn't think of using a temp table (been trying to do it all in one sql statement), I'll give it a go.

  10. #10

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    I've tried adding a table to my database (Access) but the code I'm using keeps giving me an error, basically saying it can't be done. See here.
    VB6 sp5, SQL Server 2000, C#

    There are no stupid questions. Only stupid people.

  11. #11
    New Member
    Join Date
    Aug 2000
    Posts
    7
    This should do it.

    Select A.Field, B.Field, C.Field, D.Field
    From Cheque_History

    RIGHT JOIN Employees
    ON Emplyees.Employee_ID = Checque_History.Employee_ID

    JOIN Productions
    ON Productions.Production_ID = Employees.Production_ID

    JOIN Employers
    ON Employers.Employer_ID = Productions.Employer_ID

    Hope it works.
    "Oh damn why does this not work"
    "Every computer is a F.R.E.D, Freeking Ridiculous Electronic Device, the first word depending on your level of frustration"

  12. #12
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    Originally posted by Stevie
    I've tried adding a table to my database (Access) but the code I'm using keeps giving me an error, basically saying it can't be done. See here.
    Why are you trying to create the table beforehand? The "Select...into" format creates the table for you.

  13. #13

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    Cheers for the help, but I've managed to do it all with SQL now instead of creating the table with ADO.

    Thanks JHausmann for the temp table approach idea.
    VB6 sp5, SQL Server 2000, C#

    There are no stupid questions. Only stupid people.

  14. #14
    Lively Member
    Join Date
    Jul 2000
    Location
    Stockholm, Sweden
    Posts
    83
    Could you please send the solution I want to learn from it.

    Could you not use a query instead of a temporary table ?
    Yesterday, all my troubles seemed so far away...
    Help, I need somebody, Help...
    Now MCSD and still locking for intresting job in the south parts of Stockholm, Sweden.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width