Share via

Assigning same status same row group

Northface 161 Reputation points
2022-01-25T09:39:43.547+00:00

Hi there, just need to assing always the same value (1) regardless if it has 0 or NULL.

DECLARE @T1 AS TABLE(ID2 SMALLINT, BUILDING INT, BUILDING_STATUS BIT)

INSERT INTO @T1(ID2,BUILDING, BUILDING_STATUS)
VALUES(1,5671,0),(2,5671,NULL), (3,5671,1),(4,5673,1),(5,5674,2)

SELECT * FROM @T1

Output should be:

1 5671 1
2 5671 1
3 5671 1
4 5673 1

Thanks a lot,

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2022-01-25T10:00:26.583+00:00

Try another query:

SELECT ID2, BUILDING, max(cast(isnull(BUILDING_STATUS, 1) as int)) over (partition by BUILDING) as BUILDING_STATUS
FROM @T1
order by ID2

Although, it is not clear what to display if all values in a group are NULL.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 127K Reputation points
    2022-01-25T09:43:36.71+00:00

    Try a query:

    SELECT ID2, BUILDING, 1 as BUILDING_STATUS
    FROM @T1
    

    (Note that you cannot store 2 to BIT).

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.