Advertisement
akevintg

SQLearning(AVG, HAVING, GROUP BY, etc)

Apr 10th, 2015
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 13.22 KB | None | 0 0
  1. --LINK SOAL : https://www.dropbox.com/s/cniinmajd8zm6vd/O1-T0206-DF01-06-Edited.docx?dl=0
  2.  
  3. /* SQL INI DIBUAT MENGUNAKAN SQL SERVER MANAGEMENT STUDIO(SSMS) 2014
  4. SSMS 2014 TIDAK mensupport COMPUTE sehingga mengunakan metode alternatif subquery
  5. Untuk dapat menjalankan COMPUTE diperlukan SSMS 2008/2005
  6. */
  7.  
  8. /*
  9. CREATE DATABASE OOVEO_Salon
  10. GO
  11. USE OOVEO_Salon
  12. */
  13.  
  14. -- Create MsCustomer
  15. CREATE TABLE MsCustomer(
  16.     CustomerId CHAR(5) PRIMARY KEY,
  17.     CustomerName VARCHAR(50),
  18.     CustomerGender VARCHAR(10),
  19.     CustomerPhone VARCHAR(13),
  20.     CustomerAddress VARCHAR(100),
  21.     CONSTRAINT cekIDCust CHECK(CustomerId LIKE 'CU[0-9][0-9][0-9]')
  22. )
  23.  
  24. -- Create MsStaff
  25. CREATE TABLE MsStaff(
  26.     StaffId CHAR(5) PRIMARY KEY,
  27.     StaffName VARCHAR(50),
  28.     StaffGender VARCHAR(10),
  29.     StaffPhone VARCHAR(13),
  30.     StaffAddress VARCHAR(100),
  31.     StaffSalary NUMERIC(11,2),
  32.     StaffPosition VARCHAR(20),
  33.     CONSTRAINT cekIDStaff CHECK(StaffId LIKE 'SF[0-9][0-9][0-9]')
  34. )
  35.  
  36. -- Create MsTreatmentType
  37. CREATE TABLE MsTreatmentType(
  38.     TreatmentTypeId CHAR(5) PRIMARY KEY,
  39.     TreatmentTypeName VARCHAR(50),
  40.     CONSTRAINT cekIDTType CHECK(TreatmentTypeId LIKE 'TT[0-9][0-9][0-9]')
  41. )
  42.  
  43. -- Create MsTreatment
  44. CREATE TABLE MsTreatment(
  45.     TreatmentId CHAR(5) PRIMARY KEY,
  46.     TreatmentTypeId CHAR(5) REFERENCES MsTreatmentType ON UPDATE CASCADE ON DELETE CASCADE,
  47.     TreatmentName VARCHAR(50),
  48.     Price NUMERIC(11,2),
  49.     CONSTRAINT cekIDTreat CHECK(TreatmentId LIKE 'TM[0-9][0-9][0-9]')
  50. )
  51.  
  52. -- Create HeaderSalonServices
  53. CREATE TABLE HeaderSalonServices(
  54.     TransactionId CHAR(5) PRIMARY KEY,
  55.     CustomerId CHAR(5) REFERENCES MsCustomer ON UPDATE CASCADE ON DELETE CASCADE,
  56.     StaffId CHAR(5) REFERENCES MsStaff ON UPDATE CASCADE ON DELETE CASCADE,
  57.     TransactionDate DATE,
  58.     PaymentType VARCHAR(20),
  59.     CONSTRAINT cekIDTrans CHECK(TransactionId LIKE 'TR[0-9][0-9][0-9]')
  60. )
  61.  
  62. -- Create DetailSalonServices
  63. CREATE TABLE DetailSalonServices(
  64.     TransactionId CHAR(5) REFERENCES HeaderSalonServices ON UPDATE CASCADE ON DELETE CASCADE,
  65.     TreatmentId CHAR(5) REFERENCES MsTreatment ON UPDATE CASCADE ON DELETE CASCADE,
  66.     PRIMARY KEY(TransactionId, TreatmentId)
  67. )
  68.  
  69. -- Insert Data
  70. INSERT INTO MsCustomer VALUES
  71. ('CU001', 'Franky', 'Male', '08566543338', 'Daan mogot baru Street no 6'),
  72. ('CU002', 'Ernalia Dewi', 'Female', '085264782135', 'Tanjung Duren Street no 185'),
  73. ('CU003', 'Elysia Chen', 'Female', '085754206611', 'Kebon Jeruk Street no 120'),
  74. ('CU004', 'Brando Kartawijaya', 'Male', '081170225561', 'Greenvil Street no 88'),
  75. ('CU005', 'Andy Putra', 'Male', '087751321421', 'Sunter Street no 42')
  76.  
  77. INSERT INTO MsStaff VALUES
  78. ('SF001', 'Dian Felita Tanoto', 'Female', '085265442222', 'Palmerah Street no 56', 15000000, 'Top Stylist'),
  79. ('SF002', 'Mellisa Pratiwi', 'Female', '085755552011', 'Kebon Jeruk Street no 151', 10000000, 'Top Stylist'),
  80. ('SF003', 'Livia Ashianti', 'Female', '085218542222', 'Kebon Jeruk Street no 19', 7000000, 'Stylist'),
  81. ('SF004', 'Indra Saswita', 'Male', '085564223311', 'Sunter Street no 91', 7000000, 'Stylist'),
  82. ('SF005', 'Ryan Nixon Salim', 'Male', '085710255522', 'Kebon Jeruk Street no 123', 3000000, 'Stylist')
  83.  
  84. INSERT INTO MsTreatmentType VALUES
  85. ('TT001', 'Hair Treatment'),
  86. ('TT002', 'Message / Spa'),
  87. ('TT003', 'Beauty Care'),
  88. ('TT004', 'Nail Treatment'),
  89. ('TT005', 'Body Treatment')
  90.  
  91. INSERT INTO MsTreatment VALUES
  92. ('TM001', 'TT001','Cutting by Stylist', 150000),
  93. ('TM002', 'TT001','Cutting by Top Stylist', 450000),
  94. ('TM003', 'TT001','Cutting Pony', 50000),
  95. ('TM004', 'TT001','Blow', 90000),
  96. ('TM005', 'TT001','Coloring', 480000),
  97. ('TM006', 'TT001','Highlight', 320000),
  98. ('TM007', 'TT001','Japanese Perm', 700000),
  99. ('TM008', 'TT001','Digital Perm', 1100000),
  100. ('TM009', 'TT001','Special Perm', 1100000),
  101. ('TM010', 'TT001','Rebonding Treatment', 1100000),
  102. ('TM011', 'TT002','Creambath', 150000),
  103. ('TM012', 'TT002','Hair Spa', 250000),
  104. ('TM013', 'TT002','Hair Mask', 250000),
  105. ('TM014', 'TT002','Hand Spa Reflexy', 200000),
  106. ('TM015', 'TT002','Reflexy', 250000),
  107. ('TM016', 'TT002','Back Theraphy Massage', 300000),
  108. ('TM017', 'TT003','Make Up', 500000),
  109. ('TM018', 'TT003','Make Up Wedding', 5000000),
  110. ('TM019', 'TT003','Facial', 300000),
  111. ('TM020', 'TT004','Manicure', 80000),
  112. ('TM021', 'TT004','Pedicure', 100000),
  113. ('TM022', 'TT004','Nail Extension', 250000),
  114. ('TM023', 'TT004','Nail Acrylic Infill', 340000),
  115. ('TM024', 'TT005','Japanese Treatment', 350000),
  116. ('TM025', 'TT005','Scalp Treatment', 250000),
  117. ('TM026', 'TT005','Crystal Treatment', 400000)
  118.  
  119. INSERT INTO HeaderSalonServices VALUES
  120. ('TR001', 'CU001', 'SF004', '2012/12/20', 'Credit'),
  121. ('TR002', 'CU002', 'SF005', '2012/12/20', 'Credit'),
  122. ('TR003', 'CU003', 'SF003', '2012/12/20', 'Cash'),
  123. ('TR004', 'CU004', 'SF005', '2012/12/20', 'Debit'),
  124. ('TR005', 'CU005', 'SF003', '2012/12/21', 'Debit'),
  125. ('TR006', 'CU001', 'SF005', '2012/12/21', 'Credit'),
  126. ('TR007', 'CU002', 'SF001', '2012/12/22', 'Cash'),
  127. ('TR008', 'CU003', 'SF002', '2012/12/22', 'Credit'),
  128. ('TR009', 'CU005', 'SF004', '2012/12/22', 'Debit')
  129.  
  130. INSERT INTO DetailSalonServices VALUES
  131. ('TR001', 'TM001'),
  132. ('TR001', 'TM005'),
  133. ('TR002', 'TM010'),
  134. ('TR002', 'TM015'),
  135. ('TR003', 'TM025'),
  136. ('TR003', 'TM009'),
  137. ('TR004', 'TM001'),
  138. ('TR004', 'TM006'),
  139. ('TR004', 'TM015'),
  140. ('TR004', 'TM016'),
  141. ('TR005', 'TM016'),
  142. ('TR006', 'TM006'),
  143. ('TR006', 'TM015'),
  144. ('TR007', 'TM002'),
  145. ('TR007', 'TM005'),
  146. ('TR008', 'TM002'),
  147. ('TR008', 'TM006'),
  148. ('TR009', 'TM005'),
  149. ('TR009', 'TM006')
  150.  
  151. -- Display Data
  152. SELECT * FROM MsCustomer
  153. SELECT * FROM MsStaff
  154. SELECT * FROM MsTreatment
  155. SELECT * FROM MsTreatmentType
  156. SELECT * FROM HeaderSalonServices
  157. SELECT * FROM DetailSalonServices
  158.  
  159. /*
  160. sp_msforeachtable'DROP TABLE ?'
  161. sp_msforeachtable'SELECT * FROM ?'
  162.  
  163. DROP TABLE DetailSalonServices
  164. DROP TABLE HeaderSalonServices
  165. DROP TABLE MsTreatment
  166. DROP TABLE MsTreatmentType
  167. DROP TABLE MsCustomer
  168. DROP TABLE MsStaff
  169.  
  170. DELETE FROM DetailSalonServices
  171. DELETE FROM HeaderSalonServices
  172. DELETE FROM MsTreatment
  173. DELETE FROM MsTreatmentType
  174. DELETE FROM MsCustomer
  175. DELETE FROM MsStaff
  176. */
  177.  
  178. /*1.    Display Maximum Price (obtained from the highest Price), Minimum Price (obtained from the
  179. lowest Price) and Average Price (obtained from the average of Price that is rounded with format
  180. 2 digits number after coma).(max, min, cast, round, avg)*/
  181.  
  182. SELECT [Maximum Price]=MAX(Price),[Minimum Price]=MIN(Price),[Average Price]=CAST(ROUND(AVG(Price),0)AS NUMERIC(11,2))
  183. FROM MsTreatment
  184.  
  185.  
  186. /*2.    Display StaffPosition, Gender (obtained from the first character of StaffGender), and Average
  187.  Salary (obtained from the average of StaffSalary that is rounded with format 2 digits number after
  188.  coma and starts with the word ‘Rp. ’).(left, cast, avg, group by)*/
  189.  
  190.  SELECT StaffPosition, Gender=LEFT(StaffGender,1),[Average Salary]='Rp. '+CAST(CAST(ROUND(AVG(StaffSalary),0)AS NUMERIC(11,2))AS VARCHAR)
  191.  FROM MsStaff
  192.  GROUP BY StaffPosition,StaffGender
  193.  
  194. /*3.    Display Transaction Date (obtained from TransactionDate with format ‘Mon dd, yyyy’), and Total
  195. Transaction per Day (obtained from the amount of transaction per day). Then display the average of Total
  196. Transaction per Day.(convert, count, group by, compute, avg)*/
  197.  -- COMPUTE TIDAK BERLAKU DI SQL 2012++, HANYA BERLAKU DI SQL 2008,2005
  198.  
  199.  SELECT [TRANSACTION DATE]=CONVERT(VARCHAR,TransactionDate,107), [Total TRANSACTION per DAY]=COUNT(TransactionDate)
  200.  FROM HeaderSalonServices
  201.  GROUP BY TransactionDate
  202.  --Jika mengunakan COMPUTE Ganti 'GROUP BY TransactionDate' dengan
  203. /*
  204. ORDER BY Transaction Date
  205. COMPUTE AVG([Total Transaction per Day]);
  206. */
  207.  
  208.  SELECT [avg]=AVG(temp)
  209.  FROM (
  210.     SELECT COUNT(TransactionDate)AS temp--dipangil di avg atas
  211.     FROM HeaderSalonServices
  212.     GROUP BY TransactionDate
  213.  )NamaViewSementara--view sementara asal isi aja
  214.  
  215.  
  216. /*4.    Display CustomerGender (obtained from CustomerGender with uppercase format), and Total Transaction
  217. (obtained from the amount of transaction that based on the customer’s gender). Then, display the sum of
  218. Total Transaction.(upper, count, group by, compute, sum)*/
  219.  -- COMPUTE TIDAK BERLAKU DI SQL 2012++, HANYA BERLAKU DI SQL 2008,2005
  220.  
  221. SELECT CustomerGender=UPPER(CustomerGender), [Total TRANSACTION]=COUNT(TransactionId)
  222. FROM MsCustomer MC, HeaderSalonServices HSS
  223. WHERE MC.CustomerId=HSS.CustomerId
  224. GROUP BY CustomerGender
  225. --Jika mengunakan COMPUTE Ganti GROUP BY Customer Gender dengan
  226. /*
  227. ORDER BY CustomerGender
  228. COMPUTE SUM([TotalTransaction]);
  229. */
  230.  
  231. SELECT [SUM]=SUM([Total TRANSACTION])
  232. FROM (
  233.     SELECT [Total TRANSACTION]=COUNT(TransactionId)
  234.     FROM MsCustomer MC, HeaderSalonServices HSS
  235.     WHERE MC.CustomerId=HSS.CustomerId
  236.     GROUP BY CustomerGender
  237. )temp
  238.  
  239. /*5.    Display TreatmentTypeName, Total Transaction (obtained from the amount of transaction) then sort
  240. the data by Total Transaction in descending format.(count, group by, order by)*/
  241.  
  242. SELECT TreatmentTypeName, [Total TRANSACTION]=COUNT(TransactionId)
  243. FROM MsTreatmentType MTT, MsTreatment MT,DetailSalonServices DSS
  244. WHERE MTT.TreatmentTypeId=MT.TreatmentTypeId AND DSS.TreatmentId=Mt.TreatmentId
  245. GROUP BY TreatmentTypeName
  246. ORDER BY [Total TRANSACTION]DESC
  247.  
  248. /*6.    Display Date (obtained from the date of transaction with format ‘dd Mon yy’) and Revenue per Day
  249. (obtained from the sum of Price and starts with the word ‘Rp. ’) where Revenue per Day is between 1000000
  250. and 5000000.(convert, cast, sum, group by, having, between)*/
  251.  
  252. SELECT [DATE]=CONVERT(VARCHAR,TransactionDate,103),[Revenue per DAY]='Rp. '+CAST(SUM(Price)AS VARCHAR)
  253. FROM HeaderSalonServices HSS,MsTreatment MT, DetailSalonServices DSS
  254. WHERE HSS.TransactionId=DSS.TransactionId AND DSS.TreatmentId=MT.TreatmentId  
  255. GROUP BY TransactionDate
  256. HAVING SUM(Price) BETWEEN 1000000 AND 5000000
  257.  
  258. /*7.    Display ID (obtained from TreatmentTypeID by replacing the word ‘TT0’ into ‘Treatment Type ’),
  259. TreatmentTypeName dan Total Treatment per Type (obtained from the amount of TreatmentID that is ended with
  260. the word ‘ Treatment’) where Total Treatment per Type is greater than 5. Then sort the data by Total Treatment
  261. per Type with descending format.(replace, cast, count, group by, having, order by)*/
  262.  
  263. SELECT ID=REPLACE(MT.TreatmentTypeId,'TT0','Treatment Type '),TreatmentTypeName, [Total Treatment per TYPE]=COUNT(MT.TreatmentTypeId)
  264. FROM MsTreatment MT, MsTreatmentType MTT
  265. WHERE MT.TreatmentTypeId=MTT.TreatmentTypeId
  266. GROUP BY MT.TreatmentTypeId, TreatmentTypeName
  267. HAVING COUNT(MT.TreatmentTypeId)>5
  268. ORDER BY [Total Treatment per TYPE] DESC
  269.  
  270. /*8.    Display StaffName (obtained from the first name of StaffName), TransactionID and Total Treatment
  271. per Transaction (obtained from the amount of treatment per transaction). Then, display the highest Total
  272. Treatment per Transaction.(left, charindex, count, group by, compute, max)*/
  273.  
  274. SELECT StaffName=LEFT(StaffName,CHARINDEX(' ',StaffName)-1), HSS.TransactionId, [Total Treatment per TRANSACTION]= COUNT(HSS.TransactionId)
  275. FROM MsStaff MS, MsTreatment MT, HeaderSalonServices HSS, DetailSalonServices DSS
  276. WHERE MS.StaffId=HSS.StaffId AND HSS.TransactionId=DSS.TransactionId AND DSS.TreatmentId=MT.TreatmentId
  277. GROUP BY HSS.TransactionId, StaffName
  278.  
  279. SELECT [MAX]= MAX([Total Treatment per TRANSACTION])
  280. FROM (
  281.     SELECT [Total Treatment per TRANSACTION]= COUNT(TransactionId)
  282.     FROM DetailSalonServices
  283.     GROUP BY(TransactionId)
  284. )temp
  285. /*9.    Display TransactionDate, CustomerName, TreatmentName and Price where transaction happened on
  286. Thursday and StaffName starts with the word ‘Ryan’. Then show the sum of Price based on TransactionDate
  287. and CustomerName.(datename, weekday, like, order by, compute by, sum)*/
  288.  
  289. SELECT TransactionDate, CustomerName, TreatmentName, Price
  290. FROM MsStaff MS, MsCustomer MC, HeaderSalonServices HSS, DetailSalonServices DSS, MsTreatment MT
  291. WHERE MS.StaffId=HSS.StaffId AND MC.CustomerId=HSS.CustomerId
  292.     AND HSS.TransactionId=DSS.TransactionId AND DSS.TreatmentId=MT.TreatmentId
  293.     AND DATENAME(weekday,TransactionDate)='Thursday' AND StaffName LIKE 'Ryan%'
  294. ORDER BY CustomerName
  295.  
  296. SELECT [SUM]=SUM(Price)
  297. FROM(
  298.     SELECT Price
  299.     FROM MsStaff MS, MsCustomer MC, HeaderSalonServices HSS, DetailSalonServices DSS, MsTreatment MT
  300.     WHERE MS.StaffId=HSS.StaffId AND MC.CustomerId=HSS.CustomerId
  301.         AND HSS.TransactionId=DSS.TransactionId AND DSS.TreatmentId=MT.TreatmentId
  302.         AND DATENAME(weekday,TransactionDate)='Thursday' AND StaffName LIKE 'Ryan%'
  303. )temp
  304.  
  305.  
  306. /*10.   Display TransactionDate, CustomerName and TotalPrice (obtained from the total Price) where transaction
  307. happened after date 20. Then display the grand TotalPrice and minimum TotalPrice based on date of transaction.
  308. Then sort the data based on date of transaction.(sum, day, group by, order by, compute by, min)*/
  309.  
  310. SELECT TransactionDate,CustomerName,TotalPrice=SUM(Price)
  311. FROM MsCustomer MC, HeaderSalonServices HSS, DetailSalonServices DSS, MsTreatment MT
  312. WHERE MC.CustomerId=HSS.CustomerId AND HSS.TransactionId=DSS.TransactionId AND DSS.TreatmentId=MT.TreatmentId AND DAY(TransactionDate)>20
  313. GROUP BY TransactionDate,CustomerName
  314. ORDER BY TransactionDate
  315.  
  316. SELECT [SUM]=SUM(TotalPrice), [MIN]=MIN(TotalPrice)
  317. FROM (
  318.     SELECT TotalPrice=SUM(Price)
  319.     FROM MsCustomer MC, HeaderSalonServices HSS, DetailSalonServices DSS, MsTreatment MT
  320.     WHERE MC.CustomerId=HSS.CustomerId AND HSS.TransactionId=DSS.TransactionId
  321.         AND DSS.TreatmentId=MT.TreatmentId AND DAY(TransactionDate)>20
  322.     GROUP BY TransactionDate,CustomerName
  323. )temp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement