Re: counting with reset at group by? change

From: CCA Dave (david_at_notreally.co.uk)
Date: 03/17/04


Date: Wed, 17 Mar 2004 14:30:31 -0000

Sorry, I thought the explanation was going to be simple, so I simplified the
query.

My actual query is
==========================
select Account , Product

 from heads inner join lines

  on heads.[PK] = lines.[FK]

order by account, product

==========================
and I want a 3rd "selection" which counts the product for the account

I don't quite follow how SQL knows what "a" is.....

Other than that, yes that's exactly what I want: thanks !

"Dan Guzman" <danguzman@nospam-earthlink.net> wrote in message
news:#ab9ecBDEHA.2768@tk2msftngp13.phx.gbl...
> > Is this possible with count(*) and group by????
>
> One method is to use a correlated subquery like the example below. GROUP
BY
> isn't needed in this case.
>
> CREATE TABLE MyTable
> (
> Head int NOT NULL,
> Line CHAR(1) NOT NULL,
> CONSTRAINT PK_MyTable PRIMARY KEY (Head, Line)
> )
> INSERT INTO MyTable VALUES(1, 'A')
> INSERT INTO MyTable VALUES(1, 'B')
> INSERT INTO MyTable VALUES(1, 'C')
> INSERT INTO MyTable VALUES(2, 'A')
> INSERT INTO MyTable VALUES(2, 'B')
> INSERT INTO MyTable VALUES(3, 'A')
> INSERT INTO MyTable VALUES(3, 'B')
> INSERT INTO MyTable VALUES(3, 'C')
>
> SELECT
> Head,
> Line,
> (
> SELECT COUNT(*)
> FROM MyTable AS b
> WHERE a.Head = b.Head AND
> a.Line > b.Line) + 1 AS MyCount
> FROM MyTable a
> ORDER BY
> Head,
> Line
>
> --
> Hope this helps.
>
> Dan Guzman
> SQL Server MVP
>
> "CCA Dave" <david@notreally.co.uk> wrote in message
> news:c399nt$p8s$1$8300dec7@news.demon.co.uk...
> > Hi all,
> >
> > coming from a VMS powerhouse background, we used to have a count
function
> > that we could reset when a repeating value changed. Is there a way to do
> > this in SQL?
> >
> > What I want to do is:
> >
> > with data
> >
> > Head Line
> > 1 A
> > 1 B
> > 1 C
> > 2 A
> > 2 B
> > 3 A
> > 3 B
> > 3 C
> >
> > I want to count
> >
> > head line count
> >
> > 1 A 1
> > 1 B 2
> > 1 C 3
> > 2 A 1
> > 2 B 2
> > 3 A 1
> > 3 B 2
> > 3 C 3
> >
> > Is this possible with count(*) and group by????
> >
> > Thanks in advance
> >
> >
>
>



Relevant Pages

  • Re: Converting Perl Web Report to Python
    ... by allowing the user to pass in an SQL query parameter to the sql ...
    (comp.lang.python)
  • Re: Mail Default User Address
    ... multiple "send As" features in one account unless you constantly change the ... > addresses they recieve from the POP3 and the reply from one of them ... > only have it set in the POP3 connector to put e-mail from both in his ... > breaking something and getting the wrath poured out upon my head from ...
    (microsoft.public.windows.server.sbs)
  • Re: Is war the ultimate adventure of a lifetime?
    ... definitely that UK officers get held to account in the military I work ... Certainly in the US military the officers are held to account. ... And instead invested quasi-Royal powers in the President. ... whether Canada keeps the Queen as head of state or not. ...
    (rec.aviation.military)
  • Re: Christmas Allowance
    ... I'd like to know how do I account for this within my accounting ... Probably something like Staff Welfare ... costs should be reasonable and available to all. ... per head spent on the executives and £1.50 per head on the staff, ...
    (uk.business.accountancy)
  • Re: Christmas Allowance
    ... I'd like to know how do I account for this within my accounting ... Probably something like Staff Welfare ... costs should be reasonable and available to all. ... per head spent on the executives and £1.50 per head on the staff, ...
    (uk.business.accountancy)