Jump to content

Common Identifying Column, Different Date


Go to solution Solved by requinix,

Recommended Posts

I am attempting to write an SQL which will return all instances of a certain column matching but which was posted at a different time.

 

SELECT tbl.cusip, date, color, price, FROM tbl INNER JOIN (SELECT name FROM name GROUP BY name HAVING count(name) > 1) dup ON tbl.name = dup.name;

 

That query will spit back all instances of entries with duplicates of the column "name" (which is what I want) but seems to break if I try to insert a WHERE clause such as, WHERE date > '2012-02-01' AND color='blue'.  So I'm basically stuck to viewing every single instance of a particular entry instead of only seeing the past month's.  Can anyone help me out? Thank you.

I've basically tried inserting that WHERE clause in every conceivable spot on the query. Let me explain how it breaks if I insert it at the end of the query. If I change it to:

 

SELECT tbl.cusip, date, color, price, FROM tbl INNER JOIN (SELECT name FROM name GROUP BY name HAVING count(name) > 1) dup ON tbl.name = dup.name WHERE date>'2013-02'01';

 

The query is just giving me a list of all distinct names that have occurred since that date.

 

 

If I insert it into the INNER JOIN's (), like this:  

 

SELECT tbl.cusip, date, color, price, FROM tbl INNER JOIN (SELECT name FROM name WHERE date>'2013-02'01' GROUP BY name HAVING count(name) > 1) dup ON tbl.name = dup.name;  

 

This query works, spitting out the duplicates I want to see, but it won't restrict the date. It's still giving me rows from the entire database.

  • Solution

Then try having two of those date comparisons, one in each place. The innermost query will then find duplicate names within your time period while the outermost query will find matching records with those names... within your time period.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.