Selecting from table where a name appears twice - sql

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.

Related

SQL: Need help listing all the unique names and the number of times they appear in two columns

I've got 2 columns filled with names. The list is seemingly random and a person's name could show up in column A or column B (but never in both at the same instance). I want to write a query that will tell me all the names and the number of times each person is in either list. Here's an example of what I'm talking about
Col A
Col B
Mark
Cheryl
Andy
Tom
James
Mark
Tom
Ann
Cheryl
Jeff
Andy
Mark
I'm looking for a query/function that will give me the list of names and the number of times they were seen in either column.
Any help is always appreciated!
Assuming your table name is names and your columns are a,b then this should work:
select n.name, count(n.name)
from (
select a as name from names
union all
select b as name from names
) n
group by n.name;
SELECT
Name,
COUNT(1) as cnt
FROM (
SELECT ColA As Name FROM my_table UNION ALL
SELECT ColB As Name FROM my_table
) t
GROUP BY
Name
db fiddle

SQL get differences in one column by ID

It's hard for me to word what I want which is why I've had trouble researching this issue. What I want is to look at a table by id and see if another column changes:
id name
---- ------
1 Al
2 Mia
1 Al
2 Jean
In the example, I don't care about id 1 because the name always stayed as Al but I care about id 2 because there is a record with the name Mia but then, that id 2 also has a record with the name Jean. I was thinking of using group by somehow but that doesn't work. Any ideas?
Try this:
SELECT id
FROM mytable
GROUP BY id
HAVING MIN(name) <> MAX(name)
This will select all ids having at least two different values.

Node / Postgres SQL Select distinct entries then put all other entries with the same reference into one column

this question was probably asked somewhere but I can't seem to phrase it correctly in the search to find an accurate answer.
I'm doing a query on a Postgres DB, it has quite a few joins, the results are something like this:
WON | name | item
1 Joe A
1 Joe B
2 Smith A
So one row for each entry, I need to somehow get the result back as such:
WON | name | item
1 Joe A, B
2 Smith A
This can be done in the query or with NodeJS, there are hundreds to thousands of results for the query, so getting a distinct row (WON 1) then searching the DB for all entries that match it then repeating for the rest isn't feasible, so this may be better done in Node / Javascript, but I'm somewhat new to that, what would be a (somewhat) efficient way to do this?
If there IS a way to do this in the query itself then that would be my preference though.
Thanks
A sql approach:
SELECT won, name
,STRING_AGG(item, ',' ORDER BY item) AS items
FROM myTable
GROUP BY won, name
ORDER BY won, name
You can use GROUP BY and string_agg to cancat rows, somelike this:
Create table:
CREATE TABLE test
(
won int,
name character varying(255),
item character varying(255)
);
insert into test (won, name, item) values (1,'Joe', 'A'),(1, 'Joe', 'B'),(2, 'Smith', 'A')
And do this in the query:
select won, name, string_agg(item, ',') from test group by won, name order by won
See this example in sqlFiddle

How do I make a query for if value exists in row add a value to another field?

I have a database on access and I want to add a value to a column at the end of each row based on which hospital they are in. This is a separate value. For example - the hospital called "St. James Hospital" has the id of "3" in a separate field. How do I do this using a query rather than manually going through a whole database?
example here
Not the best solution, but you can do something like this:
create table new_table as
select id, case when hospital="St. James Hospital" then 3 else null
from old_table
Or, the better option would be to create a table with the columns hospital_name and hospital_id. You can then create a foreign key relationship that will create the mapping for you, and enforce data integrity. A join across the two tables will produce what you want.
Read about this here:
http://net.tutsplus.com/tutorials/databases/sql-for-beginners-part-3-database-relationships/
The answer to your question is a JOIN+UPDATE. I am fairly sure if you looked up you would find the below link.
Access DB update one table with value from another
You could do this:
update yourTable
set yourFinalColumnWhateverItsNameIs = {your desired value}
where someColumn = 3
Every row in the table that has a 3 in the someColumn column will then have that final column set to your desired value.
If this isn't what you want, please make your question clearer. Are you trying to put the name of the hospital into this table? If so, that is not a good idea and there are better ways to accomplish that.
Furthermore, if every row with a certain value (3) gets this value, you could simply add it to the other (i.e. Hospitals) table. No need to repeat it everywhere in the table that points back to the Hospitals table.
P.S. Here's an example of what I meant:
Let's say you have two tables
HOSPITALS
id
name
city
state
BIRTHS
id
hospitalid
babysname
gender
mothersname
fathername
You could get a baby's city of birth without having to include the City column in the Births table, simply by joining the tables on hospitals.id = births.hospitalid.
After examining your ACCDB file, I suggest you consider setting up the tables differently.
Table Health_Professionals:
ID First Name Second Name Position hospital_id
1 John Doe PI 2
2 Joe Smith Co-PI 1
3 Sarah Johnson Nurse 3
Table Hospitals:
hospital_id Hospital
1 Beaumont
2 St James
3 Letterkenny Hosptial
A key point is to avoid storing both the hospital ID and name in the Health_Professionals table. Store only the ID. When you need to see the name, use the hospital ID to join with the Hospitals table and get the name from there.
A useful side effect of this design is that if anyone ever misspells a hospital name, eg "Hosptial", you need correct that error in only one place. Same holds true whenever a hospital is intentionally renamed.
Based on those tables, the query below returns this result set.
ID Second Name First Name Position hospital_id Hospital
1 Doe John PI 2 St James
3 Johnson Sarah Nurse 3 Letterkenny Hosptial
2 Smith Joe Co-PI 1 Beaumont
SELECT
hp.ID,
hp.[Second Name],
hp.[First Name],
hp.Position,
hp.hospital_id,
h.Hospital
FROM
Health_Professionals AS hp
INNER JOIN Hospitals AS h
ON hp.hospital_id = h.hospital_id
ORDER BY
hp.[Second Name],
hp.[First Name];

How to overwrite values during a select query in sql?

Suppose I have a table foo with schema (name, age). So the table looks like:
NAME | AGE
John | 20
Jane | 50
Jason | 30
Now suppose I wanted to select from this table, but I wanted to change all their ages to 20 via the select query. That is, I don't want to update the original table foo, but I want to select, something like:
SELECT name, 20 as age from foo
In other words, I want to manipulate the data on the select. Of course, my real-life example is much more complicated than this but knowing the answer to this will do. Thanks!
You basically just do what you put in your question:
SELECT name, newValue as age
from yourTable
This will give you the name from your table and then the value you specify for the other column.
See SQL Fiddle with Demo