Jump to content

Common Identifying Column, Different Date


Geno521

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.