I have some data that looks like this:
| Number | CI | PLUGIN | Opened Date | State |
| ------------ | ------------ | ------------ | ------------ | ------------ |
| 1 | XYZ | A123 | 1/1/2023 | Closed |
| 2 | XYZ | A123 | 2/1/2023 | Closed |
| 3 | XYZ | A123 | 3/1/2023 | Closed |
| 4 | XYZ | A123 | 4/1/2023 | Open |
| 5 | XYZ | A123 | 5/1/2023 | Open |
I want the result of the query to either give me:
Only the oldest record (record with minimum opened date), and the two Open records:
| Number | CI | PLUGIN | Opened Date | State |
| ------------ | ------------ | ------------ | ------------ | ------------ |
| 1 | XYZ | A123 | 1/1/2023 | Closed |
| 4 | XYZ | A123 | 4/1/2023 | Open |
| 5 | XYZ | A123 | 5/1/2023 | Open |Or only the two open records and a created a column for the minimum opened date:
| Number | CI | PLUGIN | Opened Date | State | MIN Opened Date |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| 4 | XYZ | A123 | 4/1/2023 | Open | 1/1/2023 |
| 5 | XYZ | A123 | 5/1/2023 | Open | 1/1/2023 |
I tried to filter on State = Open OR min(opened_at) = opened_at but I get the error that aggregate can't be used in the where clause.
I tried using MAX(number) in the query with MIN(opened_at), but then I only get the record for Number = 5. I also need to have record Number = 4.
I also tried creating two queries, one with only open records and one with MIN(Opened Date) and unioning them, but that just gave me all the records.
Using SSMS 19. SQL Server (Windows SQL Server) Any guidance is appreciated.
2条答案
按热度按时间6ioyuze21#
Sounds like you just didn't wrap it in a sub query so the windowed function is available to the where clause:
We're doing exactly what you described, but we can now referenced the windowed aggregate column in the where clause.
Just as a side note, providing easily reproduceable DDL/DML make it much easier for folks to answer your question.
dxxyhpgq2#
We use window functino
min()over()
to find the first date and thenwhere
to only displaystate = 'open'
. Notice it has to be nested or it will filter out the themin(Opened_Date)
.Fiddle