0

I've hit a little snag with one of my queries. I'm throwing together a simple chart to plot a number of reports being submitted by day of week.

My query to start was :

  SELECT Weekday(incidentdate) AS dayOfWeek
, Count(*) AS NumberOfIncidents
   FROM Incident
    GROUP BY Weekday(incidentdate);

This works fine and returns what I want, something like

   1      200
   2     323
   3     32
   4     322
   5     272
   6     282
   7     190

The problem is, I want the number returned by the weekday function to read the corresponding day of week, like case when 1 then 'sunday' and so forth. Since Access doesn;t have the SQL server equivalent that returns it as the word for the weekday, I have to work around.

Problem is, it's not coming out the way I want. So I wrote it using iif since I can't use CASE. The problem is, since each iif statement is treated like a column selection (the way I'm writing it), my data comes out unusable, like this

  SELECT 
 iif(weekday(incidentdate) =1,'Sunday'),
 iif(weekday(incidentdate) =2,'Monday')
 'so forth
, Count(*) AS NumberOfIncidents
  FROM tblIncident
   GROUP BY Weekday(incidentdate);



  Expr1000  Expr1001    count   

  Sunday              20
           Monday      106
                      120
                      186
                      182
                      164
                       24

Of course, I want my weekdays to be in the same column as the original query. Halp pls

Scotch
  • 3,186
  • 11
  • 35
  • 50

4 Answers4

4

Use the WeekdayName() function.

SELECT
    WeekdayName(Weekday(incidentdate)) AS dayOfWeek,
    Count(*) AS NumberOfIncidents
FROM Incident
GROUP BY WeekdayName(Weekday(incidentdate));
Andy G
  • 19,232
  • 5
  • 47
  • 69
HansUp
  • 95,961
  • 11
  • 77
  • 135
2

As BWS Suggested, Switch was what I wanted. Here's what I ended up writing

SELECT 
 switch(
 Weekday(incidentdate) = 1, 'Sunday',
 Weekday(incidentdate) = 2,'Monday',
 Weekday(incidentdate) = 3,'Tuesday',
 Weekday(incidentdate) = 4,'Wednesday',
 Weekday(incidentdate) = 5,'Thursday',
 Weekday(incidentdate) = 6,'Friday',
 Weekday(incidentdate) = 7,'Saturday'
 ) as DayOfWeek
 , Count(*) AS NumberOfIncidents
FROM tblIncident
GROUP BY Weekday(incidentdate);

Posting this here so there's actual code for future readers

Edit: WeekdayName(weekday(yourdate)) as HansUp said it probably a little easier :)

HansUp
  • 95,961
  • 11
  • 77
  • 135
Scotch
  • 3,186
  • 11
  • 35
  • 50
1

check this previous post:

What is the equivalent of Select Case in Access SQL?

Community
  • 1
  • 1
BWS
  • 3,786
  • 18
  • 25
0

Why not just create a 7 row table with day number & day name then just join to it?

LoztInSpace
  • 5,584
  • 1
  • 15
  • 27