Skip to content

Instantly share code, notes, and snippets.

@ArCiGo
Created May 20, 2019 00:37
Show Gist options
  • Save ArCiGo/7c5991337238ff6e0fab2c3566cbd0ff to your computer and use it in GitHub Desktop.
Save ArCiGo/7c5991337238ff6e0fab2c3566cbd0ff to your computer and use it in GitHub Desktop.
SELECT *
FROM [ORDER]
/** Output **/
Id OrderDate OrderNumber CustomerId TotalAmount
----------- ----------------------- ----------- ----------- ---------------------------------------
1 2012-07-04 00:00:00.000 542378 85 440.00
2 2012-07-05 00:00:00.000 542379 79 1863.40
3 2012-07-08 00:00:00.000 542380 34 1813.00
4 2012-07-08 00:00:00.000 542381 84 670.80
5 2012-07-09 00:00:00.000 542382 76 3730.00
6 2012-07-10 00:00:00.000 542383 34 1444.80
7 2012-07-11 00:00:00.000 542384 14 625.20
8 2012-07-12 00:00:00.000 542385 68 2490.50
9 2012-07-15 00:00:00.000 542386 88 517.80
10 2012-07-16 00:00:00.000 542387 35 1119.90
...
DECLARE @vFirstName AS VARCHAR(MAX),
@vLastName AS VARCHAR(MAX),
@vMoneySpent AS MONEY
DECLARE @CustomerMoneySpent AS CURSOR
SET @CustomerMoneySpent = CURSOR FOR
SELECT cus.FirstName,
cus.LastName,
SUM(ord.TotalAmount)
FROM Customer cus
LEFT JOIN [ORDER] ord ON cus.Id = ord.CustomerId
GROUP BY cus.FirstName, cus.LastName
OPEN @CustomerMoneySpent;
FETCH NEXT FROM @CustomerMoneySpent INTO @vFirstName, @vLastName, @vMoneySpent;
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @vFirstName + ' ' + @vLastName + ' ' + CAST(@vMoneySpent AS VARCHAR(10));
FETCH NEXT FROM @CustomerMoneySpent INTO @vFirstName, @vLastName, @vMoneySpent;
END
CLOSE @CustomerMoneySpent;
DEALLOCATE @CustomerMoneySpent;
/** Output **/
Warning: Null value is eliminated by an aggregate or other SET operation.
Paolo Accorti 1545.70
Pedro Afonso 3810.75
Maria Anders 4596.20
Miguel Angel Paolino 10812.15
Victoria Ashworth 6089.90
Bernardo Batista 6973.63
Helen Bennett 6146.30
Christina Berglund 26968.15
Jonas Bergulfsen 5735.15
Art Braunschweiger 12489.70
Elizabeth Brown 1719.10
Alejandra Camino 1467.29
Pascale Cartrain 24704.40
Lúcia Carvalho 30226.10
Francisco Chang 100.80
Frédérique Citeaux 19088.00
Philip Cramer 31745.75
Simon Crowther 649.00
Aria Cruz 4438.90
Isabel de Castro 5317.10
Ann Devon 15033.66
Catherine Dewey 10430.58
Anabela Domingues 7310.62
Guillermo Fernández 4242.20
Alexander Feuer 5042.20
André Fonseca 8702.23
Peter Franken 28722.71
Jean Fresnière 32203.90
Carlos González 17825.06
Sergio Gutiérrez 2844.10
Thomas Hardy 13806.50
Paul Henriot 1480.00
Carlos Hernández 23611.58
Michael Holz 20033.20
Palle Ibsen 16643.80
Felipe Izquierdo 17889.55
Karl Jablonski 29073.45
Karin Josephs 4954.00
Matti Karttunen 3161.35
Horst Kloss 117483.39
Pirkko Koskitalo 16617.10
Hari Kumar 17172.05
Janine Labrune 1615.90
Maria Larsson 32555.55
Yoshi Latimer 3063.20
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment