akevintg

SQLearning(JOIN,Aggregate,Subquery, ETC)

May 8th, 2015
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 12.53 KB | None | 0 0
  1. --DROP DATABASE OOVEO_Salon
  2. /*
  3. CREATE DATABASE OOVEO_Salon
  4. GO
  5. USE OOVEO_Salon
  6. */
  7.  
  8. -- Create MsCustomer
  9. CREATE TABLE MsCustomer(
  10.     CustomerId CHAR(5) PRIMARY KEY,
  11.     CustomerName VARCHAR(50),
  12.     CustomerGender VARCHAR(10),
  13.     CustomerPhone VARCHAR(13),
  14.     CustomerAddress VARCHAR(100),
  15.     CONSTRAINT cekIDCust CHECK(CustomerId LIKE 'CU[0-9][0-9][0-9]')
  16. )
  17.  
  18. -- Create MsStaff
  19. CREATE TABLE MsStaff(
  20.     StaffId CHAR(5) PRIMARY KEY,
  21.     StaffName VARCHAR(50),
  22.     StaffGender VARCHAR(10),
  23.     StaffPhone VARCHAR(13),
  24.     StaffAddress VARCHAR(100),
  25.     StaffSalary NUMERIC(11,2),
  26.     StaffPosition VARCHAR(20),
  27.     CONSTRAINT cekIDStaff CHECK(StaffId LIKE 'SF[0-9][0-9][0-9]')
  28. )
  29.  
  30. -- Create MsTreatmentType
  31. CREATE TABLE MsTreatmentType(
  32.     TreatmentTypeId CHAR(5) PRIMARY KEY,
  33.     TreatmentTypeName VARCHAR(50),
  34.     CONSTRAINT cekIDTType CHECK(TreatmentTypeId LIKE 'TT[0-9][0-9][0-9]')
  35. )
  36.  
  37. -- Create MsTreatment
  38. CREATE TABLE MsTreatment(
  39.     TreatmentId CHAR(5) PRIMARY KEY,
  40.     TreatmentTypeId CHAR(5) REFERENCES MsTreatmentType ON UPDATE CASCADE ON DELETE CASCADE,
  41.     TreatmentName VARCHAR(50),
  42.     Price NUMERIC(11,2),
  43.     CONSTRAINT cekIDTreat CHECK(TreatmentId LIKE 'TM[0-9][0-9][0-9]')
  44. )
  45.  
  46. -- Create HeaderSalonServices
  47. CREATE TABLE HeaderSalonServices(
  48.     TransactionId CHAR(5) PRIMARY KEY,
  49.     CustomerId CHAR(5) REFERENCES MsCustomer ON UPDATE CASCADE ON DELETE CASCADE,
  50.     StaffId CHAR(5) REFERENCES MsStaff ON UPDATE CASCADE ON DELETE CASCADE,
  51.     TransactionDate DATE,
  52.     PaymentType VARCHAR(20),
  53.     CONSTRAINT cekIDTrans CHECK(TransactionId LIKE 'TR[0-9][0-9][0-9]')
  54. )
  55.  
  56. -- Create DetailSalonServices
  57. CREATE TABLE DetailSalonServices(
  58.     TransactionId CHAR(5) REFERENCES HeaderSalonServices ON UPDATE CASCADE ON DELETE CASCADE,
  59.     TreatmentId CHAR(5) REFERENCES MsTreatment ON UPDATE CASCADE ON DELETE CASCADE,
  60.     PRIMARY KEY(TransactionId, TreatmentId)
  61. )
  62.  
  63. -- Insert Data
  64. INSERT INTO MsCustomer VALUES
  65. ('CU001', 'Franky', 'Male', '08566543338', 'Daan mogot baru Street no 6'),
  66. ('CU002', 'Ernalia Dewi', 'Female', '085264782135', 'Tanjung Duren Street no 185'),
  67. ('CU003', 'Elysia Chen', 'Female', '085754206611', 'Kebon Jeruk Street no 120'),
  68. ('CU004', 'Brando Kartawijaya', 'Male', '081170225561', 'Greenvil Street no 88'),
  69. ('CU005', 'Andy Putra', 'Male', '087751321421', 'Sunter Street no 42')
  70.  
  71. INSERT INTO MsStaff VALUES
  72. ('SF001', 'Dian Felita Tanoto', 'Female', '085265442222', 'Palmerah Street no 56', 15000000, 'Top Stylist'),
  73. ('SF002', 'Mellisa Pratiwi', 'Female', '085755552011', 'Kebon Jeruk Street no 151', 10000000, 'Top Stylist'),
  74. ('SF003', 'Livia Ashianti', 'Female', '085218542222', 'Kebon Jeruk Street no 19', 7000000, 'Stylist'),
  75. ('SF004', 'Indra Saswita', 'Male', '085564223311', 'Sunter Street no 91', 7000000, 'Stylist'),
  76. ('SF005', 'Ryan Nixon Salim', 'Male', '085710255522', 'Kebon Jeruk Street no 123', 3000000, 'Stylist')
  77.  
  78. INSERT INTO MsTreatmentType VALUES
  79. ('TT001', 'Hair Treatment'),
  80. ('TT002', 'Message / Spa'),
  81. ('TT003', 'Beauty Care'),
  82. ('TT004', 'Nail Treatment'),
  83. ('TT005', 'Body Treatment')
  84.  
  85. INSERT INTO MsTreatment VALUES
  86. ('TM001', 'TT001','Cutting by Stylist', 150000),
  87. ('TM002', 'TT001','Cutting by Top Stylist', 450000),
  88. ('TM003', 'TT001','Cutting Pony', 50000),
  89. ('TM004', 'TT001','Blow', 90000),
  90. ('TM005', 'TT001','Coloring', 480000),
  91. ('TM006', 'TT001','Highlight', 320000),
  92. ('TM007', 'TT001','Japanese Perm', 700000),
  93. ('TM008', 'TT001','Digital Perm', 1100000),
  94. ('TM009', 'TT001','Special Perm', 1100000),
  95. ('TM010', 'TT001','Rebonding Treatment', 1100000),
  96. ('TM011', 'TT002','Creambath', 150000),
  97. ('TM012', 'TT002','Hair Spa', 250000),
  98. ('TM013', 'TT002','Hair Mask', 250000),
  99. ('TM014', 'TT002','Hand Spa Reflexy', 200000),
  100. ('TM015', 'TT002','Reflexy', 250000),
  101. ('TM016', 'TT002','Back Theraphy Massage', 300000),
  102. ('TM017', 'TT003','Make Up', 500000),
  103. ('TM018', 'TT003','Make Up Wedding', 5000000),
  104. ('TM019', 'TT003','Facial', 300000),
  105. ('TM020', 'TT004','Manicure', 80000),
  106. ('TM021', 'TT004','Pedicure', 100000),
  107. ('TM022', 'TT004','Nail Extension', 250000),
  108. ('TM023', 'TT004','Nail Acrylic Infill', 340000),
  109. ('TM024', 'TT005','Japanese Treatment', 350000),
  110. ('TM025', 'TT005','Scalp Treatment', 250000),
  111. ('TM026', 'TT005','Crystal Treatment', 400000)
  112.  
  113. INSERT INTO HeaderSalonServices VALUES
  114. ('TR001', 'CU001', 'SF004', '2012/12/20', 'Credit'),
  115. ('TR002', 'CU002', 'SF005', '2012/12/20', 'Credit'),
  116. ('TR003', 'CU003', 'SF003', '2012/12/20', 'Cash'),
  117. ('TR004', 'CU004', 'SF005', '2012/12/20', 'Debit'),
  118. ('TR005', 'CU005', 'SF003', '2012/12/21', 'Debit'),
  119. ('TR006', 'CU001', 'SF005', '2012/12/21', 'Credit'),
  120. ('TR007', 'CU002', 'SF001', '2012/12/22', 'Cash'),
  121. ('TR008', 'CU003', 'SF002', '2012/12/22', 'Credit'),
  122. ('TR009', 'CU005', 'SF004', '2012/12/22', 'Debit')
  123.  
  124. INSERT INTO DetailSalonServices VALUES
  125. ('TR001', 'TM001'),
  126. ('TR001', 'TM005'),
  127. ('TR002', 'TM010'),
  128. ('TR002', 'TM015'),
  129. ('TR003', 'TM025'),
  130. ('TR003', 'TM009'),
  131. ('TR004', 'TM001'),
  132. ('TR004', 'TM006'),
  133. ('TR004', 'TM015'),
  134. ('TR004', 'TM016'),
  135. ('TR005', 'TM016'),
  136. ('TR006', 'TM006'),
  137. ('TR006', 'TM015'),
  138. ('TR007', 'TM002'),
  139. ('TR007', 'TM005'),
  140. ('TR008', 'TM002'),
  141. ('TR008', 'TM006'),
  142. ('TR009', 'TM005'),
  143. ('TR009', 'TM006')
  144.  
  145. -- Display Data
  146. SELECT * FROM MsCustomer
  147. SELECT * FROM MsStaff
  148. SELECT * FROM MsTreatment
  149. SELECT * FROM MsTreatmentType
  150. SELECT * FROM HeaderSalonServices
  151. SELECT * FROM DetailSalonServices
  152.  
  153. /*
  154. sp_msforeachtable'DROP TABLE ?'
  155. sp_msforeachtable'SELECT * FROM ?'
  156.  
  157. DROP TABLE DetailSalonServices
  158. DROP TABLE HeaderSalonServices
  159. DROP TABLE MsTreatment
  160. DROP TABLE MsTreatmentType
  161. DROP TABLE MsCustomer
  162. DROP TABLE MsStaff
  163.  
  164. DELETE FROM DetailSalonServices
  165. DELETE FROM HeaderSalonServices
  166. DELETE FROM MsTreatment
  167. DELETE FROM MsTreatmentType
  168. DELETE FROM MsCustomer
  169. DELETE FROM MsStaff
  170. */
  171.  
  172. /*⦁   Display TreatmentTypeName, TreatmentName and Price where TreatmentTypeName contains the word ‘hair’ or starts with word
  173. ‘nail’ and the price is less than 100000.
  174. (join, like)*/
  175.  
  176. SELECT TreatmentTypeName,TreatmentName,Price
  177. FROM MsTreatment MT JOIN MsTreatmentType MTT ON MTT.TreatmentTypeId=MT.TreatmentTypeId
  178. WHERE Price<100000 AND (TreatmentTypeName LIKE '%Hair%' OR TreatmentTypeName LIKE 'Nail%')
  179.  
  180. /*⦁   Display StaffName and StaffEmail (obtained from the first character of StaffName add with the last name of StaffName
  181.  and ended with the word ‘@oosalon.com’ with lowercase format) where the transaction happened on Thursday and the repeated
  182.  data is shown once.
  183. (distinct, lower, left, reverse, charindex, join, datename, weekday, like)*/
  184.  
  185. SELECT DISTINCT StaffName, StaffEmail=LOWER(LEFT(StaffName,1)+REVERSE(LEFT(REVERSE(StaffName),CHARINDEX(' ',REVERSE(StaffName))-1))+'@oosalon.com')
  186. FROM MsStaff MS JOIN HeaderSalonServices HSS ON HSS.StaffId=MS.StaffId
  187. WHERE DATENAME(weekday,TransactionDate) LIKE 'Thursday'
  188.  
  189. /*⦁   Dispaly New Transaction ID (obtained from TransactionId by replacing the word ‘TR’ to ‘Trans’), Old Transaction ID
  190. (obtained from TransactionID), TransactionDate, StaffName, and CustomerName where the difference of transaction date with
  191. 24th December 2012 is less or equal to 2 days.
  192. (replace, join, datediff, day)*/
  193.  
  194. SELECT [NEW TRANSACTION ID]=REPLACE(TransactionId,'TR','Trans'),[OLD Transcation ID]=TransactionId,TransactionDate,StaffName,CustomerName
  195. FROM MsStaff MS JOIN HeaderSalonServices HSS ON HSS.StaffId=MS.StaffId
  196.     JOIN MsCustomer MC ON HSS.CustomerId=MC.CustomerId
  197. WHERE DATEDIFF(DAY,TransactionDate,'2012-12-24')<=2
  198.  
  199.  
  200. /*⦁   Dispaly New Transaction Date (obtained from TransactionDate add with 5 days), Old Transaction Date (obtained from
  201. TransactionDate) and CustomerName where transaction happened not on date 20.
  202. (dateadd, day, join, datepart)*/
  203.  
  204. SELECT [NEW TRANSACTION DATE]=DATEADD(DAY,5,TransactionDate),[OLD TRANSACTION DATE]=TransactionDate,CustomerName
  205. FROM MsCustomer MC JOIN HeaderSalonServices HSS ON HSS.CustomerId=MC.CustomerId
  206. WHERE DATEPART(DAY,TransactionDate)!=20
  207.  
  208. /*⦁   Display Day (obtained from name of the day from TransactionDate), CustomerName and TreatmentName where StaffPosition
  209.  starts with the word ‘TOP’ or StaffGender is ‘Female’. Then sort the data based on CustomerName with ascending format.
  210. (datename, weekday, join, in, like, order by)*/
  211.  
  212. SELECT [DAY]=DATENAME(WEEKDAY,TransactionDate),CustomerName,TreatmentName
  213. FROM MsCustomer MC JOIN HeaderSalonServices HSS ON MC.CustomerId=HSS.CustomerId
  214.     JOIN MsStaff MS ON HSS.StaffId=MS.StaffId
  215.     JOIN DetailSalonServices DSS ON DSS.TransactionId=HSS.TransactionId
  216.     JOIN MsTreatment MT ON MT.TreatmentId=DSS.TreatmentId
  217. WHERE StaffPosition LIKE 'TOP%' OR StaffGender IN('Female')
  218. ORDER BY CustomerName ASC
  219.  
  220. /*⦁   Display the first data of CustomerId, CustomerName, TransactionId, and Total Treatment (obtained from the amount of
  221. Treatment per transaction). And sort the data based on Total Treatment with format descending.
  222. (top, count, join, group by, order by)*/
  223.  
  224. SELECT TOP(1) MC.CustomerId,CustomerName,HSS.TransactionId,[Total Treatment]=COUNT(TreatmentID)
  225. FROM MsCustomer MC JOIN HeaderSalonServices HSS ON HSS.CustomerId=MC.CustomerId
  226.     JOIN DetailSalonServices DSS ON DSS.TransactionId=HSS.TransactionId
  227. GROUP BY MC.CustomerId,CustomerName,HSS.TransactionId
  228. ORDER BY [Total Treatment] DESC
  229.  
  230. /*⦁   Display CustomerId, TransactionId, CustomerName, and Total Price (obtained from the sum of Price per transaction)
  231. where Total Price is greater of the average Total Price. Then sort the data based on Total Price with descending format.
  232. (sum, join, avg, sum, group by, alias subquery, having, order by)*/
  233.  
  234. SELECT MC.CustomerId,HSS.TransactionId,CustomerName,[Total Price]=SUM(Price)
  235. FROM MsCustomer MC JOIN HeaderSalonServices HSS ON HSS.CustomerId=MC.CustomerId
  236.             JOIN DetailSalonServices DSS ON DSS.TransactionId=HSS.TransactionId
  237.             JOIN MsTreatment MT ON MT.TreatmentId=DSS.TreatmentId,
  238.     (SELECT average=AVG(subquery.[Total Price])
  239.      FROM(SELECT MC.CustomerId,HSS.TransactionId,CustomerName,[Total Price]=SUM(Price)
  240.           FROM MsCustomer MC JOIN HeaderSalonServices HSS ON HSS.CustomerId=MC.CustomerId
  241.             JOIN DetailSalonServices DSS ON DSS.TransactionId=HSS.TransactionId
  242.             JOIN MsTreatment MT ON MT.TreatmentId=DSS.TreatmentId
  243.           GROUP BY MC.CustomerId,HSS.TransactionId,CustomerName
  244.          )subquery
  245.     )mainquery
  246. GROUP BY MC.CustomerId,HSS.TransactionId,CustomerName
  247. HAVING SUM(PRICE)>AVG(mainquery.average)
  248. ORDER BY [Total Price] DESC
  249.  
  250. /*⦁   Display Name (obtained from StaffName that starts with the word ‘Mr. ’), StaffPosition, and StaffSalary where StaffGender
  251. is ‘Male’. Then combine with Name (obtained from StaffName that starts with ‘Ms. ’), StaffPosition, and StaffSalary where
  252. StaffGender is ‘Female’. Then sort the data based on Name and StaffPosition with ascending format.
  253. (union, order by)*/
  254.  
  255. SELECT Name='Mr. '+StaffName,StaffPosition,StaffSalary
  256. FROM MsStaff
  257. WHERE StaffGender='Male'
  258. UNION
  259. SELECT Name='Ms. '+StaffName,StaffPosition,StaffSalary
  260. FROM MsStaff
  261. WHERE StaffGender='Female'
  262.  
  263. /*⦁   Display TreatmentName, Price (obtained from Price that starts with the word ‘Rp. ’), and Status is ‘Maximum Price’ where
  264. Price is the highest Price from the price list. Then combine with TreatmentName, Price (obtained from Price that starts with
  265. the word ‘Rp. ’), and Status is ‘Minimum Price’ where Price is the lowest Price from the price list.
  266. (cast, max, alias subquery, union, min)*/
  267.  
  268. SELECT TreatmentName, Price='Rp. '+CAST(Price AS VARCHAR), [STATUS]=('Maximum Price')
  269. FROM MsTreatment,
  270.     (SELECT Maks=MAX(Price)
  271.     FROM MsTreatment)maximumObject
  272. WHERE Price=maximumObject.Maks
  273. UNION
  274. SELECT TreatmentName, Price='Rp. '+CAST(Price AS VARCHAR), [STATUS]=('Minumum Price')
  275. FROM MsTreatment,
  276.     (SELECT Mini=MIN(Price)
  277.      FROM MsTreatment
  278.     )minimumObject
  279. WHERE Price=minimumObject.Mini
  280.  
  281. /*⦁   Display Longest Name of Staff and Customer (obtained from CustomerName), Length of Name (obtained from the length of
  282. CustomerName), and Status is ‘Customer’ where Length of Name is the longest. Then combine with Longest Name of Staff and
  283. Customer (obtained from StaffName), Length of Name (obtained from the length of StaffName), and Status is ‘Staff’ where
  284. Length of Name is the longest.
  285. (len, max, alias subquery, union)*/
  286.  
  287. SELECT [Longest Name OF Staff AND Customer]=CustomerName,[LENGTH OF Name]=LEN(CustomerName),[STATUS]=('Customer')
  288. FROM MsCustomer,
  289.     (SELECT maks=MAX(LEN(CustomerName))
  290.      FROM MsCustomer
  291.     )maksimumQuery
  292. WHERE LEN(CustomerName)=maksimumQuery.maks
  293. UNION
  294. SELECT [Longest Name OF Staff AND Customer]=StaffName,[LENGTH OF Name]=LEN(StaffName),[STATUS]=('Staff')
  295. FROM MsStaff,
  296.     (SELECT maks=MAX(LEN(StaffName))
  297.      FROM MsStaff
  298.     )maksimumQuery
  299. WHERE LEN(StaffName)=maksimumQuery.maks
Add Comment
Please, Sign In to add comment