+ Reply to Thread
Page 1 of 4 1 2 3 ... LastLast
Results 1 to 10 of 34

Thread: Counting records?

  1. #1
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    Counting records?

    I want to count the number of records from this query:

    Code:
    SELECT performances.performanceid,
           shows.showname,
           performances.status
    FROM   shows
           INNER JOIN performances
             ON shows.showid = performances.showid
    WHERE  (((performances.status) = "Available"));
    

    My logic:
    1. User enters values on form.
    2. Query uses form values to build query to count the amount of records.
    3. IF DB record count is > than user selection
    4. THEN Insert records...

    But putting SELECT COUNT (*)... causes me problems.
    And how would i write point 3 of my logic in code?
    Any ideas?

  2. #2
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Hi there,

    Did you try like this?

    Code:
    SELECT Count(*) as "MyCount",performances.performanceid,
           shows.showname,
           performances.status
    FROM   shows
           INNER JOIN performances
             ON shows.showid = performances.showid
    WHERE  (((performances.status) = "Available"));
    

  3. #3
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    Quote Originally Posted by Rebelle View Post
    Hi there,

    Did you try like this?

    Code:
    SELECT Count(*) as "MyCount",performances.performanceid,
           shows.showname,
           performances.status
    FROM   shows
           INNER JOIN performances
             ON shows.showid = performances.showid
    WHERE  (((performances.status) = "Available"));
    
    Thanks, i added that. Running the query in Access brings the number of records up.

    Now i just need to get IF statement to check the records?
    I have wrote this code:

    Code:
    tickets = 10
    
    IF availabilityRS > tickets  THEN 
    response.write "There are more than 10 records in the DB"
    ELSEIF < tickets response.write "There are less than 10 records in the DB"
    end if
    %>
    

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Actually, I would write the SQL like this for performance reasons:
    Code:
    SELECT Count(1) as "MyCount",performances.performanceid,
           shows.showname,
           performances.status
    FROM   shows
           INNER JOIN performances
             ON shows.showid = performances.showid
    WHERE  (((performances.status) = "Available"));
    
    Then, to check the count:
    Code:
    tickets = 10
    
    If CInt(availabilityRS("MyCount")) > tickets Then 
        Response.Write "There are more than 10 records in the DB"
    ElseIf CInt(availabilityRS("MyCount")) < tickets Then
        Response.Write "There are less than 10 records in the DB"
    End If
    %>
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  5. #5
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    Quote Originally Posted by jmurrayhead View Post
    Actually, I would write the SQL like this for performance reasons:
    Code:
    SELECT Count(1) as "MyCount",performances.performanceid,
           shows.showname,
           performances.status
    FROM   shows
           INNER JOIN performances
             ON shows.showid = performances.showid
    WHERE  (((performances.status) = "Available"));
    
    Then, to check the count:
    Code:
    tickets = 10
    
    If CInt(availabilityRS("MyCount")) > tickets Then 
        Response.Write "There are more than 10 records in the DB"
    ElseIf CInt(availabilityRS("MyCount")) < tickets Then
        Response.Write "There are less than 10 records in the DB"
    End If
    %>
    


    Got this error:

    Microsoft JET Database Engine error '80040e21'

    You tried to execute a query that does not include the specified expression 'performanceID' as part of an aggregate function.

    /group.asp, line 78


    What is it that you were trying to do J?

  6. #6
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    It's missing a GROUP BY clause:
    Code:
    SELECT Count(1) as "MyCount",performances.performanceid,
           shows.showname,
           performances.status
    FROM   shows
           INNER JOIN performances
             ON shows.showid = performances.showid
    WHERE  (((performances.status) = "Available"));
    GROUP BY performances.performanceid, shows.showname, performances.status
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  7. #7
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    Quote Originally Posted by jmurrayhead View Post
    It's missing a GROUP BY clause:
    Code:
    SELECT Count(1) as "MyCount",performances.performanceid,
           shows.showname,
           performances.status
    FROM   shows
           INNER JOIN performances
             ON shows.showid = performances.showid
    WHERE  (((performances.status) = "Available"));
    GROUP BY performances.performanceid, shows.showname, performances.status
    
    Yes that sounds about rite, was just reading up on the error message on google. BUT its complaining about line 83:

    Code:
    If CInt(availabilityRS("MyCount")) > tickets Then
    
    It doesnt seem to recognise the "MyCount" as it gives an error of :

    ADODB.Recordset error '800a0cc1'

    Item cannot be found in the collection corresponding to the requested name or ordinal.

    /group.asp, line 83

  8. #8
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Try this:
    Code:
    SELECT Count(1) as MyCount,performances.performanceid,
           shows.showname,
           performances.status
    FROM   shows
           INNER JOIN performances
             ON shows.showid = performances.showid
    WHERE  (((performances.status) = "Available"));
    GROUP BY performances.performanceid, shows.showname, performances.status
    
    If that doesn't work, post all relevant ASP code for the page.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  9. #9
    Contracted Slave Centurion is on a distinguished road Centurion's Avatar
    Join Date
    Dec 2008
    Posts
    533
    Rep Power
    4

    Quote Originally Posted by jmurrayhead View Post
    Try this:
    Code:
    SELECT Count(1) as MyCount,performances.performanceid,
           shows.showname,
           performances.status
    FROM   shows
           INNER JOIN performances
             ON shows.showid = performances.showid
    WHERE  (((performances.status) = "Available"));
    GROUP BY performances.performanceid, shows.showname, performances.status
    
    If that doesn't work, post all relevant ASP code for the page.
    It does work, but the problem it isnt counting the total records. The expression shows a "1" next to each record in the database(when i test in MS ACCESS) when it should say something like 65!

    my relevant code:

    Code:
     
    
    <%
    DIM availabilitySQL, availabilityConn, availabilityRS
    
    set availabilityConn=Server.CreateObject("ADODB.Connection")
    availabilityConn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0"
    availabilityConn.open server.mappath("../db/database.mdb")
    
    'query to check all the shows that are available
    availabilitySQL = "SELECT Count(*) as MyCount, performances.performanceID, shows.showName, performances.status FROM shows INNER JOIN performances ON shows.showID = performances.showID WHERE  (((performances.status) = ""Available"")) GROUP BY performances.performanceID, shows.showName, performances.status"
    
    
    Set availabilityRS = Server.CreateObject("ADODB.Recordset")
    availabilityRS.Open availabilitySQL, availabilityConn
    'response.write availabilitySQL
    
    'testing the 
    tickets = 800
    
    If CInt(availabilityRS("MyCount")) > tickets Then 
        Response.Write "There are more than 10 records in the DB"
    ElseIf CInt(availabilityRS("MyCount")) < tickets Then
        Response.Write "There are less than 10 records in the DB"
    End If
    %>
    

  10. #10
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Try using the column name in the COUNT function instead:
    Code:
    SELECT Count(performances.performanceid) as MyCount,performances.performanceid,
           shows.showname,
           performances.status
    FROM   shows
           INNER JOIN performances
             ON shows.showid = performances.showid
    WHERE  (((performances.status) = "Available"));
    GROUP BY performances.performanceid, shows.showname, performances.status
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


+ Reply to Thread
Page 1 of 4 1 2 3 ... LastLast

Similar Threads

  1. Counting a field of two columns if the values don't match in SSRS
    By kristilee in forum SQL Server Reporting Services Help
    Replies: 5
    Last Post: September 4th, 2009, 12:21 PM
  2. Adding records
    By todd2006 in forum .NET Development
    Replies: 2
    Last Post: July 21st, 2009, 11:21 PM
  3. Looping Through Records
    By BLaaaaaaaaaarche in forum ASP Development
    Replies: 3
    Last Post: July 15th, 2009, 09:42 PM
  4. Sub/Child Records
    By Flam in forum SQL Development
    Replies: 8
    Last Post: June 16th, 2009, 02:19 PM
  5. comparing records
    By javier_83 in forum Microsoft Access
    Replies: 10
    Last Post: August 7th, 2008, 02:04 PM

Bookmarks

Posting Permissions

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

SEO by vBSEO