Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 680775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:24:35+00:00 2026-05-14T01:24:35+00:00

I have several tables: CREATE TABLE [dbo].[Tracks]( [Id] [uniqueidentifier] NOT NULL, [Artist_Id] [uniqueidentifier] NOT

  • 0

I have several tables:

CREATE TABLE [dbo].[Tracks](
    [Id] [uniqueidentifier] NOT NULL,
    [Artist_Id] [uniqueidentifier] NOT NULL,
    [Album_Id] [uniqueidentifier] NOT NULL,
    [Title] [nvarchar](255) NOT NULL,
    [Length] [int] NOT NULL,
 CONSTRAINT [PK_Tracks_1] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[TrackHistory](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Track_Id] [uniqueidentifier] NOT NULL,
    [Datetime] [datetime] NOT NULL,
 CONSTRAINT [PK_TrackHistory] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


INSERT INTO [cooltunes].[dbo].[TrackHistory]
           ([Track_Id]
           ,[Datetime])
     VALUES
           ("335294B0-735E-4E2C-8389-8326B17CE813"
           ,GETDATE())



CREATE TABLE [dbo].[Ratings](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Track_Id] [uniqueidentifier] NOT NULL,
    [User_Id] [uniqueidentifier] NOT NULL,
    [Rating] [tinyint] NOT NULL,
 CONSTRAINT [PK_Ratings] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

INSERT INTO [cooltunes].[dbo].[Ratings]
           ([Track_Id]
           ,[User_Id]
           ,[Rating])
     VALUES
           ("335294B0-735E-4E2C-8389-8326B17CE813"
           ,"C7D62450-8BE6-40F6-80F1-A539DA301772"
           ,1)


Users
User_Id|Guid
Other fields

Links between the tables are pretty obvious.

TrackHistory has each track added to it as a row whenever it is played ie. a track will appear in there many times.

Ratings value will either be 1 or -1.

What I’m trying to do is select the Track with the highest rating, that is more than 2 hours old, and if there is a duplicate rating for a track (ie a track receives 6 +1 ratings and 1 – rating, giving that track a total rating of 5, another track also has a total rating of 5), the track that was last played the longest ago should be returned. (If all tracks have been played within the last 2 hours, no rows should be returned)

I’m getting somewhere doing each part individually using the link above, SUM(Value) and GROUP BY Track_Id, but I’m having trouble putting it all together.

Hopefully someone with a bit more (MS)SQL knowledge will be able to help me. Many thanks!

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:24:36+00:00Added an answer on May 14, 2026 at 1:24 am
    select top 1 t.Id, SUM(r.Rating) as Rating, MAX(Datetime) as LastPlayed
    from Tracks t
    inner join TrackHistory h on t.Id = h.Track_Id
    inner join Ratings r on t.Id = r.Track_Id
    where h.Track_Id not in (
        select Track_Id 
        from TrackHistory 
        where Datetime > DATEADD(HOUR, -2, getdate())
    )
    group by t.Id
    order by Rating desc, LastPlayed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table: CREATE TABLE [dbo].[Tree]( [AutoID] [int] IDENTITY(1,1) NOT NULL, [Category]
I have a log table that is inherited by several tables: CREATE TABLE log
Hi i'm trying to create a league table in which i have several different
I have several tables, books, bookcategories, categories bookcategories is a join table to allow
I have several *.sql files with script to create table and sprocs for a
I'm trying to write a MySQL script that creates several tables. I have: CREATE
I have a table called 1 Main Contacts (Main), and several other tables. They
I have several tables 1 contacts and multiple data. Contacts has an ID field
I have several tables that I am selecting data from. I am trying to
I have several tables that I want to put side by side, however when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.