MySQL values to be in one column just separated by a comma - mysql

So I have a query that is pulling alot of data together and I would like it to only have one row vs x amount, and it just be one but the column Product ID, which is the last one, to just be all on one line, example data:
ID FN LN MEM REP EMAIL PID
001 Test User 1001 1001 testemail#gmail.com 001
001 Test User 1001 1001 testemail#gmail.com 002
001 Test User 1001 1001 testemail#gmail.com 003
001 Test User 1001 1001 testemail#gmail.com 004
001 Test User 1001 1001 testemail#gmail.com 005
001 Test User 1001 1001 testemail#gmail.com 006
001 Test User 1001 1001 testemail#gmail.com 007
But would like the output to be:
001 Test User 1001 1001 testemail#gmail.com 001,002,003,004,005,006,007
My SQL knowledge is not super strong so im kinda lost, any help would be awesome. I tried GROUP BY but some of the data as different values in the fourth column so it wont always work.

You're looking for GROUP_CONCAT along with GROUP BY.
SELECT ID, LN, EMAIL, GROUP_CONCAT(PID) as products
FROM tableName
GROUP BY ID
Or something like that. GROUP_CONCAT(productID) will combine them into one row and GROUP BY tells it how to combine the rows together.
If you remove the GROUP BY, you will get one row with all the results found. If you add it, it tells it how to combine the rows, which field to match.

GROUP_CONCAT is probably what you're looking for. So you can group and concatenate the Product ID and group it by a common attribute of your data records.

Try GROUP_CONCAT()
It's the same thing as Stuff For XML in TSQL

Related

Perform action on selected columns depending on their name

I've got a huge table, containing three "selection"-columns and many "data"-columns.
ID Thing1 Thing2 Thing3 avgData1 avgData2 highestEtc
---- -------- -------- -------- ---------- ---------- ------------
1 1 2 2 321 654 999
2 2 1 1 123 456 11
3 2 1 1 987 789 77
4 2 1 1 765 567 11
In my queries, I'm now selecting all entries with "Thing1" = x, "Thing2" = y, "Thing3" = z (Those three columns are selection-criteria.)
The purpose of getting those lines is to perform an action on each of the following data-columns: If it starts with "avg", I want to calculate an average of the specific column on all selected entries. On another prefix I want to count which number appears the most.
Is there a way of letting the MySQL Database do all this for me? I need a SQL-Statement that calculates the averages of the columns automatically, and performs other actions too.
For example, let's say I'd select the criteria Thing1=2, Thing2=1 and Thing3=1. Is there a way of writing the statement so that it returns only ONE entry, with the calculated things?
Result
----------------- ----------------- ----
(123+987+765)/3 (456+789+567)/3 11
I heard that this should be possible, and that it is a bad method of NOT letting the database perform those actions directly. Unfortunately, I have no idea how to do it.
Try this:-
SELECT ID, AVG(avgData1) AS RESULT1, AVG(avgData2) AS RESULT2, highestEtc
FROM YOUR_TAB
WHERE Thing1 = 2
AND Thing2 = 1
AND Thing3 = 1
GROUP BY ID
HAVING COUNT(highestEtc) > 1;
Hope this helps you.

Compute the total number of accepted papers that are authored by at least two authors

It is a conference management SQL database
The task is to compute the total number of accepted papers that are authored by at least two authors.
I am just stuck with the sum up function after group by the paper ID
paperparticipant
RegNum PPNum
0001 001
0002 0
0003 0
0004 002
0005 0
0006 001
0007 0
0008 003
0009 003
0010 003
The above is the sample data and the table
What I want to output is just the number of 2
I can just code
Select Count(AuID) from paperauthorid group by PPNum having Count(AuID)>= 2
that it output something like
Count(AuID)
3
3
I just want to output that there are two papers (that have number of more than 2 authors)
You could just wrap the existing query in another aggregate query:
SELECT COUNT(*)
FROM (SELECT COUNT(AuID)
FROM paperauthorid
GROUP BY PPNum
HAVING COUNT(AuID)>= 2) t
Try the following code:
SELECT COUNT(1)
FROM paperauthorid
GROUP BY ppnum
HAVING COUNT(auid)>= 2
LIMIT 1;

SQL Database with undefined columns

