Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.57 KB | None | 0 0
  1. create table #temp
  2. (
  3. title nvarchar(2000),
  4. id int not null,
  5. cover_url nvarchar(255),
  6. m_description nvarchar(MAX),
  7. mdex_url nvarchar(255),
  8. rating nvarchar(255),
  9. views_count nvarchar(255)
  10. )
  11. -----------------------------------
  12. -----COPY DATA WITH INCLUDE TAGS---
  13. insert into #temp
  14. select distinct m.title,m.id,m.cover_url,m.m_description,m.mdex_url,m.rating,m.views_count from MangaGenres mg INNER JOIN Manga m on mg.manga_ID=m.ID
  15. INNER JOIN Genres g on mg.genre_ID=g.genreID
  16. WHERE g.genreID IN (1,2,3)
  17. and m.id not in
  18. (
  19. -----------------------------------
  20. -------------EXCLUDE---------------
  21. select distinct m.id from MangaGenres mg INNER JOIN Manga m on mg.manga_ID=m.ID
  22. INNER JOIN Genres g on mg.genre_ID=g.genreID
  23. WHERE g.genreID IN (4,5,6)
  24. )
  25. ------------------------------------
  26. -------------AGGREGATE--------------
  27. select #temp.title,MangaGenres.genre_ID,#temp.ID,#temp.cover_url,#temp.m_description,#temp.mdex_url,#temp.rating,#temp.views_count into #temp1 from #temp
  28. inner join MangaGenres on MangaGenres.manga_ID = #temp.ID
  29. select #temp1.title,string_agg(genres.genre_name,',') as Genres,#temp1.cover_url,#temp1.m_description,#temp1.mdex_url,#temp1.rating,#temp1.views_count from #temp1
  30. inner join genres on genres.genreID = #temp1.genre_ID
  31. where not title = ''
  32. group by #temp1.title,#temp1.cover_url,#temp1.m_description,#temp1.mdex_url,#temp1.rating,#temp1.views_count
  33. ---------DUMPING DATA INTO #temp2----
  34. -------------------------------------
  35. select #temp1.title,string_agg(genres.genre_name,',') as Genres,#temp1.cover_url,#temp1.m_description,#temp1.mdex_url,#temp1.rating,#temp1.views_count into #temp2 from #temp1
  36. inner join genres on genres.genreID = #temp1.genre_ID
  37. group by #temp1.title,#temp1.cover_url,#temp1.m_description,#temp1.mdex_url,#temp1.rating,#temp1.views_count;
  38. ---------------------------------
  39. ------------SORTING-----------------
  40. with ResultGenreMappingCTE (ANIME_TITLE, GENRE_NAME)
  41. as (
  42.     select t1.title, t2.[value] as GENRE_NAME
  43.     from #temp2 as t1
  44.     cross apply STRING_SPLIT(t1.Genres, ',') as t2
  45. ),
  46. TargetGenreCTE (GENRE_NAME)
  47. as (
  48.     select * from STRING_SPLIT('1,2,3', ',')
  49. ),
  50. MatchMakeCTE (ANIME_TITLE, MATCH_SCORE)
  51. as (
  52.     select t1.ANIME_TITLE, count(t1.GENRE_NAME) as MATCH_SCORE
  53.     from ResultGenreMappingCTE as t1
  54.     inner join TargetGenreCTE as t2 on t1.GENRE_NAME = t2.GENRE_NAME
  55.     group by t1.ANIME_TITLE
  56. ) select *
  57. from #temp2 as t1
  58. inner join MatchMakeCTE as t2 on t1.title = t2.ANIME_TITLE
  59. order by t2.MATCH_SCORE desc
  60. -----------------------------------
  61. drop table #temp1
  62. drop table #temp
  63. drop table #temp2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement