Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If a certain date = Monday?, how to do this 3

Status
Not open for further replies.

NFLDUser

IS-IT--Management
Apr 17, 2006
47
CA
I have a table with each row having a date field. I am trying to return the total number of rows from each day of the week.

Mon Tues Wed Thurs Fri Sat Sun
44 45 3 45 23 2 34

I'd like to have something like the above to work with.

Any ideas????
 
The date is in the table as a Date/Time field as Short Date.

Hope this helps!

So basically, I want to count every row that has a date from Monday...Tuesday...Wednesday, and so on.
 
SELECT WeekDay([SomeDateField]), Count(*) FROM TableName GROUP BY WeekDay([SomeDateField])

will return 1 - 7 and a count. There may be a function that returns the name of the day, but if not, you could use an if statements to get the names. Then you can pivot these results to get the format you need.

 
Code:
Select WeekDay([DateField]) As [Week Day],
       Count(*) As [CountOfWeekDay]

From myTable

Group By WeekDay([DateField])
That will give you
[tt]
Week Day CountOFWeekDay

1 10
2 15
3 9
etc.
[/tt]
Where 1=Sunday, 2=Monday, etc.


[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
One follow-up though...

So now I have the totals for each day, but what if I wanted avg for each day of the week?
 
What do you mean by "Average"?

Normally, "average" is computed as the mean of a set of observations (e.g. Sum / number of observations). Do you perhaps mean "% of total"?

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Well, each row in my table represents an invoice, with a date field.

I am trying to figure out how many ppl purchase on Mon-Sun.

So if I have 20 invoices for Monday and the invoices are for 4 Mondays, then my average would be 5.

I guess somewhere in the SQL, I need to keep track of how many different dates there are. Then, TotalForDay / TotalDaysForEachDayOfWeek ... or something like that.

Hope I am making sense.
:)
 
Try
Code:
Select WeekDay(M.[DateField]) As [Week Day],
       Count(*) As [CountOfWeekDay],

       (Count(*) / 
       (SELECT Count(*) FROM myTable AS X
        WHERE Weekday(X.[DateField])=WeekDay(M.[DateField]) 
        GROUP BY CDate(Int(X.[DateField]) ) As [AverageValue]

From myTable M

Group By WeekDay(M.[DateField])

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
I keep getting an error. It's probably something I am overlooking here.

'Missing ], ), or Item in query expression '(Count(*) /
(SELECT Count(*) FROM Breakdown AS X
WHERE Weekday(X.[visit_date])=WeekDay(M.[visit_date])
GROUP BY CDate(Int(X.[visit_date]) ) As [AverageValue]

From Breakdown M

Group By WeekDay(M.[visit_date])'.


Breakdown is my table.
visit_date is my date field.
 
Select WeekDay(M.[DateField]) As [Week Day],
Count(*) As [CountOfWeekDay],

(Count(*) /
(SELECT Count(*) FROM myTable AS X
WHERE Weekday(X.[DateField])=WeekDay(M.[DateField])
GROUP BY CDate(Int(X.[DateField]) ) As [AverageValue])

From myTable M

Group By WeekDay(M.[DateField])
 
Sorry. You need more right parens
Code:
'(Count(*) /
       (SELECT Count(*) FROM Breakdown AS X
        WHERE Weekday(X.[visit_date])=WeekDay(M.[visit_date])
        GROUP BY CDate(Int(X.[visit_date]))[COLOR=red yellow]))[/color] As [AverageValue]

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
One more error now.

'At most, one query can be returned by this subquery.'

Thanks!
 
Guess it needs to be
Code:
(Count(*) /

(SELECT Count(*) 
 FROM 
      (SELECT DISTINCT X.[visit_date]
       FROM Breakdown AS X
       WHERE Weekday(X.[visit_date])=WeekDay(M.[visit_date])
      ) As J) As [AverageValue]

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Not sure whyk, but its still causing problems.

'Syntax error in union query.'
 
what's the exact SQL you are trying? There is no UNION in what's been suggested!!
 
(Count(*) /

(SELECT Count(*)
FROM
(SELECT DISTINCT X.[visit_date]
FROM Breakdown AS X
WHERE Weekday(X.[visit_date])=WeekDay(M.[visit_date])
) As J) As [AverageValue]

I assumed that this was the complete SQL. This is where I get the union error.

When I try the following, I get a syntax erro.

Select WeekDay(M.[DateField]) As [Week Day],
Count(*) As [CountOfWeekDay],

(Count(*) /

(SELECT Count(*)
FROM
(SELECT DISTINCT X.[visit_date]
FROM Breakdown AS X
WHERE Weekday(X.[visit_date])=WeekDay(M.[visit_date])
) As J) As [AverageValue]

From myTable M

Group By WeekDay(M.[DateField])


My apologizes for not being able to pick at it to get this right. It's been a few years since I played with SQL nd I'm just stumped here!!
 
Just another missing paren
Code:
Select WeekDay(M.[DateField]) As [Week Day],
       Count(*) As [CountOfWeekDay],

(Count(*) /

(SELECT Count(*)
 FROM
      (SELECT DISTINCT X.[visit_date]
       FROM Breakdown AS X
       WHERE Weekday(X.[visit_date])=WeekDay(M.[visit_date])
      ) As J)[b][COLOR=red])[/color][/b]  As [AverageValue]

From [COLOR=blue]Breakdown [/color]As M

Group By WeekDay(M.[DateField])
As Leslie said, there's no UNION in this statement.

I've also changed "myTable" to Breakdown since I assume that's the correct name for your table.

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top