csePixelated
asked on
date range from table to show weather timestamp falls within date range.
I have the following query
the new table is in [subscriber] and it is dbo.Subscriber status. this of course has the standard key 'AccountID' in it
what i need to do is check this table for 2 columns, A 'TestStart' column and a 'TestEnd' column, if the 'AlarmDate' fits within the test window for the 'AccountID' i need to have a column that states weather each line matches.
Im assuming I will have to add another left join
(AlarmDate >= [TestStart] AND AlarmDate <= [TestEnd]) = yes
SELECT DATEADD(HOUR, -6, SignalHistory.AlarmDate) 'Alarm Date',
IsNull(Subscriber.AcctLineCode, 'XX') AS LC, Subscriber.AcctNum, Subscriber.Name, Codes.Description, SignalHistory.AlarmZones as 'zone #', IsNull(Zones.Description, '') AS 'Zone Description', Subscriber.Address, Subscriber.City, Subscriber.State, SignalHistory.RawData
FROM [History0218].[dbo].[SignalHistory] SignalHistory
LEFT JOIN [SUBSCRIBER].[dbo].[Subscriber Data] Subscriber ON SignalHistory.AccountID = Subscriber.AccountID
LEFT JOIN [SUBSCRIBER].[dbo].[Zone Lists] Zones ON SignalHistory.AccountID = Zones.AccountID AND LTRIM(SignalHistory.AlarmZones) = LTRIM (Zones.Number)
LEFT JOIN [MISCELLANEOUS].[dbo].[Signal Codes] Codes ON SignalHistory.AlarmCode = Codes.Code
WHERE SignalHistory.AlarmDate >= DATEADD(HOUR, -1, GETUTCDATE())AND (Subscriber.AcctLineCode ='DS'OR Subscriber.AcctLineCode ='DSA')
ORDER BY AlarmDate DESC
I now have one more table I need to reference, to add one more column to my results, and i'm not sure how to get the result i am looking for....the new table is in [subscriber] and it is dbo.Subscriber status. this of course has the standard key 'AccountID' in it
what i need to do is check this table for 2 columns, A 'TestStart' column and a 'TestEnd' column, if the 'AlarmDate' fits within the test window for the 'AccountID' i need to have a column that states weather each line matches.
Im assuming I will have to add another left join
(AlarmDate >= [TestStart] AND AlarmDate <= [TestEnd]) = yes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
SELECT DATEADD(HOUR, -6, SignalHistory.AlarmDate) AS [Alarm Date],
ISNULL(Subscriber.AcctLineCode, 'XX') AS LC,
Subscriber.AcctNum,
Subscriber.Name,
Codes.Description,
SignalHistory.AlarmZones AS [Zone #],
ISNULL(Zones.Description, '') AS [Zone Description],
Subscriber.Address,
Subscriber.City,
Subscriber.State,
SignalHistory.RawData,
Stat.Status AS [Subscriber Status]
FROM [History0218].[dbo].[SignalHistory] SignalHistory
LEFT JOIN [SUBSCRIBER].[dbo].[Subscriber Data] Subscriber ON SignalHistory.AccountID = Subscriber.AccountID
LEFT JOIN [SUBSCRIBER].[dbo].[Zone Lists] Zones ON SignalHistory.AccountID = Zones.AccountID
AND LTRIM(SignalHistory.AlarmZones) = LTRIM (Zones.Number)
LEFT JOIN [MISCELLANEOUS].[dbo].[Signal Codes] Codes ON SignalHistory.AlarmCode = Codes.Code
LEFT JOIN [SUBSCRIBER].[dbo].[Subscriber Status] Stat ON SignalHistory.AccountID = Stat.AccountID
AND SignalHistory.AlarmDate BETWEEN Stat.TestStart AND Stat.TestEnd
WHERE SignalHistory.AlarmDate >= DATEADD(HOUR, -1, GETUTCDATE())
AND Subscriber.AcctLineCode IN ('DS', 'DSA')
ORDER BY SignalHistory.AlarmDate DESC
Parses ok, but when I run the sqlMsg 207, Level 16, State 1, Line 12
Invalid column name 'Status'.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, it works now, how would i get it to list a simple true or false?
ASKER
is it possible to shorten the column name, I tried
(Stat.AccountID AS [Subscriber Status]) AS 'TE'
of course that gave an incorrect syntax error...
Change that column to:
This will give you a 0 (false) or 1 (true).
SIGN(ISNULL(Stat.AccountID, 0)) AS [Subscriber Status]
This will give you a 0 (false) or 1 (true).
ASKER
Ty Máté Farkas, that works great, any ideas as to renaming the column?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
IIF(ISNULL(Stat.AccountID, 0) > 0,'True','False') AS TE
parse results " Incorrect syntax near '>' "
That is correct. Check that previous column is terminated by a comma.
ASKER
it is terminated by a comma, still shows " Incorrect syntax near '>' " Im running sql server 2008r2 if that makes a difference?
ASKER
SELECT DATEADD(HOUR, -6, SignalHistory.AlarmDate) AS [Alarm Date],
ISNULL(Subscriber.AcctLineCode, 'XX') AS LC,
Subscriber.AcctNum,
Subscriber.Name,
Codes.Description,
SignalHistory.AlarmZones AS [Zone #],
ISNULL(Zones.Description, '') AS [Zone Description],
Subscriber.Address,
Subscriber.City,
Subscriber.State,
SignalHistory.RawData,
IIF(ISNULL(Stat.AccountID, 0) > 0,'True','False') AS TE
FROM [History0218].[dbo].[SignalHistory] SignalHistory
LEFT JOIN [SUBSCRIBER].[dbo].[Subscriber Data] Subscriber ON SignalHistory.AccountID = Subscriber.AccountID
LEFT JOIN [SUBSCRIBER].[dbo].[Zone Lists] Zones ON SignalHistory.AccountID = Zones.AccountID
AND LTRIM(SignalHistory.AlarmZones) = LTRIM (Zones.Number)
LEFT JOIN [MISCELLANEOUS].[dbo].[Signal Codes] Codes ON SignalHistory.AlarmCode = Codes.Code
LEFT JOIN [SUBSCRIBER].[dbo].[Subscriber Status] Stat ON SignalHistory.AccountID = Stat.AccountID
AND SignalHistory.AlarmDate BETWEEN Stat.TestStart AND Stat.TestEnd
WHERE SignalHistory.AlarmDate >= DATEADD(HOUR, -8, GETUTCDATE())
AND Subscriber.AcctLineCode IN ('rc', 'rcd')
ORDER BY SignalHistory.AlarmDate DESC
Msg 102, Level 15, State 1, Line 12Incorrect syntax near '>'.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
works like a charm Mark, thanks
I shortened the answers as i need them as low profile as possible
SELECT DATEADD(HOUR, -6, SignalHistory.AlarmDate) AS [Alarm Date],
ISNULL(Subscriber.AcctLineCode, 'XX') AS LC,
Subscriber.AcctNum,
Subscriber.Name,
Codes.Description,
SignalHistory.AlarmZones AS [Zone #],
ISNULL(Zones.Description, '') AS [Zone Description],
Subscriber.Address,
Subscriber.City,
Subscriber.State,
SignalHistory.RawData,
case when isnull(Stat.AccountID,0) > 0 then '1' else 'F' end as TE
FROM [History0218].[dbo].[SignalHistory] SignalHistory
LEFT JOIN [SUBSCRIBER].[dbo].[Subscriber Data] Subscriber ON SignalHistory.AccountID = Subscriber.AccountID
LEFT JOIN [SUBSCRIBER].[dbo].[Zone Lists] Zones ON SignalHistory.AccountID = Zones.AccountID
AND LTRIM(SignalHistory.AlarmZones) = LTRIM (Zones.Number)
LEFT JOIN [MISCELLANEOUS].[dbo].[Signal Codes] Codes ON SignalHistory.AlarmCode = Codes.Code
LEFT JOIN [SUBSCRIBER].[dbo].[Subscriber Status] Stat ON SignalHistory.AccountID = Stat.AccountID
AND SignalHistory.AlarmDate BETWEEN Stat.TestStart AND Stat.TestEnd
WHERE SignalHistory.AlarmDate >= DATEADD(HOUR, -8, GETUTCDATE())
AND Subscriber.AcctLineCode IN ('rc', 'rcd')
ORDER BY SignalHistory.AlarmDate DESC
I shortened the answers as i need them as low profile as possible
AND (AlarmDate >= [TestStart] AND AlarmDate <= [TestEnd])
in the WHERE clause
no need for the = yes that you had