Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. -- 1. Give me all invoices for user x and which city he is from - easy
  2. declare @userid int = 1324;
  3. select TInvoice.nInvoiceId, TCity.cName
  4. from TInvoice
  5. inner join TCity on TCity.nCityId=(select TUser.nCityId from TUser where TUser.nUserId=@userid)
  6. where TInvoice.nUserId=@userid;
  7.  
  8.  
  9. -- 2. Give me all the products and how many products user x has bought in those invoices mentioned above and their average ratings
  10. select TProduct.cName, sum(TInVoiceLine.nQuantity), TProduct.nAverageRating
  11. from TInvoice
  12. inner join TInVoiceLine on TInvoice.nInvoiceId=TInVoiceLine.nInvoiceId
  13. inner join TProduct on TProduct.nProductId=TInVoiceLine.nProductId
  14. where TInvoice.nUserId=@userid
  15. group by TProduct.cName, TProduct.nAverageRating;
  16. -- Why do we need group by here but not above?
  17.  
  18.  
  19. -- 3. What is the average price of those products user x bought
  20. select AVG(TInVoiceLine.nUnitPrice)
  21. from TInVoiceLine
  22. where TInVoiceLine.nInvoiceId in (select TInvoice.nInvoiceId from TInvoice where TInvoice.nUserId=@userid);
  23.  
  24.  
  25. -- 4. What is the rating of each product user x bought
  26. Select TRating.nStar, TRating.cComment, TRating.nProductId
  27. from TRating
  28. where TRating.nProductId in (select TInVoiceLine.nProductId from TInVoiceLine where TInVoiceLine.nInvoiceId in (select TInvoice.nInvoiceId from TInvoice where TInvoice.nUserId=@userid));
  29.  
  30.  
  31. -- 5. Which creditcards did user x use
  32. select TCreditCard.cCardNumber
  33. from TCreditCard
  34. where TCreditCard.nUserId=@userid and TCreditCard.nTotalPurchase > 0;
  35.  
  36.  
  37. -- 6. Give me the dates for which user x bought every product
  38. select TInvoice.dInvoiceDate
  39. from TInvoice
  40. where TInvoice.nUserId=@userid;
  41.  
  42.  
  43. -- 7. Give me, if any, the ratings and comments user x has made
  44. Select TRating.nStar, TRating.cComment, TRating.nProductId
  45. from TRating
  46. where TRating.nUserId=@userid;
  47.  
  48.  
  49. -- 8. Change the last name of user x
  50. update TUser
  51. set cSurname='lastname123'
  52. where nUserId=@userid;
  53. -- proof
  54. select TUser.cName, TUser.cSurname
  55. from TUser where TUser.nUserId=@userid;
  56.  
  57.  
  58. -- 9. Change a comment for a rating user x has made
  59. select TRating.cComment, TRating.nUserId, TRating.nProductId
  60. from TRating;
  61.  
  62. update TRating
  63. set cComment = 'ændret'
  64. where TRating.nUserId=2 and nProductId=3;
  65.  
  66.  
  67. -- 10. Delete a comment user x has made
  68. delete from TRating
  69. where nUserId=2 and nProductId=3;
  70.  
  71.  
  72. -- 11. Show me the changes
  73. select * from RatingHistory where nUserId=2 and nProductId=3;
  74.  
  75.  
  76. -- 12. Create a stored procedure to get by last names
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement