cancel
Showing results for 
Search instead for 
Did you mean: 

Help with a SQL query

Ed_Prunier
Confirmed Champ
Confirmed Champ

I am very new to SQL and trying to learn my way around the tables.  I'd like to retrieve a list of all document types from select document groups and show show which scan queue they belong to.  The problem I'm having with my query is that no row is returned if the document type is not assigned to a scan queue.  I'd like to see a blank entry.  

SELECT dt.itemtypenum AS "Document ID", dt.itemtypename AS "Document Type", itg.itemtypegroupname
AS "Document Type Group", sq.queuename AS "Scan Queue"
FROM hsi.doctype dt
INNER JOIN hsi.itemtypegroup itg ON dt.itemtypegroupnum = itg.itemtypegroupnum
INNER JOIN hsi.scanqueuexit si ON dt.itemtypenum = si.itemtypenum
INNER JOIN hsi.scanqueue sq ON sq.queuenum = si.queuenum
WHERE dt.itemtypenum > 100
AND dt.itemtypegroupnum IN (102,104,111,112)
ORDER BY "Document Type"

1 ACCEPTED ANSWER

Jeffrey_Edmunds
Confirmed Champ
Confirmed Champ

Hi Edward,

I think the problem is in the type of JOIN statement you're using. Try changing the INNER JOINs into LEFT JOINS like this:

SELECT dt.itemtypenum AS "Document ID", dt.itemtypename AS "Document Type", itg.itemtypegroupname
AS "Document Type Group", sq.queuename AS "Scan Queue"
FROM hsi.doctype dt
LEFT JOIN hsi.itemtypegroup itg ON dt.itemtypegroupnum = itg.itemtypegroupnum
LEFT JOIN hsi.scanqueuexit si ON dt.itemtypenum = si.itemtypenum
LEFT JOIN hsi.scanqueue sq ON sq.queuenum = si.queuenum
WHERE dt.itemtypenum > 100
AND dt.itemtypegroupnum IN (102,104,111,112)
ORDER BY "Document Type"

An INNER join is going to show only those records that exist in both tables. A LEFT join (aka LEFT OUTER JOIN) is going to show all of the records in the left table and only those in the right table that match.

Jeff

View answer in original post

6 REPLIES 6

Thank you Mike. I appreciate the info.

John_Szefcyk
Confirmed Champ
Confirmed Champ
Ed,

Here's a little more info you can pull back!

SELECT
dt.itemtypenum AS "Document ID",
RTRIM(itg.itemtypegroupname) AS "Document Type Group",
RTRIM(dt.itemtypename) AS "Document Type",
ISNULL(RTRIM(sq.queuename),'<>') AS "Scan Queue",
ISNULL(RTRIM(dg.diskgroupname),'') AS "Scan Queue DG",
ISNULL(RTRIM(pp.lastuseddrive),'') AS "Current Location"
FROM
hsi.doctype dt WITH(NOLOCK)
JOIN hsi.itemtypegroup itg WITH(NOLOCK) ON dt.itemtypegroupnum = itg.itemtypegroupnum
LEFT JOIN hsi.scanqueuexit si WITH(NOLOCK) ON dt.itemtypenum = si.itemtypenum
LEFT JOIN hsi.scanqueue sq WITH(NOLOCK) ON sq.queuenum = si.queuenum
LEFT JOIN hsi.diskgroup dg WITH(NOLOCK) ON sq.diskgroupnum = dg.diskgroupnum
LEFT JOIN hsi.physicalplatter pp WITH(NOLOCK) ON sq.diskgroupnum = pp.diskgroupnum AND dg.lastlogicalplatter = pp.logicalplatternum
WHERE
dt.itemtypenum > 100
AND dt.itemtypegroupnum IN (102,104,111,112)
ORDER BY
2,3,4