Advertisement
Guest User

Untitled

a guest
Jan 5th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.30 KB | None | 0 0
  1.  
  2. BEGIN
  3.  
  4. DECLARE @dtDate DATETIME
  5. DECLARE @lastDay DATE;
  6. DECLARE @firstDay DATE;
  7. DECLARE @currentDate DATE;
  8. DECLARE @month_table AS TABLE(DATA DATE);
  9.  
  10. SET @dtDate =  @DATA;
  11. SET @lastDay    =  CAST(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dtDate)+1,0))  AS DATE)
  12. SET @firstDay   =  DATEADD(d,-DAY(@dtDate)+1,@dtDate);
  13. SET @currentDate = @firstDay;
  14.  
  15. WHILE @currentDate <> dateadd(d,1,@lastDay)
  16. BEGIN
  17.     PRINT @currentDate;
  18.     INSERT INTO @month_table VALUES(@currentDate);
  19.     SET @currentDate = dateadd(d,1,@currentDate);
  20. END
  21.  
  22.      SELECT * FROM (
  23.  
  24.     SELECT DATA,motivo,SUM(total) AS total,CAST(campanha AS VARCHAR(11)) AS campanha,[TYPE]
  25.     FROM [TP_clientes_Barclaycard].[dbo].[ftcc_ppp_motivo_nelegivel_totais]
  26.     GROUP BY DATA,motivo,campanha,[TYPE]   
  27.  
  28.     UNION ALL  
  29.  
  30.     SELECT DATA,motivo,0 AS total,CAST('ftcc_ppp' AS VARCHAR(11)) campanha,'LOP' AS [TYPE]
  31.     FROM @month_table
  32.     CROSS JOIN dbo.ftcc_ppp_motivo_nelegivel_keys
  33.  
  34.     UNION ALL
  35.     SELECT DATA,motivo,0 AS total,CAST('ftcc_ppp' AS  VARCHAR(11)) campanha,'ALOP' AS [TYPE]
  36.     FROM @month_table
  37.     CROSS JOIN dbo.ftcc_ppp_motivo_nelegivel_keys
  38.    
  39.     UNION ALL
  40.     SELECT DATA,motivo,0 AS total,CAST('ftcc_ppp_lx' AS VARCHAR(11)) campanha,'CARD' AS [TYPE]
  41.     FROM @month_table
  42.     CROSS JOIN dbo.ftcc_ppp_motivo_nelegivel_keys
  43.     ) xpto
  44.     WHERE campanha = 'ftcc_ppp'
  45.  
  46.  
  47.    
  48. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement