I need a help with the below statement:
if( FIND_IN_SET(m.iCodFormaPgto, p.cValor) > 0, 'Delivery', seg.CNome) as fato_vnd_seg_virtual
I tried all sorts of solutions, but I can't reproduce the same scenario in SQL Server.
Below, I'll put all the select (which runs on MySQL)
SELECT 'dia_pgto_pdv' as fato_tipo_ds,
FORMAT(m.dtMovimento, '%Y%m%d') as periodo_pk,
concat(m.iCodEmpresa, '|', m.iCodFranqueado, '|',m.iCodLoja ) as loja_pk,
concat(m.iCodLoja, '|', m.cCodPDV) as pdv_pk,
concat(m.iCodFranqueado, '|', m.iCodEmpresa, '|',m.iCodLoja, '|', m.iCodSegmento) as segmento_pk,
m.iCodFranqueado as franqueado_pk,
m.iCodFormaPgto as forma_pgto_pk,
m.cFormaPgto as forma_pgto_ds,
concat(m.iCodEmpresa, '|', m.iCodFranqueado) as empresa_pk,
if( FIND_IN_SET(m.iCodFormaPgto, p.cValor) > 0, 'Delivery', seg.CNome) as fato_vnd_seg_virtual,
SUM(m.deValor) as fato_vnd_valor,
SUM(m.iGC) as fato_vnd_igc
FROM md_stage.dbo.mov_dia_pgto_pdv m
left join md_stage.dbo.segmento seg ON seg.iCodSegmento = m.iCodSegmento and
seg.iCodLoja = m.iCodLoja
JOIN md_stage.dbo.params p ON p.cIdParam = 'VENDA_FORMA_PAGAMENTO_TIPOS_DELIVERY'
GROUP BY m.dtMovimento ,
m.iCodLoja,
m.cCodPDV,
m.iCodSegmento,
m.iCodFranqueado,
m.iCodFormaPgto,
m.cFormaPgto,
m.iCodEmpresa,
p.cValor
The last select I created in SQL Server, was the one below. But it ends up giving an error because the subquery returns more than one value:
case
(select cNome from (
(select
tmp_m.iCodFormaPgto,
'Delivery' as cNome
from md_stage.dbo.mov_dia_pgto_pdv tmp_m
where exists (
select
tmp_p.cValor
from md_stage.dbo.params tmp_p
where concat(',',tmp_p.cValor,',') like concat('%,',tmp_m.iCodFormaPgto,',%')
)
)
UNION ALL
(select
tmp_m.iCodFormaPgto,
tmp_seg.cNome
from md_stage.dbo.mov_dia_pgto_pdv tmp_m
left join md_stage.dbo.segmento tmp_seg ON tmp_seg.iCodSegmento = tmp_m.iCodSegmento and
tmp_seg.iCodLoja = tmp_m.iCodLoja
where not exists (
select
tmp_p.cValor
from md_stage.dbo.params tmp_p
where concat(',',tmp_p.cValor,',') like concat('%,',tmp_m.iCodFormaPgto,',%')
)
)
) as x
) when 'Delivery' then 'Delivery' else cNome END as fato_vnd_seg_virtual
Extending @Akina, the complete SQL Server query would be:
SELECT
'dia_pgto_pdv' as fato_tipo_ds,
--
format(m.dtMovimento, 120) as periodo_pk,
--
... as loja_pk,
... as pdv_pk,
... as segmento_pk,
--
m.iCodFranqueado as franqueado_pk,
m.iCodFormaPgto as forma_pgto_pk,
m.cFormaPgto as forma_pgto_ds,
... as empresa_pk,
--
CASE
WHEN CHARINDEX(','+p.cValor+',', ','+m.iCodFormaPgto+',') > 0
THEN 'Delivery'
ELSE seg.CNome
END AS fato_vnd_seg_virtual,
--
SUM(m.deValor) as fato_vnd_valor,
SUM(m.iGC) as fato_vnd_igc
FROM
md_stage.dbo.mov_dia_pgto_pdv m
--
left join md_stage.dbo.segmento seg
ON seg.iCodSegmento = m.iCodSegmento and
seg.iCodLoja = m.iCodLoja
--
JOIN md_stage.dbo.params p
ON p.cIdParam = 'VENDA_FORMA_PAGAMENTO_TIPOS_DELIVERY'
GROUP BY m.dtMovimento,
m.iCodLoja,
m.cCodPDV,
m.iCodSegmento,
m.iCodFranqueado,
m.iCodFormaPgto,
m.cFormaPgto,
m.iCodEmpresa,
p.cValor
The ...
represents the concat
s, that are somewhat more complex in SQL Server, for example:
concat(m.iCodEmpresa, '|', m.iCodFranqueado, '|', m.iCodLoja)
Should be written as:
convert(varchar, m.iCodEmpresa) + '|' +
convert(varchar, m.iCodFranqueado) + '|' +
convert(varchar, m.iCodLoja)