Linking Tables For the First Time

I have never linked tables in a query before, so I need some help please.  I wrote the following query with some help from this forum.  

SELECT CreateUserID, DocumentCategory, Count(*) As DocumentCount
FROM dbo.tblCsDocument
WHERE EnterDate BETWEEN '20140101' AND '20141231'
GROUP BY CreateUserID, DocumentCategory;

The 'DocumentCategory' is a code/abbreviation for a text description that is more meaningful than the category.  The text description has the column name 'Description' in table dbo.tblCtDocumentCategories.  I need the query to display "Description' instead of 'DocumentCategory'.  I do not know how to link the tables.  Once I get a description that I can relate to, then I can build queries from there.  Again, I appreciate the help.

August 22nd, 2015 7:54pm

Hi Kurt,

Try this if CreateUserID is present as FK in table tblCtDocumentCategories.

SELECT a.CreateUserID, b.description, Count(*) As DocumentCount
 FROM dbo.tblCsDocument a
 JOIN dbo.dbo.tblCtDocumentCategories b
 ON a.CreateUserID = b.CreateUserID
 WHERE a.EnterDate BETWEEN '20140101' AND '20141231'
 GROUP BY a.CreateUserID,  b.description;

It would be really helpful if you can paste the DDL for both the tables that you are trying to join

Thanks

Bhanu

Free Windows Admin Tool Kit Click here and download it now
August 22nd, 2015 9:54pm

May be

SELECT D.CreateUserID, D.DocumentCategory, DC.Description,  Count(*) As DocumentCount
FROM dbo.tblCsDocument D
INNER JOIN dbo.tblCtDocumentCategories DC ON D.DocumentCategory = DC.DocumentCategory
WHERE D.EnterDate BETWEEN '20140101' AND '20141231'
GROUP BY D.CreateUserID, D.DocumentCategory, DC.Description;

August 22nd, 2015 10:43pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics