sql >> Databasteknik >  >> RDS >> Sqlserver

Samla upp helgdata annorlunda än vardagar

detta kommer att göra:

SELECT CAST(Submitted AS DATE) AS [Submission Date]
    , DATEPART(weekday, CAST(Submitted AS DATE)) AS [DayOfWeek]
    , DATENAME(dw, CAST(Submitted AS DATE)) AS [DayOfWeekForm]
    , count(*) AS [Submission Count]
    , CASE 
        WHEN (DATEPART(weekday, CAST(Submitted AS DATE))) IN (
                '1'
                ,'7'
                )
            THEN ('Weekend')
        ELSE DATENAME(dw, CAST(Submitted AS DATE))
        END AS [End Column]
    , xyz_cnt._cnt AS [End Sub]
FROM dbo.xyz LEFT JOIN
 (SELECT CASE DATEPART(weekday, CAST(Submitted AS DATE))  
        WHEN '1' THEN DATEADD(dd, -2, CAST(Submitted AS DATE))
        WHEN '7' THEN DATEADD(dd, -1, CAST(Submitted AS DATE))
        ELSE CAST(Submitted AS DATE) END AS [SubmitDate]
    , count(*) AS _cnt 
  FROM dbo.xyz
  GROUP BY
  CASE DATEPART(weekday, CAST(Submitted AS DATE))  
        WHEN '1' THEN DATEADD(dd, -2, CAST(Submitted AS DATE))
        WHEN '7' THEN DATEADD(dd, -1, CAST(Submitted AS DATE))
        ELSE CAST(Submitted AS DATE) END ) AS xyz_cnt 
  ON CAST(xyz.Submitted AS DATE) = xyz_cnt.SubmitDate
WHERE CAST(Submitted AS DATE) BETWEEN '2012-09-01'
        AND '2012-09-10'
GROUP BY CAST(Submitted AS DATE), xyz_cnt._cnt
ORDER BY CAST(Submitted AS DATE)


  1. Hur man ansluter till Heroku postgres databas från en lokal anslutning i php

  2. Hur kan jag lösa inkompatibla med sql_mode=only_full_group_by i laravel vältalig?

  3. Är kopiering av katalogen /var/lib/mysql ett bra alternativ till mysqldump?

  4. Hitta trasiga objekt i SQL Server