Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. declare @rdsInf table (reqId uniqueidentifier, month int);
  2. declare @shippingTypes table (type int);
  3. declare @codes table (code nvarchar (100));
  4. declare @name nvarchar(500) = 'Джубга - Сочи';
  5.  
  6. declare @months table (val int);
  7. insert into @months (val) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12);
  8.  
  9. insert into @codes (code) values ('А-147'), ('М-27');
  10. insert into @shippingTypes (type) values (1), (2);
  11.  
  12. insert into @rdsInf (reqId, month)
  13. select
  14. r.RequestID as reqId,
  15. month(rs.ValidSince) as month
  16. from
  17. RDSv4_RDS.dbo.Request r with (nolock)
  18. inner join RDSv4_RDS.dbo.RequestStatus rs with (nolock) on rs.RequestId = r.RequestId
  19. where
  20. rs.StatusCode = 6
  21. and rs.ValidUntil = convert(datetime, '9999-12-31 23:59:59.997', 121)
  22. and rs.ValidSince >= convert(datetime, '2017-01-01 00:00:00.000', 121)
  23. and rs.ValidSince < convert(datetime, '2018-01-01 00:00:00.000', 121)
  24. and r.ShippingType in (select t.type from @shippingTypes t)
  25. and (r.ResolutionRoute like '%"' + @name + '"%' or r.ResolutionRoute like '%“' + @name + '”%')
  26. and not exists (select 1 from @codes c where r.ResolutionRoute like '%' + c.code + '%');
  27.  
  28. insert into @rdsInf (reqId, month)
  29. select
  30. r.RequestID as reqId,
  31. month(rs.ValidSince) as month
  32. from
  33. RDSv4_DEP.dbo.Request r with (nolock)
  34. inner join RDSv4_DEP.dbo.RequestStatus rs with (nolock) on rs.RequestId = r.RequestId
  35. where
  36. rs.StatusCode = 6
  37. and rs.ValidUntil = convert(datetime, '9999-12-31 23:59:59.997', 121)
  38. and rs.ValidSince >= convert(datetime, '2017-01-01 00:00:00.000', 121)
  39. and rs.ValidSince < convert(datetime, '2018-01-01 00:00:00.000', 121)
  40. and r.ShippingType in (select t.type from @shippingTypes t)
  41. and (r.ResolutionRoute like '%"' + @name + '"%' or r.ResolutionRoute like '%“' + @name + '”%')
  42. and not exists (select 1 from @codes c where r.ResolutionRoute like '%' + c.code + '%');
  43.  
  44. select
  45. M.VAL,
  46. COALESCE(count(i.reqId),0)
  47. from
  48. @months M
  49. LEFT JOIN @rdsInf I ON M.VAL = I.MONTH
  50. group by
  51. M.VAL
  52. order by
  53. M.VAL asc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement