In a simple query, I have a table with the following structure:
SELECT * FROM [SAMX].[dbo].[REPORTE_ESTADO_CUENTA_GLOBAL] where intencion_id=1657 ORDER BY intencion_id,fecha
I need to fill the account column with a '0' or a '1' depending on whether the query contains an initial charge of the contract as follows: - if it does not, account must be '0' - if it does, you must check the status of that record - if status is 'C', account should still be '0' - if status is '', account should be '1', and all records thereafter, account should be '1' '
I tried using a variable, but I can't combine a SELECT
value assignment with a data fetch.
DECLARE @var1 int;
select @var1=0;
SELECT @var1=(CASE tipomovimiento WHEN 'Cargo Inicial del Contrato' THEN 1 ELSE 0 END)
,@var1 AS cuenta,[fecha],[tipomovimiento],[abono],[cargo],[estatus],[intencion_id] FROM REPORTE_ESTADO_CUENTA_GLOBAL where intencion_id=1657 ORDER BY intencion_id,fecha
Could you suggest me a solution?
Ok, a
LEFT JOIN
and an expressionCASE
should work: