Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Need Help on SQL query about sum

$
0
0
Hey guys,
New to the site, new to writing SQL queries and have no background in this type of thing.
But I've been googling my way to victory for the most part.

I'm struggling on probably a very simple problem.
I'd like to Sum the contents of a column, if a different value in the row is the same as in another... That was worded horribly - how about an illustration:

Input Table:

Code:

Unit ID Value    orderid    orderdate
1        20          12    20121121
1        30          13    20121121
1        20          14    20121121
1        30          15    20121120
1        10          16    20121121
2        51          17    20121113
3        50          18    20121114
4        20          19    20121112
4        30          20    20121112
4        10          21    20121112
4          5          22    20121112
4        50          23    20121112
5          5          25    20121110
5        50          26    20121111
6          5          27    20121112
6          3          28    20121112
6        50          29    20121114
7        35          30    20121112
7          4          31    20121112


So basically I'm looking for an output when I run the script that shows
aggregated of Value must >= 50 after sum up with ID.The flag indicator will tag with 'Y' if only when single unit >= 50 without sum with each other.
The single unit >=50 will shown orderId and orderdate, not single unit >=50(sum up with other value) will shown nothing.
Expected output :
Code:


1. ID Value                      Indicator flag OrderID orderDate
2. 1  110(20+30+20+30+10)  N
3. 2 51                            Y                17 20121113
4. 3 50                            Y                18 20121114
5. 4 115(20+30+10+5+50)      N
6. 5 55 (50+5)                    N
7. 6 58 (50+8)                    N


And various other simplistic queries, but at this point I'm willing to admit that I haven't a clue what I'm doing.
FYI, the platform I using is IBM DB2 I-series.

For your convenience I have create the code below to generated the table for testing.You may can refer it.
Code:

CREATE TABLE #Test (
        ID int,Value int,orderID int,Orderdate int)
INSERT #Test VALUES
(1,20,12,20121121),
(1,30,13,20121121),
(1,20,14,20121121),
(1,30,15,20121120),
(1,10,16,20121121),
(2,51,17,20121113),
(3,50,18,20121114),
(4,20,19,20121112),
(4,30,20,20121112),
(4,10,21,20121112),
(4,5,22,20121112),
(4,50,23,20121112),
(5,5,25,20121110),
(5,50,26,20121111),
(6,5,27,20121112),
(6,3,28,20121112),
(6,50,29,20121114),
(7,35,30,20121112),
(7,4,31,20121112)

SELECT *
FROM #Test

Any help would be appreciated,
Thanks!

Viewing all articles
Browse latest Browse all 13329

Trending Articles