Re: GROUP BY's on 3 tables in one SELECT?



On Thu, 18 Aug 2005 16:12:02 -0700, Martin S. wrote:

(snip)
>Unfortunately, the tables were not created by me (I just want to extract
>something), so I can't include any CREATE TABLE statement.

Hi Martin,

The website I refered you to includes a description of how you can
easily create a script from your database. It also has a link to a
script that will generate INSERT statements from existing data.

The sample data you posted doesn't help much either, as their formatting
is off (at least in my news reader). There's a reason I asked you to
post the sample data as INSERT statements, you know...

Anyway, based on your narrative and my limited understanding of the
sample data you posted, the following might work:

SELECT T1.Person_ID, T1.Product_ID, MAX(Costs),
(SELECT SUM(T2b.Balancies)
FROM T3, T2 as T2b
WHERE T2b.PersonID = T1.PersonID
AND T2b.Product_ID = T3.Product_ID
AND T3.Product_Type = 'BB')
FROM T1, T2
WHERE (T1.Product_ID = T3.Product_ID) AND (T3.Product_Type='AA')
GROUP BY T1.Person_ID, T1.Product_ID

(untested)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
.



Relevant Pages

  • Re: Looping/Counting
    ... second script bins the information for each 10 percent of the data. ... The last decile is further broken down into ... It turns out that for many deposits, 1% of the sample data ...
    (comp.lang.awk)
  • Re: Need help
    ... Hi Mjuricek, ... enough information to know what the script does. ... Include all constraints but exclude irrelevant ... based on the sample data you gave. ...
    (microsoft.public.sqlserver.programming)
  • Re: Splitting and comparing file names
    ... with the general format would really be much preferred... ... sample data would be nice. ... You have a case of premature declaration. ... previous script that required it because of a write to the directory. ...
    (perl.beginners)
  • Re: script help - stripping trailing spaces in exisitng script
    ... below script on the below sample data. ... The only problem is the trailing spaces at the end of each line. ... A quick web search put me onto ... original to just include all this in a single script. ...
    (comp.lang.perl.misc)
  • Re: Distinct Max Value?
    ... that is not a Create Table script. ... some INSERT statements containing sample data so we can see how the data is ... > value for each runnumber group. ... This email account is my spam trap so I ...
    (microsoft.public.sqlserver.programming)