This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
Closed 8 years ago.
I have a question regarding how to merge lines with same characteristics:
I have this data:
Client | Product | Date
Hannah | TV | 1 Jan
Tom | Laptop | 3 Feb
Peter | iPod | 2 Jan
Hannah | Laptop | 5 Feb
Tom | iPod | 5 Feb
And I want to create this:
Client | Product-History|
Hanna | TV-Laptop |
Tom | Laptop-iPod |
Peter | iPod |
Anyone know if this is possible in SQL?
If you require actual SQL code to make it easier to respond let me know, it's the first time I ask a question.
Thanks!
Edit: I am using SQL Server
You can Try This query:
SELECT client,group_concat(product) as Product-History
from YOUR_TABLE group by client
select client,group_concat(product)
from tablename group by client
Related
This question already has answers here:
LIMIT 10..20 in SQL Server
(15 answers)
Closed 2 years ago.
I tried using limit order to get a result like
student_id | name | major
1 | kate | bio
2 | david | chem
3 | jake | math
instead of
student_id | name | major
1 | kate | bio
2 | david | chem
3 | jake | math
4 | ... | ...
5 | ... | ...
which means 3 results instead of 5 but i got a syntax error and couldn't find solutions. any suggestions?
SQL Server uses SELECT TOP or OFFSET/FETCH to limit rows. So, your query should look like:
select top (3) t.*
from t
order by student_id;
Note that the ORDER BY is important if you care about which rows you want. SQL tables represent unordered sets. You need an ORDER BY if you care about the ordering.
I'm not sure whether this is possible with some of the new BigQuery scripting capabilities, UDFs, array/string functions (or anything else!), however I simply can't figure it out.
I'm trying to write the SQL for a view in BigQuery which dynamically defines columns based on query results, similar to a pivot table in a spreadsheet/BI tool (or melt in pandas). I can do this externally in Python or hard-code it using case statements, but I'm sure that a SQL solution to this would be incredibly useful to a huge number of people.
Essentially I'm trying to write a query which would transform a table like this:
year | name | number
-----------------------
1963 | Michael | 9246
1961 | Michael | 9055
1958 | Michael | 9203
1957 | Michael | 9116
1953 | Robert | 9061
1952 | Robert | 9205
1951 | Robert | 9054
1948 | Robert | 9015
1947 | Robert | 10025
1947 | John | 9634
1946 | Robert | 9295
----------------------
SQL to generate initial example table:
SELECT year, name, number
FROM `bigquery-public-data.usa_names.usa_1910_2013`
WHERE number > 9000
ORDER BY year DESC
Into a table with the following structure:
year | John | Michael | Robert
---------------------------------
1946 | | 9,295 |
1947 | 9,634 | | 10,025
1948 | | 9,015 |
...
This then needs to be connected to downstream tools, without requiring maintenance when the data changes. I know that this is not always a great idea and that tidy form data is more universally useful, but there are still some scenarios where this behaviour is desirable.
I have seen a few solutions on here, but they all seem to involve string generation and then manually pasting the query... I can do this via the BigQuery API but am desperate to find a dynamic solution using nothing but SQL so I don't have to maintain an external function.
Thanks in advance for any pointers!
I have a table with multiple duplicate values. What I'd like to do is use some sort of SELECT query that will filter the duplicates by name and yet keep all the appropriate values from the "expertise" column and show them next to each other in one row separated by a coma.
In other words I want to turn a table that looks like this:
names | organization | education_years | expertise
---------+--------------+-----------------+------------------------------------------------------------------------------------------------------------------------------------------
John | University | 2016 – 2020 | Programming
John | University | 2016 – 2020 | machine Learning
John | University | 2016 – 2020 | AI
John | University | 2016 – 2020 | Robotics
John | University | 2016 – 2020 | Symbolic Reasoning
Gabriel | Company | 2015 – Present | models/networks
Gabriel | Company | 2015 – Present | AI
Gabriel | Company | 2015 – Present | Speech/language
Gabriel | Company | 2015 – Present | machine learning
Gabriel | Company | 2015 – Present | Programming
Nicolas | Company | Present | machine learning, factorization, learning theory, supervised learning
Into This:
names | organization | education_years | expertise
---------+--------------+-----------------+------------------------------------------------------------------------------------------------------------------------------------------
John | University | 2016 – 2020 | Programming, machine Learning, AI, Robotics, Symbolic Reasoning
Gabriel | Company | 2015 – Present | models/networks, AI, Speech/language, machine learning, Programming
Nicolas | Company | Present | machine learning, factorization, learning theory, supervised learning
I'm using PostgreSQL as my database.
Can anyone help? Haven't done queries for so long, and couldn't find a similar question that addresses this.
Use aggregation:
select name, organization, education_years,
string_agg(expertise, ', ')
from t
group by name, organization, education_years;
The above uses string_agg() because the result looks like a string. I would advise you to use arrays instead:
select name, organization, education_years,
array_agg(expertise)
from t
group by name, organization, education_years;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Concatenate many rows into a single text string?
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
I'm working with MS Sql Server 2008,
I have the following table
----------------
Uid | Alias |
--------------- |
1 | Pierre |
1 | Patrick |
1 | Jean |
2 | Alice |
2 | Diana |
and I want to display it in this manner:
------------------------|
Uid | Alias |
------------------------|
1 | Pierre Patrick Jean|
2 | Alice Diana |
Any idea will be appreciate.
Please try:
select b.Uid,
(select a.Alias +' ' from TableName a WHERE a.Uid=b.Uid group by a.Alias FOR XML PATH(''))as Names
from TableName b
group by b.Uid
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Oracle: Combine multiple results in a subquery into a single comma-separated value
Hi there,
this is my problem...
I have a table:
+------+------+------+
| CODE | NAME | TYPE |
+------+------+------+
| 1 | AAA | x |
+------+------+------+
| 2 | BBB | x |
+------+------+------+
| 3 | CCC | y |
+------+------+------+
| 4 | DDD | y |
+------+------+------+
I wanna make a view in ORACLE .... I wanna that the result is:
+---------+------+
| NAME | TYPE |
+---------+------+
| AAA;BBB | x |
+---------+------+
| CCC;DDD | y |
+---------+------+
Can I grouping AAA and BBB because they have same TYPE in a VIEW that in a NAME will be "AAA;BBB" ... so grouping various names divided with ;
Can anyone help me?
Regards,
Tommaso
Tim Hall has a page that covers the various string aggregation techniques available in Oracle depending on the Oracle version, what packages are installed in the database, and whether you can create new procedures to support this or whether you want it done in pure SQL.
If you are using 11.2, the simplest option would be to use the built-in LISTAGG analytic funciton
SELECT listagg(name, ';') within group (order by code), type
FROM your_table
GROUP BY type
If you are using an earlier version, my preference would be to use the custom aggregate function (Tim's string_agg).