Gossamer Forum
Home : General : Databases and SQL :

coorelated sub query with group by

Quote Reply
coorelated sub query with group by
I've got an sql statement that ive developed for an application which has been giving back incorrect results for the past few months and ive finally figured out why but cant figure out how to fix it. Anyways, Here it is:

*****************************

SELECT DATABASEST AS storm_num, MAX(MAXWIND_KT) AS max_wind_speed, DATEDIFF(dd, MIN(CONVERT(datetime, [DATE], 104)), MAX(CONVERT(datetime,
[DATE], 104))) AS duration_days, COUNT(*) AS num_tracks,
(SELECT TOP 1 [NAME]
FROM NHCBestTracks1851To2001$
WHERE NHCBestTracks1851To2001$.DATABASEST = s.DATABASEST
ORDER BY maxwind_kt DESC, [date], hour_utc) AS storm_name,
(SELECT TOP 1 PRESSURE_M
FROM NHCBestTracks1851To2001$
WHERE NHCBestTracks1851To2001$.DATABASEST = s.DATABASEST
ORDER BY maxwind_kt DESC, [date], hour_utc) AS PRESSURE_M,
(SELECT TOP 1 [date]
FROM NHCBestTracks1851To2001$
WHERE NHCBestTracks1851To2001$.DATABASEST = s.DATABASEST
ORDER BY maxwind_kt DESC, [date], hour_utc) AS storm_date,
(SELECT TOP 1 hour_utc
FROM NHCBestTracks1851To2001$
WHERE NHCBestTracks1851To2001$.DATABASEST = s.DATABASEST
ORDER BY maxwind_kt DESC, [date], hour_utc) AS hour_utc,
(SELECT TOP 1 saffirsimp
FROM NHCBestTracks1851To2001$
WHERE NHCBestTracks1851To2001$.DATABASEST = s.DATABASEST
ORDER BY maxwind_kt DESC, [date], hour_utc) AS ss_scale,
(SELECT TOP 1 yearlystor
FROM NHCBestTracks1851To2001$
WHERE NHCBestTracks1851To2001$.DATABASEST = s.DATABASEST
ORDER BY maxwind_kt DESC, [date], hour_utc) AS year_storm_num,
(SELECT TOP 1 comment
FROM NHCBestTracks1851To2001$
WHERE NHCBestTracks1851To2001$.DATABASEST = s.DATABASEST
ORDER BY maxwind_kt DESC, [date], hour_utc) AS comment,
(SELECT TOP 1 AVG(systemspee)
FROM NHCBestTracks1851To2001$
WHERE NHCBestTracks1851To2001$.DATABASEST = s.DATABASEST AND lastpositi = 0) AS avg_trans_speed
FROM dbo.NHCBestTracks1851To2001$ s
WHERE (DATABASEST in (223)) AND (COMMENT in ('*'))
GROUP BY DATABASEST

******************************

The info thats comming back is correct :

storm_num, max_wind_speed, duration_days and num_tracks.

the incorrect data are:

storm_name, pressure_M, storm_date, hour_utc, ss_scale, year_storm_num, comment, avg_trans_speed.

The reason is because the Where statement at the end ie "WHERE (DATABASEST = 223) AND (COMMENT = '*')...." does not effect the rows that I just mentioned. I want to figure out a way to have the overall WHERE (the one at the end of entire statment) to effect these results.

The tricky thing is that I cant put the where stuff right up in the coorelated selects (i think thats what they are called) because All that part is hard coded.

See I generate this staement on the fly. that is i add stuff to the end where clause on the fly in an asp page. I could have other things in there at the end like Where commment in ('*','E')...

Note:
There can be other things in the where clause like a date range. (Where date between 11/11/11 and 12/12/12).


This might be totally easy to figure out but IM no sql expert. But I really really need to get this fixed.
I hope someone can figure this out.

Thanks in advance

Dave
Subject Author Views Date
Thread coorelated sub query with group by dbenoit64 5993 Jul 7, 2003, 5:16 PM
Thread Re: [dbenoit64] coorelated sub query with group by
dregs2 5829 Jul 9, 2003, 2:55 PM
Post Re: [dregs2] coorelated sub query with group by
Stealth 5750 Jul 9, 2003, 5:09 PM
Post Re: [dregs2] coorelated sub query with group by
dbenoit64 5750 Jul 10, 2003, 10:21 AM