Question : SQL query on a financial reporting table

Please find the attached SQL code we have been using.

As you can see, its a View we are using. The Query is pulling the firts five columns straight from a table but the sixth column is a select statement to group a bunch of ReportRows.

The sixth column is trying to group the Report Rows 1,2,3,4,5,6,7  which have a SubtotalGroup as NULL and when it reaches Row 8, it knows that SubTotalGroup value is 1 and NLFinancialReportLineTypeID = 1. so the Query returns number 8 for all the above rows.

But the issue we are having is,
If a row does not satisy the condition of Subtotal =1 and NLFinancialReportLineTypeID = 1
then it should print "NULL" in that row and move onto the next row.
So the 9th Row in the sixth coulmn should be printing "NULL" and then the Rows 10, 11,12 and 13 are doing fine.

Could you please assist me amending this Query, please ask me for more details if needed.

SELECT     NLFinancialReportLayoutID, NLFinancialReportLineTypeID, Title, ReportRow, SubtotalGroup,
(SELECT     MIN(ReportRow) AS Expr1
 FROM        dbo.NLFinancialReportRow AS S2
WHERE      (SubtotalGroup = 1) AND
 (ReportRow >= dbo.NLFinancialReportRow.ReportRow)) AS G1
 FROM         dbo.NLFinancialReportRow  
 
Query Preview
320303
 

Answer : SQL query on a financial reporting table

Well, think you kinda knew subconsiously maybe, other wise why did you include the different layoutID ? It was pretty much the only difference between your data and mine :)

So, well done yourself :) And likewise, enjoyed the challenge, the question, and working with you.

Here is the completed query :

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
Random Solutions  
 
programming4us programming4us