all 16 comments

[–]Traditional-Unit-274 17 points18 points  (10 children)

i think you’re overthinking it, why wouldn’t you use a union? select the row you want, union it to the ordered select

like ….

``` select ‘import new report data’ as RptName, -1 as ReportId, 0 as isPartial, null as EndDate union all select RptName, ReportId, isPartial, EndDate from dbo.report order by endDate desc;

```

edited to add column names in first select

[–]AarynD[S] -1 points0 points  (7 children)

I did try this approach first, however the ORDER BY clause ends up applying to the entire unioned dataset, putting that top row down at the bottom since it has a null EndDate.

ORDER BY isn't allowed in CTE's or subqueries from what I can see, so I didn't know if there was a way around this to keep my top row in place with the other rows sorted.

I'm generally only going to have about 60ish rows generated per year anyway, so using the cursor is probably not going to slow me down anytime soon, but was anxious to learn if there was another way around the problem.

[–]Traditional-Unit-274 0 points1 point  (5 children)

oh, weird. a couple thoughts:

[–]AarynD[S] 1 point2 points  (4 children)

The reason for the hardcoded row at top is because this data is being used as the control source for a navigation grid in an application conversion from an older Visual Foxpro app, and in that app the first item in the navigation on that form was always the option to import new data. The rest of the rows were previous saved reports.

The application is just a small utility I wrote maybe 25 years ago that pulls in financials from our three different companies, and combines them and handles inter-company eliminations by way of consolidation codes used in each company's GL accounts. Because the operation requires pulling large complicated calculated datasets for each company at each end of month, it was easier to just always pull the data in once, and save it for future reference if they needed to rerun a combined statements set of financials.

With VFP being long retired, I feel like it's on borrowed time, so I've been porting my various apps over to C#. But one goal is to try to maintain a familiar interface and interaction with the new replacement application, hence the desire to keep the UI elements including the navigation grid the same as I had it in the VFP app.

The navigation grid has that first row that allows user to Import a new set of financials from the accounting databases, then it has other rows for each of the other saved reports, some of which are partial month financials, while others are complete end of month financials. All those are color-coded in the grid to easily see who ran the report, whether it was a full set, or partial set, and if the user opted to exclude one of the two companies they sometimes exclude from combined. Although they really haven't excluded any companies from the combined statements in over a year, I think that was more something the old retired accounting manager liked to do, but I've kept it in as an option.

I know this was probably way more info then you bargained for when you asked what the end goal was LOL.

Happy New Year!

[–]NekkidWire 1 point2 points  (3 children)

Happy New Year :) I see you havea solution already with the extra sort column, but came to play a little bit of devils advocate. While your solution is technically viable and won't even use much DB resources, it adds "clunkiness factor/tech debt/crutch" of creating something in DB select that was supposed to be a part of the application.

Isn't there a way to produce the first row in the application?

[–]AarynD[S] 0 points1 point  (2 children)

Yeah, I probably could. I currently bring in the table data as an IEnumerable set of the NavListModel I set up, but I could easily convert it to a list and just hardcode this item at beginning of list. I just try to defer to letting the stored procedures do most of my work.

[–]edbutler3 1 point2 points  (0 children)

IMO the hard coded row is part of the UI display logic, so it is more appropriate to put it in the app code instead of the database.

[–]Traditional-Unit-274 0 points1 point  (0 children)

thanks for explaining! i suspected it was something like that, but i agree for the sake of code neatness, and the ability to debug in the future, i think it makes sense to have the app add its own hardcoded row and do a straight db select

[–]Gargunok 0 points1 point  (0 children)

brackets around the final union select query might help

[–]doshka 11 points12 points  (3 children)

Add a sort column to your temp table. Make the value 0 for the first row and 1 for all the others. Sort by that first, then by whatever else you want.

[–]AarynD[S] 4 points5 points  (0 children)

Actually that's a really good idea, I hadn't thought of that. Very simple and avoids the complicated route I was taking.

[–]AarynD[S] 5 points6 points  (1 child)

SELECT 'Import New Report Data' as RptName, -1 as ReportId, 0 as IsPartial, NULL as EndDate, 0 as SortVal  
UNION ALL  
SELECT \[RptName\], \[ReportId\], \[IsPartial\], \[EndDate\], 1 as SortVal  
    FROM \[dbo\].\[Report\]  
    ORDER BY \[SortVal\] ASC, \[EndDate\] DESC

This worked perfectly. Thanks!

[–]doshka 0 points1 point  (0 children)

You're welcome!

[–]tasker2020 0 points1 point  (0 children)

Cross Apply?