| Author |
Topic |
|
rc1138
Starting Member
35 Posts |
Posted - 2010-05-07 : 00:36:56
|
| Okay I just received advice on how to count the number of items within the months of the yearSELECT COUNT (CASE WHEN MONTH(IncDate) = 1 THEN 1 END) AS JAN, COUNT (CASE WHEN MONTH(IncDate) = 2 THEN 1 END) AS FEBFROM irIncident AS I INNER JOIN pdEmployee AS E ON I.EmpKey = E.EmpKeyWHERE E.Department = '1000'AND I.HlthCare = 'Y'Which displaysJan Feb5 0Now I want to find out the number of occurences per day within the yearIt works when i use this syntaxSELECT COUNT(CASE WHEN DAY(IncDate) = 1 THEN 1 END) AS Mon, COUNT(CASE WHEN DAY(IncDate) = 2 THEN 1 END) AS TueFROM irIncident AS I INNER JOIN pdEmployee AS E ON I.EmpKey = E.EmpKeyWHERE (E.Department = '1000') AND (I.HlthCare = 'Y') But I guess just because Day(IncDate) = 1 does not always mean it will be monday is there a specific syntax filter to indicate days of the week (e.g. Day(IncDate) = Monday)Thanks again! |
|
|
malpashaa
Constraint Violating Yak Guru
264 Posts |
Posted - 2010-05-07 : 01:11:03
|
You should use DATEPART function with WEEKDAY as the datepart, but beware that this function is dependent on environment settings like SET LANGUAGE and SET DATEFIRST. So to get consistent results regardless of those settings, then you need to do something like this:DECLARE @date DATETIME = '20100101';SELECT CASE WHEN DATEPART(WEEKDAY, @date) + @@DATEFIRST > 7 THEN DATEPART(WEEKDAY, @date) + @@DATEFIRST - 7 ELSE DATEPART(WEEKDAY, @date) + @@DATEFIRST END AS date_part Notice that this will always return 1 for sunday, 2 for monday, ... and 7 for saturday so you can use the date_part value in your calculation safely. |
 |
|
|
rc1138
Starting Member
35 Posts |
Posted - 2010-05-07 : 01:59:41
|
| Hi and thanks for the reply By occurrences I meantThis particular output Sunday Monday Tuesday Wednesday Thursday Friday2 0 0 0 0 0From a certain date range* Please disregard the values underneath the days those are only placeholders |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-05-07 : 03:05:25
|
| If you are using SQL 2005 then you can use Pivot operator..If you can give sample table structure and sample data then we can frame the select statement for you.Regards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
rc1138
Starting Member
35 Posts |
Posted - 2010-05-07 : 04:16:27
|
| The 2 tables are CREATE TABLE [dbo].[irIncident]( [IncNo] [int] NULL, [IncDate] [datetime] NULL, [IncType] [int] NULL, [EmpKey] [int] NULL, [ClaimNo] [char](15) NULL, [LostTime] [char](1) NULL, [HlthCare] [char](1) NULL, IncNo IncDate IncType EmpKey HlthCare1 1/1/1996 1 1 Y2 1/1/1996 1 2 Y3 1/1/1996 1 3 Y andCREATE TABLE [dbo].[pdEmployee]( [EmpKey] [int] NULL, [LastName] [char](25) NULL, [FirstName] [char](20) NULL, [EmpId] [char](9) NULL, [Department] [char](10) NULL, EmpKey LastName FirstName EmpId Department1 X Y 101 10002 M N 102 10003 G H 103 1000 ) ON [PRIMARY]Cant seem to wrap my mind around the days partSELECT COUNT(CASE WHEN DATEPART(IncDate) = 1 THEN 1 END) AS Sun, COUNT(CASE WHEN DATEPART(IncDate) = 2 THEN 1 END) AS MonFROM irIncident AS I INNER JOIN pdEmployee AS E ON I.EmpKey = E.EmpKeyWHERE (E.Department = '1000') AND (I.HlthCare = 'Y') it outputs the data but incorrect data |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-05-07 : 05:15:00
|
| Try this:CREATE TABLE [dbo].[irIncident]([IncNo] [int] NULL,[IncDate] [datetime] NULL,[IncType] [int] NULL,[EmpKey] [int] NULL,[ClaimNo] [char](15) NULL,[LostTime] [char](1) NULL,[HlthCare] [char](1) NULL)CREATE TABLE [dbo].[pdEmployee]([EmpKey] [int] NULL,[LastName] [char](25) NULL,[FirstName] [char](20) NULL,[EmpId] [char](9) NULL,[Department] [char](10) NULL)Insert into [irIncident](IncNo, IncDate, IncType, EmpKey, HlthCare) Values (1,'1996-01-01',1,1,'Y')Insert into [irIncident](IncNo, IncDate, IncType, EmpKey ,HlthCare) Values (2,'1996-01-01',1,2,'Y')Insert into [irIncident](IncNo ,IncDate, IncType ,EmpKey ,HlthCare) Values (3,'1996-01-01',1,3,'Y')Insert into [irIncident](IncNo ,IncDate, IncType ,EmpKey ,HlthCare) Values (4,'1996-02-01',1,2,'Y')Insert into [irIncident](IncNo ,IncDate, IncType ,EmpKey ,HlthCare) Values (5,'1996-02-03',1,3,'Y')Insert into [pdEmployee] (EmpKey,LastName,FirstName,EmpId,Department) Values(1,'X','Y',101,1000)Insert into [pdEmployee] (EmpKey,LastName,FirstName,EmpId,Department) Values(2,'M','N',102,1000)Insert into [pdEmployee] (EmpKey,LastName,FirstName,EmpId,Department) Values(3,'G','H',103,1000)select * from (SELECT EMPID,EMPID as E1,DATENAME(DW,[IncDate]) AS WEEKDYFROM irIncident AS I INNER JOINpdEmployee AS E ON I.EmpKey = E.EmpKeyWHERE (E.Department = '1000') AND (I.HlthCare = 'Y') ) as SourceTablePIVOT(COUNT(E1)FOR WEEKDY IN ([SUNDAY],[MONDAY],[TUESDAY],[WEDNESDAY],[THURSDAY],[FRIDAY],[SATURDAY])) AS PivotTableIf this is not your requirement then be more clear on your expected output.Regards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
malpashaa
Constraint Violating Yak Guru
264 Posts |
Posted - 2010-05-07 : 06:38:49
|
quote: SELECT EMPID,EMPID as E1,DATENAME(DW,[IncDate]) AS WEEKDY
I have mentioned that DATEPART function is dependent on environment settings like SET LANGUAGE and SET DATEFIRST, and DATENAME is not different.quote: By occurrences I meantThis particular output Sunday Monday Tuesday Wednesday Thursday Friday
Your original query is good, and all you need is to replace the DAY(IncDate) with CASE expression I provided in my reply.Anyway this is your query after modification:SELECT COUNT(CASE WHEN date_part = 1 THEN 1 END) AS sunday_count, COUNT(CASE WHEN date_part = 2 THEN 1 END) AS monday_count, COUNT(CASE WHEN date_part = 3 THEN 1 END) AS tuesday_count, COUNT(CASE WHEN date_part = 4 THEN 1 END) AS wednesday_count, COUNT(CASE WHEN date_part = 5 THEN 1 END) AS thursday_count, COUNT(CASE WHEN date_part = 6 THEN 1 END) AS friday_count, COUNT(CASE WHEN date_part = 7 THEN 1 END) AS saturday_count FROM (SELECT CASE WHEN DATEPART(WEEKDAY, IncDate) + @@DATEFIRST > 7 THEN DATEPART(WEEKDAY, IncDate) + @@DATEFIRST - 7 ELSE DATEPART(WEEKDAY, IncDate) + @@DATEFIRST END AS date_part FROM irIncident AS I INNER JOIN pdEmployee AS E ON I.EmpKey = E.EmpKey WHERE E.Department = '1000' AND I.HlthCare = 'Y') AS T |
 |
|
|
|
|
|