Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL group by day, with count

Tags:

I've got a log table in SQL Server that looks like this:

CREATE TABLE [dbo].[RefundProcessLog](  [LogId] [bigint] IDENTITY(1,1) NOT NULL,  [LogDate] [datetime] NOT NULL,  [LogType] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,  [RefundId] [int] NULL,  [RefundTypeId] [smallint] NULL,  [LogMessage] [varchar](1000) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,  [LoggedBy] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,  CONSTRAINT [PK_RefundProcessLog] PRIMARY KEY CLUSTERED  (  [LogId] ASC ) ON [PRIMARY] ) ON [PRIMARY]  GO 

What I want is a list of results that represents how many different refundids were processed each day, throwing out any NULLs.

What SQL would I need to write to produce these results?

like image 374
Chris McCall Avatar asked Sep 21 '09 15:09

Chris McCall


People also ask

Can you GROUP BY count in SQL?

SQL – count() with Group By clause The count() function is an aggregate function use to find the count of the rows that satisfy the fixed conditions. The count() function with the GROUP BY clause is used to count the data which were grouped on a particular attribute of the table.

Does count work with GROUP BY?

The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

How do I count the day of the week in SQL?

SQL Server has a couple of inbuilt functions to get the day of week from the given date. To get the name of the day of week, you can use DATENAME function and to get the number of the day of week, you can use DATEPART function.


1 Answers

I like this approach in (MS SQL):

SELECT    Convert(char(8), LogDate, 112),   count(distinct RefundId) FROM RefundProcessing GROUP BY Convert(char(8), LogDate, 112) 
like image 173
scottm Avatar answered Sep 27 '22 19:09

scottm