Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to use Select within select in the same Select

Author  Topic 

bhushan_juare
Starting Member

45 Posts

Posted - 2013-02-04 : 07:56:21
Hi All,

I Have table where it includes fields model_size, model_type, actual_qty, process_qty, order_type. Now I want to display all fields + model_size who has order_type = 'Some X' but I am unable to do that using Case statement..

All suggestion are welcome..

Thanks..

Bhushan

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-04 : 08:09:23
Is one of these what you are looking for?
-- THIS?
SELECT model_size, model_type, actual_qty, process_qty, order_type,
CASE WHEN order_type = 'Some X' THEN model_size END AS ModelSizeForSomeX
FROM TheTable;

-- Or this?
SELECT model_size, model_type, actual_qty, process_qty, order_type,
FROM TheTable
WHERE order_type = 'Some X'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-04 : 12:22:28
best thing would be to put some sample data and required output to give us an idea of what exactly you're trying to achieve


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bhushan_juare
Starting Member

45 Posts

Posted - 2013-02-05 : 00:02:17
Yes, you are right but thing is that I have to use Select in Select to get data set for 'Some X' Reason is
SELECT model_size, model_type, actual_qty, process_qty, order_type--> Gave me result set for all 'Some X', 'Some Y', 'Some Z'... Now at the same time i need result set for all 'Some X' in that select...
I can't achieve this using Case statement...
Any suggestion on that..?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-05 : 00:05:33
can you show some sample data and explain what output you want out of it? Without that we cant understand what you're asking for

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-05 : 08:04:47
The thing that I am not following is the concept of "select in select" that you are using. Using WHERE clause (as I have in my second query that I posted earlier) is one way of filtering the data to get subset. Perhaps you mean that the 'Some X' itself comes from another query? If that is the case, something like shown below may be what you need:
SELECT model_size, model_type, actual_qty, process_qty, order_type,
FROM TheTable
WHERE order_type IN (SELECT t.some_x FROM TheOtherTable t);
But, the best way to get accurate responses is what Visakh suggested - post some sample data along with the results you are looking for. This article might help in posting: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

srimami
Posting Yak Master

160 Posts

Posted - 2013-02-05 : 08:37:07
Hi Bhushan,

I think you are referring to subqueries when you say select in select; please follow James reply for your "select in select".
Go to Top of Page
   

- Advertisement -