Solved

Count, GroupBy and where SQL Statement, without GroupBy

Posted on 2007-03-28
7
317 Views
Last Modified: 2012-06-21
Hi all

I am having trouble in writing a SQL statement.  Actually don't know if it is possible.

What I am trying to do is count a number of records but group by 1 record but also have a date from which to start counting.

So, Table 1 = UserDetails
StaffID (Numeric,Unique Identifyer), DateStarted (DateTime)

Table 2 = DataWritten
RecordNumber (Numeric, Inique Identifyer), DateEntered (DateTime)

Table 3 = RecordsRead
RecorNumber (Numeric, Unique Identifyer), StaffID (Numeric), DateRead (DateTime)

So now, I want to
Count = RecordsRead.RecordNumber
GroupBy = RecordsRead.StaffID
Where RecordsRead.DateRead = UserDetails.DateStarted (But not GroupBy DateRead).

As stated earlier, not sure, if it is possible, but would make live a lot easier.

I will be displaying this on a web page ASP Classic if this helps.

Baker
0
Comment
Question by:Bakersville
  • 3
  • 3
7 Comments
 
LVL 28

Expert Comment

by:imran_fast
Comment Utility
you mean this


select count(RecordsRead.RecordNumber),RecordsRead.StaffID
from RecordsRead
inner join UserDetails
on RecordsRead.DateRead = UserDetails.DateStarted
group by RecordsRead.StaffID
0
 
LVL 28

Expert Comment

by:imran_fast
Comment Utility
or this
select count(RecordsRead.RecordNumber),RecordsRead.StaffID
from RecordsRead
inner join UserDetails
on RecordsRead.DateRead = UserDetails.DateStarted
and UserDetails.StaffID=RecordsRead.StaffID
group by RecordsRead.StaffID
0
 
LVL 11

Assisted Solution

by:MacNuttin
MacNuttin earned 100 total points
Comment Utility
is RecorNumber a typo?
What does this give you?

select DateStarted,dw.RecordNumber,DateEntered,count(rr.RecordNumber) as NumCount,rr.StaffID,DateRead FROM RecordsRead rr left join UserDetails ud  on ud.StaffID = rr.StaffID join DataWritten dw on dw.RecordNumber = rr.recordnumber
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:Bakersville
Comment Utility
Thank you both for your quick replies.  Yes RecorNumber is a typo.

I will give it a go and let you know how i get on.

Thank again
Baker
0
 
LVL 1

Author Comment

by:Bakersville
Comment Utility
imran_fast, thanks again for this but on the Second option, it does count, but it counts the Records Read,RecordNumber not DataWritten.RecordNumber where > DataWritten.DateStarted.
I've tried to change it but my head hurts now.

MacNuttin, i've tried your but it comes up with a GroupBy clause error and once that fixed a where clause error.

Baker
0
 
LVL 28

Accepted Solution

by:
imran_fast earned 400 total points
Comment Utility
You mean this

select count(DataWritten.RecordNumber),UserDetails.StaffID
from DataWritten
inner join UserDetails
on DataWritten.DateStarted < UserDetails.DateStarted
group by UserDetails.StaffID
0
 
LVL 1

Author Comment

by:Bakersville
Comment Utility
Thank you imran_fast, works perfectly.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now