Kod Kütüphanem ama elime ne gelirse burada dursun diye…

Group by Day – Örnekleri ile

Grouping By Day Of month:
In order to group the data by day you can use sql server in built DAY() funtion.

Grouping By Day Of week:
For this you can use DATEPART(weekday,[date column])

Grouping By week:
For this you can use DATEPART(week,[date column])

Grouping By DAY of Year:
For this you can use DATEPART(dayofyear,[date column])

Örnek:

CREATE TABLE #Sales
(
    Name VARCHAR (100),
    SalesDateTime DATETIME     
)
 
 
GO
INSERT INTO #Sales
SELECT 'Product1',
       '2012-04-01 00:00:00.000'
UNION ALL
SELECT 'Product2',
       '2012-04-02 00:00:00.000'
UNION ALL
SELECT 'Product3',
       '2012-04-02 00:00:00.000'
UNION ALL
SELECT 'Product4',
       '2012-04-03 00:00:00.000'
UNION ALL
SELECT 'Product5',
       '2012-04-03 00:00:00.000'
UNION ALL
SELECT 'Product1',
       '2012-04-30 00:00:00.000'
UNION ALL
SELECT 'Product1',
       '2012-04-30 00:00:00.000'
UNION ALL
SELECT 'Product1',
       '2012-05-02 00:00:00.000'
UNION ALL
SELECT 'Product5',
       '2012-05-02 00:00:00.000'
UNION ALL
SELECT 'Product5',
       '2012-05-02 00:00:00.000'
 
 
GO
--GROUP BY DAY of Month
SELECT   count(*) AS SalesCount, DAY(SalesDateTime) Day
FROM #Sales
GROUP BY DAY(SalesDateTime)
 
--GROUP BY DAY of Week
SELECT   count(*) AS SalesCount, DATEPART(weekday,SalesDateTime) Day
FROM #Sales
GROUP BY DATEPART(weekday,SalesDateTime)
 
--GROUP BY Week
SELECT   count(*) AS SalesCount, DATEPART(week,SalesDateTime) Day
FROM #Sales
GROUP BY DATEPART(week,SalesDateTime)
 
--GROUP BY DAY of Year
SELECT   count(*) AS SalesCount, DATEPART(dayofyear,SalesDateTime) Day
FROM #Sales
GROUP BY DATEPART(dayofyear,SalesDateTime)
 
GO 
DROP TABLE #Sales

 

Kaynak

Related articles

SELECT yapısı içinde Önceki ve Sonraki Kayıt

SELECT LAG(p.FirstName) OVER (ORDER BY p.BusinessEntityID) PreviousValue, p.FirstName, LEAD(p.FirstName) OVER (ORDER BY p.BusinessEntityID) NextValue FROM Person.Person p       Kaynak: sqlauthority.com

Learn More

Dinamik Pivot Sütunlar SQL Server

DECLARE @cols AS NVARCHAR(MAX); DECLARE @query AS NVARCHAR(MAX); select @cols = STUFF((SELECT distinct ‘,’ + QUOTENAME(Name) FROM property FOR XML PATH(”), TYPE ).value(‘.’, ‘NVARCHAR(MAX)’) , 1, 1, ”); SELECT @query = ‘SELECT * FROM ( SELECT o.object_id, p.Name, o.value FROM propertyObjects AS o INNER JOIN property AS p ON o.Property_Id = p.Id ) AS t […]

Learn More

STUFF ile istenilen alana ait değerleri “,” ile bir araya getirme

SELECT T2.FieldSomething, STUFF(( SELECT ‘,’ T1.Field FROM Table1 T1 WHERE T1.IDField =T2.IDField ORDER BY T1.Field FOR XML PATH(”) ), 1, 1, ”) AS FieldName FROM Table2 T2  

Learn More

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir