|
tantcu
Yak Posting Veteran
58 Posts |
Posted - 2012-07-03 : 09:49:03
|
| Hey guys,I'm working on this query in order to sort of the category based on their values. However, when I was using roll up function it's not sort out for me. This is the query. SELECT TOP 5 ((([P21].[dbo].p21_view_inventory_value_report.qty_on_hand - [P21].[dbo].p21_view_inventory_value_report.special_layer_qty) * [P21].[dbo].[p21_view_inventory_value_report].[cost]) + [P21].[dbo].p21_view_inventory_value_report.special_layer_qty) AS extended_value ,CASE WHEN (SUM (CASE WHEN [P21].[dbo].[p21_inventory_usage_all_view].year_and_period BETWEEN LEFT(CONVERT(CHAR(8), DATEADD(MM, -12, getdate()),112),6) AND LEFT(CONVERT(CHAR(8), DATEADD(MM, 0, getdate()),112),6)THEN [P21].[dbo].[p21_inventory_usage_all_view].actual_usage ELSE 0 END) = 0) THEN 'Hardcore' WHEN ([P21].[dbo].p21_view_inventory_value_report.[qty_on_hand]+[P21].[dbo].p21_view_inventory_value_report.special_layer_qty) <= SUM(CASE WHEN [P21].[dbo].[p21_inventory_usage_all_view].year_and_period BETWEEN LEFT(CONVERT(CHAR(8), DATEADD(MM, -3, getdate()),112),6) AND LEFT(CONVERT(CHAR(8), DATEADD(MM, 0, getdate()),112),6) THEN [P21].[dbo].[p21_inventory_usage_all_view].actual_usage ELSE 0 END) THEN 'Good' WHEN ([P21].[dbo].p21_view_inventory_value_report.[qty_on_hand]+[P21].[dbo].p21_view_inventory_value_report.special_layer_qty) <= SUM (CASE WHEN [P21].[dbo].[p21_inventory_usage_all_view].year_and_period BETWEEN LEFT(CONVERT(CHAR(8), DATEADD(MM, -4, getdate()),112),6) AND LEFT(CONVERT(CHAR(8), DATEADD(MM,0, getdate()),112),6) THEN [P21].[dbo].[p21_inventory_usage_all_view].actual_usage ELSE 0 END) THEN 'NNU' WHEN ([P21].[dbo].p21_view_inventory_value_report.[qty_on_hand]+[P21].[dbo].p21_view_inventory_value_report.special_layer_qty) > SUM(CASE WHEN [P21].[dbo].[p21_inventory_usage_all_view].year_and_period BETWEEN LEFT(CONVERT(CHAR(8), DATEADD(MM, -3, getdate()),112),6) AND LEFT(CONVERT(CHAR(8), DATEADD(MM,0, getdate()),112),6) THEN [P21].[dbo].[p21_inventory_usage_all_view].actual_usage ELSE 0 END) AND ([P21].[dbo].p21_view_inventory_value_report.[qty_on_hand]+[P21].[dbo].p21_view_inventory_value_report.special_layer_qty)<= SUM(CASE WHEN [P21].[dbo].[p21_inventory_usage_all_view].year_and_period BETWEEN LEFT(CONVERT(CHAR(8), DATEADD(MM, -12, getdate()),112),6) AND LEFT(CONVERT(CHAR(8), DATEADD(MM, 0, getdate()),112),6)THEN [P21].[dbo].[p21_inventory_usage_all_view].actual_usage ELSE 0 END)THEN 'Overstock' ELSE 'Surplus' END 'Status' FROM [P21].[dbo].[p21_view_inventory_value_report] LEFT OUTER JOIN [P21].[dbo].p21_view_inv_loc ON ([P21].[dbo].p21_view_inv_loc.inv_mast_uid = [P21].[dbo].p21_view_inventory_value_report.inv_mast_uid) AND ([P21].[dbo].p21_view_inv_loc.location_id = [P21].[dbo].p21_view_inventory_value_report.location_id) LEFT OUTER JOIN [P21].[dbo].[p21_inventory_usage_all_view] ON ([p21_view_inventory_value_report].inv_mast_uid = [p21_inventory_usage_all_view].inv_mast_uid AND [p21_view_inventory_value_report].location_id = [p21_inventory_usage_all_view].location_id) Group BY ([P21].[dbo].[p21_view_inventory_value_report].[qty_on_hand]+[P21].[dbo].p21_view_inventory_value_report.special_layer_qty) ,[P21].[dbo].[p21_view_inventory_value_report].[qty_on_hand]+[P21].[dbo].p21_view_inventory_value_report.special_layer_qty -[P21].[dbo].p21_view_inv_loc.qty_allocated ,(([P21].[dbo].[p21_view_inventory_value_report].qty_on_hand - [P21].[dbo].p21_view_inventory_value_report.special_layer_qty) * [P21].[dbo].[p21_view_inventory_value_report].[cost]) + [P21].[dbo].p21_view_inventory_value_report.special_layer_qty WITH ROLLUP The result came out like this. Extended Value Status0.045000 HardcoreNULL HardcoreNULL Hardcore495.250000 Hardcore631.500000 NNUHow can I sort them out as one sum extended value for Hardcore and NNU as two column. Because the data is very big to retrieve therefore I only use top 5. Assuming that I have total of 50000 data and only 5 status. I want to retrieve it as 5 rows with total on extended value. Thank you |
|