separating international from us states
am 09.11.2005 20:49:42 von laredotornado
Hello, I'm using MySQL 3.23. If I have a table with two columns
REGION VARCHAR(16),
SALES FLOAT UNSIGNED
How can I write an SQL SELECT statement to give me the sales per state
-- where the "REGION" column would hold the two-letter state
abbreviation -- and sales per everything else. So a possible result
set would look like
Region Total_Sales
--------- ----------------
CA 10000.00
TX 5000.00
International 3000.00
NJ 1000.00
Your help is much appreciated - Dave
Re: separating international from us states
am 09.11.2005 21:12:00 von Hilarion
> Hello, I'm using MySQL 3.23. If I have a table with two columns
>
> REGION VARCHAR(16),
> SALES FLOAT UNSIGNED
>
> How can I write an SQL SELECT statement to give me the sales per state
> -- where the "REGION" column would hold the two-letter state
> abbreviation -- and sales per everything else. So a possible result
> set would look like
>
> Region Total_Sales
> --------- ----------------
> CA 10000.00
> TX 5000.00
> International 3000.00
> NJ 1000.00
Please read the manual. What you are asking for is a basic use of agregation.
SELECT region, SUM( sales ) AS total_sales
FROM table_name
-- You can stick WHERE clause here to limit what gets into agregarion.
-- Eg.: WHERE region = 'CA' AND (sales BETWEEN 500 AND 1000)
GROUP BY region
-- You can stick HAVING clause here to limit what is returned after
-- it got agregated, so you can use agregated data in theis clause.
-- Eg.: HAVING AVG( sales ) BETWEEN 700 AND 900
ORDER BY region
Hilarion
Re: separating international from us states
am 09.11.2005 22:43:25 von IchBin
laredotornado@zipmail.com wrote:
> Hello, I'm using MySQL 3.23. If I have a table with two columns
>
> REGION VARCHAR(16),
> SALES FLOAT UNSIGNED
>
> How can I write an SQL SELECT statement to give me the sales per state
> -- where the "REGION" column would hold the two-letter state
> abbreviation -- and sales per everything else. So a possible result
> set would look like
>
> Region Total_Sales
> --------- ----------------
> CA 10000.00
> TX 5000.00
> International 3000.00
> NJ 1000.00
>
> Your help is much appreciated - Dave
>
Try
SELECT REGION, SUM(SALES) AS Total_Sales
FROM YourTableName
GROUP BY REGION
ORDER BY REGION;
--
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
____________________________________________________________ ______________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)