I have a temporary table making a list of names which are ordered by a different column, e.g.
#table:
John, 1
Mary, 3
Mary, 5
Mary, 7
John, 8
Kyle, 9
Brad, 10
when I call a simple select * from #table, that's what I get, but when I call a select distinct name from #table I get this:
Kyle
John
Mary
Brad
Why is it not using in-place ordering? Is this a sql quirk I don't know about? I would expect (and want) it to be:
John
Mary
Kyle
Brad
EDIT: Additional Question: Since I 'Ordered By' on the original table, is there a functional reason why it wouldn't persist?
When using SELECT DISTINCT you can't order by a column that's not being selected. The easiest way to do what you want is:
SELECT name FROM #table GROUP BY name ORDER BY min(id);
Related
I have a table that contains data like below:
Name
ID
Dept
Joe
1001
Accounting
Joe
1001
Marketing
Mary
1003
Administration
Mary
1009
Accounting
Each row is uniquely identified with a combo of Name and ID. I want the resulting table to combine rows that have same Name and ID and put their dept's together separated by a comma in alpha order. So the result would be:
Name
ID
Dept
Joe
1001
Accounting, Marketing
Mary
1003
Administration
Mary
1009
Accounting
I am not sure how to approach this. So far I have this, which doesn't really do what I need:
SELECT Name, ID, COUNT(*)
FROM employees
GROUP BY Name, ID
I know COUNT(*) is irrelevant here, but I am not sure what to do. Any help is appreciated! By the way, I am using PostgreSQL and I am new to the language.
Apparently there is an aggregate function for string concatenation with PostgreSQL. Find documentation here. Try the following:
SELECT Name, ID, string_agg(Dept, ', ' ORDER BY Dept ASC) AS Departments
FROM employees
GROUP BY Name, ID
I want to select from a table where a name appears twice.
For example I have a table like this,
ID Name
---- ------
1 Jane John
2 Kevin Smith
3 Jane John
What I want is for the output to show where Jane John appear twice so it should look something like this:
ID Name
---- ------
1 Jane John
3 Jane John
I tried looking around on stackoverflow but couldn't find an exact and easy answer.
I'm using oracle SQL Developer.
You ask for a record that appears twice. If a row appears three times it won't show unless you modify the having clause as commented.
SELECT id
,NAME
FROM tablen
WHERE NAME IN (
SELECT NAME
FROM TableN n
GROUP BY (NAME)
HAVING counT(NAME) = 2 --Use >1 instead of =2 for more than one record
)
EDIT
I'll add a new solution in regard to your last comment.
As you can only ask for one field in IN() I'll use a special character or string making sure it does not belongs to valid values in any field.
Look at this: http://sqlfiddle.com/#!6/2af55/3
SELECT id
,NAME
,name2
FROM tablen
WHERE concat(NAME,'=',name2) IN (
SELECT concat(NAME,'=',name2)
FROM TableN n
GROUP BY concat(NAME,'=',name2)
HAVING count(concat(NAME,'=',name2)) = 2
)
Note I wrote this thinking in SQL Server, not sure if concat function works as well in Oracle or look for an alternative.
what is the equivalent sql of this table? i want to get only the names of the persons from all_user table who are not in your_friend table
Angelina Jolie
Brad Pitt
Peter Parker
Clark Kent
table name: all_user
(users)
Angelina Jolie
Brad Pitt
Peter Parker
Mary Jane
Clark Kent
Lois Lane
table name: your_friend
(friend)
Lois Lane
Marj Jane
select distinct * from all_user where not in (select * from your_friend where all_user.users = your_friend.friend)
This is my answer and i am getting an error near IN syntax.
You haven't specified a RDBMS. If your engine supports it the EXCEPT operation is what you are looking for. It evaluates the output of two query expressions and returns the difference between the results. The result set contains all rows returned from the first query expression except those rows that are also returned from the second query expression.
SELECT DISTINCT <Columns_To_Be_Included>
FROM all_user
EXCEPT
SELECT DISTINCT <Columns_To_Be_Included>
FROM your_friend
But be careful that this works at the record level. So, you have to only specify the columns that you want to include in the comparison.
Try this
select distinct * from all_users where users not in (select distinct friend from your_friend)
How do I write a query that outputs the row number as a column? This is DB2 SQL on an iSeries.
eg if I have
table Beatles:
John
Paul
George
Ringo
and I want to write a statement, without writing a procedure or view if possible, that gives me
1 John
2 Paul
3 George
4 Ringo
SELECT ROW_NUMBER() OVER (ORDER BY beatle_name ASC) AS ROWID, * FROM beatles
Check out the row_number() function; you should be able to do this in DB2 via:
SELECT row_number(), first_name FROM beatles
I'm almost certain this is not part of the SQL standard though, so it is not likely to be portable should that ever be an issue.
SELECT ROW_NUMBER() OVER(ORDER BY BEATLE_NAME) ROWNUM,BEATLE_NAME FROM BEATLES;
I'm trying to reorder/group a set of results using SQL. I have a few fields (which for the example have been renamed to something a bit less specific), and each logical group of records has a field which remains constant - the address field. There are also fields which are present for each address, these are the same for every address.
id forename surname address
1 John These Address1
2 Lucy Values Address1
3 Jenny Are Address1
4 John All Address2
5 Lucy Totally Address2
6 Jenny Different Address2
7 Steve And Address2
8 Richard Blah Address2
address John Lucy Jenny Steve Richard
Address1 These Values Are (null) (null)
Address2 All Totally Different And Blah
For example: John,Lucy,Jenny,Steve and Richard are the only possible names at each address. I know this because it's stored in another location.
Can I select values from the actual records in the left hand image, and return them as a result set like the one on the right? I'm using MySQL if that makes a difference.
Assuming that the column headings "john", "lucy" etc are fixed, you can group by the address field and use if() functions combined with aggregate operators to get your results:
select max(if(forename='john',surname,null)) as john,
max(if(forename='lucy',surname,null)) as lucy,
max(if(forename='jenny',surname,null)) as jenny,
max(if(forename='steve',surname,null)) as steve,
max(if(forename='richard',surname,null)) as richard,
address
from tablename
group by address;
It is a bit brittle though.
There is also the group_concat function that can be used (within limits) to do something similar, but it will be ordered row-wise rather than column-wise as you appear to require.
eg.
select address, group_concat( concat( forename, surname ) ) tenants
from tablename
group by address;
I'm not certain, but I think what you're trying to do is GROUP BY.
SELECT Address,Name FROM Table GROUP BY Name
if you want to select more columns, make sure they're included in the GROUP BY clause. Also, you can now do aggregate functions, like MAX() or COUNT().
I am not sure about the question, but from what I understand you can do:
SELECT concat(column1,column2,column3) as main_column, address from table;