Advertisement
Guest User

Untitled

a guest
Nov 11th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.68 KB | None | 0 0
  1. BEGIN TRANSACTION;
  2.  
  3. /* Create a table called NAMES */
  4. create table clients
  5. (
  6. client_id INTEGER PRIMARY KEY,
  7. client_name TEXT not null,
  8. address TEXT not null,
  9. email TEXT not null,
  10. client_type TEXT not null
  11. );
  12.  
  13. create table dates
  14. (
  15. datex datetime not null,
  16. year_month int not null,
  17. yearx int not null
  18. );
  19.  
  20. create table sales
  21. (
  22. client_id int not null,
  23. datex date not null,
  24. product_id int not null,
  25. sales_value decimal (20, 6) not null
  26. );
  27.  
  28. create table returns
  29. (
  30. client_id int not null,
  31. datex date not null,
  32. product_id int not null,
  33. returned_value decimal (20, 6) not null
  34. );
  35.  
  36. insert INTO dates
  37. (datex, year_month, yearx)
  38. values
  39. ('2018/09/10', 9, 2018),
  40. ('2018/11/11', 11, 2018),
  41. ('2018/12/12', 12, 2018);
  42.  
  43. insert INTO sales
  44. (client_id, datex, product_id, sales_value)
  45. values
  46. (1, '2018/09/10', 123, 50.56),
  47. (2, '2018/11/11', 113, 5848.43),
  48. (3, '2018/12/12', 123, 423.65);
  49.  
  50. insert INTO returns
  51. (client_id, datex, product_id, returned_value)
  52. values
  53. (1, '2018/09/10', 123, 40.56),
  54. (2, '2018/11/11', 113, 4848.43),
  55. (3, '2018/12/12', 123, 323.65);
  56.  
  57. insert INTO clients
  58. (client_id, client_name, address, email, client_type)
  59. values
  60. (1, 'Tomat', 'Moscow', 'tomat@mail.ru', 'Type A'),
  61. (2, 'Vasya', 'Chicago', 'vasya@gmail.com', 'Type B'),
  62. (3, 'Vanya', 'Vorkuta', 'uporok@mail.ru', 'Type A'),
  63. (4, 'Valera', 'Nahim', 'valera@mail.ru', 'Type B');
  64.  
  65. /* Ex 1 */
  66. /*select client_id, client_name, address, email from clients where email like '%mail.ru';*/
  67.  
  68. /* Ex 2 */
  69.  
  70. /* Ex 3 */
  71. select a.datex as 'Date', sum(a.sales_value) as 'Sales', sum(b.returned_value) as 'Returns'
  72. from sales as a, returns as b
  73. left join returns on a.client_id = b.client_id
  74. group by a.datex;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement