Newbie about grouping query

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Newbie about grouping query

    In my mdb, the data is as below :

    ProductID Qty Name Month CID
    ------------------------------------------------------------------------
    Product A 2 Harddisk July A-Company
    Product A 5 Harddisk July A-Company
    Product A 1 Harddisk June A-Company
    Product B 3 cd-rom July A-Company
    Product B 2 cd-rom July B-Company
    Product B 2 cd-rom June B-Company

    When I select "A-Company" and "July" from listbox and submit,
    I hope the result should return like this :

    A-Company. July
    ProductID Qty Name
    --------------------------------------
    Product A 7 Harddisk (Grouped, the qty will sum up
    automatically)
    Product B 3 cd-rom

    First, I need to select the records of A-Company in July :

    SQL = "Select * From Invoice where month = " & Month
    SQL = SQL & " And CID ='" & CID & "'"

    Then grouping the ProductID and sum the qty, also need to display the name :

    SQL = "Select ProductID , Sum(Qty) As DDD , First(Name) As EEE From
    Invoice GROUP BY ProductID "

    Then use the script below :

    ProductID Name Qty
    ----------------------------------------------------------------------------
    ----------
    While not rs.eof
    <%=rs("ProductID")%> <%=rs("EEE")%> <%=rs("DDD")%>

    It sound perfect, is it possible to merge two query together ?
    How to do that ? Thks for everyone who help me out.


    Alan Guest

  2. Similar Questions and Discussions

    1. Query results - grouping
      I am sure there is an easy answer to this, but I am stumped! I have a query pulling in these fields: Area, Heading, Year, IndustryGroup (Year and...
    2. Help with grouping and sum in query
      My query looks like: <cfquery name="getTotals" datasource="db"> SELECT SUM(Extension) AS TotalDayBilling, DatePart(dw, TimesheetDate) AS...
    3. query/grouping question
      I have a database from which I'm trying to output both a count and a grouped list of items. For example, from the following mock table I would want...
    4. Newbie SQL Grouping Problem
      Hi All I know you're going to ask for table definitions and insert statements, but I think I'm just confused on the syntax of how to get this...
    5. Grouping Query Results
      "John Smith" <3rtwemte001@sneakemail.com> wrote in message news:1p5khvc1kd5qatngsm6mfvvcq0ehn763va@4ax.com... Well im not sure what you want. ...
  3. #2

    Default Re: Newbie about grouping query

    SELECT ProductID, SUM(Qty), Name FROM tablename
    WHERE Month='July' AND CID='A-Company'
    GROUP BY ProductID, Name
    ORDER BY ProductID

    --
    [url]http://www.aspfaq.com/[/url]
    (Reverse address to reply.)




    "Alan" <alan@neind.net> wrote in message
    news:er83it1bEHA.3596@tk2msftngp13.phx.gbl...
    > In my mdb, the data is as below :
    >
    > ProductID Qty Name Month CID
    > ------------------------------------------------------------------------
    > Product A 2 Harddisk July A-Company
    > Product A 5 Harddisk July A-Company
    > Product A 1 Harddisk June A-Company
    > Product B 3 cd-rom July A-Company
    > Product B 2 cd-rom July B-Company
    > Product B 2 cd-rom June B-Company
    >
    > When I select "A-Company" and "July" from listbox and submit,
    > I hope the result should return like this :
    >
    > A-Company. July
    > ProductID Qty Name
    > --------------------------------------
    > Product A 7 Harddisk (Grouped, the qty will sum up
    > automatically)
    > Product B 3 cd-rom
    >
    > First, I need to select the records of A-Company in July :
    >
    > SQL = "Select * From Invoice where month = " & Month
    > SQL = SQL & " And CID ='" & CID & "'"
    >
    > Then grouping the ProductID and sum the qty, also need to display the name
    :
    >
    > SQL = "Select ProductID , Sum(Qty) As DDD , First(Name) As EEE From
    > Invoice GROUP BY ProductID "
    >
    > Then use the script below :
    >
    > ProductID Name Qty
    > --------------------------------------------------------------------------
    --
    > ----------
    > While not rs.eof
    > <%=rs("ProductID")%> <%=rs("EEE")%> <%=rs("DDD")%>
    >
    > It sound perfect, is it possible to merge two query together ?
    > How to do that ? Thks for everyone who help me out.
    >
    >

    Aaron [SQL Server MVP] Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139