Advertisement
YasserKhalil2019

SQL Query

Aug 9th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. Sub CreateSQLQuery()
  2. Dim SQLQuery As String
  3. SQLQuery = _
  4. "SELECT t1.[Genre], t.[Number of Films], t1.[Total Run Time], t1.[Average Run Time] " & _
  5. "FROM " & _
  6. "(SELECT " & _
  7. "0 AS [SortColumn] " & _
  8. ",t.[Genre] " & _
  9. ",Sum(t.[Number of Films]) AS [Number of Films] " & _
  10. ",Sum(t.[Total Run Time]) AS [Total Run Time] " & _
  11. ",Sum(t.[Total Run Time]) / Sum(t.[Number of Films]) AS [Average Run Time] " & _
  12. "FROM " & _
  13. "(SELECT [Genre], [Number of Films], [Total Run Time], 2010 AS [Year] FROM [Genres2010$] " & _
  14. "UNION ALL SELECT [Genre], [Number of Films], [Total Run Time], 2011 FROM [Genres2011$] " & _
  15. "UNION ALL SELECT [Genre], [Number of Films], [Total Run Time], 2012 FROM [Genres2012$] " & _
  16. "UNION ALL SELECT [Genre], [Number of Films], [Total Run Time], 2013 FROM [Genres2013$] " & _
  17. "UNION ALL SELECT [Genre], [Number of Films], [Total Run Time], 2014 FROM [Genres2014$] " & _
  18. "UNION ALL SELECT [Genre], [Number of Films], [Total Run Time], 2015 FROM [Genres2015$] " & _
  19. "UNION ALL SELECT [Genre], [Number of Films], [Total Run Time], 2016 FROM [Genres2016$]) AS t " & _
  20. "GROUP BY " & _
  21. "t.[Genre] "
  22. SQLQuery = SQLQuery & _
  23. "UNION ALL SELECT " & _
  24. "1 " & _
  25. ",'SUMMARY' " & _
  26. ",Sum(t.[Number of Films]) AS [Number of Films] " & _
  27. ",Sum(t.[Total Run Time]) AS [Total Run Time] " & _
  28. ",Sum(t.[Total Run Time]) / Sum(t.[Number of Films]) AS [Average Run Time] " & _
  29. "FROM " & _
  30. "(SELECT [Genre], [Number of Films], [Total Run Time], 2010 AS [Year] FROM [Genres2010$] " & _
  31. "UNION ALL SELECT [Genre], [Number of Films], [Total Run Time], 2011 FROM [Genres2011$] " & _
  32. "UNION ALL SELECT [Genre], [Number of Films], [Total Run Time], 2012 FROM [Genres2012$] " & _
  33. "UNION ALL SELECT [Genre], [Number of Films], [Total Run Time], 2013 FROM [Genres2013$] " & _
  34. "UNION ALL SELECT [Genre], [Number of Films], [Total Run Time], 2014 FROM [Genres2014$] " & _
  35. "UNION ALL SELECT [Genre], [Number of Films], [Total Run Time], 2015 FROM [Genres2015$] " & _
  36. "UNION ALL SELECT [Genre], [Number of Films], [Total Run Time], 2016 FROM [Genres2016$]) AS t) AS t1 " & _
  37. "ORDER BY " & _
  38. "t1.[SortColumn] ASC, t1.[Number of Films] DESC "
  39. GetQueryResults SQLQuery
  40. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement