Guest User

SQL Prompt

a guest
Jul 21st, 2025
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. I need you to write me a PostgreSQL query, here is the model:
  2. generator client {
  3. provider = "prisma-client"
  4. output = "../src/generated/"
  5. previewFeatures = ["driverAdapters", "queryCompiler", "relationJoins", "typedSql"]
  6. }
  7.  
  8. datasource db {
  9. provider = "postgresql"
  10. url = env("DATABASE_URL")
  11. directUrl = env("DIRECT_URL")
  12. }
  13.  
  14. model User {
  15. id String @id @default(dbgenerated("(gen_random_uuid())::text"))
  16. email String @unique
  17. password String?
  18. name String
  19. role UserRole @default(USER)
  20. createdAt DateTime @default(now())
  21. updatedAt DateTime @default(now()) @updatedAt
  22. referalCode String? @unique
  23. referalCodeUsed Boolean @default(false)
  24. authenticationMethod AuthenticationMethod @default(EMAIL)
  25. answers Answer[]
  26. submissions Submission[]
  27. allowedProblemSets UserOnProblemSet[]
  28. }
  29.  
  30. model ProblemSet {
  31. id String @id @default(dbgenerated("(gen_random_uuid())::text"))
  32. name String @unique
  33. createdAt DateTime @default(now())
  34. updatedAt DateTime @default(now()) @updatedAt
  35. availableFrom DateTime
  36. availableTo DateTime
  37. type ProblemSetType @default(REAL)
  38. scenarios Scenario[]
  39. submissions Submission[]
  40. allowedUsers UserOnProblemSet[]
  41. }
  42.  
  43. model UserOnProblemSet {
  44. id String @id @default(dbgenerated("(gen_random_uuid())::text"))
  45. userId String
  46. problemSetId String
  47. createdAt DateTime @default(now())
  48. updatedAt DateTime @default(now()) @updatedAt
  49. problemSet ProblemSet @relation(fields: [problemSetId], references: [id], onDelete: Cascade)
  50. user User @relation(fields: [userId], references: [id], onDelete: Cascade)
  51.  
  52. @@unique([userId, problemSetId])
  53. }
  54.  
  55. model Scenario {
  56. id String @id @default(dbgenerated("(gen_random_uuid())::text"))
  57. type ScenarioType
  58. text String?
  59. videoUrl String?
  60. videoTranscription String?
  61. desc String?
  62. problemSetId String
  63. createdAt DateTime @default(now())
  64. updatedAt DateTime @default(now()) @updatedAt
  65. videoMinutes Int? @db.SmallInt
  66. videoSeconds Int? @db.SmallInt
  67. number Int @default(1) @db.SmallInt
  68. questions Question[]
  69. problemSet ProblemSet @relation(fields: [problemSetId], references: [id], onDelete: Cascade)
  70. ScenarioReport ScenarioReport[]
  71.  
  72. @@index([problemSetId, number])
  73. }
  74.  
  75. model Question {
  76. id String @id @default(dbgenerated("(gen_random_uuid())::text"))
  77. number Int @default(1) @db.SmallInt
  78. statement String
  79. createdAt DateTime @default(now())
  80. updatedAt DateTime @default(now()) @updatedAt
  81. scenarioId String
  82. scenario Scenario @relation(fields: [scenarioId], references: [id], onDelete: Cascade)
  83.  
  84. answer Answer[]
  85. QuestionReport QuestionReport[]
  86. @@index([scenarioId, number])
  87. }
  88.  
  89. model Submission {
  90. id String @id @default(dbgenerated("(gen_random_uuid())::text"))
  91. createdAt DateTime @default(now())
  92. problemSetId String
  93. updatedAt DateTime @default(now()) @updatedAt
  94. userId String?
  95. answers Answer[]
  96. problemSet ProblemSet @relation(fields: [problemSetId], references: [id], onDelete: Cascade)
  97. user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
  98. }
  99.  
  100. model Answer {
  101. id String @id @default(dbgenerated("(gen_random_uuid())::text"))
  102. answer String
  103. userId String?
  104. questionId String
  105. submissionId String
  106. createdAt DateTime @default(now())
  107. updatedAt DateTime @default(now()) @updatedAt
  108. question Question @relation(fields: [questionId], references: [id], onDelete: Cascade)
  109. submission Submission @relation(fields: [submissionId], references: [id], onDelete: Cascade)
  110. user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
  111. likes Int @default(0)
  112. answerCategoryId String?
  113. AnswerCategory AnswerCategory? @relation(fields: [answerCategoryId], references: [id])
  114.  
  115. @@index([submissionId, questionId])
  116. }
  117.  
  118. model ScenarioReport {
  119. id String @id @default(dbgenerated("(gen_random_uuid())::text"))
  120. scenarioId String
  121. createdAt DateTime @default(now())
  122. updatedAt DateTime @default(now()) @updatedAt
  123.  
  124. scenario Scenario @relation(fields: [scenarioId], references: [id], onDelete: Cascade)
  125. QuestionReport QuestionReport[]
  126.  
  127. @@unique([scenarioId])
  128. }
  129.  
  130. model QuestionReport {
  131. id String @id @default(dbgenerated("(gen_random_uuid())::text"))
  132. createdAt DateTime @default(now())
  133. updatedAt DateTime @default(now()) @updatedAt
  134. scenarioReportId String
  135. questionId String
  136.  
  137. question Question @relation(fields: [questionId], references: [id], onDelete: Cascade)
  138. scenarioReport ScenarioReport @relation(fields: [scenarioReportId], references: [id], onDelete: Cascade)
  139. AnswerCategory AnswerCategory[]
  140. }
  141.  
  142. model AnswerCategory {
  143. id String @id @default(dbgenerated("(gen_random_uuid())::text"))
  144. createdAt DateTime @default(now())
  145. updatedAt DateTime @default(now()) @updatedAt
  146. title String @unique
  147. questionReportId String
  148.  
  149. QuestionReport QuestionReport @relation(fields: [questionReportId], references: [id])
  150. Answer Answer[]
  151. }
  152.  
  153. enum ScenarioType {
  154. TEXT
  155. VIDEO
  156. }
  157.  
  158. enum UserRole {
  159. ADMIN
  160. USER
  161. }
  162.  
  163. enum ProblemSetType {
  164. DEMO
  165. REAL
  166. }
  167.  
  168. enum AuthenticationMethod {
  169. EMAIL
  170. GOOGLE
  171. }
  172.  
  173.  
  174. Here is the code: You don't necessarily need to use all of it, you can make some changes if you think it will help, this code just provides the starting ground on where to begin.
  175. First, initialize the answerIdsAlreadyIncluded and answersToInclude with the format "{"question1Id":[]}" for all questionids in the scenarioQuestionIds array.
  176.  
  177. Then, it will find the last ScenarioReport for the given scenarioId.
  178. If there wasn't a ScenarioReport for the scenarioId then:
  179. - include all answers with the right questionIds in the answersToInclude variable, with the format "{"question1Id":[{"answerId": , "answer": }]}".
  180.  
  181. However, if there was a ScenarioReport for the scenarioId then:
  182. - get all answers that was included in the last ScenarioReport for the given scenarioId, and store them in the answerIdsAlreadyIncluded variable in the format "{"question1Id":["answerId1", "answerId2"]}". You can get the answers of the ScenarioReport by looking at the QuestionReport and then its AnswerCategory.
  183. - only include answers that are not already included in the last ScenarioReport for the given scenarioId, that is in the answerIdsAlreadyIncluded variable, and add them to the answersToInclude variable in the same format as above.
  184.  
  185. BUT, answerIdsAlreadyIncluded is optional, you don't have to use this variable and populate it if you don't need it. If you can keep track of the answers to include without it, you can skip that part.
  186.  
  187. You must write PostgreSQL code that is correct, performant as possible and highly optimized, and readable.
  188.  
  189. You can optionally use indexes, just make sure to tell me which indexes you are using and why they are necessary as well as try to make your query index-friendly if possible if you decide to use indexes.
  190.  
  191. Finally, return the answersToInclude variable.
  192. -- @param {String} $1:scenarioId
  193.  
  194. DO $$
  195. DECLARE
  196. scenarioQuestionIds TEXT[];
  197. prevScenarioReportId TEXT;
  198. answerIdsAlreadyIncluded JSONB;
  199. answersToInclude JSONB;
  200. BEGIN
  201. SELECT array_agg(q.id) INTO scenarioQuestionIds
  202. FROM "question" AS q
  203. JOIN "Scenario" AS s ON q."scenarioId" = s.id
  204. WHERE s.id = $1;
  205. IF NOT FOUND THEN
  206. RAISE EXCEPTION 'No questions found for scenario with id %', $1;
  207. END IF;
  208.  
  209. SELECT sr.id INTO prevScenarioReportId
  210. FROM "ScenarioReport" AS sr
  211. WHERE sr."scenarioId" = $1
  212. ORDER BY sr."createdAt" DESC
  213. LIMIT 1;
  214.  
  215. IF NOT FOUND THEN
  216.  
  217. ELSE
  218. END $$;
Advertisement
Add Comment
Please, Sign In to add comment