| Author |
Topic |
|
cyberpd
Yak Posting Veteran
60 Posts |
Posted - 2008-08-09 : 14:52:47
|
| I have a table, named table1, consisting of date (datetime), securityid (varchar)let us take some sample data.1/1/2008 A2/1/2008 A1/1/2008 B2/1/2008 C1/1/2008 D2/1/2008 DNow, the query should return those securityid that has not all the date related with it.I mean, there is two unique date. The securityid "A" is related to both. So the query will ignore this. But the securityid "B" has only one date assossiated with it. Another date is missing. So the query will return the securityid and date missing, e.g 2/1/2008 BThe same for "C". The query will return 1/1/2008 CThe query will ignore the securityid D because it is associated with both the dates.Plz help me with this query.Thanks and regards. |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2008-08-09 : 14:59:37
|
| Are those two dates going to be static or dynamic? |
 |
|
|
cyberpd
Yak Posting Veteran
60 Posts |
Posted - 2008-08-09 : 15:03:17
|
| no ayamasIt was just an example.The date will certainly increase.that is, when i run my query i don't know the dates in the table.The dates will be dynamic. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-09 : 15:05:50
|
quote: Originally posted by cyberpd I have a table, named table1, consisting of date (datetime), securityid (varchar)let us take some sample data.1/1/2008 A2/1/2008 A1/1/2008 B2/1/2008 C1/1/2008 D2/1/2008 DNow, the query should return those securityid that has not all the date related with it.I mean, there is two unique date. The securityid "A" is related to both. So the query will ignore this. But the securityid "B" has only one date assossiated with it. Another date is missing. So the query will return the securityid and date missing, e.g 2/1/2008 BThe same for "C". The query will return 1/1/2008 CThe query will ignore the securityid D because it is associated with both the dates.Plz help me with this query.Thanks and regards.
SELECT t.securityidFROM YourTable tCROSS JOIN (SELECT COUNT(DISTINCT date) AS TotalDates FROM YourTable) t1GROUP BY t.securityidHAVING COUNT(DISTINCT date) <> MIN(t1.TotalDates) EDIT:Added MIN() thanks to Peso |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2008-08-09 : 15:09:07
|
quote: Originally posted by visakh16
quote: Originally posted by cyberpd I have a table, named table1, consisting of date (datetime), securityid (varchar)let us take some sample data.1/1/2008 A2/1/2008 A1/1/2008 B2/1/2008 C1/1/2008 D2/1/2008 DNow, the query should return those securityid that has not all the date related with it.I mean, there is two unique date. The securityid "A" is related to both. So the query will ignore this. But the securityid "B" has only one date assossiated with it. Another date is missing. So the query will return the securityid and date missing, e.g 2/1/2008 BThe same for "C". The query will return 1/1/2008 CThe query will ignore the securityid D because it is associated with both the dates.Plz help me with this query.Thanks and regards.
SELECT t.securityidFROM YourTable tCROSS JOIN (SELECT COUNT(DISTINCT date) AS TotalDates FROM YourTable) t1GROUP BY t.securityidHAVING COUNT(DISTINCT date) <> t1.TotalDates
Wow that was fast. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-09 : 15:15:29
|
Column 't1.TotalDates' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.Put a MIN() around t1.TotalDates to fix that problem. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2008-08-09 : 15:18:24
|
| Or else add TotalDates in your group by clause. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-09 : 15:18:35
|
quote: Originally posted by Peso Column 't1.TotalDates' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.Put a MIN() around t1.TotalDates to fix that problem. E 12°55'05.25"N 56°04'39.16"
Oops...nice catch |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-09 : 15:19:43
|
[code]SELECT DISTINCT s.SecurityIDFROM @Sample AS sCROSS JOIN @Sample AS tLEFT JOIN @Sample AS w ON w.Date = t.Date AND w.SecurityID = s.SecurityIDWHERE w.Date IS NULL[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
cyberpd
Yak Posting Veteran
60 Posts |
Posted - 2008-08-09 : 15:20:28
|
Thanks Visakh16 for the very fast reply.The query is throwing the following error :-Column 't1.TotalDates' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause. I have added it in the group by clause and the query is giving me result.But there is a problem..! I have 3000 rows in my table.and it is going to take a little time to check the validity of the records returned.Please can you upgrade the query so that it gives those missing dates also along with the securityid.I will get back after checking the records.Thanks. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-09 : 15:24:44
|
This should run in less than a secondSELECT DISTINCT s.SecurityID, t.DateFROM @Sample AS sCROSS JOIN @Sample AS tLEFT JOIN @Sample AS w ON w.Date = t.Date AND w.SecurityID = s.SecurityIDWHERE w.Date IS NULL E 12°55'05.25"N 56°04'39.16" |
 |
|
|
cyberpd
Yak Posting Veteran
60 Posts |
Posted - 2008-08-09 : 15:32:52
|
| ayamasI would like to say that my need was very urgent.and i could not figure out the sql query.so i wrote a SP.with 3000 rows it is giving me 866 rows in 5 secs.your query is taking 0 secs but giving me 830 rows.again pesos' query is running for 13 secs and giving 866 rows.with 1gb ram, 2.8ghz p4, intel mbd. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-09 : 15:34:48
|
13 seconds?Holy cow... Is there ANY index present?Try this since it seems you have a lot of SecurityID's for the same date-- Peso 2SELECT s.SecurityID, d.DateFROM ( SELECT SecurityID FROM @Sample GROUP BY SecurityID ) AS sCROSS JOIN ( SELECT Date FROM @Sample GROUP BY Date ) AS dLEFT JOIN @Sample AS w ON w.Date = d.Date AND w.SecurityID = s.SecurityIDWHERE w.Date IS NULL E 12°55'05.25"N 56°04'39.16" |
 |
|
|
cyberpd
Yak Posting Veteran
60 Posts |
Posted - 2008-08-09 : 15:39:26
|
| no sir, the table is not indexed.this is my table with dummy records i am talking about.the clients' table will contain > 400,000 rows.what shall i do?shall i index the table ? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-09 : 15:40:54
|
Of course!Put a covering index with this statementCREATE NONCLUSTERED INDEX IX_Table1 ON Table1 (Date, SecurityID) E 12°55'05.25"N 56°04'39.16" |
 |
|
|
cyberpd
Yak Posting Veteran
60 Posts |
Posted - 2008-08-09 : 15:42:53
|
| oh my god.the query is giving me 866 rows in 0 secs.this is awesome. thank you so much. |
 |
|
|
|