473,545 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Query Help

Thank you for taking the time to look at my question. Any help would
be much appreciated !

Database:
Microsoft SQL Server 2000

Database Structure:
1. tblEmployee
a. EmployeeID

2. tblCertificatio n1
a. VisitDate
b. CertificationSc ore1

3. tblCertificatio n2
a. VisitDate
b. CertificationSc ore2

Desired Result:
I want to list out each EmployeeID and add include the score from each
certification based on the VisitDate

EmployeeID | VisitDate | CertificationSc ore1 | CertificationSc ore2
1 | 02/02/05 | 50 | 50

If they didn't take Certification2 then leave it NULL

EmployeeID | VisitDate | CertificationSc ore1 | CertificationSc ore2
1 | 02/02/05 | 50 | <NULL>

Question:
I'm having difficulty linking the tables together and appending the
fields to the right of the EmployeeID based on their VisitDate. Any
ideas ?

Thank you so much !

cw*****@gmail.c om

Jul 23 '05 #1
9 2726

<cw*****@gmail. com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
Thank you for taking the time to look at my question. Any help would
be much appreciated !

Database:
Microsoft SQL Server 2000

Database Structure:
1. tblEmployee
a. EmployeeID

2. tblCertificatio n1
a. VisitDate
b. CertificationSc ore1

3. tblCertificatio n2
a. VisitDate
b. CertificationSc ore2

Desired Result:
I want to list out each EmployeeID and add include the score from each
certification based on the VisitDate

EmployeeID | VisitDate | CertificationSc ore1 | CertificationSc ore2
1 | 02/02/05 | 50 | 50

If they didn't take Certification2 then leave it NULL

EmployeeID | VisitDate | CertificationSc ore1 | CertificationSc ore2
1 | 02/02/05 | 50 | <NULL>

Question:
I'm having difficulty linking the tables together and appending the
fields to the right of the EmployeeID based on their VisitDate. Any
ideas ?

Thank you so much !

cw*****@gmail.c om


I am assuming that you didn't list the employee key that should be on both
certificaiton tables.

select *
from Employee e
left join certificaiton1 c1 on e.EmployeeID = c1.EmployeeID
left join certificaiton2 c2 on e.EmployeeID = c2.EmployeeID

If you don't want to see employees that don't have any certificaitons add
the following:

where c1.VisitDate is not null or c2.VisitDate is not null

Jul 23 '05 #2
Thanks for your help. But this doesn't get me where I want to be.

My database structure is the following:
Database Structure:
1. tblEmployee
a. EmployeeID

2. tblCertificatio n1
a. EmployeeID
b. VisitDate
c. CertificationSc ore1

3. tblCertificatio n2
a. EmployeeID
b. VisitDate
c. CertificationSc ore2

I did accidently leave out the EmployeeID fiield in my post above.

The solution you gave me doesn't group by EmployeeID and VisitDate.
The employee will have to take two certification test on the same day
and I want both scores to show up on the same line per that employee
and date.

Thanks again for helping me out with this ! I really do appreciate it.

cw*****@gmail.c om

Jul 23 '05 #3
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Sample data is also a good idea, along with clear
specifications.

You might also want to learn the ISO-11179 Standards for data element
names, so you don't have those silly "tbl-" prefixes, etc.
The employee will have to take two certification test on the same

day and I want both scores to show up on the same line per that
employee and date. <<

CREATE TABLE Personnel -- collective or plural table names
(emp_id CHAR(9) NOT NULL PRIMARY KEY,
..);

Based on your specs, the two tests are attributes of the test event.
Therefore, you should not split them into separate tables.

CREATE TABLE CertificationTe sts
(emp_id CHAR(9) NOT NULL
REFERENCES Personnel(emp_i d)
ON UPDATE CASCADE
ON DELETE CASCADE,
test_date DATETIME DEFAULT CURRENT_TIMESTA MP NOT NULL,
certification_1 _score INTEGER DEFAULT 0 NOT NULL,
certification_2 _score INTEGER DEFAULT 0 NOT NULL,
PRIMARY KEY (emp_id, test_date));

Jul 23 '05 #4
(cw*****@gmail. com) writes:
Thanks for your help. But this doesn't get me where I want to be.

My database structure is the following:
Database Structure:
1. tblEmployee
a. EmployeeID

2. tblCertificatio n1
a. EmployeeID
b. VisitDate
c. CertificationSc ore1

3. tblCertificatio n2
a. EmployeeID
b. VisitDate
c. CertificationSc ore2

I did accidently leave out the EmployeeID fiield in my post above.

The solution you gave me doesn't group by EmployeeID and VisitDate.
The employee will have to take two certification test on the same day
and I want both scores to show up on the same line per that employee
and date.


For this kind if questions, it's always a good idea to post:

o CREATE TABLE statements for your tables (possibly simplified)
o INSERT statements with sample data.
o The desired output given the sample.

This makes it easy for anyone who is trying your problem to easy cut
and paste into to Query Analyzer, and thus you will get a tested query
in response.

The below may address your problem:

SELECT e.EmployeeID, f.VisitDate, f.Score1, f.Score2
FROM tblEmployee e
JOIN (SELECT EmployeeID = coalesce(c1.Emp loyeeID, c2.EmployeeID),
VisitDate = coalesce(c1.Vis itDate, c2.VisitDate),
Score1 = c1.Certificacti onScore1,
Score2 = c1.Certificacti onScore2
FROM tblCertificatio n1 c1
FULL JOIN tblCertificatio n2 c2
ON c1.EmployeeID = c2.EmployeeID
AND c1.VisitDate = c2.VisitDate) AS f
ON e.EmployeeID = f.EmployeeID

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #5
Thank you so much ! You guys rock. I got it figured out.

Jul 23 '05 #6
Erland,

One more question to go along with your previous post.

How would I add another table with another score to this query ? Such
as CertificationSc ore3 ?

Thanks,

cwwilly

Jul 23 '05 #7
On 3 Mar 2005 10:36:54 -0800, cw*****@gmail.c om wrote:
How would I add another table with another score to this query ? Such
as CertificationSc ore3 ?


Hi cwwilly,

First, you should reconsider your design. It sounds as if you should
have just one table for the certifications. Something like the below
(note that most datatypes are based on assumptions)

CREATE TABLE Certifications
(CertificationN o int NOT NULL
,EmployeeID int NOT NULL
,VisitDate smalldatetime NOT NULL
,CertificationS core smallint
,PRIMARY KEY (CertificationN o, EmployeeID, VisitDate)
,FOREIGN KEY (EmployeeID) REFERENCES Employees
,CHECK (CertificationN o BETWEEN 1 AND 3)
)
For a kludge to get the desired results in the current situation, you
can extend the query posted by Erland to something like this:

SELECT e.EmployeeID, f.VisitDate, f.Score1, f.Score2, f.Score3
FROM tblEmployee e
JOIN (SELECT EmployeeID = coalesce(c1.Emp loyeeID, c2.EmployeeID,
c3.EmployeeID),
VisitDate = coalesce(c1.Vis itDate, c2.VisitDate,
c3.VisitDate),
Score1 = c1.Certificacti onScore1,
Score2 = c2.Certificacti onScore2,
Score3 = c3.Certificacti onScore3
FROM tblCertificatio n1 c1
FULL JOIN tblCertificatio n2 c2
ON c2.EmployeeID = c1.EmployeeID
AND c2.VisitDate = c1.VisitDate
FULL JOIN tblCertificatio n3 c3
ON c3.EmployeeID = COALESCE (c1.EmployeeID, c2.EmployeeID)
AND c3.VisitDate = COALESCE (c1.VisitDate, c2.VisitDate))
AS f
ON e.EmployeeID = f.EmployeeID

The above is untested, since you didn't follow Erland's advise to post
CREATE TABLE and INSERT statements and desired output.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #8
Thanks Hugo ! It worked like a charm. I really appreciate you helping
me with this.

