1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
|
SELECT rr.NLFinancialReportLayoutID, rr.NLFinancialReportLineTypeID, rr.Title, rr.ReportRow, rr.SubtotalGroup,
case when rr.reportrow between g1.min_row and g1.reportrow then g1.reportrow end as g1,
case when rr.reportrow between g2.min_row and g2.reportrow then g2.reportrow end as g2,
case when rr.reportrow between g3.min_row and g3.reportrow then g3.reportrow end as g3,
case when rr.reportrow between g4.min_row and g4.reportrow then g4.reportrow end as g4,
case when rr.reportrow between g5.min_row and g5.reportrow then g5.reportrow end as g5,
case when rr.reportrow between g6.min_row and g6.reportrow then g6.reportrow end as g6,
case when rr.reportrow between g7.min_row and g7.reportrow then g7.reportrow end as g7,
case when rr.reportrow between g8.min_row and g8.reportrow then g8.reportrow end as g8,
case when rr.reportrow between g9.min_row and g9.reportrow then g9.reportrow end as g9
FROM NLFinancialReportRow RR
left outer join (SELECT NLFinancialReportLayoutID, ReportRow ,isnull((select top 1 reportrow FROM NLFinancialReportRow s2 WHERE S2.NLFinancialReportLayoutID = s1.NLFinancialReportLayoutID and isnull(s2.SubtotalGroup,0) >= 1 and s2.reportrow < s1.reportrow order by reportrow desc),0) as min_row FROM NLFinancialReportRow s1 WHERE SubtotalGroup = 1 AND NLFinancialReportLineTypeID = 1) g1 on g1.NLFinancialReportLayoutID = rr.NLFinancialReportLayoutID and rr.reportrow between g1.min_row and g1.reportrow
left outer join (SELECT NLFinancialReportLayoutID, ReportRow ,isnull((select top 1 reportrow FROM NLFinancialReportRow s2 WHERE S2.NLFinancialReportLayoutID = s1.NLFinancialReportLayoutID and isnull(s2.SubtotalGroup,0) >= 2 and s2.reportrow < s1.reportrow order by reportrow desc),0) as min_row FROM NLFinancialReportRow s1 WHERE SubtotalGroup = 2 AND NLFinancialReportLineTypeID = 1) g2 on g2.NLFinancialReportLayoutID = rr.NLFinancialReportLayoutID and rr.reportrow between g2.min_row and g2.reportrow
left outer join (SELECT NLFinancialReportLayoutID, ReportRow ,isnull((select top 1 reportrow FROM NLFinancialReportRow s2 WHERE S2.NLFinancialReportLayoutID = s1.NLFinancialReportLayoutID and isnull(s2.SubtotalGroup,0) >= 3 and s2.reportrow < s1.reportrow order by reportrow desc),0) as min_row FROM NLFinancialReportRow s1 WHERE SubtotalGroup = 3 AND NLFinancialReportLineTypeID = 1) g3 on g3.NLFinancialReportLayoutID = rr.NLFinancialReportLayoutID and rr.reportrow between g3.min_row and g3.reportrow
left outer join (SELECT NLFinancialReportLayoutID, ReportRow ,isnull((select top 1 reportrow FROM NLFinancialReportRow s2 WHERE S2.NLFinancialReportLayoutID = s1.NLFinancialReportLayoutID and isnull(s2.SubtotalGroup,0) >= 4 and s2.reportrow < s1.reportrow order by reportrow desc),0) as min_row FROM NLFinancialReportRow s1 WHERE SubtotalGroup = 4 AND NLFinancialReportLineTypeID = 1) g4 on g4.NLFinancialReportLayoutID = rr.NLFinancialReportLayoutID and rr.reportrow between g4.min_row and g4.reportrow
left outer join (SELECT NLFinancialReportLayoutID, ReportRow ,isnull((select top 1 reportrow FROM NLFinancialReportRow s2 WHERE S2.NLFinancialReportLayoutID = s1.NLFinancialReportLayoutID and isnull(s2.SubtotalGroup,0) >= 5 and s2.reportrow < s1.reportrow order by reportrow desc),0) as min_row FROM NLFinancialReportRow s1 WHERE SubtotalGroup = 5 AND NLFinancialReportLineTypeID = 1) g5 on g5.NLFinancialReportLayoutID = rr.NLFinancialReportLayoutID and rr.reportrow between g5.min_row and g5.reportrow
left outer join (SELECT NLFinancialReportLayoutID, ReportRow ,isnull((select top 1 reportrow FROM NLFinancialReportRow s2 WHERE S2.NLFinancialReportLayoutID = s1.NLFinancialReportLayoutID and isnull(s2.SubtotalGroup,0) >= 6 and s2.reportrow < s1.reportrow order by reportrow desc),0) as min_row FROM NLFinancialReportRow s1 WHERE SubtotalGroup = 6 AND NLFinancialReportLineTypeID = 1) g6 on g6.NLFinancialReportLayoutID = rr.NLFinancialReportLayoutID and rr.reportrow between g6.min_row and g6.reportrow
left outer join (SELECT NLFinancialReportLayoutID, ReportRow ,isnull((select top 1 reportrow FROM NLFinancialReportRow s2 WHERE S2.NLFinancialReportLayoutID = s1.NLFinancialReportLayoutID and isnull(s2.SubtotalGroup,0) >= 7 and s2.reportrow < s1.reportrow order by reportrow desc),0) as min_row FROM NLFinancialReportRow s1 WHERE SubtotalGroup = 7 AND NLFinancialReportLineTypeID = 1) g7 on g7.NLFinancialReportLayoutID = rr.NLFinancialReportLayoutID and rr.reportrow between g7.min_row and g7.reportrow
left outer join (SELECT NLFinancialReportLayoutID, ReportRow ,isnull((select top 1 reportrow FROM NLFinancialReportRow s2 WHERE S2.NLFinancialReportLayoutID = s1.NLFinancialReportLayoutID and isnull(s2.SubtotalGroup,0) >= 8 and s2.reportrow < s1.reportrow order by reportrow desc),0) as min_row FROM NLFinancialReportRow s1 WHERE SubtotalGroup = 8 AND NLFinancialReportLineTypeID = 1) g8 on g8.NLFinancialReportLayoutID = rr.NLFinancialReportLayoutID and rr.reportrow between g8.min_row and g8.reportrow
left outer join (SELECT NLFinancialReportLayoutID, ReportRow ,isnull((select top 1 reportrow FROM NLFinancialReportRow s2 WHERE S2.NLFinancialReportLayoutID = s1.NLFinancialReportLayoutID and isnull(s2.SubtotalGroup,0) >= 9 and s2.reportrow < s1.reportrow order by reportrow desc),0) as min_row FROM NLFinancialReportRow s1 WHERE SubtotalGroup = 9 AND NLFinancialReportLineTypeID = 1) g9 on g9.NLFinancialReportLayoutID = rr.NLFinancialReportLayoutID and rr.reportrow between g9.min_row and g9.reportrow
WHERE NLFinancialReportLineTypeID > 0
GO
|