How can i select row from table according to column values of row in sql server

lalit sharma

i want to select row on condition based on column values in sql server please check below example with required result.

 WITH allData
AS (
select mlid=1,value=0,checkid=1
union all
select mlid=2,value=6,checkid=2
union all
select mlid=3,value=6,checkid=1
union all
select mlid=4,value=0,checkid=2
)
select * from allData 

Result

   Mlid  Value checked
    1    0      1
    2    6      2
    3    6      1
    4    0      2

required result -->
condition:- if checked column values is 1 and values column is 0 than display checked values values 2 rows only
either display checked column values 1 like below result

Mlid value checked   
2     6      2
3     6      1
Richard Hansell

This will work for your sample data, but would fail for pretty much anything else?

WITH allData AS (
    SELECT MLID = 1, [VALUE] = 0, CHECKID = 1
    UNION ALL
    SELECT MLID = 2, [VALUE] = 6, CHECKID = 2
    UNION ALL
    SELECT MLID = 3, [VALUE] = 6, CHECKID = 1
    UNION ALL
    SELECT MLID = 4, [VALUE] = 0, CHECKID = 2)
SELECT 
    CASE WHEN a1.CHECKID = 1 AND a1.VALUE = 0 THEN a2.MLID ELSE a1.MLID END AS MLID,
    CASE WHEN a1.CHECKID = 1 AND a1.VALUE = 0 THEN a2.[VALUE] ELSE a1.[VALUE] END AS [VALUE],
    CASE WHEN a1.CHECKID = 1 AND a1.VALUE = 0 THEN a2.CHECKID ELSE a1.CHECKID END AS CHECKID
FROM 
    allData a1
    INNER JOIN allData a2 ON a2.MLID = a1.MLID + 1 AND a2.CHECKID = 2
WHERE
    a1.CHECKID = 1;

I guess this might get you started on a better query, or even raise some questions about what you actually need, and how these rows are related?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I select a column from my table, and get very nth row based on the value in that column in SQL Server?

From Dev

Select values of a column into one row - SQL Server

From Dev

How can I SELECT a row with MAX(value) from a derived table where the values are all calculated sums?

From Dev

How can I select first inserted row in SQL Server?

From Dev

How can I select first inserted row in SQL Server?

From Dev

How can I get row's index from a table in SQL Server?

From Dev

Can I insert values to a column according to a condition? SQL Server

From Dev

How to select a specific row from the table with one column as a sum of values of other rows?

From Java

How can I get column names from a table in SQL Server?

From Dev

Separating a column as a result in a query basing from row values in SQL Server

From Dev

Separating a column as a result in a query basing from row values in SQL Server

From Dev

Select corresponding to row from the same table SQL Server

From Dev

How can I replace values in one Excel column with values concatenated from a variable number of columns in the same row?

From Dev

How can I transform the the values from a column according to the index of a vector?

From Dev

How do I select the distinct row count of a column in a data table?

From Dev

How can I find information about updating or inserting a sql table row in sql server 2008?

From Dev

How to SELECT multiple values from a row and join it as a single column value

From Dev

How do I compare values from different column and different row but same table?

From Dev

How do I compare values from different column and different row but same table?

From Dev

How to add row to a SQL Server table with uniqueidentifier type column?

From Dev

Update SQL Server Table Row by row from values of other table with joins

From Dev

How to select row from a table based on two values c#?

From Dev

How can I subtract two row's values within same column using sql query?

From Dev

How can I subtract two row's values within same column using a SQL query?

From Dev

Mysql - How can I select column where row is in array?

From Dev

How can I SELECT the first row with MAX(Column value)?

From Dev

Mysql - How can I select column where row is in array?

From Dev

How can I select a specific column from each row in a Pandas DataFrame?

From Dev

How can I use jQuery to randomly select one column from each row?

Related Related

  1. 1

    How can I select a column from my table, and get very nth row based on the value in that column in SQL Server?

  2. 2

    Select values of a column into one row - SQL Server

  3. 3

    How can I SELECT a row with MAX(value) from a derived table where the values are all calculated sums?

  4. 4

    How can I select first inserted row in SQL Server?

  5. 5

    How can I select first inserted row in SQL Server?

  6. 6

    How can I get row's index from a table in SQL Server?

  7. 7

    Can I insert values to a column according to a condition? SQL Server

  8. 8

    How to select a specific row from the table with one column as a sum of values of other rows?

  9. 9

    How can I get column names from a table in SQL Server?

  10. 10

    Separating a column as a result in a query basing from row values in SQL Server

  11. 11

    Separating a column as a result in a query basing from row values in SQL Server

  12. 12

    Select corresponding to row from the same table SQL Server

  13. 13

    How can I replace values in one Excel column with values concatenated from a variable number of columns in the same row?

  14. 14

    How can I transform the the values from a column according to the index of a vector?

  15. 15

    How do I select the distinct row count of a column in a data table?

  16. 16

    How can I find information about updating or inserting a sql table row in sql server 2008?

  17. 17

    How to SELECT multiple values from a row and join it as a single column value

  18. 18

    How do I compare values from different column and different row but same table?

  19. 19

    How do I compare values from different column and different row but same table?

  20. 20

    How to add row to a SQL Server table with uniqueidentifier type column?

  21. 21

    Update SQL Server Table Row by row from values of other table with joins

  22. 22

    How to select row from a table based on two values c#?

  23. 23

    How can I subtract two row's values within same column using sql query?

  24. 24

    How can I subtract two row's values within same column using a SQL query?

  25. 25

    Mysql - How can I select column where row is in array?

  26. 26

    How can I SELECT the first row with MAX(Column value)?

  27. 27

    Mysql - How can I select column where row is in array?

  28. 28

    How can I select a specific column from each row in a Pandas DataFrame?

  29. 29

    How can I use jQuery to randomly select one column from each row?

HotTag

Archive