Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL - get data based on two columns

I want to create the following output from below image:

sample

The input data is from a view (Select * from test). The goal is to get all data where the progress column contains the text tbd and the counter number is 1.

Can this be solved with case when statement?

Since sqlfiddle is not working here's the schema:

CREATE TABLE test
(
    [ID] [int] NOT NULL,
    [Counter] [int] NOT NULL,
    [Name] nvarchar(200) NULL,
    [Progress] nvarchar(200) NOT NULL
)

INSERT INTO test
VALUES (1, 1, 'userA', 'tbd'),
       (1, 2, 'userB', 'done'),
       (1, 3, 'userC', 'tbd'),
       (2, 1, 'userB', 'done'),
       (2, 5, 'userA', 'tbd'),
       (3, 1, 'userD', 'tbd'),
       (3, 2, 'userA', 'done'),
       (3, 7, 'userC', 'tbd'),
       (3, 11, 'userB', 'tbd')

I couldn't get it to work.

Hope you can help me out.

Thanks alot.

like image 435
ratanmalko Avatar asked Mar 09 '16 10:03

ratanmalko


People also ask

How do I retrieve data from two columns in SQL?

To select multiple columns from a table, simply separate the column names with commas! For example, this query selects two columns, name and birthdate , from the people table: SELECT name, birthdate FROM people; Sometimes, you may want to select all columns from a table.

Can we use ORDER BY for 2 columns?

You can also ORDER BY two or more columns, which creates a nested sort . The default is still ascending, and the column that is listed first in the ORDER BY clause takes precedence. The following query and Figure 3 and the corresponding query results show nested sorts.

How do I select multiple columns based on condition in SQL?

When we have to select multiple columns along with some condition, we put a WHERE clause and write our condition inside that clause. It is not mandatory to choose the WHERE clause there can be multiple options to put conditions depending on the query asked but most conditions are satisfied with the WHERE clause.

What is ORDER BY 2 in SQL?

customers ORDER BY 1, 2; In this example, 1 means the first_name column and 2 means the last_name column. Using the ordinal positions of columns in the ORDER BY clause is considered as bad programming practice for a couple of reasons.


1 Answers

Using Exists clause you can achieve your desired result.

Query

SELECT
    *
FROM test t
WHERE EXISTS (SELECT 1 FROM test
            WHERE t.ID = ID
            AND progress = 'tbd'
            AND counter = 1)

Result

ID  Counter  Name    Progress
-----------------------------
1      1     userA   tbd
1      2     userB   done
1      3     userC   tbd
3      1     userD   tbd
3      2     userA   done
3      7     userC   tbd
3      11    userB   tbd

And another alternative solution is simple SELF JOIN like this-

Query

SELECT
    le.ID, le.Counter, le.Name, le.Progress
    FROM test le
INNER JOIN test re ON le.ID = re.ID
WHERE re.progress = 'tbd'
    AND re.counter = 1

Above query returns the same result.

like image 122
Krishnraj Rana Avatar answered Sep 24 '22 01:09

Krishnraj Rana