I have two tables CX_SOLICITUD_SERVICIO
and CX_SOLICITUD_SALA
, and I need a SQL Server query that add a column if NU_CONS_SOSA
has more than one CD_CODI_SER_SOSE
.
Table 1: CX_SOLICITUD_SERVICIO
NU_CONS_SOSA_SOSE CD_CODI_SER_SOSE
------------------------------------
777 i684000
777 i705210
777 i595101
778 i684000
778 i549201
756 i132300
763 i132300
758 i132300
761 i132300
761 i132301
Table 2: CX_SOLICITUD_SALA
NU_CONS_SOSA FE_FECH_SOSA
--------------------------------------------
777 2017-03-01 10:14:00.000
778 2017-03-04 11:14:00.000
756 2017-02-01 12:14:00.000
763 2017-01-15 14:14:00.000
758 2017-02-27 09:14:00.000
761 2017-03-01 10:14:00.000
Expected result:
NU_CONS_SOSA CD_CODI_SER_SOSE_1 CD_CODI_SER_SOSE_2 CD_CODI_SER_SOSE_3
777 i684000 i684000 i595101
778 i684000 i549201 null
756 i132300 null null
763 i132300 null null
758 i132300 null null
761 i132300 i132301 null
I normally try the following query but it doesn't work:
SELECT
NU_CONS_SOSA_SOSE, CD_CODI_SER_SOSE
FROM
CX_SOLICITUD_SERVICIO
INNER JOIN
CX_SOLICITUD_SALA ON NU_CONS_SOSA = NU_CONS_SOSA_SOSE