Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. SELECT TIMESTAMP(sent_date) AS timestamp, COUNT(contacts.id) AS stopped
  2. FROM contacts
  3. JOIN campaigns
  4. ON campaigns.list_id = contacts.list_id
  5. WHERE STOP = 1 AND campaigns.user_id = 1 AND sent_date != 0
  6. GROUP BY YEAR(sent_date), MONTH(sent_date)
  7. ORDER BY sent_date DESC LIMIT 12;
  8.  
  9. SELECT
  10. TIMESTAMP(sent_date) AS timestamp,
  11. COUNT(sent_messages.id) AS messages,
  12. SUM(IF(sent_messages.status = 1, 1, 0)) AS received,
  13. SUM(IF(sent_messages.status > 1, 1, 0)) AS errored,
  14. FROM campaigns
  15. JOIN contacts
  16. ON campaigns.list_id = contacts.list_id
  17. WHERE user_id = 1 AND sent_date != 0
  18. GROUP BY YEAR(sent_date), MONTH(sent_date)
  19. ORDER BY sent_date DESC LIMIT 12;
  20.  
  21. SELECT
  22. TIMESTAMP(sent_date) AS timestamp,
  23. COUNT(sent_messages.id) AS messages,
  24. SUM(IF(sent_messages.status = 1, 1, 0)) AS received,
  25. SUM(IF(sent_messages.status > 1, 1, 0)) AS errored,
  26. SUM(IF(stop = 1, 1, 0)) AS stopped
  27. FROM campaigns
  28. JOIN contacts
  29. ON campaigns.list_id = contacts.list_id
  30. JOIN sent_messages
  31. ON campaigns.id = sent_messages.campaign_id
  32. WHERE user_id = 1 AND sent_date != 0
  33. GROUP BY YEAR(sent_date), MONTH(sent_date)
  34. ORDER BY sent_date DESC LIMIT 12;
  35.  
  36. CREATE TABLE contacts (
  37. id BIGINT UNSIGNED AUTO_INCREMENT,
  38. list_id INT UNSIGNED NOT NULL,
  39. telephone CHAR(12) NOT NULL,
  40. firstname VARCHAR(50),
  41. lastname VARCHAR(50),
  42. attributes VARCHAR(10000),
  43. stop BOOLEAN DEFAULT 0,
  44. PRIMARY KEY (id),
  45. INDEX contact_ind_list_id (list_id),
  46. INDEX contact_ind_stop (stop),
  47. UNIQUE KEY contact_index_telephone_and_list (telephone, list_id),
  48. CONSTRAINT contact_fk_list
  49. FOREIGN KEY (list_id)
  50. REFERENCES lists (id)
  51. ON DELETE CASCADE
  52. );
  53.  
  54. CREATE TABLE campaigns (
  55. id INT UNSIGNED AUTO_INCREMENT,
  56. user_id INT UNSIGNED NOT NULL,
  57. list_id INT UNSIGNED,
  58. name VARCHAR(50),
  59. text VARCHAR(500),
  60. send_date TIMESTAMP,
  61. sent_date TIMESTAMP,
  62. sender_name VARCHAR(11),
  63. status TINYINT UNSIGNED DEFAULT 0, -- 0 draft, 1 to be sent, 2 sent
  64. batch_number SMALLINT UNSIGNED DEFAULT 0,
  65. PRIMARY KEY (id),
  66. INDEX campaign_ind_owner (user_id),
  67. INDEX campaign_ind_push_id (push_id),
  68. UNIQUE KEY campaign_ind_list_and_name (list_id, name),
  69. CONSTRAINT campaign_fk_owner
  70. FOREIGN KEY (user_id)
  71. REFERENCES users (id)
  72. ON DELETE CASCADE,
  73. CONSTRAINT campaign_fk_list
  74. FOREIGN KEY (list_id)
  75. REFERENCES lists (id)
  76. ON DELETE CASCADE
  77. );
  78.  
  79. CREATE TABLE sent_messages (
  80. id BIGINT UNSIGNED AUTO_INCREMENT,
  81. campaign_id INT UNSIGNED NOT NULL,
  82. telephone CHAR(12) NOT NULL,
  83. status TINYINT NOT NULL, -- 0 = waiting / 1 = received / 2 = npai / 3 = net_error (error_credits, unknown_error)
  84. message TINYINT NOT NULL,
  85. PRIMARY KEY (id),
  86. INDEX sent_messages_ind_campaign (campaign_id),
  87. UNIQUE KEY sent_messages_ind_campaign_and_telephone (campaign_id, telephone),
  88. CONSTRAINT sent_messages_fk_campaign
  89. FOREIGN KEY (campaign_id)
  90. REFERENCES campaigns (id)
  91. ON DELETE CASCADE
  92. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement