Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
abomb
Starting Member
13 Posts |
Posted - 2008-12-22 : 13:31:04
|
I am writing a query that returns information regarding whether or not an email was sent to a supplier. It should return the supplierID, subject and datesent.I have a supplier table(supplierID), an email table (emailID, subject) and a supplieremail (datesent, supplierID, emailID) table.What I want to see is for every supplier, show every email subject, and then either the datesent, or NULL if no record exists for that supplierID.SELECT s.[supplierID], [subject], [dateSent]FROM Compliance.Email e LEFT JOIN Compliance.SupplierEmail se ON se.emailID = e.emailID, Compliance.Supplier sWHERE s.supplierID = se.supplierID OR se.supplierID IS NULLORDER BY supplierID, dateSent DESC There are 4 email records in the email table, and 19 suppliers in the supplier table, so this should return 76 records, but it only returns 68.Can someone help me figure out what's wrong with this query?ThanksAaron |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
abomb
Starting Member
13 Posts |
Posted - 2008-12-22 : 14:07:54
|
this is what I would like to see:supplierID, subject, dateSent3093, Email Subject 1, 2008-12-22 08:47:40.5403093, Email Subject 2, 2008-12-22 08:47:40.5403093, Email Subject 3, 2008-12-22 08:47:40.5403093, Email Subject 4, NULL3094, Email Subject 1, 2008-12-22 08:47:40.5403094, Email Subject 2, 2008-12-22 08:47:40.5403094, Email Subject 3, NULL3094, Email Subject 4, NULL3095, Email Subject 1, 2008-12-22 08:47:40.5403095, Email Subject 2, NULL3095, Email Subject 3, 2008-12-22 08:47:40.5403095, Email Subject 4, NULLbut this is what i get:3093, Email Subject 1, 2008-12-22 08:47:40.5403093, Email Subject 2, 2008-12-22 08:47:40.5403093, Email Subject 3, 2008-12-22 08:47:40.5403093, Email Subject 4, NULL3094, Email Subject 1, 2008-12-22 08:47:40.5403094, Email Subject 2, 2008-12-22 08:47:40.5403095, Email Subject 1, 2008-12-22 08:47:40.5403095, Email Subject 2, NULLThere are 19 supplier records, 4 email records and 49 supplieremail records. Most suppliers have received 2 or 3 emails, and some have received only one. Where the NULL values are, there are no supplieremail records. I hope this helps |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
abomb
Starting Member
13 Posts |
Posted - 2008-12-22 : 14:18:04
|
yes |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
abomb
Starting Member
13 Posts |
Posted - 2008-12-22 : 14:36:39
|
suppliersupplierID 3094emailemailID, subject1, Email Subject 12, Email Subject 23, Email Subject 34, Email Subject 4SupplierEmailsupplierID, dateSent, emailID3094, 2008-12-22 08:47:40.540, 13094, 2008-12-22 08:47:40.540, 2 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
abomb
Starting Member
13 Posts |
Posted - 2008-12-22 : 14:52:15
|
they are not.That's where i would like to show NULL in datesent for those values without relative records |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
abomb
Starting Member
13 Posts |
Posted - 2008-12-22 : 14:58:19
|
thanks a lot for your help! |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-12-22 : 15:04:30
|
Hmmm, when I run your sample and your query, I get the desired results. Run this in a test environment to see:CREATE TABLE supplier (supplierID int)CREATE TABLE email (emailID int, subject varchar(20))CREATE TABLE SupplierEmail (supplierID int, dateSent datetime, emailID int)INSERT INTO supplier VALUES(3094)INSERT INTO email VALUES(1, 'Email Subject 1')INSERT INTO email VALUES(2, 'Email Subject 2')INSERT INTO email VALUES(3, 'Email Subject 3')INSERT INTO email VALUES(4, 'Email Subject 4')INSERT INTO SupplierEmail VALUES(3094, '2008-12-22 08:47:40.540', 1)INSERT INTO SupplierEmail VALUES(3094, '2008-12-22 08:47:40.540', 2)SELECT s.[supplierID], [subject], [dateSent]FROM Email e LEFT JOIN SupplierEmail se ON se.emailID = e.emailID, Supplier sWHERE s.supplierID = se.supplierID OR se.supplierID IS NULLORDER BY supplierID, dateSent DESCDROP TABLE supplier, email, SupplierEmail Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
|
|
abomb
Starting Member
13 Posts |
Posted - 2008-12-22 : 15:15:13
|
weird.i ran that and it worked fine.but when i try the same query on my actual tables, it still returns some suppliers with only 2 entries |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|
|
|