School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,139 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,793 people online right now. Registration is fast and FREE... Join Now!




Getting Rid of Duplicates in a SELECT Query

 

Getting Rid of Duplicates in a SELECT Query

Mike007

26 Jun, 2009 - 11:53 AM
Post #1

D.I.C Regular
Group Icon

Joined: 30 Aug, 2007
Posts: 306



Thanked: 5 times
Dream Kudos: 75
My Contributions
Hi, I am trying to get rid of some duplicates in this query:

sql

SELECT DISTINCT Songs.songId AS songId, title, artist, recordingType, recordingTitle, year, playtime, popularity, frequency, date FROM Songs INNER JOIN SongsInfo ON Songs.songId = SongsInfo.songId LEFT JOIN Playlists ON Songs.songId = Playlists.songId WHERE (date <= 1273122000000 OR date >= 1273208340000) OR date IS NULL


What I am trying to do here, is find all the songs that were not added to a playlist on a certian date. That means that they either do not appear in the Playlists table at all, or they have a date that is not in range. There is a one-to-one relationship between Songs and SongsInfo (it just provides some extra information that needs to be kept separate). Songs and Playlists have a one-to-many relationship.

The DISTINCT key word gets rid of only half of the duplicates. What am I doing wrong?

I am using SQLite btw.

User is offlineProfile CardPM
+Quote Post


P4L

RE: Getting Rid Of Duplicates In A SELECT Query

26 Jun, 2009 - 12:07 PM
Post #2

Geek 4 Life
Group Icon

Joined: 7 Feb, 2008
Posts: 2,178



Thanked: 16 times
Dream Kudos: 125
My Contributions
QUOTE(Mike007 @ 26 Jun, 2009 - 11:53 AM) *

Hi, I am trying to get rid of some duplicates in this query:

sql

SELECT DISTINCT Songs.songId AS songId, title, artist, recordingType, recordingTitle, year, playtime, popularity, frequency, date FROM Songs INNER JOIN SongsInfo ON Songs.songId = SongsInfo.songId LEFT JOIN Playlists ON Songs.songId = Playlists.songId WHERE (date <= 1273122000000 OR date >= 1273208340000) OR date IS NULL


What I am trying to do here, is find all the songs that were not added to a playlist on a certian date. That means that they either do not appear in the Playlists table at all, or they have a date that is not in range. There is a one-to-one relationship between Songs and SongsInfo (it just provides some extra information that needs to be kept separate). Songs and Playlists have a one-to-many relationship.

The DISTINCT key word gets rid of only half of the duplicates. What am I doing wrong?

I am using SQLite btw.


Most likely it's your WHERE statement. I am not sure if in SQLite that you can use BETWEEN, but for a date range, I would try using
CODE

Where date Bewteen 1273122000000 and 1273208340000
OR date IS NULL


try that, and see what happens. Other than that, everything else looks normal.



This post has been edited by P4L: 26 Jun, 2009 - 12:08 PM
User is offlineProfile CardPM
+Quote Post

Mike007

RE: Getting Rid Of Duplicates In A SELECT Query

26 Jun, 2009 - 12:11 PM
Post #3

D.I.C Regular
Group Icon

Joined: 30 Aug, 2007
Posts: 306



Thanked: 5 times
Dream Kudos: 75
My Contributions
They do, but this is not what I was trying to do, i was looking for records outside that range. I used date NOT BETWEEN ... but that gave me the same result as before.

EDIT: Ok I got it! smile.gif You were in the right direction. I was just looking for distinct records that did not include the date. So I just removed date field from the selection part. Thank you for the quick reply however, it is much appreciated.

This post has been edited by Mike007: 26 Jun, 2009 - 12:18 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 03:26PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month