Solved

Query based on two tables

Posted on 2007-03-20
4
179 Views
Last Modified: 2010-03-20
Hi experts,

I'm new to SQL so please help me. I need to write a query based on 2 tables. First table is "tblRecords" it contains the following fields:

UserID (number) , WeekID (number) , Approved (yes/no)

The second table is called "tblWeeks" and it contains:

WeekID (total of 4 weekIDs starting from 1-4), Week (description of the job in  that week)

What I need to do is to create a query that would display a recordset based on users, putting each user in one row and have 4 approved fields  for each week based on the weekID so it should look something like this:

   User    |  Approved for Week1 |  Approved for Week2 |  Approved for Week3 |  Approved for Week4 |
_____________________________________________________________________________________
  user1                   1                                     1                                  0                                        0
  user2                   1                                     0                                  1                                        0


and so on....

please help me out on this thanks!
0
Comment
Question by:techman36
  • 2
  • 2
4 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 18757345
What DBMS?

I'll answer like it's MS SQL Server ... may need to adjust a little if you're using something else.

Select R1.UserID as [user],
[Approved for Week1] = sum(Case when r1.Approved) Then 1 else 0 End,
[Approved for Week2] = sum(Case when r2.Approved) Then 1 else 0 End,
[Approved for Week3] = sum(Case when r3.Approved) Then 1 else 0 End,
[Approved for Week4] = sum(Case when r4.Approved) Then 1 else 0 End

From   tblRecords R1 Inner Join
  tblRecords R2 On R1.UserID = R2.UserID And R1.WeekID = 1 and R2.WeekID = 2 Inner Join
  tblRecords R3 On R2.UserID = R3.UserID And R3.WeekID = 3 Inner Join
  tblRecords R4 On R3.UserID = R4.UserID And R4.WeekID = 4

Group by R1.user
0
 

Author Comment

by:techman36
ID: 18757570
Can you please adjust it for access? I'm new at this.
0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 100 total points
ID: 18758038
Should have guessed it was Access based on the data types.

Just tested this w/ Access 2003.


Select R1.UserID as [user],
  sum(abs(cint(R1.Approved))) as [Approved for Week1],
  sum(abs(cint(R2.Approved))) as [Approved for Week2],
  sum(abs(cint(R3.Approved))) as [Approved for Week3],
  sum(abs(cint(R4.Approved))) as [Approved for Week4]

From   (((tblRecords AS  R1 Inner Join
  tblRecords R2 On R1.UserID = R2.UserID)  Inner Join
  tblRecords R3 On R2.UserID = R3.UserID)  Inner Join
  tblRecords R4 On R3.UserID = R4.UserID)
Where  R1.WeekID = 1 and R2.WeekID = 2 And R3.WeekID = 3 And R4.WeekID = 4
Group by R1.userID
0
 

Author Comment

by:techman36
ID: 18763022
Thanks!
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

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

18 Experts available now in Live!

Get 1:1 Help Now