Let's say I've got the following database table
Name | Nickname | ID
----------------------
Joe Joey 14
Joe null 14
Now I want to do a select statement that merges these two columns to one while replacing the null values. The result should look like this:
Joe, Joey, 14
Which sql statement manages this (if it's even possible)?
Simplest solution:
SQL> select * from t69
2 /
NAME NICKNAME ID
---------- ---------- ----------
Joe Joey 14
Joe 14
Michael 15
Mick 15
Mickey 15
SQL> select max(name) as name
2 , max(nickname) as nickname
3 , id
4 from t69
5 group by id
6 /
NAME NICKNAME ID
---------- ---------- ----------
Joe Joey 14
Michael Mickey 15
SQL>
If you have 11gR2 you could use the new-fangled LISTAGG() function but otherwise it is simple enough to wrap the above statement in a SELECT which concatenates the NAME and NICKNAME columns.
AFAIK,
the question is not clear.so i am making some assumptions over here.
your output has the first and 3rd columns for both the rows as same.
Only the 2nd field is different.
so u can simply write a select quest
select one.name,two.nick_name,one.id from
(select name,id from your_tb group by name,id) one,
your_tb two
where two.nickname is not NULL
and two.name=one.name
and two.id=one.id;
may be we can tune this but i am not good in tuning sql squeries,but this is the way i suppose u need.
Related
How can I sort by number first and further sort same number names by alphabet?
Example:
Score | Name
-----------
12 John
11 Paul
10 Dave
9 Adam
9 Ben
9 David
Just use the SQL syntax for ordering by multiple columns:
order by Score, Name
Select * from Table Order by Score , Name
I'm using SQL-Server 2008. How to take value from one column and pass It to another column?
As you see in sample data below there are 4 columns. Where I need to take column name (in this case UserName) and pass It to FieldName column and value from UserName column pass to Value column.
SAMPLE DATA
GroupId UserName FieldName Value
1 John Smith Foo 28
1 John Smith Bar 2
1 John Smith FooBar 11
1 John Smith Bizz 22
2 Peter Jones Foo 4
2 Peter Jones Bar 13
2 Peter Jones FooBar 27
2 Peter Jones Bizz 23
DESIRED RESULTS
GroupId FieldName Value
1 Foo 28
1 Bar 2
1 FooBar 11
1 Bizz 22
1 UserName John Smith
2 Foo 4
2 Bar 13
2 FooBar 27
2 Bizz 23
2 UserName Peter Jones
How could I achieve It? By using PIVOT? But I'm not sure how to merge pivoted data to existing column. Have you any ideas? If something unclear - ask me, I'll try provide more details.
select GroupId, FieldName, Value
from table
union
select distinct GroupId, 'username', UserName
from table
No need to PIVOT , just a simple UNION ALL will do the job :
SELECT DISTINCT t.groupID,'USERNAME',t.userName
FROM YourTable t
UNION ALL
SELECT s.groupID,s.FieldName,s.Value
FROM YourTable s
SELECT DISTINCT t.*
FROM tbl
CROSS APPLY (
VALUES
(GroupId, FieldName, Value),
(GroupId, 'UserName', UserName)
) t(GroupId, FieldName, Value)
Also check a small information about UNPIVOT and VALUES from my post
I have two tables and I need to find the person with the highest value.
TABLE1
NAME ID
--------------- ---------------
MIKE 101
MIKE 102
BETTY 103
BETTY 104
BETTY 105
TIM 106
TABLE2
ID VALUE
-------- --------------
101 12
102 10
103 20
104 20
105 10
106 5
I can write a select statement that will give a result of name and values:
SELECT name, value
FROM table1, table2
WHERE table1.id = table.id;
NAME VALUE
----- ----------
MIKE 12
MIKE 10
BETTY 20
BETTY 20
BETTY 10
TIM 5
Now I need to aggregate the values of the rows with equal names and I can not figure it out. Am I going about it the correct way?
Please try this :
select a.name,max(b.value) as value
from table1 a
inner join table2 b on a.id = b.id
group by a.name
Anyway, you tagged mysql and oracle in the question. Luckily, this sql works in both. But you have to remove one of these two tags, to make sure which dbms are you use.
Use this.. and since your column names are unique, you don't need any alias.
select name, max(value) as MaxValue
from table1
inner join table2 on table1.id = table2.id
group by name
I have the following table:
data_id new_data_id first_name last_name
1 john smith
2 john smith
3 john smith
4 jeff louis
5 jeff louis
6 jeff louis
The above table has duplicate first and last names, and the data_id is different for all of them. In order to remove these duplicates, I would need to write a SQL query to replace the highest data_id in new_data_id column. My output would look something like this:
data_id new_data_id first_name last_name
1 3 john smith
2 3 john smith
3 3 john smith
4 6 jeff louis
5 6 jeff louis
6 6 jeff louis
How would I do this?
What you're looking for is an Oracle analytic function.
The aggregate function MAX can be used to select the highest data_id from your entire resultset, but that's not exactly what you need. Instead, use its alter ego, the MAX analytic function like so:
SELECT
data_id,
MAX(data_id) OVER (PARTITION BY first_name, last_name) AS new_data_id,
first_name,
last_name
FROM employees
ORDER BY data_id
This works by "partitioning" your resultset by first_name and last_name, and then it performs the given function within that subset.
Good luck!
Here's a fiddle: http://sqlfiddle.com/#!4/48b29/4
More info can be found here:
http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions004.htm#SQLRF06174
If you need a change in place, a correlated update is probably the simplest way to write that:
UPDATE T
SET "new_data_id" =
(SELECT MAX("data_id") FROM T T2
WHERE T2."first_name" = T."first_name"
AND T2."last_name" = T."last_name")
See http://sqlfiddle.com/#!4/51a69/1
Let's say I've got the following database table
Name | Nickname | ID
----------------------
Joe Joey 14
Joe null 14
Now I want to do a select statement that merges these two columns to one while replacing the null values. The result should look like this:
Joe, Joey, 14
Which sql statement manages this (if it's even possible)?
Simplest solution:
SQL> select * from t69
2 /
NAME NICKNAME ID
---------- ---------- ----------
Joe Joey 14
Joe 14
Michael 15
Mick 15
Mickey 15
SQL> select max(name) as name
2 , max(nickname) as nickname
3 , id
4 from t69
5 group by id
6 /
NAME NICKNAME ID
---------- ---------- ----------
Joe Joey 14
Michael Mickey 15
SQL>
If you have 11gR2 you could use the new-fangled LISTAGG() function but otherwise it is simple enough to wrap the above statement in a SELECT which concatenates the NAME and NICKNAME columns.
AFAIK,
the question is not clear.so i am making some assumptions over here.
your output has the first and 3rd columns for both the rows as same.
Only the 2nd field is different.
so u can simply write a select quest
select one.name,two.nick_name,one.id from
(select name,id from your_tb group by name,id) one,
your_tb two
where two.nickname is not NULL
and two.name=one.name
and two.id=one.id;
may be we can tune this but i am not good in tuning sql squeries,but this is the way i suppose u need.