Re: help with complex SQL query
From: NotGiven (noname_at_nonegiven.net)
Date: 08/26/04
- Next message: theouimets_at_hotmail.com: "Re: Sendmail Question"
- Previous message: NotGiven: "Re: help with complex SQL query"
- In reply to: Bill Karwin: "Re: help with complex SQL query"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 25 Aug 2004 19:00:24 -0400
thanks - great suggestions and I'll try it.
I am using a hosting company and I've found most all hosting companies use
MySQL 3.34 - something to do with the licensing agreement changing after
that verison.
"Bill Karwin" <bill@karwin.com> wrote in message
news:cgis4u02744@enews3.newsguy.com...
> NotGiven wrote:
> > I have two tables, Client and Project, related by clientID.
> >
> > I want to display a table with total CURRENT projects and total
COMPLETED
> > projects per client.
> >
> > I'd like for it to show like this:
> >
> > Client Name Current Proj Completed Proj
> > Stan's Dormers 4 12
> > Larry Lincoln Dealership 2 1
> > ...
> >
> >
>
> As you've no doubt discovered, this is fairly easy if you only want one
> or the other status (current or completed). But if you want both,
> you're in effect trying to get totals grouped by status while outputting
> grouped by clientId. This is a logical impossibility.
>
> The only solution I can think of offhand is to use correlated subqueries
> in the select-list. You'd need to be running MySQL 4.1 for this query
> to work.
>
> SELECT C.ClientName,
> (SELECT COUNT(*) FROM Project P
> WHERE P.clientID = C.clientID AND P.Status = 'CURRENT')
> AS CurrentProj,
> (SELECT COUNT(*) FROM Project P
> WHERE P.clientID = C.clientID AND P.Status = 'COMPLETED')
> AS CompletedProj,
> FROM Client C;
>
> Another option, which should work in earlier versions of MySQL, is to
> prepare the right output in _almost_ the right format, and then reformat
> the results in your application code.
>
> SELECT C.ClientName, P.Status, COUNT(*) AS ProjCountByClientAndStatus
> FROM Project P INNER JOIN Client C ON P.clientID = C.clientID
> GROUP BY C.ClientName, P.Status;
>
> SQL was never meant to be a complete programming language. It assumes
> that you will do some manipulation of the results in an application.
>
> Regards,
> Bill K.
- Next message: theouimets_at_hotmail.com: "Re: Sendmail Question"
- Previous message: NotGiven: "Re: help with complex SQL query"
- In reply to: Bill Karwin: "Re: help with complex SQL query"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|