wozniol

Untitled

Nov 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 4.27 KB | None | 0 0
  1. PRZYŁĄCZANIE
  2.  
  3. SELECT p.[Name], c.[Name]
  4. FROM [SalesLT].[Product] AS p
  5. FULL  JOIN [SalesLT].[ProductCategory] AS c
  6.     ON c.ProductCategoryID = p.ProductCategoryID
  7. /* LEFT, RIGHT, FULL JOIN */
  8.  
  9.  
  10. _____________
  11. OSOBY KTORE NIE ZLOZYLY ZAMOWIENIA
  12.  
  13. SELECT DISTINCT c.FirstName, c.LastName,
  14. FROM [SalesLT].[SalesOrderHeader] AS p
  15. FULL JOIN [SalesLT].[Customer] AS c
  16.     ON c.CustomerID = p.CustomerID
  17.     WHERE p.CustomerID IS NULL
  18.  
  19. ________________
  20. KIEDY TABELE NIE SA BEZPOSREDNIO POLACZONE RELACJAMI
  21.  
  22. SELECT  c.FirstName, c.LastName, p.Name
  23. FROM [SalesLT].[Customer] AS c
  24. INNER JOIN [SalesLT].[SalesOrderHeader] AS oh
  25.     ON c.CustomerID = oh.CustomerID
  26.  
  27. INNER JOIN [SalesLT].[SalesOrderDetail] AS od
  28.     ON oh.SalesOrderID = od.SalesOrderID
  29.  
  30. INNER JOIN [SalesLT].[Product] AS p
  31.     ON od.ProductID = p.ProductID
  32.  
  33.  
  34.  
  35. ___________________________________
  36.  
  37. SELECT p.Name, oh.SalesOrderNumber, od.SalesOrderDetailID, od.LineTotal
  38. FROM [SalesLT].[Product] AS p
  39. LEFT JOIN
  40.     (
  41.         [SalesLT].[SalesOrderDetail] AS od
  42.         JOIN [SalesLT].[SalesOrderHeader] AS oh
  43.         ON od.SalesOrderID = oh.SalesOrderID
  44.     )
  45.  
  46.     ON p.ProductID = od.ProductID
  47.  
  48.     ORDER BY p.Name
  49.  
  50.  
  51. ___________________________________
  52.  
  53. SELECT c.[Name], COUNT(p.[ProductID]) AS l, avg([ListPrice]) AS s
  54. FROM [SalesLT].[ProductCategory] AS c
  55. JOIN [SalesLT].[Product] AS p
  56.     ON c.ProductCategoryID = p.ProductCategoryID
  57. WHERE [Color] IN ('Black', 'Red')
  58. GROUP BY c.Name
  59. HAVING   COUNT([ProductID]) > 5
  60. ORDER BY s DESC
  61.  
  62. __________________________________
  63. SELECT NULL, a.City, COUNT(ca.CustomerID) AS cnt
  64. FROM [SalesLT].[Address] AS a
  65. LEFT JOIN [SalesLT].[CustomerAddress] AS ca
  66.     ON a.AddressID = ca.AddressID
  67.     WHERE a.CountryRegion = 'United States'
  68.     GROUP BY a.City
  69.  
  70.  
  71.  
  72.     UNION ALL
  73.  
  74.     SELECT a.StateProvince, NULL,  COUNT(ca.CustomerID) AS cnt
  75. FROM [SalesLT].[Address] AS a
  76. LEFT JOIN [SalesLT].[CustomerAddress] AS ca
  77.     ON a.AddressID = ca.AddressID
  78.     WHERE a.CountryRegion = 'United States'
  79.     GROUP BY a.StateProvince
  80.    
  81.     ORDER BY 1
  82.  
  83.    
  84. _________________________________________________
  85. SELECT a.StateProvince, a.City,  COUNT(ca.CustomerID) AS cnt
  86. FROM [SalesLT].[Address] AS a
  87. JOIN [SalesLT].[CustomerAddress] AS ca
  88.     ON a.AddressID = ca.AddressID
  89.     WHERE a.CountryRegion = 'United States'
  90.     GROUP BY GROUPING sets (
  91.          (a.StateProvince), (a.City)    )
  92.          ORDER BY a.StateProvince
  93.  
  94. _____________________________________________
  95.  
  96. SELECT c1.Name AS cat, c2.Name AS subcat, COUNT(p.[ProductID]) AS cnt, avg([ListPrice]) AS a
  97. FROM [SalesLT].[ProductCategory] AS c1
  98. JOIN [SalesLT].[ProductCategory] AS c2
  99.     ON c1.ProductCategoryID = c2.ParentProductCategoryID
  100. JOIN [SalesLT].[Product] AS p
  101.     ON c2.ProductCategoryID = p.ProductCategoryID
  102.     GROUP BY GROUPING sets ((), (c1.Name), (c2.Name))
  103. ORDER BY 1, 2
  104.  
  105.  
  106. ______________________________________________________________________
  107.  
  108. SELECT a.City, SUM([OrderQty]) AS Bikes, SUM([LineTotal]) AS Total
  109. FROM [SalesLT].[ProductCategory] AS pc
  110. JOIN [SalesLT].[Product] AS p
  111.     ON pc.ProductCategoryID = p.ProductCategoryID
  112.  
  113. JOIN [SalesLT].[SalesOrderDetail] AS od
  114.     ON p.ProductID = od.ProductID
  115.  
  116. JOIN [SalesLT].[SalesOrderHeader] AS oh
  117.     ON od.SalesOrderID = oh.SalesOrderID
  118.  
  119. JOIN [SalesLT].[Address] AS a
  120.     ON a.AddressID = oh.ShipToAddressID
  121.  
  122. WHERE pc.[name] LIKE '%bikes%'
  123. GROUP BY GROUPING sets (
  124. (a.City), (a.City))
  125. HAVING SUM([OrderQty]) > 10
  126. ORDER BY Bikes DESC
  127.  
  128.  
  129. ____________________________________________________________
  130. SELECT [SalesOrderNumber], [TotalDue]
  131.     ,avg([TotalDue]) OVER()
  132.     ,sign([TotalDue] - avg([TotalDue]) OVER())
  133. FROM [SalesLT].[SalesOrderHeader]
  134.  
  135. _________________________________________________________
  136. SELECT [TotalDue]
  137.     ,[TotalDue]*100 / SUM([TotalDue]) OVER(partition BY [CustomerID])
  138.     ,[TotalDue]*100 / SUM([TotalDue]) OVER()
  139. FROM [SalesLT].[SalesOrderHeader]
  140.  
  141. ______________________________________________________________
  142.  
  143. PODZAPYTANIA
  144. ELECT [FirstName], [LastName]
  145. FROM [SalesLT].[Customer]
  146. WHERE [CustomerID] =
  147.  
  148. (SELECT [CustomerID]
  149. FROM [SalesLT].[SalesOrderHeader]
  150. WHERE [SalesOrderNumber] = 'SO71832')
  151.  
  152. __________________________________________________________
  153.  
  154. Stronnicowanie produktów
  155. SELECT *
  156. FROM
  157. ( SELECT [Name], [ListPrice], [Color]
  158.     ,ntile(12) OVER (ORDER BY name) AS strona
  159. FROM [SalesLT].[Product]) AS s
  160. WHERE s.strona = 1
Add Comment
Please, Sign In to add comment