Sqlserver
 sql >> डेटाबेस >  >> RDS >> Sqlserver

सप्ताहांत डेटा को कार्यदिवसों से अलग तरीके से रोल अप करें

यह करेगा:

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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. स्ट्रिंग तुलना varchar चर के साथ विफल हो जाती है

  2. SQL सर्वर तालिका में JSON फ़ाइल कैसे आयात करें

  3. SQL रखरखाव क्लीनअप कार्य कार्य कर रहा है लेकिन हटा नहीं रहा है

  4. अस्थायी तालिका से फ़ील्ड नाम कैसे प्राप्त करें (SQL Server 2008)

  5. SQL सर्वर में Msg 8116 "तर्क डेटा प्रकार दिनांक सबस्ट्रिंग फ़ंक्शन के तर्क 1 के लिए अमान्य है" को ठीक करें