Closed Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 19

Thread: Help retrieving # of records per group

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

    Help retrieving # of records per group

    Hi All,

    With my current code the data displays as...how can I get the info in blue/bold (# of records) ?

    Code:
    PartNo. - Desc (# of eq - 3)
    -Desc-EQ#
    -Desc-EQ#
    -Desc-EQ#
    PartNo.1 - Desc1 (# of eq - 2)
    -Desc1-EQ#
    -Desc1-EQ#
    ETC...
    
    Code:
    <%
    iRecCount = rs.RecordCount
    if iNumRecs <> -1 And iRecCount > iStartRec+iNumRecs-1 then
    	iStopRec = iStartRec+iNumRecs-1
    else
    	iStopRec = iRecCount
    end if	
    %>
    
    <%
    Dim strLastFLName
    %>
    
    <TABLE border="3" width=900>
    <col width=300><col width=50>
    
          <TR bgcolor="#6699CC">
    <TD><B>Description</B></TD>
    <TD><B>EQNumber</B></TD>
    </TR>
    
    </CENTER>
    
    <%
    strLastFLName = ""
    
    	do while not rs.EOF
    	if i > iStopRec then
    		exit do
    	end if
    
    	strCurrentFLName = rs("PartNo")
    	strCurrentGrp = rs("Desc")
    	
    	If strCurrentFLName<>strLastFLName Then	
    	Response.Write("<tr bgcolor=#999999><td align=""left"" colwidth=""100"" colspan=""2""><font color=""white"">" & strCurrentFLName & " -  " & strCurrentGrp & "</font></td></tr>")
    	
    	End If
    	strLastFLName = strCurrentFLName
    
    
    	
    	i = i + 1
    
    %>
    
    <%
    IF i Mod 2 = 0 THEN 
    %>
    <TR bgcolor="#f0e68c">
    <%
    ELSE
    %>
    <TR bgcolor="#FFFFCC">
    <%
    END IF
    %>
    
    <TD align=left><%=rs("Desc")%> &nbsp;</TD>
    <TD align=right><%=rs("EQNumber")%> &nbsp;</TD>
    </TR>
    	
    <%	
    rs.MoveNext
    loop
    %>
    

  2. #2
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Since you want the subcount at the top of the list, I would just go and do this a 2 queries -- the first with the appropriate count() and GROUP BY, and the second to get your detail info.

    Or, you could do the count() and GROUP BY query and UNION the results (with appropriate NULL columns as necessary) with the detail query. Add a dummy column with a value of 1 for the summary data and 2 for the detail data and then sort the whole thing as ORDER BY DESCRIPTION, DUMMY_COLUMN. Something like:
    Code:
    select description, count(eq_number) eq_count, 1 as dummy_column
    from foo
    group by description
    union 
    select description, eq_number, 2 as dummy_column
    from foo
    order by description, dummy_column
    
    Last edited by Wolffy; April 13th, 2009 at 05:51 PM.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

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

    Wolf wolf Wolffffffy

    Should I be doing this on my asp sql statement or on a view in sql2000?

    Also, could I use count on partno. with a Distinct?

    Thanks much!
    Last edited by Rebelle; April 14th, 2009 at 03:19 PM.

  4. #4
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    My preference is for creating a View. I actually never use a query against the SQL Tables directly -- only through a view.

    Also, could I use count on partno. with a Distinct?
    Huh? You lost me. GROUP BY usually takes the place of Distinct in aggregation. If you need to add PartNo into the mix, just GROUP BY it as well.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

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

    Hey wolffy ... sorry i'm having a hard time with this one...

    Ok, in sql2000 analyzer I'm getting the following error:

    Code:
    Server: Msg 205, Level 16, State 1, Line 1
    All queries in an SQL statement containing a UNION operator must have an equal number of expressions in their target lists.
    
    Here is what I have:
    Code:
    SELECT     Description,EQNumber,COUNT(EQNumber) AS eq_count, 1 as dummy
    FROM         dbo.tblShip
    GROUP BY Description,EQNumber
    union
    select Description,EQNumber, 2 as dummy
    FROM dbo.tblShip
    ORDER BY Description, dummy
    

  6. #6
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Just add the following to the second query in your UNION
    Code:
    SELECT Description, EQNumber, NULL, 2 as DUMMY
    
    The NULL will take the place of eq_count so that the number of columns is equal in both queries
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

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

    okies that took care of the error...but i'm still trying to understand....when i look at the results now, it looks like:

    Code:
    Description -- EQNumber -- eq_count -- dummy
    Desc1            3235            1           1
    Desc1            3253            NULL      2
    Desc2            1113            1           1
    Desc2            1155            1           1
    Desc2            1156            NULL      2
    Desc2            1535            NULL      2
    ETC...
    
    I'm confused...shouldn't it show 2 for Desc1 and 4 for Desc2?

  8. #8
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    I don't think you want to GROUP BY EQNumber in the first query in the union -- you just wanted the total number of EQNumbers for a given Description, right?

    If you take EQNumber out of the first query, take the NULL out of the second select.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

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

    Yes...closer now.... but its off by 1 number...ex:

    desc1 has 2 records but eq_count shows 1
    desc2 has 4 records but eq_count shows 3
    desc3 has 8 records but eq_count shows 7

    so its skipping 1 ...plzzz help

    i just noticed that equipment#'s and the count#'s show in the same column (eq_count)...so that column data reads:
    Code:
    eq_count
    1
    3242
    8
    8298
    8299
    9000
    9001
    9002
    9003
    9004
    3
    3232
    3244
    
    Last edited by Rebelle; April 14th, 2009 at 05:12 PM.

  10. #10
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Well, that's interesting. Just add 1 to the Count then -- only kidding.

    Is the query you posted the EXACT query you are testing with? If not, NULLs can cause interesting things to happen.

    Here's the data I tested with
    Code:
    description eq_number
    ----------- -----------
    AAA         10
    AAA         20
    AAA         30
    AAA         40
    BBB         10
    BBB         20
    BBB         30
    CCC         10
    CCC         50
    
    And the results
    Code:
    description eq_count    dummy_column
    ----------- ----------- ------------
    AAA         4           1
    AAA         10          2
    AAA         20          2
    AAA         30          2
    AAA         40          2
    BBB         3           1
    BBB         10          2
    BBB         20          2
    BBB         30          2
    CCC         2           1
    CCC         10          2
    CCC         50          2
    
    If you want to zip-up your test data, I can look at the problem on the hoof.

    Could also try replacing UNION with UNION ALL in this case.
    Last edited by Wolffy; April 14th, 2009 at 05:15 PM.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

Closed Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Group by Problem
    By todd2006 in forum ASP Development
    Replies: 1
    Last Post: March 11th, 2009, 10:41 PM
  2. Report date range as years in option group
    By tuxalot in forum Microsoft Access
    Replies: 8
    Last Post: March 9th, 2009, 11:47 PM
  3. order by group by problem
    By todd2006 in forum SQL Development
    Replies: 9
    Last Post: March 9th, 2009, 03:16 PM
  4. Sql group by
    By BLaaaaaaaaaarche in forum SQL Development
    Replies: 4
    Last Post: February 12th, 2009, 12:27 PM
  5. Summing for each group only
    By Rebelle in forum ASP Development
    Replies: 13
    Last Post: June 5th, 2008, 02:22 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