473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Make query2 contain same rows as query1?

Hi,

I've been using this newsgroup heaps recently but after vigorous
searching I can't find quite what I'm looking for:

I have a DB where there are employees who have their skills reviewed.

ie. Employee 1
Review 1:
Skill1=2
Skill2=2
Skill4=1

Review 2:
Skill1=2
Skill2=3
Skill3=2
Skill4=2

In the report I want to have a matrix come up with the latest 5 reviews
and show what the employee was ranked for each skill each time he was
reviewed

ie.

Skill Rev2 Rev1
1 2 2
2 3 2
3 2 0
4 2 1

I looked at crosstab queries but ran into the problem that the field
titles were always changing so I couldnt make my report that way. What
I have at the moment is 5 queries, the first one gets the skill ratings
from the latest review, the next one the skill ratings from the next
latest review etc. I use 5 subreports and put them along side each
other and this makes my report. The latest report is used to display
the Skill Column as well as the Rev2 column (in the example above).

My problem comes in the case where the number of skills has changed.
(ie. in the example there is Skill3 in the latest review but not in the
older one). This means that the older review ratings aren't necessarily
'lining up' to skill column as it comes out like this:
Skill Rev2 Rev1
1 2 2
2 3 2
3 2 1
4 2

Essentially I need a way of getting my older review queries to look at
the latest one, if the Skill column is not the same it needs to add it
in and make the rating 0.

Any suggestions how to do that? (or solve it some other way :Þ )

May 19 '06 #1
3 1403
Andy wrote:
Hi,

I've been using this newsgroup heaps recently but after vigorous
searching I can't find quite what I'm looking for:

I have a DB where there are employees who have their skills reviewed.

ie. Employee 1
Review 1:
Skill1=2
Skill2=2
Skill4=1

Review 2:
Skill1=2
Skill2=3
Skill3=2
Skill4=2

In the report I want to have a matrix come up with the latest 5 reviews
and show what the employee was ranked for each skill each time he was
reviewed

ie.

Skill Rev2 Rev1
1 2 2
2 3 2
3 2 0
4 2 1

I looked at crosstab queries but ran into the problem that the field
titles were always changing so I couldnt make my report that way. What
I have at the moment is 5 queries, the first one gets the skill ratings
from the latest review, the next one the skill ratings from the next
latest review etc. I use 5 subreports and put them along side each
other and this makes my report. The latest report is used to display
the Skill Column as well as the Rev2 column (in the example above).

My problem comes in the case where the number of skills has changed.
(ie. in the example there is Skill3 in the latest review but not in the
older one). This means that the older review ratings aren't necessarily
'lining up' to skill column as it comes out like this:
Skill Rev2 Rev1
1 2 2
2 3 2
3 2 1
4 2

Essentially I need a way of getting my older review queries to look at
the latest one, if the Skill column is not the same it needs to add it
in and make the rating 0.

Any suggestions how to do that? (or solve it some other way :Þ )


I would put all the reviews in one table after normalizing as follows:

tblEmployees
EID Autonumber (PK)
EFirstName
ELastName

tblSkills
SID Autonumber (PK)
SkillName

tblEmployeeSkills
ESID Autonumber (PK)
EID Long (FK)
SID Long (FK)
ReviewNumber Long
ReviewDate Date/Time
ReviewRating Long

tblEmployees
1 Joe Young
2 King Kong
3 The Hulk

tblSkills
1 Climb Buildings
2 Destroy Buildings
3 Strength

tblEmployeeSkills
1 1 1 1 1/1/06 7
2 1 2 1 1/1/06 1
3 1 3 1 1/1/06 3
4 2 1 1 1/2/06 10
5 2 2 1 1/2/06 3
6 2 3 1 1/2/06 8
7 3 1 2 3/1/06 10
8 3 2 2 3/1/06 9
9 3 3 2 3/1/06 10
10 3 1 1 1/1/06 9

Review results are added to tblEmployeeSkills. Don't reuse skill ID's.

qrySkillsCrosstab:
TRANSFORM Avg(ReviewRating) AS [The Value] SELECT
tblEmployees.EFirstName, tblEmployees.ELastName, tblSkills.SkillName
FROM (tblEmployeeSkills INNER JOIN tblSkills ON tblEmployeeSkills.SID =
tblSkills.SID) INNER JOIN tblEmployees ON tblEmployeeSkills.EID =
tblEmployees.EID GROUP BY tblEmployeeSkills.EID,
tblEmployees.EFirstName, tblEmployees.ELastName, tblSkills.SkillName
PIVOT tblEmployeeSkills.ReviewNumber;

!qrySkillsCrosstab:
EFirstName ELastName SkillName 1 2
Joe Young Climb Buildings 7 Null
Joe Young Destroy Buildings 1 Null
Joe Young Strength 3 Null
King Kong Climb Buildings 10 Null
King Kong Destroy Buildings 3 Null
King Kong Strength 8 Null
The Hulk Climb Buildings 9 10
The Hulk Destroy Buildings Null 9
The Hulk Strength Null 10

