December 17, 2018 at 5:02 am
LEGALPARCELID | SEQUENCE_NO | LEGALDESCRIPTION | PLANNO |
0102824230001 | 002 | LOT 6 AND THE S 8 FT OF LOT 5, LOTS 5 AND 6 | NULL |
0102824230001 | 993 | NULL | 15510 |
0102824230001 | 994 | NULL | 15610 |
0102824230001 | 995 | NULL | NULL |
Above is the current data in my table but i need below output. First not null value i need each column based on SEQUENCE_NO. I have 5 more columns also in this table. . not MAX/MIN value .. In case if any column i have all null values i need to place null.. Please help on this
My Output Should be like below | ||
LEGALPARCELID | LEGALDESCRIPTION | PLANNO |
0102824230001 | LOT 6 AND THE S 8 FT OF LOT 5, LOTS 5 AND 6 | 15510 |
December 17, 2018 at 6:35 am
SELECT TOP(1) *
FROM myTable
WHERE LEGALDESCRIPTION IS NOT NULL
ORDER BY LEGALPARCELID, SEQUENCE_NO
December 17, 2018 at 6:39 am
DBA.A - Monday, December 17, 2018 5:02 AM
LEGALPARCELID SEQUENCE_NO LEGALDESCRIPTION PLANNO 0102824230001 002
LOT 6 AND THE S 8 FT OF LOT 5, LOTS 5 AND 6NULL 0102824230001 993 NULL 15510 0102824230001 994 NULL 15610 0102824230001 995 NULL NULL Above is the current data in my table but i need below . First not null value i need . not MAX/MIN value .. Please help on this
My Output Should be like below LEGALPARCELID LEGALDESCRIPTION PLANNO 0102824230001 LOT 6 AND THE S 8 FT OF LOT 5, LOTS 5 AND 6 15510
Which columns can be null?
"First" of what sequence?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
December 17, 2018 at 6:52 am
Jonathan AC Roberts - Monday, December 17, 2018 6:35 AMSELECT TOP(1) *
FROM myTable
WHERE LEGALDESCRIPTION IS NOT NULL
ORDER BY LEGALPARCELID, SEQUENCE_NO
Thanks for the response.. But this will not work.. Hear multiple rows i have with single id .. but each column i need to select first not null value , if any column have all null value i need to place null..
December 17, 2018 at 6:56 am
ChrisM@Work - Monday, December 17, 2018 6:39 AMDBA.A - Monday, December 17, 2018 5:02 AM
LEGALPARCELID SEQUENCE_NO LEGALDESCRIPTION PLANNO 0102824230001 002
LOT 6 AND THE S 8 FT OF LOT 5, LOTS 5 AND 6NULL 0102824230001 993 NULL 15510 0102824230001 994 NULL 15610 0102824230001 995 NULL NULL Above is the current data in my table but i need below . First not null value i need . not MAX/MIN value .. Please help on this
My Output Should be like below LEGALPARCELID LEGALDESCRIPTION PLANNO 0102824230001 LOT 6 AND THE S 8 FT OF LOT 5, LOTS 5 AND 6 15510 Which columns can be null?
"First" of what sequence?
Thanks for the reply .. Based on Sequence_ID and LEGALPARCELID i need to select each column first NOT NULL value. In case if any column have all rows with group by SEQID and LEGALPARCELID with NULL then i need to place NULL only.
December 17, 2018 at 6:58 am
DBA.A - Monday, December 17, 2018 6:52 AMJonathan AC Roberts - Monday, December 17, 2018 6:35 AMSELECT TOP(1) *
FROM myTable
WHERE LEGALDESCRIPTION IS NOT NULL
ORDER BY LEGALPARCELID, SEQUENCE_NOThanks for the response.. But this will not work.. Hear multiple rows i have with single id .. but each column i need to select first not null value , if any column have all null value i need to place null..
;WITH CTE AS
(
SELECT DISTINCT LEGALPARCELID
FROM myTable t
)
SELECT CTE.LEGALPARCELID, t2.*
FROM CTE
OUTER APPLY(SELECT TOP(1) *
FROM myTable t2
WHERE t2.LEGALPARCELID = t.LEGALPARCELID
AND t2.LEGALDESCRIPTION IS NOT NULL
ORDER BY SEQUENCE_NO) t2
December 17, 2018 at 6:58 am
DBA.A - Monday, December 17, 2018 6:56 AMChrisM@Work - Monday, December 17, 2018 6:39 AMDBA.A - Monday, December 17, 2018 5:02 AM
LEGALPARCELID SEQUENCE_NO LEGALDESCRIPTION PLANNO 0102824230001 002
LOT 6 AND THE S 8 FT OF LOT 5, LOTS 5 AND 6NULL 0102824230001 993 NULL 15510 0102824230001 994 NULL 15610 0102824230001 995 NULL NULL Above is the current data in my table but i need below . First not null value i need . not MAX/MIN value .. Please help on this
My Output Should be like below LEGALPARCELID LEGALDESCRIPTION PLANNO 0102824230001 LOT 6 AND THE S 8 FT OF LOT 5, LOTS 5 AND 6 15510 Which columns can be null?
"First" of what sequence?Thanks for the reply .. Based on Sequence_ID and LEGALPARCELID i need to select each column first NOT NULL value. In case if any column have all rows with group by SEQID and LEGALPARCELID with NULL then i need to place NULL only.
Are you expecting one row for each individual value of LEGALPARCELID, or one row for the entire dataset?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
December 17, 2018 at 7:09 am
ChrisM@Work - Monday, December 17, 2018 6:58 AMDBA.A - Monday, December 17, 2018 6:56 AMChrisM@Work - Monday, December 17, 2018 6:39 AMDBA.A - Monday, December 17, 2018 5:02 AM
LEGALPARCELID SEQUENCE_NO LEGALDESCRIPTION PLANNO 0102824230001 002
LOT 6 AND THE S 8 FT OF LOT 5, LOTS 5 AND 6NULL 0102824230001 993 NULL 15510 0102824230001 994 NULL 15610 0102824230001 995 NULL NULL Above is the current data in my table but i need below . First not null value i need . not MAX/MIN value .. Please help on this
My Output Should be like below LEGALPARCELID LEGALDESCRIPTION PLANNO 0102824230001 LOT 6 AND THE S 8 FT OF LOT 5, LOTS 5 AND 6 15510 Which columns can be null?
"First" of what sequence?Thanks for the reply .. Based on Sequence_ID and LEGALPARCELID i need to select each column first NOT NULL value. In case if any column have all rows with group by SEQID and LEGALPARCELID with NULL then i need to place NULL only.
Are you expecting one row for each individual value of LEGALPARCELID, or one row for the entire dataset?
Yes Each Individual LegalparcelID i need each column first not null value in row, I have Description , parcelno,status etc column. groupby LegalpID and select each column first not null value.
December 17, 2018 at 7:12 am
Jonathan AC Roberts - Monday, December 17, 2018 6:58 AMDBA.A - Monday, December 17, 2018 6:52 AMJonathan AC Roberts - Monday, December 17, 2018 6:35 AMSELECT TOP(1) *
FROM myTable
WHERE LEGALDESCRIPTION IS NOT NULL
ORDER BY LEGALPARCELID, SEQUENCE_NOThanks for the response.. But this will not work.. Hear multiple rows i have with single id .. but each column i need to select first not null value , if any column have all null value i need to place null..
;WITH CTE AS
(
SELECT DISTINCT LEGALPARCELID
FROM myTable t
)
SELECT CTE.LEGALPARCELID, t2.*
FROM CTE
OUTER APPLY(SELECT TOP(1) *
FROM myTable t2
WHERE t2.LEGALPARCELID = t.LEGALPARCELID
AND t2.LEGALDESCRIPTION IS NOT NULL
ORDER BY SEQUENCE_NO) t2
I am sorry.. I should not filter LEGALDESCRIPTION is not null because for another LEGALPARCELID we have change LEGALDESCRIPTION IS NULL FOR all rows but PARCELNO we have some data that i need to select..
December 17, 2018 at 7:20 am
DBA.A - Monday, December 17, 2018 7:12 AMJonathan AC Roberts - Monday, December 17, 2018 6:58 AMDBA.A - Monday, December 17, 2018 6:52 AMJonathan AC Roberts - Monday, December 17, 2018 6:35 AMSELECT TOP(1) *
FROM myTable
WHERE LEGALDESCRIPTION IS NOT NULL
ORDER BY LEGALPARCELID, SEQUENCE_NOThanks for the response.. But this will not work.. Hear multiple rows i have with single id .. but each column i need to select first not null value , if any column have all null value i need to place null..
;WITH CTE AS
(
SELECT DISTINCT LEGALPARCELID
FROM myTable t
)
SELECT CTE.LEGALPARCELID, t2.*
FROM CTE
OUTER APPLY(SELECT TOP(1) *
FROM myTable t2
WHERE t2.LEGALPARCELID = t.LEGALPARCELID
AND t2.LEGALDESCRIPTION IS NOT NULL
ORDER BY SEQUENCE_NO) t2I am sorry.. I should not filter LEGALDESCRIPTION is not null because for another LEGALPARCELID we have change LEGALDESCRIPTION IS NULL FOR all rows but PARCELNO we have some data that i need to select..
I don't understand exactly what you need but I think you want the first NOT NULL value for each column by some order (you haven't specified the order). You can just extend the above SQL for all columns:;WITH CTE AS
(
SELECT DISTINCT LEGALPARCELID
FROM myTable t
)
SELECT CTE.LEGALPARCELID,
t2.LEGALDESCRIPTION,
t3.PLANNO
FROM CTE
OUTER APPLY(SELECT TOP(1) LEGALDESCRIPTION
FROM myTable t2
WHERE t2.LEGALPARCELID = t.LEGALPARCELID
AND t2.LEGALDESCRIPTION IS NOT NULL
ORDER BY SEQUENCE_NO) t2
OUTER APPLY(SELECT TOP(1) PLANNO
FROM myTable t2
WHERE t2.LEGALPARCELID = t.LEGALPARCELID
AND t2.PLANNO IS NOT NULL
ORDER BY SEQUENCE_NO) t3
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply