Let's say I have a Text document. There are two columns. Column 1 contains a list of names while column contains a list of value relating to those names. The problem is column 1 may have same names repeating on different rows. This is not an error though.
For ex:
Frank Burton 13
Joe Donnigan 22
John Smith 45
Cooper White 53
Joe Donnigan 19
What are the ways to organize my data in a way that I would have column 1 with unique data names and column 2 with the values summed together relating column 1? What can I do if I have these data in excel?
For ex:
Frank Burton 13
Joe Donnigan 41
John Smith 45
Cooper White 53
Thanks a bunch!
In mySQL you could write a query similar to...
Select col1, Sum(col2) FROM TableName group by col1
In Excel you could use a pivot table to group the information together
Insert Pivot table, select range enter values as in image below.
Related
For example, let us consider this table:
In this image consists of rows of 8 where names like Mike,Glenn,Daryl,Shane and Patricia is included with their respective ages
Id
Name
Age
1
Mike
25
2
Glenn
19
3
Glenn
19
4
Daryl
56
5
Shane
30
6
Shane
30
7
Patricia
16
Now I want to insert the type of query that will show the names without repetitions like This, not like This
EDIT: I entered the data from first picture. The request is to list the names without duplicates, as shown in the second and third picture but I will not convert them to text.
DISTINCT specifies removal of duplicate rows from the result set.
SELECT DISTINCT Name
FROM tablename
see: use DISTINCT in SELECT
You can use GROUP BY to achieve it.
SELECT * FROM your_table
GROUP BY your_table.name
ORDER BY id
With the data you gave, the result from this query will be:
id
name
age
1
Mike
25
2
Glenn
19
4
Deryl
56
5
Shane
30
7
Patricia
16
I know this is probably so odd to ask. But lets say I have 3 tables:
Table 1
ID
Name
1
Adam
2
David
3
Conor
Table 2
ID
Name
1
Adam
2
Derek
3
Niall
Table 3
ID
Name
1
Adam
2
David
3
John
Is there any way I can write a query to get the unique names across all 3 tables. So it would return "Adam, David, Conor, Derek, Niall, John"
Order doesn't matter
If it helps, all name values are related to a names table
yes , one way is to union them
select name from table1
union
select name from table2
union
select name from table3
union automatically removes duplicate cases
I have a column which contains all the values in lists.
Column A|Column B
AAA |1 2 45 67 89
BBB |16 25 36 45 89 63
CCC |52 63 98 41 22 66
Here in the above table, column B contains string values which are actually lists.
I need to ignore the first two and the last two values in Column B.
I tried using split function where i can ignore first two values. But ignoring last two values is the challenge as I have different sized lists.
The code which I used is:
select distinct column_A,column_B,split(column_B,'\\s')[2] AS ign_first_val,
split(column_B,'\\s')[-2] as ign_last_val
FROM Xyz
Is there any easy way to ignore first two and last two values in a list using HQL?
You should be able to use regexp_extract:
select regexp_extract(column_B, '^\\s*(\\d+\\s+){2}(.*?)(\\s+\\d+){2}\\s*$', 2)
The first part of the regex skips the first two values, and the last part skips the last two values, leaving just the middle part to be extracted into group 2 which is what is returned by the expression.
Here's a demo of the regex working on regex101.com
Forgive me, I'm still learning but am in need of some assistance. Some of what I’ve done is an amalgam of previous questions but I can’t find quite what I’m looking for.
I have a table with 30 columns of data, let’s call it table1. Every two columns are actually a set of the same type of data that have meaning together and singly. For example col1 with col2, is say a set of names.
Like this:
1 Jim Jeff
2 Mike Ben
3 Mike Mike
4 Peter Jeff
5 Jeff Jim
6 etc etc
The remaining 28 columns aren't important at this point. I want to return a single list of the unique names in col1 AND col2 along with their counts in total from both columns. Here’s what I have and it seems to work to a point but there is a problem with the return.
SELECT col1, COUNT(*)
FROM table1
GROUP BY col1
UNION
SELECT col2, COUNT(*)
FROM table2
GROUP BY col2
The problem is, when col1 has a name in it that is also in col2 it will return two counts. For example, if I had 6 different names, a total of 100 times, 50 in each column I might see something like this returned with the above query.
Jim 4
Jim 13
Jeff 8
Jeff 19
Mike 11
Mike 34
Ben 4
Brian 2
Peter 5
Obviously, Jim, Jeff and Mike appear in both columns and Ben, Brian and Peter appear in only one (It seems to me that it doesn’t matter which one).
What I need returned is:
Jim 17
Jeff 27
Mike 45
Ben 4
Brian 2
Peter 5
I tried putting a subquery in GROUP BY to force what is returned by a union without the count (forgive me, I don’t know much SQL, I'm just making assumptions by what little I understand of the language), meaning:
GROUP BY (SELECT col1 FROM table1 UNION SELECT col2 FROM table2)
but I guess I’m making silly assumptions. Any suggestions?
You can use a CTE to get the list of all names, then do a count based on that.
;WITH Names AS
(
SELECT col1 AS [Name]
FROM table1
UNION ALL
SELECT col2 AS [Name]
FROM table2
)
SELECT [Name], COUNT(*)
FROM Names
GROUP BY [Name]
Sorry for not many clear title.
1st table "place":
ID PLACE AREA_1 AREA_2 AREA_3
1 Mago 23 45 67
2 Gelato 23 45 12
And so on
2nd table "area_1"
ID ID_AREA_1 AREA_1
1 23 Lazio
3rd table "area_2"
ID ID_AREA_2 AREA_2
1 45 Roma
4th table "area_3"
ID ID_AREA_3 AREA_3
1 12 Roma
2 67 Velletri
Of course with 3 INNER JOIN I can extract in this mode
ID PLACE AREA_1_NAME AREA_2_NAME AREA_3_NAME
1 Mago Lazio Roma Velletri
But I need to re-factory the database, losing 2nd, 3rd and 4th table and I need to obtain a new, unique table directly with
ID PLACE AREA_1_NAME AREA_2_NAME AREA_3_NAME
1 Mago Lazio Roma Velletri
1) I need absolutely to mantain inalterate the ID (id of place) of the 1st table (are used in others tables that I will mantain)
2) I don't need anymore the 2nd, 3rd and 4th tables
Could you help me to create the SQL statement for MySQL to prattically move the result of the JOINS to a new table?
Thank you very, very much!