Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 help needed in query

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 A
2/1/2008 A
1/1/2008 B
2/1/2008 C
1/1/2008 D
2/1/2008 D
Now, 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 B
The same for "C". The query will return 1/1/2008 C
The 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?
Go to Top of Page

cyberpd
Yak Posting Veteran

60 Posts

Posted - 2008-08-09 : 15:03:17
no ayamas

It 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.
Go to Top of Page

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 A
2/1/2008 A
1/1/2008 B
2/1/2008 C
1/1/2008 D
2/1/2008 D
Now, 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 B
The same for "C". The query will return 1/1/2008 C
The 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.securityid
FROM YourTable t
CROSS JOIN (SELECT COUNT(DISTINCT date) AS TotalDates FROM YourTable) t1
GROUP BY t.securityid
HAVING COUNT(DISTINCT date) <> MIN(t1.TotalDates)


EDIT:Added MIN() thanks to Peso
Go to Top of Page

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 A
2/1/2008 A
1/1/2008 B
2/1/2008 C
1/1/2008 D
2/1/2008 D
Now, 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 B
The same for "C". The query will return 1/1/2008 C
The 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.securityid
FROM YourTable t
CROSS JOIN (SELECT COUNT(DISTINCT date) AS TotalDates FROM YourTable) t1
GROUP BY t.securityid
HAVING COUNT(DISTINCT date) <> t1.TotalDates




Wow that was fast.
Go to Top of Page

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"
Go to Top of Page

ayamas
Aged Yak Warrior

552 Posts

Posted - 2008-08-09 : 15:18:24
Or else add TotalDates in your group by clause.
Go to Top of Page

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
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-09 : 15:19:43
[code]SELECT DISTINCT s.SecurityID
FROM @Sample AS s
CROSS JOIN @Sample AS t
LEFT JOIN @Sample AS w ON w.Date = t.Date
AND w.SecurityID = s.SecurityID
WHERE w.Date IS NULL[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

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.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-09 : 15:24:44
This should run in less than a second
SELECT DISTINCT	s.SecurityID,
t.Date
FROM @Sample AS s
CROSS JOIN @Sample AS t
LEFT JOIN @Sample AS w ON w.Date = t.Date
AND w.SecurityID = s.SecurityID
WHERE w.Date IS NULL


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

cyberpd
Yak Posting Veteran

60 Posts

Posted - 2008-08-09 : 15:32:52
ayamas
I 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.
Go to Top of Page

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 2
SELECT s.SecurityID,
d.Date
FROM (
SELECT SecurityID
FROM @Sample
GROUP BY SecurityID
) AS s
CROSS JOIN (
SELECT Date
FROM @Sample
GROUP BY Date
) AS d
LEFT JOIN @Sample AS w ON w.Date = d.Date
AND w.SecurityID = s.SecurityID
WHERE w.Date IS NULL


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

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 ?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-09 : 15:40:54
Of course!
Put a covering index with this statement

CREATE NONCLUSTERED INDEX IX_Table1 ON Table1 (Date, SecurityID)


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

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.
Go to Top of Page
   

- Advertisement -