Demo on DB Fiddle :. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question. Asked 1 year, 11 months ago. Active 1 year, 3 months ago. Viewed 5k times. Improve this question. GMB k 23 23 gold badges 58 58 silver badges bronze badges. Ryan Ryan 87 1 1 silver badge 5 5 bronze badges. You need a column to order the records.
This isn't a real-world execution plan though - surely we never scan heaps our production environments. Both of the above queries will return the first value without NULLs. But they probably won't perform exactly the same as they did on my examples above. Copy the above code for sure - but test it out. There are numerous ways of implementing the sequencing logic. Since the days, one of the most popular and easier methods was to use a subquery based on APPLY operator to get associated values in sequence.
The illustration given below is taken from a real project scenario with some sample dummy data replacing the actual ones. The scenario on hand consists of operational data for a series of machines whose opening and closing reading needs to be captured for each execution cycle. The catch here is that machines may or may not give reading at the prescribed times based on the availability of input.
The readings will not be in any kind of sequence. The requirement was to get the opening first and closing last reading for each operational cycle for each of the machines. On careful analysis of the above result, we can find that both first and last readings are returned as NULL values for every machine. This assumes that there is only one non-empty comment.
The question doesn't specify something different though. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 2 years, 10 months ago. Active 2 years, 10 months ago. Viewed 2k times. I have this data: Id Name amount Comments 1 n1 Hello 2 n2 Bye 3 n2 null 4 n2 '' Each name may or may not have extra rows with null or empty comments.
Output I want: Id Name sum amount Comments 1 n1 Hello 2 n2 Bye I can't figure this out.
0コメント