Results 1 to 3 of 3

Thread: help with SQL statement

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168

    Question

    Hello I am working with an ms access 97 database and Table1 has two columns called ListPrice and CostPrice. Is there a way to return a recordset that only two rows with 2 columns, the columns would be "Field Name" and "Count". The "Field Name" would be ListPrice and CostPrice and "Count" would count how many times that field name had a higher price than the other. For example:

    TABLE:
    CostPrice ListPrice
    2.95 1.95
    3.95 5.95
    .95 1.95

    Returned Recordset:
    CostPrice 1
    ListPrice 2

    Is that possible?

    Thanks,
    Thai

  2. #2
    Addicted Member
    Join Date
    Jan 2000
    Location
    Oshkosh, WI
    Posts
    163
    I was able to do it by writing 2 queries in Access. The first queries determines which price is higher. The second query references the first one and simply counts the records. Here's the SQL for the queries:

    Query 1:

    SELECT IIf([listprice]>[costprice],"ListPrice","CostPrice") AS FieldName
    FROM Table1;

    Query 2:

    SELECT Query1.FieldName, Count(Query1.FieldName) AS Count
    FROM Query1
    GROUP BY Query1.FieldName;

    Hope this helps.

    Glenn D
    Development/Analyst

  3. #3
    Lively Member
    Join Date
    Dec 1999
    Location
    Karlsruhe, Germany
    Posts
    122
    Hi,

    SELECT Count(*) AS Count, 'Costprice' as fieldname
    FROM Table1
    WHERE CostPrice>listprice union SELECT Count(*) AS Count, 'Listprice' as fieldname
    FROM Table1
    WHERE CostPrice<listprice

    Roger

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