Solved

sql query 2 queries return as one dataset

Posted on 2011-02-28
7
218 Views
Last Modified: 2012-05-11
I have the two queries attached.  How would I return them as 1 dataset?
SELECT COUNT(*) AS PrimaryImageCount
	  FROM [Store].[dbo].[Products] P
	    LEFT JOIN [Store].[dbo].[Images] I ON p.Product_Image = I.Image_ID  
	     WHERE Product_ID = @Product_ID 
	     
	
	  SELECT count(*) AS ThumbImageCount
	   FROM [Store].[dbo].[Products] P
	    LEFT JOIN [Store].[dbo].[Images] I ON p.Product_Thumb = I.Image_ID
		 WHERE Product_ID = @Product_ID

Open in new window

0
Comment
Question by:vbnetcoder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 16

Accepted Solution

by:
santoshmotwani earned 500 total points
ID: 35003640
SELECT COUNT(*) AS PrimaryImageCount
        FROM [Store].[dbo].[Products] P
          LEFT JOIN [Store].[dbo].[Images] I ON p.Product_Image = I.Image_ID  
           WHERE Product_ID = @Product_ID
           union all
      
        SELECT count(*) AS ThumbImageCount
         FROM [Store].[dbo].[Products] P
          LEFT JOIN [Store].[dbo].[Images] I ON p.Product_Thumb = I.Image_ID
             WHERE Product_ID = @Product_ID

have you tried union ?
0
 
LVL 6

Expert Comment

by:anushahanna
ID: 35003651
Both are the same query with the same answer- if you include your product ID or name, you will get a complete and distinct set of values:

SELECT Product_ID, COUNT(*) AS ImageCounts
        FROM [Store].[dbo].[Products] P
          LEFT JOIN [Store].[dbo].[Images] I ON p.Product_Image = I.Image_ID  
           WHERE Product_ID = @Product_ID
0
 
LVL 6

Expert Comment

by:anushahanna
ID: 35003660
sorry i missed the Product_Image vs Product_Thumb


maybe, then, you want to add (Sum) them?

SELECT

SELECT COUNT(*) AS PrimaryImageCount
        FROM [Store].[dbo].[Products] P
          LEFT JOIN [Store].[dbo].[Images] I ON p.Product_Image = I.Image_ID  
           WHERE Product_ID = @Product_ID
+
     
        SELECT count(*) AS ThumbImageCount
         FROM [Store].[dbo].[Products] P
          LEFT JOIN [Store].[dbo].[Images] I ON p.Product_Thumb = I.Image_ID
             WHERE Product_ID = @Product_ID
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:vbnetcoder
ID: 35003663
union all seems to work
0
 
LVL 18

Expert Comment

by:sventhan
ID: 35003688
Try this...

select (SELECT COUNT(*) AS PrimaryImageCount
        FROM [Store].[dbo].[Products] P
          LEFT JOIN [Store].[dbo].[Images] I ON p.Product_Image = I.Image_ID  
           WHERE Product_ID = @Product_ID ),
       (
        SELECT count(*) AS ThumbImageCount
         FROM [Store].[dbo].[Products] P
          LEFT JOIN [Store].[dbo].[Images] I ON p.Product_Thumb = I.Image_ID
             WHERE Product_ID = @Product_ID)
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35004303
Not sure if your counts are correct. You have LEFT JOIN with another Images table and trying to get COUNT(*). If you do not have matching record in Images table, still that record is counted.
Is the Image_Id unique in Images table? I think you need an INNER JOIN. But I am not sure about your requirement. Please check.
0
 

Author Closing Comment

by:vbnetcoder
ID: 35036624
ended up doing this
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question