Jump to content

GuiPetenuci

Members
  • Content count

    1
  • Joined

  • Last visited

Community Reputation

0 Comum

About GuiPetenuci

  • Birthday 07/31/1995

Informações Pessoais

  • Sexo
    Masculino
  • Localização
    Milano, Italia
  1. Boa tarde, Estou com um problema e estou ficando louco, ja li praticamente todos as respostas de View de SQL Server no Stack Overflow e a maioria da documentacao da MSFT Tenho uma view que faz algumas queries malucas que o cliente precisa, mas ela nao retorna o resultado justo, e se eu pegar o codigo e fazer uma query simples com o codigo, o resultado è ok. Codigo da View SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO /****** Object: View [QEHS].[V_FactMonitorings25261gui] Script Date: 11/07/2019 16:24:53 ******/ CREATE view [QEHS].[V_FactMonitorings25261gui] as with Plants as ( select distinct [plant-id] from [QEHS].[Fact-Monitorings-H&S] ) , MIDs as ( select mid from ( values ( 'MON.02.25 Total number of LTA free days (calendar days)') ,( 'MON.02.26 Total number of TRI free days (calendar days)') ) as x(mid) ), PlantList AS ( select a.date, b.[plant-id], c.mid from QEHS.Calendar_Gen A cross join plants b cross join mids c where year(a.date) >= 2011 ), help1 as ( SELECT p.date, p.[plant-id], p.mid, t.value, ( case when (year(p.date) = year(getdate()) and month(p.date) = month(getdate())) then day(getdate()) else iif(t.value is not null, 0, day(eomonth(p.date))) end ) Days, cast(isnull(cast(t.value as bit),0) as integer) reset FROM PLANTLIST p left join [QEHS].[Fact-Monitorings-H&S] T on ( p.date = t.date and p.[plant-id] = t.[plant-id] and p.mid = t.[monitoring-id] ) ), help2 as ( select date, [plant-id], mid, value, days, reset, formatmessage('%s%s%d', [plant-id], mid,sum(reset) over ( partition by [plant-id], mid order by date, [plant-id], mid ) ) grp from help1 where year(date) >= (select year(min(date)) from [QEHS].[Fact-Monitorings-H&S]) ), help3 as ( select date, [plant-id], mid, value, days, first_value(value) over (partition by grp order by date, [plant-id], mid) fullvalue from help2 ) -- select date, [plant-id], mid, isnull(a.Value, fullvalue +sum(days) over ( partition by [plant-id], mid, fullvalue order by mid, date, [plant-id] rows between unbounded preceding and current row ) ) total from help3 a GO a view retorna assim: Agora se eu pegar e fazer a query "na mao", retorna assim, que è o justo: Agora, porque na view retorna um e a query retorna outro? o.o
×

Important Information

Ao usar o fórum, você concorda com nossos Terms of Use.