Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- declare @rdsInf table (reqId uniqueidentifier, month int);
- declare @shippingTypes table (type int);
- declare @codes table (code nvarchar (100));
- declare @name nvarchar(500) = 'Джубга - Сочи';
- declare @months table (val int);
- insert into @months (val) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12);
- insert into @codes (code) values ('А-147'), ('М-27');
- insert into @shippingTypes (type) values (1), (2);
- insert into @rdsInf (reqId, month)
- select
- r.RequestID as reqId,
- month(rs.ValidSince) as month
- from
- RDSv4_RDS.dbo.Request r with (nolock)
- inner join RDSv4_RDS.dbo.RequestStatus rs with (nolock) on rs.RequestId = r.RequestId
- where
- rs.StatusCode = 6
- and rs.ValidUntil = convert(datetime, '9999-12-31 23:59:59.997', 121)
- and rs.ValidSince >= convert(datetime, '2017-01-01 00:00:00.000', 121)
- and rs.ValidSince < convert(datetime, '2018-01-01 00:00:00.000', 121)
- and r.ShippingType in (select t.type from @shippingTypes t)
- and (r.ResolutionRoute like '%"' + @name + '"%' or r.ResolutionRoute like '%“' + @name + '”%')
- and not exists (select 1 from @codes c where r.ResolutionRoute like '%' + c.code + '%');
- insert into @rdsInf (reqId, month)
- select
- r.RequestID as reqId,
- month(rs.ValidSince) as month
- from
- RDSv4_DEP.dbo.Request r with (nolock)
- inner join RDSv4_DEP.dbo.RequestStatus rs with (nolock) on rs.RequestId = r.RequestId
- where
- rs.StatusCode = 6
- and rs.ValidUntil = convert(datetime, '9999-12-31 23:59:59.997', 121)
- and rs.ValidSince >= convert(datetime, '2017-01-01 00:00:00.000', 121)
- and rs.ValidSince < convert(datetime, '2018-01-01 00:00:00.000', 121)
- and r.ShippingType in (select t.type from @shippingTypes t)
- and (r.ResolutionRoute like '%"' + @name + '"%' or r.ResolutionRoute like '%“' + @name + '”%')
- and not exists (select 1 from @codes c where r.ResolutionRoute like '%' + c.code + '%');
- select
- M.VAL,
- COALESCE(count(i.reqId),0)
- from
- @months M
- LEFT JOIN @rdsInf I ON M.VAL = I.MONTH
- group by
- M.VAL
- order by
- M.VAL asc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement