dbTalk Databases Forums  

SQL Query date doubt

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SQL Query date doubt in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
valdemirs
 
Posts: n/a

Default SQL Query date doubt - 02-27-2010 , 08:45 AM






Hi:
Please, could you help me with this SQL query ?

I need get data from last 3 months,
but itīs shows me only January's data.
(Query doesnīt get data from november and december/2009)

SELECT TOP 10000 CONVERT(DateTime,
LTRIM(MONTH(DateTime)) + '/01/' + LTRIM(YEAR(DateTime)),
101) AS SummaryMonth,
Nodes.NodeID AS NodeID,
Nodes.Caption AS NodeName,
Nodes.IP_Address AS IP_Address,
APM_AlertsAndReportsData.ComponentName AS Component_Name,
AVG(APM_ResponseTime.StatisticData) AS AVERAGE_of_StatisticsData

FROM
(Nodes INNER JOIN APM_AlertsAndReportsData ON (Nodes.NodeID =
APM_AlertsAndReportsData.NodeId)) INNER JOIN APM_ResponseTime ON
(APM_AlertsAndReportsData.ComponentId = APM_ResponseTime.ComponentID)


WHERE


( DateTime >= dateadd(mm,datediff(mm,0,getdate())-3,0) AND DateTime
<dateadd(mm,datediff(mm,0,getdate()),0) )




AND
(
(DATEPART(weekday, DateTime) <> 1) AND
(DATEPART(weekday, DateTime) <> 7) and
(Convert(Char,DateTime,108) >= '08:00') AND
(Convert(Char,DateTime,108) <= '17:00')

)

AND
(
(Nodes.Grupo_Funcao = 'Ipiranga') AND
(
(Nodes.Caption = 'serv-abp3') OR
(Nodes.Caption = 'notesmatriz02'))
)


GROUP BY CONVERT(DateTime, LTRIM(MONTH(DateTime)) + '/01/' +
LTRIM(YEAR(DateTime)), 101),
Nodes.NodeID, Nodes.Caption, Nodes.IP_Address,
APM_AlertsAndReportsData.ComponentName


ORDER BY SummaryMonth ASC

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: SQL Query date doubt - 02-27-2010 , 01:21 PM






valdemirs (valdemirs (AT) gmail (DOT) com) writes:
Quote:
I need get data from last 3 months,
but itīs shows me only January's data.
(Query doesnīt get data from november and december/2009)
Have you verified that there is data from November and December 2009?
And does this data also include a time portion between 08:00 and 17:00?

And is the data type of the DateTime column datetime? Or is it something
else?

By the way, how many rows does your query return? The full 10000?


Quote:
SELECT TOP 10000 CONVERT(DateTime,
LTRIM(MONTH(DateTime)) + '/01/' + LTRIM(YEAR(DateTime)),
101) AS SummaryMonth,
As lot easier way to write this is:

convert(datetime, convert(char(6), DateTime, 112) + '01')



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.