I have the following table called 'mdx'
yearnumber yearstart yearend weeknumber quantity
1 11-22-15 11-19-16 1 1826
1 11-22-15 11-19-16 2 1225
1 11-22-15 11-19-16 3 452
1 11-22-15 11-19-16 4 276
1 11-22-15 11-19-16 5 673
1 11-22-15 11-19-16 6 1986
1 11-22-15 11-19-16 7 3806
1 11-22-15 11-19-16 8 3608
1 11-22-15 11-19-16 9 3841
1 11-22-15 11-19-16 10 3356
1 11-22-15 11-19-16 11 3436
1 11-22-15 11-19-16 12 3437
1 11-22-15 11-19-16 13 3611
1 11-22-15 11-19-16 14 3354
1 11-22-15 11-19-16 15 3743
1 11-22-15 11-19-16 16 3508
1 11-22-15 11-19-16 17 3440
1 11-22-15 11-19-16 18 3467
1 11-22-15 11-19-16 19 2816
1 11-22-15 11-19-16 20 2998
1 11-22-15 11-19-16 21 2796
1 11-22-15 11-19-16 22 2624
1 11-22-15 11-19-16 23 2458
1 11-22-15 11-19-16 24 1924
1 11-22-15 11-19-16 25 1816
1 11-22-15 11-19-16 26 1671
1 11-22-15 11-19-16 27 1111
1 11-22-15 11-19-16 28 1036
1 11-22-15 11-19-16 29 746
1 11-22-15 11-19-16 30 590
1 11-22-15 11-19-16 31 665
1 11-22-15 11-19-16 32 393
1 11-22-15 11-19-16 33 442
1 11-22-15 11-19-16 34 2042
1 11-22-15 11-19-16 35 387
1 11-22-15 11-19-16 36 24
1 11-22-15 11-19-16 37 30
1 11-22-15 11-19-16 38 44
1 11-22-15 11-19-16 39 113
1 11-22-15 11-19-16 40 23
1 11-22-15 11-19-16 41 18
1 11-22-15 11-19-16 42 1624
1 11-22-15 11-19-16 43 3760
1 11-22-15 11-19-16 44 3645
1 11-22-15 11-19-16 45 3964
1 11-22-15 11-19-16 46 3807
1 11-22-15 11-19-16 47 4048
1 11-22-15 11-19-16 48 3862
1 11-22-15 11-19-16 49 3677
1 11-22-15 11-19-16 50 3695
1 11-22-15 11-19-16 51 3871
1 11-22-15 11-19-16 52 3686
2 11-23-14 11-21-15 1 3694
2 11-23-14 11-21-15 2 4240
2 11-23-14 11-21-15 3 4287
2 11-23-14 11-21-15 4 4751
2 11-23-14 11-21-15 5 5536
2 11-23-14 11-21-15 6 3712
2 11-23-14 11-21-15 7 3367
2 11-23-14 11-21-15 8 3598
2 11-23-14 11-21-15 9 3461
2 11-23-14 11-21-15 10 3334
2 11-23-14 11-21-15 11 3325
2 11-23-14 11-21-15 12 3313
2 11-23-14 11-21-15 13 3417
2 11-23-14 11-21-15 14 3396
2 11-23-14 11-21-15 15 3326
2 11-23-14 11-21-15 16 3274
2 11-23-14 11-21-15 17 3473
2 11-23-14 11-21-15 18 3442
2 11-23-14 11-21-15 19 3293
2 11-23-14 11-21-15 20 3114
2 11-23-14 11-21-15 21 3116
2 11-23-14 11-21-15 22 3664
2 11-23-14 11-21-15 23 3165
2 11-23-14 11-21-15 24 2896
2 11-23-14 11-21-15 25 3113
2 11-23-14 11-21-15 26 3183
2 11-23-14 11-21-15 27 2912
2 11-23-14 11-21-15 28 3043
2 11-23-14 11-21-15 29 3046
2 11-23-14 11-21-15 30 3033
2 11-23-14 11-21-15 31 3212
2 11-23-14 11-21-15 32 3294
2 11-23-14 11-21-15 33 3210
2 11-23-14 11-21-15 34 3303
2 11-23-14 11-21-15 35 3229
2 11-23-14 11-21-15 36 3313
2 11-23-14 11-21-15 37 3338
2 11-23-14 11-21-15 38 3432
2 11-23-14 11-21-15 39 3340
2 11-23-14 11-21-15 40 3597
2 11-23-14 11-21-15 41 3628
2 11-23-14 11-21-15 42 3412
2 11-23-14 11-21-15 43 3332
2 11-23-14 11-21-15 44 3313
2 11-23-14 11-21-15 45 3637
2 11-23-14 11-21-15 46 3498
2 11-23-14 11-21-15 47 3230
2 11-23-14 11-21-15 48 2591
2 11-23-14 11-21-15 49 2937
2 11-23-14 11-21-15 50 2964
2 11-23-14 11-21-15 51 2740
2 11-23-14 11-21-15 52 1982
3 11-24-13 11-22-14 1 4607
3 11-24-13 11-22-14 2 5018
3 11-24-13 11-22-14 3 5054
3 11-24-13 11-22-14 4 5834
3 11-24-13 11-22-14 5 5944
3 11-24-13 11-22-14 6 3992
3 11-24-13 11-22-14 7 4298
3 11-24-13 11-22-14 8 4307
3 11-24-13 11-22-14 9 4187
3 11-24-13 11-22-14 10 4269
3 11-24-13 11-22-14 11 4012
3 11-24-13 11-22-14 12 4392
3 11-24-13 11-22-14 13 4536
3 11-24-13 11-22-14 14 4173
3 11-24-13 11-22-14 15 4053
3 11-24-13 11-22-14 16 3882
3 11-24-13 11-22-14 17 4053
3 11-24-13 11-22-14 18 3990
3 11-24-13 11-22-14 19 3992
3 11-24-13 11-22-14 20 3489
3 11-24-13 11-22-14 21 3797
3 11-24-13 11-22-14 22 3355
3 11-24-13 11-22-14 23 3438
3 11-24-13 11-22-14 24 3275
3 11-24-13 11-22-14 25 3403
3 11-24-13 11-22-14 26 3345
3 11-24-13 11-22-14 27 3282
3 11-24-13 11-22-14 28 3378
3 11-24-13 11-22-14 29 3286
3 11-24-13 11-22-14 30 3304
3 11-24-13 11-22-14 31 3179
3 11-24-13 11-22-14 32 3248
3 11-24-13 11-22-14 33 3469
3 11-24-13 11-22-14 34 3497
3 11-24-13 11-22-14 35 3360
3 11-24-13 11-22-14 36 3217
3 11-24-13 11-22-14 37 3170
3 11-24-13 11-22-14 38 3462
3 11-24-13 11-22-14 39 3528
3 11-24-13 11-22-14 40 3466
3 11-24-13 11-22-14 41 3546
3 11-24-13 11-22-14 42 3549
3 11-24-13 11-22-14 43 3350
3 11-24-13 11-22-14 44 3286
3 11-24-13 11-22-14 45 3598
3 11-24-13 11-22-14 46 3725
3 11-24-13 11-22-14 47 3573
3 11-24-13 11-22-14 48 3681
3 11-24-13 11-22-14 49 3378
3 11-24-13 11-22-14 50 3481
3 11-24-13 11-22-14 51 3477
3 11-24-13 11-22-14 52 3673
I'm trying to generate xml in sql server that will loo like this
<years>
<year yearnumber="1" yearstart="11-22-15" yearend="11-19-16">
<week weeknumber="1" quantity="10127" />
<week weeknumber="10" quantity="10959" />
<week weeknumber="11" quantity="10773" />
<week weeknumber="12" quantity="11142" />
<week weeknumber="13" quantity="11564" />
<week weeknumber="14" quantity="10923" />
<week weeknumber="15" quantity="11122" />
<week weeknumber="16" quantity="10664" />
<week weeknumber="17" quantity="10966" />
<week weeknumber="18" quantity="10899" />
<week weeknumber="19" quantity="10101" />
<week weeknumber="2" quantity="10483" />
<week weeknumber="20" quantity="9601" />
<week weeknumber="21" quantity="9709" />
<week weeknumber="22" quantity="9643" />
<week weeknumber="23" quantity="9061" />
<week weeknumber="24" quantity="8095" />
<week weeknumber="25" quantity="8332" />
<week weeknumber="26" quantity="8199" />
<week weeknumber="27" quantity="7305" />
<week weeknumber="28" quantity="7457" />
<week weeknumber="29" quantity="7078" />
<week weeknumber="3" quantity="9793" />
<week weeknumber="30" quantity="6927" />
<week weeknumber="31" quantity="7056" />
<week weeknumber="32" quantity="6935" />
<week weeknumber="33" quantity="7121" />
<week weeknumber="34" quantity="8842" />
<week weeknumber="35" quantity="6976" />
<week weeknumber="36" quantity="6554" />
<week weeknumber="37" quantity="6538" />
<week weeknumber="38" quantity="6938" />
<week weeknumber="39" quantity="6981" />
<week weeknumber="4" quantity="10861" />
<week weeknumber="40" quantity="7086" />
<week weeknumber="41" quantity="7192" />
<week weeknumber="42" quantity="8585" />
<week weeknumber="43" quantity="10442" />
<week weeknumber="44" quantity="10244" />
<week weeknumber="45" quantity="11199" />
<week weeknumber="46" quantity="11030" />
<week weeknumber="47" quantity="10851" />
<week weeknumber="48" quantity="10134" />
<week weeknumber="49" quantity="9992" />
<week weeknumber="5" quantity="12153" />
<week weeknumber="50" quantity="10140" />
<week weeknumber="51" quantity="10088" />
<week weeknumber="52" quantity="9341" />
<week weeknumber="6" quantity="9690" />
<week weeknumber="7" quantity="11471" />
<week weeknumber="8" quantity="11513" />
<week weeknumber="9" quantity="11489" />
</year>
<year yearnumber="2" yearstart="11-23-14" yearend="11-21-15">
<week weeknumber="1" quantity="10127" />
<week weeknumber="10" quantity="10959" />
<week weeknumber="11" quantity="10773" />
<week weeknumber="12" quantity="11142" />
<week weeknumber="13" quantity="11564" />
<week weeknumber="14" quantity="10923" />
<week weeknumber="15" quantity="11122" />
<week weeknumber="16" quantity="10664" />
<week weeknumber="17" quantity="10966" />
<week weeknumber="18" quantity="10899" />
<week weeknumber="19" quantity="10101" />
<week weeknumber="2" quantity="10483" />
<week weeknumber="20" quantity="9601" />
<week weeknumber="21" quantity="9709" />
<week weeknumber="22" quantity="9643" />
<week weeknumber="23" quantity="9061" />
<week weeknumber="24" quantity="8095" />
<week weeknumber="25" quantity="8332" />
<week weeknumber="26" quantity="8199" />
<week weeknumber="27" quantity="7305" />
<week weeknumber="28" quantity="7457" />
<week weeknumber="29" quantity="7078" />
<week weeknumber="3" quantity="9793" />
<week weeknumber="30" quantity="6927" />
<week weeknumber="31" quantity="7056" />
<week weeknumber="32" quantity="6935" />
<week weeknumber="33" quantity="7121" />
<week weeknumber="34" quantity="8842" />
<week weeknumber="35" quantity="6976" />
<week weeknumber="36" quantity="6554" />
<week weeknumber="37" quantity="6538" />
<week weeknumber="38" quantity="6938" />
<week weeknumber="39" quantity="6981" />
<week weeknumber="4" quantity="10861" />
<week weeknumber="40" quantity="7086" />
<week weeknumber="41" quantity="7192" />
<week weeknumber="42" quantity="8585" />
<week weeknumber="43" quantity="10442" />
<week weeknumber="44" quantity="10244" />
<week weeknumber="45" quantity="11199" />
<week weeknumber="46" quantity="11030" />
<week weeknumber="47" quantity="10851" />
<week weeknumber="48" quantity="10134" />
<week weeknumber="49" quantity="9992" />
<week weeknumber="5" quantity="12153" />
<week weeknumber="50" quantity="10140" />
<week weeknumber="51" quantity="10088" />
<week weeknumber="52" quantity="9341" />
<week weeknumber="6" quantity="9690" />
<week weeknumber="7" quantity="11471" />
<week weeknumber="8" quantity="11513" />
<week weeknumber="9" quantity="11489" />
</year>
<year yearnumber="3" yearstart="11-24-13" yearend="11-22-14">
<week weeknumber="1" quantity="10127" />
<week weeknumber="10" quantity="10959" />
<week weeknumber="11" quantity="10773" />
<week weeknumber="12" quantity="11142" />
<week weeknumber="13" quantity="11564" />
<week weeknumber="14" quantity="10923" />
<week weeknumber="15" quantity="11122" />
<week weeknumber="16" quantity="10664" />
<week weeknumber="17" quantity="10966" />
<week weeknumber="18" quantity="10899" />
<week weeknumber="19" quantity="10101" />
<week weeknumber="2" quantity="10483" />
<week weeknumber="20" quantity="9601" />
<week weeknumber="21" quantity="9709" />
<week weeknumber="22" quantity="9643" />
<week weeknumber="23" quantity="9061" />
<week weeknumber="24" quantity="8095" />
<week weeknumber="25" quantity="8332" />
<week weeknumber="26" quantity="8199" />
<week weeknumber="27" quantity="7305" />
<week weeknumber="28" quantity="7457" />
<week weeknumber="29" quantity="7078" />
<week weeknumber="3" quantity="9793" />
<week weeknumber="30" quantity="6927" />
<week weeknumber="31" quantity="7056" />
<week weeknumber="32" quantity="6935" />
<week weeknumber="33" quantity="7121" />
<week weeknumber="34" quantity="8842" />
<week weeknumber="35" quantity="6976" />
<week weeknumber="36" quantity="6554" />
<week weeknumber="37" quantity="6538" />
<week weeknumber="38" quantity="6938" />
<week weeknumber="39" quantity="6981" />
<week weeknumber="4" quantity="10861" />
<week weeknumber="40" quantity="7086" />
<week weeknumber="41" quantity="7192" />
<week weeknumber="42" quantity="8585" />
<week weeknumber="43" quantity="10442" />
<week weeknumber="44" quantity="10244" />
<week weeknumber="45" quantity="11199" />
<week weeknumber="46" quantity="11030" />
<week weeknumber="47" quantity="10851" />
<week weeknumber="48" quantity="10134" />
<week weeknumber="49" quantity="9992" />
<week weeknumber="5" quantity="12153" />
<week weeknumber="50" quantity="10140" />
<week weeknumber="51" quantity="10088" />
<week weeknumber="52" quantity="9341" />
<week weeknumber="6" quantity="9690" />
<week weeknumber="7" quantity="11471" />
<week weeknumber="8" quantity="11513" />
<week weeknumber="9" quantity="11489" />
</year>
</years>
As you can see the quantity sum is the same for all the years, which shouldn't be the case, it should be unique for each year, how do i format it appropriately.
OP's code (taken from comment):
select (CAST(yearnumber as int)) as '@yearnumber'
, min(convert(varchar(10)
, cast(yearstart as date), 10)) as '@yearstart'
, max(convert(varchar(10)
, cast(yearend as date), 10)) as '@yearend'
, ( select cast(weeknumber as int) as '@weeknumber'
, SUM(isnull(cast(quantity as int),0)) as '@quantity'
FROM [meiForecasting].[dbo].george_mdx
group by weeknumber
FOR XML PATH('week'), TYPE
)
from [meiForecasting].[dbo].george_mdx
group by yearnumber for xml path('year'), root('years')