I'm working on a project where I'm dumping data into a mysql database about different people, however, each person can have many entries so I'm not sure how to have a lot of columns.
e.g
Name id
jack 234 01241990 13241990 03451993 10945
james 222 01131998 14242001 03414235 10945435 3456363 3465758
jill 1234 01131998 14242001 03414235 10945435 3456363 3465758 4253156316 6427247 583583
As you can see there can be many entries for each person, not in 100's, but I think the max can be around 20-30ish? So how do I build a database that I can insert values into without knowing how many entries will be per person, beforehand.
I am using perl script to insert values. Any ideas will be helpful
EDIT: People are suggesting to create two tables, however, when I joint he tables, I want one row for each person.
e.g After joining my view should look like
james 222 01131998 14242001 03414235 10945435 3456363 3465758
My suggestion would be to split the data into two tables (People and Data):
People:
NAME ID
Jack 234
James 222
Jill 1234
Data:
ID PeopleID Data
1 234 01241990
2 234 13241990
.
.
99 1234 6427247
100 1234 583583
You can then use joins to get the data for each person
SELECT p.Name,
p.ID,
d.Data
FROM People p
JOIN Data d
ON d.PeopleID = p.ID
ORDER BY p.Name --(assuming you want names in alphabetical order)
You should get something like the following
Name ID Data
Jack 234 01241990
Jack 234 13241990
.
.
Jill 1234 6427247
Jill 1234 583583

Counting value from various columns

I have a table
school code school_name subcode1 subcode2 subcode3
001 xyz 56 55 54
002 abc 55 56 54
003 xyz 54 55 56
Suppose 56=english or 55=hindi and I want to check that school xyz is have how many English subject or how many Hindi subject etc.
I have used count(*) function on where condition like:
select count(*)
from schooltable
where (subcode1 = '55' or subcode2 = '55' or subcode3 = 55)
and school_name='abc'
But it give only the one result I want get all records about all schools and want to insert it like this
scode schoolname sub sub sub
eng hindi history
001 abc 3 2 3
Help required.
you could do a conditional sum, something like this
select sum(case when subcode1='55' or subcode2='55' or subcode3=55 then 1 else 0 end) as hindi,
sum(case when subcode1='56' or subcode2='56' or subcode3=56 then 1 else 0 end) as english
from schooltable
--where school_name='abc' (not sure if you really require this constraint, although you have it in your question)
you could add a GROUP BY clause if required, but this would depend on your exact requirements, database structure, primary keys, etc. But the above should at least get you started.
Try this,
select school_name,count(*) as TotalSub from schooltable
group by school_name, subcode1,subcode2,subcode3
having school_name = 'abc'

Merge multiple records in the same table in access

Hello I am dealing with some unfriendly import files which import as:
timestamp position name
001 2 Jon
001 3 Bob
001 1 Ann
001 4 Mike
002 1 Joe
002 2 Sue
003 1 Jeff
004 5 James
004 1 Andy
004 2 Beth
004 4 Mitch
004 3 Chris
And would like to create a new table that displays thusly:
timestamp position1 position2 position3 position4 position5
001 Ann Jon Bob Mike
002 Joe Sue
003 Jeff
004 Andy Beth Chris Mitch James
By browsing this forum the closest I have come to a solution is:
SELECT pos1.timestamp, pos1.name AS position1, pos2.name AS position2
FROM table1 AS pos1 INNER JOIN table1 AS pos2
ON pos1.timestamp = pos2.timestamp
WHERE (((pos1.position)=1) AND ((pos2.position)=2))
I cannot figure out how to expand this to my specs, any help is much appreciated.
Try something like this
TRANSFORM First(Table.[name]) AS FirstOfname
SELECT Table.[timestamp]
FROM [Table]
GROUP BY Table.[timestamp]
PIVOT Table.[position];
I created this using the MS Access Cross Tab Query Wizard.
Also have a look at
What is a CrossTab Query?
Some better explenation.
Click the Create tab.
Click the Query Wizard.
Select Second Option (Crosstab Query
Wizard) and hit OK.
Select the table for your input, and
hit next.
Select Timestamp and click the arrow
(single) pointing fromleft to right,
and hit next.
Select position and hit next.
name will be the remaining field,
Select First (default is count), and
hit next.
Hit finish.
It should have saved a query called Table_Crosstab (or something similar).
Right click this and select design view.
On the view button, select Sql View.
You should see something similar to
TRANSFORM First(Table.name) AS FirstOfname
SELECT Table.timestamp, First(Table.name) AS [Total Of name]
FROM [Table]
GROUP BY Table.timestamp
PIVOT Table.position;
From the second line remove
, First(Table.name) AS [Total Of name]
so that you end up with
TRANSFORM First(Table.name) AS FirstOfname
SELECT Table.timestamp
FROM [Table]
GROUP BY Table.timestamp
PIVOT Table.position;
And that should be it. Save and you are ready.
Your new table is a bad design. What happens when there is a sixth and a seventh person in the incoming data? It will also be difficult to work with when you need to pull out all the data by a specific person as you will then need to query five columns.