Need help with a subquery - WHERE EXISTS

Greetings,

I have a query that goes like this:


SELECT O.Officename, HF.Label, AF.StrValue, D.Dept
            FROM      Office O
                        INNER JOIN Dept D ON O.UID = D.OfficeUID
                        INNER JOIN Asset A ON D.UID = A.DeptUID
                        INNER JOIN CatHWItem I ON I.UID = A.ItemUID
                        INNER JOIN CatHWManifacturer M ON I.UIDManifacturer = M.UID
                        INNER JOIN CATHWType T ON M.TypeUID = T.UID
                        INNER JOIN AssetFields AF ON A.UID = AF.AssetUID
                        INNER JOIN HWFields HF ON AF.FieldUID = HF.UID

WHERE T.NAME = 'Workstation'
AND AND HF.Label = 'service tag'
AND HF.Label = 'barcode'

GROUP BY O.Officename, HF.Label, AF.StrValue, D.Dept
ORDER BY OfficeName ASC


My problem with this query is that I can't get back both the 'barcode' and the 'service tag' at the same time.  It's either one or the other.

How can I get both in one shot?  I figured I need a subquery and a WHERE EXISTS but I'm not sure.

Thanks
John500Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
SELECT O.Officename, HF.Label, AF.StrValue, D.Dept
            FROM      Office O
                        INNER JOIN Dept D ON O.UID = D.OfficeUID
                        INNER JOIN Asset A ON D.UID = A.DeptUID
                        INNER JOIN CatHWItem I ON I.UID = A.ItemUID
                        INNER JOIN CatHWManifacturer M ON I.UIDManifacturer = M.UID
                        INNER JOIN CATHWType T ON M.TypeUID = T.UID
                        INNER JOIN AssetFields AF ON A.UID = AF.AssetUID
                        INNER JOIN HWFields HF ON AF.FieldUID = HF.UID

WHERE T.NAME = 'Workstation'
AND HF.Label IN ('service tag', 'barcode')
GROUP BY O.Officename, HF.Label, AF.StrValue, D.Dept
ORDER BY OfficeName ASC
0
Ara-Commented:
Don't know if this is related to your problem at all, but I noticed you having two ANDs after each other. AND AND HF.Label = 'service tag'
0
Kevin CrossChief Technology OfficerCommented:
Since HF.Label cannot equal both values at the same time, you have to use an IN statement or you could do this with an OR.

SELECT O.Officename, HF.Label, AF.StrValue, D.Dept
            FROM      Office O
                        INNER JOIN Dept D ON O.UID = D.OfficeUID
                        INNER JOIN Asset A ON D.UID = A.DeptUID
                        INNER JOIN CatHWItem I ON I.UID = A.ItemUID
                        INNER JOIN CatHWManifacturer M ON I.UIDManifacturer = M.UID
                        INNER JOIN CATHWType T ON M.TypeUID = T.UID
                        INNER JOIN AssetFields AF ON A.UID = AF.AssetUID
                        INNER JOIN HWFields HF ON AF.FieldUID = HF.UID

WHERE T.NAME = 'Workstation'
AND (HF.Label = 'service tag' OR HF.Label = 'barcode')
GROUP BY O.Officename, HF.Label, AF.StrValue, D.Dept
ORDER BY OfficeName ASC
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

John500Author Commented:
Thanks for the input!

The double 'AND' was a typo.  As far as the results, I need a query that will bring back both the barcode and service tag in one row for a particular asset.

In the case of 'mwvisa1' - I get all the data but as double the rows.

What do you think?

Thanks
0
John500Author Commented:
The OR approach still results in double the rows instead of both values in one row.  What would the 'IN' statement look like?

Thanks!
0
John500Author Commented:
This could be done with a temp table no?  Do the first query, then a second and then the final from the third?

Any suggestions on that?  

Thanks
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kevin CrossChief Technology OfficerCommented:
You don't need a temp table, you could do this with just one especially if this is SQL 2005.
0
Kevin CrossChief Technology OfficerCommented:
My suggestion was based on the fact that you said it was either one or the other.  If it is one or the other, then the query should not return duplicaes (double rows) as each value should only have a match on barcode or match on service tag with what was originally said.

With new information that does change things a bit.

If you are happy with temp table that will work as you are creating a table you can use in self join which is one of the suggestions.  You could use a view with original query or a common table expression to virtualize first query and do same thing as well.

If you wanted the service tag data always in same columns as for other rows that match and null otherwise and likewise for barcode, you could do this with a UNION statement as well.

Anyway, glad you found something that worked...

Kev
0
John500Author Commented:
As far as this question/solution goes, I was only concerned with closing it out with a solution that worked.  At the time of my last post, I had the solution that would return both service tag and barcode as the original post requested.......
0
John500Author Commented:
I would split the points for a partial solution but that option doesn't exist.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.