SQL subquery problem - bookings database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Evans

    #1

    SQL subquery problem - bookings database

    Hi,



    Can anyone help me on a little problem I am having with some SQL - in
    particular on a subquery.



    I am setting up a database in Access for a voluntary group which runs
    voluntary youth weekend camps. The bit I am on at the moment is the booking
    system for these camps.



    My tables are setup as:

    - people_people which contains name / address details for the volunteers.
    Primary key is id field.

    - camps_info which contains information on each camp. Primary key is
    camp_number field.

    - camps_bookings. This has a record for each booking made.



    I want a query which will give me a list of each camp (ie corresponding to
    each record in camps_info) and alongside that display the number of bookings
    there are for each camp.



    I have successfully set up a parameter query that will tell me the number of
    bookings for a particular camp:



    SELECT sum(camps_info. camp_number) AS num_bookings_on _this_camp

    FROM camps_info INNER JOIN camps_bookings ON camps_info.camp _number =
    camps_bookings. camp_number

    WHERE (((camps_info.c amp_number)=[What camp number?]));



    However, rather than typing in each camp number, I want the query to do the
    number of bookings for each camp number in the camps_info table.



    I imagine that I need a sub-query for this? I have been trying various
    things
    (http://www.experts-exchange.com/Data...r/Q_20863705.h
    tml is the most useful page I've found so far). things I have tried are:



    select o.*, s.*

    from camps_info o,

    (

    SELECT sum(camps_info. camp_number), camp_number

    FROM camps_info INNER JOIN camps_bookings ON camps_info.camp _number =
    camps_bookings. camp_number

    ) s

    where o.camp_number = s.camp_number

    ;

    ...which gives an error "The specified field 'camp_number' could refer to
    more than one table listed in the FROM clause of your SQL statement"



    I also tried:

    Select c1.camp_number, num_bookings_on _this_camp

    from camps_info c1, camps_info c2, (

    SELECT sum(c2.camp_num ber) AS num_bookings_on _this_camp

    FROM c2 INNER JOIN camps_bookings ON c2.camp_number =
    camps_bookings. camp_number

    )

    WHERE (c2.camp_number =c1.camp_number )

    ;

    ..which gives the error "The Microsoft Jet database engine cannot find the
    input table of query 'c2'. Make sure it exists and that its name is spelled
    correctly".



    Another thing I thought of was to somehow set up a function
    NumberOfBooking s(CampNumber) via an Access VBA module that ran my first
    query for each camp. However, I haven't had much success with this, and
    logic tells me there must be a way of doing this within SQL alone.



    Does anyone have any tips on the SQL statement I should be using for my
    query?



    Very many thanks.



    --Dan Evans.

    dd_dd75@hotmail .com


  • Gary MacKay

    #2
    Re: SQL subquery problem - bookings database

    I think you can change your WHERE .. statement to GROUP BY o.camp_number



    Dan Evans wrote:
    [color=blue]
    > Hi,
    >
    >
    >
    > Can anyone help me on a little problem I am having with some SQL - in
    > particular on a subquery.
    >
    >
    >
    > I am setting up a database in Access for a voluntary group which runs
    > voluntary youth weekend camps. The bit I am on at the moment is the booking
    > system for these camps.
    >
    >
    >
    > My tables are setup as:
    >
    > - people_people which contains name / address details for the volunteers.
    > Primary key is id field.
    >
    > - camps_info which contains information on each camp. Primary key is
    > camp_number field.
    >
    > - camps_bookings. This has a record for each booking made.
    >
    >
    >
    > I want a query which will give me a list of each camp (ie corresponding to
    > each record in camps_info) and alongside that display the number of bookings
    > there are for each camp.
    >
    >
    >
    > I have successfully set up a parameter query that will tell me the number of
    > bookings for a particular camp:
    >
    >
    >
    > SELECT sum(camps_info. camp_number) AS num_bookings_on _this_camp
    >
    > FROM camps_info INNER JOIN camps_bookings ON camps_info.camp _number =
    > camps_bookings. camp_number
    >
    > WHERE (((camps_info.c amp_number)=[What camp number?]));
    >
    >
    >
    > However, rather than typing in each camp number, I want the query to do the
    > number of bookings for each camp number in the camps_info table.
    >
    >
    >
    > I imagine that I need a sub-query for this? I have been trying various
    > things
    > (http://www.experts-exchange.com/Data...r/Q_20863705.h
    > tml is the most useful page I've found so far). things I have tried are:
    >
    >
    >
    > select o.*, s.*
    >
    > from camps_info o,
    >
    > (
    >
    > SELECT sum(camps_info. camp_number), camp_number
    >
    > FROM camps_info INNER JOIN camps_bookings ON camps_info.camp _number =
    > camps_bookings. camp_number
    >
    > ) s
    >
    > where o.camp_number = s.camp_number
    >
    > ;
    >
    > ..which gives an error "The specified field 'camp_number' could refer to
    > more than one table listed in the FROM clause of your SQL statement"
    >
    >
    >
    > I also tried:
    >
    > Select c1.camp_number, num_bookings_on _this_camp
    >
    > from camps_info c1, camps_info c2, (
    >
    > SELECT sum(c2.camp_num ber) AS num_bookings_on _this_camp
    >
    > FROM c2 INNER JOIN camps_bookings ON c2.camp_number =
    > camps_bookings. camp_number
    >
    > )
    >
    > WHERE (c2.camp_number =c1.camp_number )
    >
    > ;
    >
    > .which gives the error "The Microsoft Jet database engine cannot find the
    > input table of query 'c2'. Make sure it exists and that its name is spelled
    > correctly".
    >
    >
    >
    > Another thing I thought of was to somehow set up a function
    > NumberOfBooking s(CampNumber) via an Access VBA module that ran my first
    > query for each camp. However, I haven't had much success with this, and
    > logic tells me there must be a way of doing this within SQL alone.
    >
    >
    >
    > Does anyone have any tips on the SQL statement I should be using for my
    > query?
    >
    >
    >
    > Very many thanks.
    >
    >
    >
    > --Dan Evans.
    >
    > dd_dd75@hotmail .com
    >
    >[/color]

    Comment

    • Gary MacKay

      #3
      Re: SQL subquery problem - bookings database

      I think you can change your WHERE .. statement to GROUP BY o.camp_number



      Dan Evans wrote:
      [color=blue]
      > Hi,
      >
      >
      >
      > Can anyone help me on a little problem I am having with some SQL - in
      > particular on a subquery.
      >
      >
      >
      > I am setting up a database in Access for a voluntary group which runs
      > voluntary youth weekend camps. The bit I am on at the moment is the booking
      > system for these camps.
      >
      >
      >
      > My tables are setup as:
      >
      > - people_people which contains name / address details for the volunteers.
      > Primary key is id field.
      >
      > - camps_info which contains information on each camp. Primary key is
      > camp_number field.
      >
      > - camps_bookings. This has a record for each booking made.
      >
      >
      >
      > I want a query which will give me a list of each camp (ie corresponding to
      > each record in camps_info) and alongside that display the number of bookings
      > there are for each camp.
      >
      >
      >
      > I have successfully set up a parameter query that will tell me the number of
      > bookings for a particular camp:
      >
      >
      >
      > SELECT sum(camps_info. camp_number) AS num_bookings_on _this_camp
      >
      > FROM camps_info INNER JOIN camps_bookings ON camps_info.camp _number =
      > camps_bookings. camp_number
      >
      > WHERE (((camps_info.c amp_number)=[What camp number?]));
      >
      >
      >
      > However, rather than typing in each camp number, I want the query to do the
      > number of bookings for each camp number in the camps_info table.
      >
      >
      >
      > I imagine that I need a sub-query for this? I have been trying various
      > things
      > (http://www.experts-exchange.com/Data...r/Q_20863705.h
      > tml is the most useful page I've found so far). things I have tried are:
      >
      >
      >
      > select o.*, s.*
      >
      > from camps_info o,
      >
      > (
      >
      > SELECT sum(camps_info. camp_number), camp_number
      >
      > FROM camps_info INNER JOIN camps_bookings ON camps_info.camp _number =
      > camps_bookings. camp_number
      >
      > ) s
      >
      > where o.camp_number = s.camp_number
      >
      > ;
      >
      > ..which gives an error "The specified field 'camp_number' could refer to
      > more than one table listed in the FROM clause of your SQL statement"
      >
      >
      >
      > I also tried:
      >
      > Select c1.camp_number, num_bookings_on _this_camp
      >
      > from camps_info c1, camps_info c2, (
      >
      > SELECT sum(c2.camp_num ber) AS num_bookings_on _this_camp
      >
      > FROM c2 INNER JOIN camps_bookings ON c2.camp_number =
      > camps_bookings. camp_number
      >
      > )
      >
      > WHERE (c2.camp_number =c1.camp_number )
      >
      > ;
      >
      > .which gives the error "The Microsoft Jet database engine cannot find the
      > input table of query 'c2'. Make sure it exists and that its name is spelled
      > correctly".
      >
      >
      >
      > Another thing I thought of was to somehow set up a function
      > NumberOfBooking s(CampNumber) via an Access VBA module that ran my first
      > query for each camp. However, I haven't had much success with this, and
      > logic tells me there must be a way of doing this within SQL alone.
      >
      >
      >
      > Does anyone have any tips on the SQL statement I should be using for my
      > query?
      >
      >
      >
      > Very many thanks.
      >
      >
      >
      > --Dan Evans.
      >
      > dd_dd75@hotmail .com
      >
      >[/color]

      Comment

      • Gary MacKay

        #4
        Re: SQL subquery problem - bookings database

        I think you can change your WHERE .. statement to GROUP BY o.camp_number



        Dan Evans wrote:
        [color=blue]
        > Hi,
        >
        >
        >
        > Can anyone help me on a little problem I am having with some SQL - in
        > particular on a subquery.
        >
        >
        >
        > I am setting up a database in Access for a voluntary group which runs
        > voluntary youth weekend camps. The bit I am on at the moment is the booking
        > system for these camps.
        >
        >
        >
        > My tables are setup as:
        >
        > - people_people which contains name / address details for the volunteers.
        > Primary key is id field.
        >
        > - camps_info which contains information on each camp. Primary key is
        > camp_number field.
        >
        > - camps_bookings. This has a record for each booking made.
        >
        >
        >
        > I want a query which will give me a list of each camp (ie corresponding to
        > each record in camps_info) and alongside that display the number of bookings
        > there are for each camp.
        >
        >
        >
        > I have successfully set up a parameter query that will tell me the number of
        > bookings for a particular camp:
        >
        >
        >
        > SELECT sum(camps_info. camp_number) AS num_bookings_on _this_camp
        >
        > FROM camps_info INNER JOIN camps_bookings ON camps_info.camp _number =
        > camps_bookings. camp_number
        >
        > WHERE (((camps_info.c amp_number)=[What camp number?]));
        >
        >
        >
        > However, rather than typing in each camp number, I want the query to do the
        > number of bookings for each camp number in the camps_info table.
        >
        >
        >
        > I imagine that I need a sub-query for this? I have been trying various
        > things
        > (http://www.experts-exchange.com/Data...r/Q_20863705.h
        > tml is the most useful page I've found so far). things I have tried are:
        >
        >
        >
        > select o.*, s.*
        >
        > from camps_info o,
        >
        > (
        >
        > SELECT sum(camps_info. camp_number), camp_number
        >
        > FROM camps_info INNER JOIN camps_bookings ON camps_info.camp _number =
        > camps_bookings. camp_number
        >
        > ) s
        >
        > where o.camp_number = s.camp_number
        >
        > ;
        >
        > ..which gives an error "The specified field 'camp_number' could refer to
        > more than one table listed in the FROM clause of your SQL statement"
        >
        >
        >
        > I also tried:
        >
        > Select c1.camp_number, num_bookings_on _this_camp
        >
        > from camps_info c1, camps_info c2, (
        >
        > SELECT sum(c2.camp_num ber) AS num_bookings_on _this_camp
        >
        > FROM c2 INNER JOIN camps_bookings ON c2.camp_number =
        > camps_bookings. camp_number
        >
        > )
        >
        > WHERE (c2.camp_number =c1.camp_number )
        >
        > ;
        >
        > .which gives the error "The Microsoft Jet database engine cannot find the
        > input table of query 'c2'. Make sure it exists and that its name is spelled
        > correctly".
        >
        >
        >
        > Another thing I thought of was to somehow set up a function
        > NumberOfBooking s(CampNumber) via an Access VBA module that ran my first
        > query for each camp. However, I haven't had much success with this, and
        > logic tells me there must be a way of doing this within SQL alone.
        >
        >
        >
        > Does anyone have any tips on the SQL statement I should be using for my
        > query?
        >
        >
        >
        > Very many thanks.
        >
        >
        >
        > --Dan Evans.
        >
        > dd_dd75@hotmail .com
        >
        >[/color]

        Comment

        • James Alexander Starritt

          #5
          Re: SQL subquery problem - bookings database




          "Dan Evans" <dd_dd75@hotmai l.com> wrote in message
          news:403a8f00$0 $61968$65c69314 @mercury.nildra m.net...[color=blue]
          >
          > Can anyone help me on a little problem I am having with some SQL - in
          > particular on a subquery.
          >
          > I am setting up a database in Access for a voluntary group which runs
          > voluntary youth weekend camps. The bit I am on at the moment is the[/color]
          booking[color=blue]
          > system for these camps.
          >[/color]

          news://comp.database.ms-access


          Comment

          • James Alexander Starritt

            #6
            Re: SQL subquery problem - bookings database




            "Dan Evans" <dd_dd75@hotmai l.com> wrote in message
            news:403a8f00$0 $61968$65c69314 @mercury.nildra m.net...[color=blue]
            >
            > Can anyone help me on a little problem I am having with some SQL - in
            > particular on a subquery.
            >
            > I am setting up a database in Access for a voluntary group which runs
            > voluntary youth weekend camps. The bit I am on at the moment is the[/color]
            booking[color=blue]
            > system for these camps.
            >[/color]

            news://comp.database.ms-access


            Comment

            • James Alexander Starritt

              #7
              Re: SQL subquery problem - bookings database




              "Dan Evans" <dd_dd75@hotmai l.com> wrote in message
              news:403a8f00$0 $61968$65c69314 @mercury.nildra m.net...[color=blue]
              >
              > Can anyone help me on a little problem I am having with some SQL - in
              > particular on a subquery.
              >
              > I am setting up a database in Access for a voluntary group which runs
              > voluntary youth weekend camps. The bit I am on at the moment is the[/color]
              booking[color=blue]
              > system for these camps.
              >[/color]

              news://comp.database.ms-access


              Comment

              Working...