cwwilly

Jul 23 '05 #9
Hugo Kornelis (hugo@pe_NO_rFa ct.in_SPAM_fo) writes:
SELECT e.EmployeeID, f.VisitDate, f.Score1, f.Score2, f.Score3
FROM tblEmployee e
JOIN (SELECT EmployeeID = coalesce(c1.Emp loyeeID, c2.EmployeeID,
c3.EmployeeID),
VisitDate = coalesce(c1.Vis itDate, c2.VisitDate,
c3.VisitDate),
Score1 = c1.Certificacti onScore1,
Score2 = c2.Certificacti onScore2,
Score3 = c3.Certificacti onScore3
FROM tblCertificatio n1 c1
FULL JOIN tblCertificatio n2 c2
ON c2.EmployeeID = c1.EmployeeID
AND c2.VisitDate = c1.VisitDate
FULL JOIN tblCertificatio n3 c3
ON c3.EmployeeID = COALESCE (c1.EmployeeID, c2.EmployeeID)
AND c3.VisitDate = COALESCE (c1.VisitDate, c2.VisitDate))
AS f
ON e.EmployeeID = f.EmployeeID

The above is untested, since you didn't follow Erland's advise to post
CREATE TABLE and INSERT statements and desired output.


Now, triple full joins are definitely the simplest one to write. I
had to do this in real life a year back. I arrived at:

SELECT coaleace(ab.col 1, c.col1), ...
FROM (SELECT col1 = coleasce(a.col1 , b.col1), ...
FROM a
FULL JOIN b ON a.col1 = b.col1) AS ab
FULL JOIN c ON c.col1 = ab.col1

The obvious problem with the above is that the coalesce is very
likely to prohibit the use of indexes. Still I have a feeling
that I overdid it, but I have not found anything better.


--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #10

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

Similar topics

9
3114
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be...
7
5947
by: Simon Bailey | last post by:
How do you created a query in VB? I have a button on a form that signifies a certain computer in a computer suite. On clicking on this button i would like to create a query searching for all details invovling that computer, for the user to then view. Any ideas on some code? Many thanks for any help.
4
2058
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating premiums are dependant on the country in which the client is in. Therefore, we have a Country table, with its list of rates, a client table and...
4
2849
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table query. I'm having trouble doing it. Help! Here is my code so far: Sub OldRegionQuery()
36
2997
by: Liam.M | last post by:
hey guys, I have one last problem to fix, and then my database is essentially done...I would therefore very much appreciate any assistance anyone would be able to provide me with. Currently I have set up a Query to show only records that meet a certain criteria...therefore excluding all of the records that do not meet this criteria (just for...
5
7353
by: elitecodex | last post by:
Hey everyone. I have this query select * from `TableName` where `SomeIDField` 0 I can open a mysql command prompt and execute this command with no issues. However, Im trying to issue the same command inside of mysql_real_query and I keep on getting this error back. "You have an error in your SQL syntax; check the manual that...
10
6206
by: aaronrm | last post by:
I have a real simple cross-tab query that I am trying to sum on as the action but I am getting the "data type mismatch criteria expression" error. About three queries up the food chain from this cross-tab query I am using a simple query with no grouping where I am filtering some data out in the criteria line. I have been out of access for...
11
16283
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction. We have been using oracle client 10.1.0.2 with it's odbc for a while without problem. The problem arose when we decided to reconnect all the tables...
4
2031
by: Doris | last post by:
It does not look like my message is posting....if this is a 2nd or 3rd message, please forgive me as I really don't know how this site works. I want to apologize ahead of time for being a novice with MS Access and VBA. I desperately need help with 2 queries that I am trying to put together. I want to thank anyone that can help me out with...
6
4378
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one field between them. The join field in both tables are indexed and I'm selecting 1 field from each table to lookup. The Access query is taking more...
0
7487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7934
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7446
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.