E-mail Print PDF

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value

Пример SQL

O_IdOrderDateOrderPriceCustomer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen

1
2
3
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000

Query Result:

Customer
Nilsen 1700

1
2
3
4
SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Hansen' OR Customer=
GROUP BY Customer
HAVING SUM(OrderPrice)>1500

Query Result:

Customer
Hansen 2000
Jensen 2000

Comments

 
+7 #1 Шелест Константин 2010-10-29 01:04 ERROR !!!

SELECT Customer,SUM(Or derPrice) FROM Orders - (Orders) and you call the table "Persons"!!! Correct the error and then lost millions of dummies, and why it is you !!!!? ...
Quote
 
 
-2 #2 Алекс 2010-11-19 16:39 Corrected, BASIL! Quote
 
 
+5 #3 KJHJ 2010-12-14 09:02 [Quote name = "Alex"] corrected, BASIL !!![/ quo
the fool
Quote
 
 
0 #4 KJHJ 2010-12-14 09:03 Lech do not sleep Quote
 
 
+1 #5 Arai 2011-03-30 06:58 All clear!! Thank you very much!! Quote
 
 
-1 #6 SERG_ 2011-04-11 13:49 Nitsche is not clear.

and displays a line with the amount in 1700 (Nilsen).
In the table itself and not close to that amount.
As it appeared, or calculate?
As for me, it should print 5 lines, with the exception of the row in which the sum of exactly 2000.
Or am I wrong?
Then explain.
Quote
 
 
-1 #7 TheDreamCatcher 2011-04-24 08:29 Quote:
- added to SQL because the WHERE clause can use functions.

WHERE YEAR (DATE) = 1996
What is this?? write competently
Quote
 
 
+2 #8 Yury 2011-05-17 12:46 to SERG_

believe SUM (OrderPrice) Ie sum (OrderPrice) for each Customer'a.

We have two entries for Nilsen: 1600 and 100. SUM (OrderPrice) =1700
Quote
 
 
0 #9 Andrey 2011-08-22 22:48 Excellent! All clear Quote
 

Authorization

Nice Ajax Poll

Which one of my extensions is the best?

Statistics

Translate

русскийitalianoDeutschEnglishLATVIANукраїнськаfrançaispolski
1

Advertisement