Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql query 2 queries return as one dataset

Posted on 2011-02-28
7
Medium Priority
?
219 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 2000 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

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.
In this article I will describe the Backup & Restore 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.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

718 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