Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sql Group and total
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01451592
Message ID:
01451596
Views:
55
>I have a table as folows:
>
>Inv No. Continent Country INV_VAL Town Units_SOLD
>
>1 EUROPE UK 100 LONDON 10
>1 EUROPE UK 100 MANCHESTER 3
>
>2 EUROPE UK 500 LIVERPOOL 1
>2 EUROPE UK 500 BIRMINGHAM 4
>
>
>3 ASIA CHINA 300 BEJING 23
>3 ASIA CHINA 300 SHANGHAI 12
>
>
>I want to do a sql select which totals and groups by Continent/Country but ensure
> that the INV_VAL column is also totalled correctly.
>If I do a sum(Inv_Val) I will get 1200 for Europe instead of 600 (100 + 500)
>The Inv_Val already has the total INV_VAL for each invoice.
>
>The resuting table that I need is
>Continent Country CTRY_SALES POP_TOTAL
>EUROPE UK 600 18
>ASIA CHINA 300 35
>
>I have'nt been able to figure out a way to do this with one Sql Seelct command
> and am wondering if its possible.
>Tia,
>Gerard
CREATE CURSOR crsTest (Continent varchar(20), Country varchar(10), inv_tot int, Town varchar(20), Units_SOLD int)
INSERT INTO crsTest VALUES ("EUROPE","UK",100,"LONDON", 10)
INSERT INTO crsTest VALUES ("EUROPE","UK",100,"MANCHESTER", 3)

INSERT INTO crsTest VALUES ("EUROPE","UK",500,"LIVERPOOL", 1)
INSERT INTO crsTest VALUES ("EUROPE","UK",500,"BIRMINGHAM", 4)


INSERT INTO crsTest VALUES ("ASIA","CHINA",300,"BEJING", 23)
INSERT INTO crsTest VALUES ("ASIA","CHINA",300,"SHANGHAI", 12)

SELECT Continent,;
       Country,;
       SUM(DISTINCT inv_tot) AS inv_tot,;
       SUM(Units_SOLD) AS Units_SOLD;
FROM crsTest;
GROUP BY Continent,;
         Country;
INTO CURSOR aaa
BROWSE NORMAL
But there is something wrong with your design. Why you should skip the values for London or Manchester?
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Reply
Map
View

Click here to load this message in the networking platform