Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
How do i calculate percentage based on a group by statement. I have a road condition table, which is divided by specific areas and conditions. I need to group by area and condition and calculate segment's length percentage to the area. Table:from to area cond0 20 1 120 50 1 250 100 1 1100 250 1 2250 300 2 2300 350 2 2350 475 2 2475 700 2 1desired Result:from to area cond Percentage0 20 1 1 820 50 1 2 1250 100 1 1 20100 250 1 2 60250 300 2 2 11.11111111300 350 2 2 11.11111111350 475 2 2 27.77777778475 700 2 1 50Thanks!!!
Great! Thanks a lot!!! How about one more level of complexity. How about grouping these results by area and cond based on these rules:keep cond = 1 when cumulative sum of percentages for all segments > 30 and cond = 1, otherwise keep cond = 2. So the result would look like this:DECLARE @T2 TABLE([from] int, [to] int, area int, cond int)INSERT @T2 ([from], [to], area, cond)VALUES(0, 250, 1, 2),(250, 700, 2, 1)Thanks again!!!