How do I do this correctly? Want to use sum inside sa_rowgenerator.
CREATE TABLE t ( mat NVARCHAR(20), quantity INTEGER NOT NULL ); INSERT t VALUES ( 'ABC', 10 ); INSERT t VALUES ( 'ABC', 5 ); INSERT t VALUES ( 'ABC21', 7 ); INSERT t VALUES ( 'ABC21', 13 ); COMMIT; SELECT NoOfRovs.row_num FROM t CROSS APPLY sa_rowgenerator(1, sum(t.quantity)) NoOfRovs WHERE mat = 'ABC';
Request clarification before answering.
The SUM has to be calculated in the SELECT FROM t and the result can then be passed to sa_rowgenerator via CROSS APPLY ( and congratulations on showing another cool use for CROSS APPLY 🙂
CREATE TABLE t (
mat NVARCHAR(20),
quantity INTEGER NOT NULL );
INSERT t VALUES ( 'ABC', 10 );
INSERT t VALUES ( 'ABC', 5 );
INSERT t VALUES ( 'ABC21', 7 );
INSERT t VALUES ( 'ABC21', 13 );
COMMIT;
SELECT NoOfRovs.row_num
FROM ( SELECT SUM ( t.quantity ) AS sum_quantity
FROM T
WHERE mat = 'ABC'
) AS sum_t
CROSS APPLY sa_rowgenerator ( 1, sum_t.sum_quantity ) AS NoOfRovs;
row_num
-----------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(15 rows)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.