Solved

sql query 2 queries return as one dataset

Posted on 2011-02-28
7
215 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
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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 40

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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