Then replace Avg(ReviewRating) with Nz(Avg(ReviewRating), 0) if you
want to get Nulls to show up as 0's. Immediately before the "GROUP BY"
-- as a warmup -- put "WHERE ReviewNumber Between 1 And 5 " to limit
the ReviewNumber range. Then try:

WHERE ReviewNumber Between (SELECT Max(ReviewNumber) FROM
tblEmployeeSkills) - 4 And (SELECT Max(ReviewNumber) FROM
tblEmployeeSkills)

That should show results for only the last five ReviewNumber's.

Finally, I changed the final part to:
PIVOT 'Rev' & tblEmployeeSkills.ReviewNumber;

so that 1 2 became Rev1 Rev2. Here's a function I use in some reports
to get arbitrary values from tables not in the Record Source into
control sources using SQL:

Public Function ReturnSQLResult(strSQL As String) As Variant
Dim MyDB As Database
Dim MyRS As Recordset

Set MyDB = CurrentDb
Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
If MyRS.RecordCount <> 0 Then
MyRS.MoveFirst
ReturnSQLResult = MyRS(0)
Else
ReturnSQLResult = Null
End If
MyRS.Close
Set MyRS = Nothing
Set MyDB = Nothing
End Function

It used the Fields collection to grab the first field asked for in the
SQL string. Maybe you can reference your five fields directly in a
second query that uses fixed aliases. I don't remember trying this
before.

Perhaps you can write some queries that will put your review tables
into a normalized table structure. One sneaky part was using
Avg(ReviewRating) when it is known that there is only one value for the
grouping as a pseudo-aggregrate. I hope this gives you a starting
point.

James A. Fortune
CD********@FortuneJames.com

May 19 '06 #2
Hi James,

Sorry it took a while to reply. Thanks for the help! Was a very
detailed response and I appreciate it. I have had a look at what you've
suggested, got the query up and running in the actual DB, however
unless im mistaken I will be running into almost the same problem I was
before with the crosstab queries, specifically that instead of them
generating a column with all the skills reviewed for that employee it
spreads them out, 1 skill review per column.

I think it may be best if i follow your advice at the end of your
response and create a query that will generate a temporary table with
the information and use that as the data source for my report.

Thanks for the help, it's helped put me on the right course,

Andrew

May 23 '06 #3
Andy wrote:
Hi James,

Sorry it took a while to reply. Thanks for the help! Was a very
detailed response and I appreciate it. I have had a look at what you've
suggested, got the query up and running in the actual DB, however
unless im mistaken I will be running into almost the same problem I was
before with the crosstab queries, specifically that instead of them
generating a column with all the skills reviewed for that employee it
spreads them out, 1 skill review per column.
Give an example of what you're trying to do. You may not have to give
up on crosstabs yet.

I think it may be best if i follow your advice at the end of your
response and create a query that will generate a temporary table with
the information and use that as the data source for my report.
My advice about the queries was not to create a temporary table. It
was to move your table data into the new structure. Don't resort to a
temporary table unless your other alternatives are worse.

Thanks for the help, it's helped put me on the right course,

Andrew


I'm happy to help,

James A. Fortune
CD********@FortuneJames.com

May 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frans Schmidt | last post by:
I want to make a new database with several tables, so I did the following: <?php CREATE DATABASE bedrijf; CREATE TABLE werknemers (voornaam varchar(15), achternaam varchar(20), leeftijd...
3
by: mark1822 | last post by:
Hello, I am working on designing a Web site using PHP and MySQL and I am currently figuring out how to best build my MySQL database. My Web site is going to use a MySQL database extensively...
2
by: Ben | last post by:
Hi, I need a search engine for my web. The service Index Server (for web) is started (win 2K) but i don't know how to make an ASP page for that. I can make a form like this: <form...
0
by: volumstein | last post by:
I have a make-table query in a FOR loop that outputs to 'table1.' 'table1' is then referenced by 'Form1.' here's the code: For j = 1 To rsTable.RecordCount SerialNumRef.Value =...
10
by: Gordon Youd | last post by:
I have a customer table and each customer has an INDATE and an OUTDATE, I am trying to create a report that shows all customers from INDATE and all cutomers from OUTDATE, where INDATEand OUTDATE...
2
by: jason.teen | last post by:
Hi, If I have a table as such: Code | Value --------------------- A | Null A | asdf A | foo B | Null
3
by: M26 | last post by:
I get this message while i'm trying to run Query1, then after I click ok, my previously saves Query1 is deleted. . . .. any ideas?
3
by: =?Utf-8?B?S2F5xLFoYW4=?= | last post by:
In my project,i added datagridview to my form , i transfered my table to datagridview and added multiple rows and when i called dataadapther.update ,,result is ok. But when i tried it for the...
3
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.