2016-01-20 23 views
2

我这里有这个代码,我需要得到结合月份的名称和日与天的SQL Server相结合月份名称

SELECT DATENAME(MONTH, GETDATE()) AS NameOfMonth, 
     DATEPART(DAY, TIMESTAMP) AS DateDay, 
     AVG(CAST(REPLACE(roundtrip, 'ms', '') AS NUMERIC(30, 2))) AS DailyDuration 
FROM (
    SELECT TIMESTAMP, 
      roundtrip 
    FROM tblServer_Status 
    WHERE MONTH(TIMESTAMP) = MONTH(DATEADD(dd, -1, GETDATE())) 
     AND YEAR(TIMESTAMP) = YEAR(DATEADD(dd, -1, GETDATE())) 
     AND DATEPART(HOUR, TIMESTAMP) BETWEEN 0 AND 23 
     AND server = 'WSMV217' 
     AND site_code = 'MK' 
) AS Calls 
GROUP BY DATEPART(DAY, TIMESTAMP) 
ORDER BY DATEPART(DAY, TIMESTAMP) 

这代码的输出是这样

| Name of Month | Date Day | Daily Duration | 
    January   1   351 
    January   2   351 
    January   3   351 
    January   4   351 
    January   5   351 
    January   6   351 
    January   7   351 

,我需要的名字和天这样的组合:

| Name of Month/Date Day | Daily Duration | 
    January 1     351 
    January 2     351 
    January 3     351 
    January 4     351 
    January 5     351 
    January 6     351 
    January 7     351 

任何一个可以帮助我。我知道这是一个简单的代码,但我发誓。我不知道该怎么做。

回答

0

尝试这样的:

SELECT (DATENAME(month,getdate()) + ' ' + CAST(DATEPART(day,timestamp) as varchar(5))) AS [Name of Month/Date Day], 
AVG(CAST(REPLACE(roundtrip, 'ms', '') AS NUMERIC (30,2))) AS DailyDuration 
FROM 
(SELECT timestamp, roundtrip 
    FROM tblServer_Status 
    WHERE Month(timestamp) = Month(DateAdd(dd, -1, GetDate())) 
    And Year(timestamp) = Year(DateAdd(dd, -1, GetDate())) 
    And DATEPART(hour, timestamp) BETWEEN 0 AND 23 and server = 'WSMV217' 
    And site_code = 'MK') AS Calls 
GROUP BY DATEPART(day, timestamp) 
ORDER BY DATEPART(day, timestamp) 
+0

非常感谢你。它的工作。 – Elphrian

+0

@Elphrian: - 不客气1 –