I have a question related to select query. here I am explaining down below.
I have a table with the following data
**Column1(Primary Key) Column2 Column3**
------ --------- --------------
1 C
2 C
3 Null
4 H
5 L
6 H
my problem is I have to replace the value of Column3 with the corresponding value of Column1 for every occurrence of data "C", "H" and "L". Please provide me query related to this problem. How can I solve this using query or stored procedure. Please elaborate the same.
I need final select query result as follows
**Column1(Primary Key) Column2 Column3**
------ --------- --------------
1 C 1
2 C 2
3 Null
4 H 4
5 L 5
6 H 6
Select Column1, Column2,
CASE
WHEN Column2 is not null THEN Column1
ELSE null --or whatever value you want for blank
END as Column3
From TableName t1
Alternatively you could it it like this:
Select Column1, Column2,
CASE
WHEN Column2 = 'L' or Column2 = 'H' or Column2 = 'C' THEN Column1
ELSE null --or whatever value you want for blank
END as Column3
From TableName t1
Do you mean UPDATE?
UPDATE tbl SET Column3 = Column1 WHERE Column2 IN ('C', 'H', 'L', 'F')
Or for all;
UPDATE tbl SET Column3 = Column1 WHERE Column2 IS NOT NULL
UPDATE mytable
SET Column3 = Column1
WHERE Column2 in ('C', 'H', 'L')
like this?
select Column1, Column2, Column1 as Column3 from table
Or I'm not sure what you are actually asking for ..?
Ah, sorry. Dind't read all of the question there ...
Most SQL dialects have a way to do conditional subselects, but they are different in differend dialects. So which SQL are you using?
Related
I need to separate two columns into two rows in sql
I have this:
Column1 Column2 Column3
Car 2 5
Boat 4
Truck 6
And I want this:
Column1 Column2
Car 2
Car 5
Boat 4
Truck 6
How can I do this in SQL?
This operation is unpivoting. I would recommend apply:
select t.column1, v.col
from t cross apply
(values (col1), (col2)) v(col)
where v.col is not null;
This should do it
select Column1, Column2
from tbl where Column2 is not null and Column2 <> ''
union
select Column1, Column3
from tbl where Column3 is not null and Column3 <> ''
Mine SQL Query returns a few columns but only one row. I need to transpose this one row and column's names into two columns - first with column's names and second with row values. For some reasons I can't use crosstab.
Operation that I want to perform is shown below.
Before:
Column1 | Column2 | Column3 | Column4
---------+---------+---------+--------
Value1 | Value2 | Value3 | Value4
And I want to get this:
Cols | Vals
--------+---------
Column1 | Value1
Column2 | Value2
Column3 | Value3
Column4 | Value4
Does this do what you want?
SELECT
'Column1' AS Cols
, Column1 AS Vals
FROM Table
UNION ALL
SELECT
'Column2' AS Cols
, Column2 AS Vals
FROM Table
UNION ALL
SELECT
'Column3' AS Cols
, Column3 AS Vals
FROM Table
UNION ALL
SELECT
'Column4' AS Cols
, Column4 AS Vals
FROM Table
select 'Column1' as Cols, Column1 as Vals from YourTable
union all
select 'Column2', Column2 from YourTable
union all
select 'Column3', Column3 from YourTable
union all
select 'Column4', Column4 from YourTable
Try crosstab from the tablefunc extension. http://www.postgresql.org/docs/9.0/static/tablefunc.html
It should do exactly what you want.
You can install the extension like this:
CREATE EXTENSION tablefunc;
Thank you for reviewing. I'm in desperate need of help and having trouble even being clear on the question below...
Using SQL 2008 I have a table set up with five columns.
Data effectively looks like this:
column1 column2 column3 column4 column5
T1 N1 A1 L1 S1
T1 N2 A2 L2 S4
T1 N2 A3 L2 S2
T1 N2 A1 L2 S4
T2 N6 A3 L3 S2
T2 N7 A3 L3 S4
T2 N7 A3 L4 S4
...
For records that have the same column1 value, I want to identify each unique permutation of column2 through column5.
I want to answer questions like this:
Case 1
How many records exist where
column1 is the same value and
column2 is the same value and
column3 is the same value and
column4 is the same value and
column5 is different
then
Case 2
column1 is the same value and
column2 is the same value and
column3 is the same value and
column4 is different and
column5 is the same value
then
Case 3
column1 is the same value and
column2 is the same value and
column3 is the same value and
column4 is different and
column5 is different
and so on. Thanks!
You can do this in one query, by setting up a table of the permutations you want and some case logic in the query.
Here is an example with three columns.
with t as (
select 'a' as a, 'b' as b, 'c' as c union all
select 'a', 'b', 'd' union all
select 'e', 'b', 'd'
),
perms as (
select 1 as col1, 1 as col2, 1 as col3 union all
select 1, 1, 0 union all
select 1, 0, 0 union all
select 0, 1, 1 union all
select 0, 1, 0 union all
select 0, 0, 1
)
select (case when p.col1 = 1 then a end) as col1,
(case when p.col2 = 1 then b end) as col2,
(case when p.col3 = 1 then c end) as col3,
count(*)
from t cross join
perms p
group by (case when p.col1 = 1 then a end),
(case when p.col2 = 1 then b end),
(case when p.col3 = 1 then c end)
Hmm. If you want to do it by hand, something like group by with multiple keys should do it. E.g. for case 1 you should do something like:
select column1,column2,column3, column4, count(column5) group by column1,column2,column3, column4
This should provide unique records for each unique combination of column1-column4. If you want distinct, then you probably need to do some preprocessing.
If you want a more flexible, probably a store procedure performing a group by could be a solution.
I have 3 columns as
Column1 column2 column3
GU1 1 a
GU1 2 a
GU1 3 a
GU2 4 b
GU3 5 c
GU4 6 a
GU4 7 b
I would like to filter out the column1 where the values are having multiple column3 values
In this example, I want to pull GU4 where it is having both a & b in the column3.
Use distinct in your count
select column1
from your_table
group by column1
having count(distinct column3) = 1
I want have a query on the table values
Column1 Column2 Column3
-----------------------
a b c
d e f
Result should be
Column1 a
Column2 b
Column3 c
Column1 d
Column2 e
Column3 f
Basically a key value pairs. Is it possible in Oracle?
Thanks for the help in advance
You can do it with UNION ALL, like this:
SELECT 'Column1' as Name, Column1 as Value FROM my_table
UNION ALL
SELECT 'Column2' as Name, Column2 as Value FROM my_table
UNION ALL
SELECT 'Column3' as Name, Column3 as Value FROM my_table
Here is a demo on sqlfiddle.