Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. use thedatabase;
  2.  
  3. declare @fromDate datetime = '2016-02-01 00:00:00.000';
  4. declare @toDate datetime = '2016-02-02 23:59:59.999';
  5. declare @source varchar(15) = 'server001';
  6.  
  7. DECLARE @countForType bigint;
  8. DECLARE @totalForType decimal(30,8);
  9.  
  10. DECLARE @country varchar(10);
  11.  
  12. SELECT @countForType = count(*),
  13. @totalForType = SUM(typeTable.amount),
  14. @country =
  15. case
  16. when (charindex('[', typeTable.source) > 0 and charindex(']', typeTable.source) > 0)
  17. then substring(typeTable.source, charindex('[', typeTable.source) +1, (charindex(']', typeTable.source) - 1) - charindex('[', typeTable.source))
  18. else null
  19. end
  20.  
  21. FROM theTypeTable typeTable (nolock)
  22. WHERE typeTable.startDate > @fromDate
  23. AND typeTable.startDate < @toDate
  24. AND typeTable.source like @source
  25. GROUP BY typeTable.source; -- i believe the issue may be here -- source is the entire string 'server001[en-US]'. I need to group and provide stats per country, which is a substring of source.
  26.  
  27.  
  28. --Print report:
  29. PRINT 'countForType: ' + CAST(@countForType AS VARCHAR);
  30. PRINT 'totalForType: ' + CAST(@totalForType AS VARCHAR);
  31. --for each country, print the amounts/ percentages etc...
  32. PRINT 'country: ' + CAST (@country AS VARCHAR);
  33.  
  34. countForType: 104
  35. totalForType: 110000.00000000
  36.  
  37. country: en-US
  38. countForType: 55
  39. totalForType: 95000.00000000
  40.  
  41. country: de-CH
  42. countForType: 25
  43. totalForType: 5000.00000000
  44.  
  45. country: tr-TR
  46. countForType: 30
  47. totalForType: 10000.00000000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement