Solved

sql query 2 queries return as one dataset

Posted on 2011-02-28
7
213 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now