Solved

sql query 2 queries return as one dataset

Posted on 2011-02-28
7
212 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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.
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

18 Experts available now in Live!

Get 1:1 Help Now