Mysql pivot table (concat) misuse (processing time) - mysql

I want to pivot the example below, I'm using:
SELECT Person, GROUP_CONCAT( Var ) , GROUP_CONCAT( Val )
FROM table
GROUP BY Person
This works fine, but... it takes about 20 seconds per line and my table has +/- 2.500.000 records :-P
(BTW; the table below is an example, not the actual one)
id person Var Val
-------------------------------
1 Bob Height 185
2 Bob Weight 74
3 Bob Age 40
4 Hank Height 193
5 Hank Weight 90
6 Hank Age 45
7 Bert Height 180
8 Bert Weight 85
9 Bert Age 43
PS:
Besides an answer (what would make you awesome) I also would like to know what is 'wrong' with this example (makes you even more awesomer)

There's nothing wrong with it, except that I'd write it like this
SELECT Person, GROUP_CONCAT(CONCAT(Var, ': ', Val))
FROM table
GROUP BY Person
because when you split it up in two columns, you won't know which Val belongs to which Var.
Apart from that, do you have indexes defined on those columns? If not, play around to see which index works best, separate indexes for each column or compound indexes on person, var, val.

Related

SQL QUERY to show records of names that are recorded several times and are unique towards each other

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

Create new column storing average of rows from another column in MySQL

I am trying to create a new column that has stores the 'Average weight of the field'. For example, the answer for RaceID = 123 would be 54.5. The RaceID's are not organised from smaller to largest and are displayed randomly like the example below.
RaceID
Weight
No. Starters
123
56
2
124
58
2
123
53
2
125
60
2
125
51
2
124
62
2
Try below query, It will display current table data along with average column :
select t.*,
avg(Weight) over(partition by raceID order by raceID ) avg_raceID
from table t;
SELECT RaceID, AVG(Weight) AS val_1
FROM dataset_name
GROUP BY RaceID;
By using above code we can get the average value of weights for every unique RaceID. Check the below image for better understanding.
https://i.stack.imgur.com/kMA68.png
Let me know if there are any modifications or error.

Sorting course_numbers like sorting using natsort

I have these sample course_numbers
cmsc 11
cmsc 2
cmsc 56
cmsc 21
cmsc 128
I use this query
SELECT * FROM subject ORDER BY LENGTH(`course_number`)
to natural sort the result
and it worked, but when i add these course_numbers
it 1
it 256
it 20
they kinda mess up.
What query should i use to order them like this
cmsc 2
cmsc 11
cmsc 21
cmsc 56
cmsc 128
it 1
it 11
it 20
it 100
it 256
I've searched and saw 'case' on their select statements but I do not know how to use them
You should consider splitting up the both parts of your course number, since "CMSC"/"IT" is one Part (even with variable length), and the real number is another part. If you store the number in a number column (int), you could easily correct them.
so it would be
SELECT concat(course_type, " ", course_subnumber) as course_number, ...
from subject
order by course_type, course_subnumber
As long as you have a bit luck, you could try with you structure the following:
SELECT * from subject
order by left(course_number, 2), length(course_number), course_number
then you get only in trouble if different course_types start with the same two letters.

MS Access 2007 Rows to columns in recordset

I have a table which is like a questionnaire type ..
My original table contains 450 columns and 212 rows.
Slno is the person's id who answer the questionaire .
SlNo Q1a Q1b Q2a Q2b Q2c Q2d Q2e Q2f .... Q37c <450 columns>
1 1
2 1 1
3 1
4 1 1
5 1
I have to do analysis for this data , eg Number of persons who is male (Q1a) and who owns a boat (Q2b) i.e ( select * from Questionnaire where Q1a=1 and Q2b=1 ).. etc .. many more combinations are there ..
I have designed in MS access all the design worked perfectly except for a major problem ( Number of table columns is restricted to 255 ).
To be able to enter this into access table i have inserted in as 450 rows and 212 columns (now am able to enter this into access db). Now while fetching the records i want the record set to transpose the results into the form that i wanted so that i do not have to change my algorithm or logic .... How to achieve this with the minimum changes ? This is my first time working with Access Database
You might be able to use a crosstab query to generate what you are expecting. You could also build a transpose function.
Either way, I think you'll stil run into the 255 column limit and MS Access is using temporary table, etc.
However, I think you'll have far less work and better results if you change the structure of your table.
I assume that this like a fill-in-the-bubble questionnaire, and it's mostly multiple choice. In which case instead of recording the result, I would record the answer for the question
SlNo Q1 Q2
1 B
2 B
3 A
4 A C
5 A
Then you have far fewer columns to work with. And you query for where Q1='A' instead of Q1a=1.
The alternative is break the table up into sections (personal, career, etc.) and then do a join, and only show the column you need (so as not to exceed that 255 column limit).
An way to do this that handles more questions is have a table for the person, a table for the question, and a table for the response
Person
SlNo PostalCode
1 90210
2 H0H 0H0
3
Questions
QID, QTitle, QDesc
1 Q1a Gender Male
2 Q1b Gender Female
3 Q2a Boat
4 Q2b Car
Answers
SlNo QID Result
1 2 True
1 3 True
1 4 True
2 1 True
2 3 False
2 4 True
You can then find the question takers by selecting Persons from a list of Answers
select * from Person
where SlNo in (
select SlNo from Answers, Questions
where
questions.qid = answers=qid
and
qtitle = 'Q1a'
and
answers.result='True')
and SlNo in (
select SlNo from Answers, Questions
where
questions.qid = answers=qid
and
qtitle = 'Q2a'
and
answers.result='True')
I finally got the solutions
I created two table one having 225 columns and the other having 225 column
(total 450 columns)
I created a SQL statement
select count(*) from T1,T2 WHERE T1.SlNo=T2.SlNo
and added the conditions what i want
It is coming correct after this ..
The database was entered wrongly by the other staff in the beginning but just to throw away one week of work was not good , so had to stick to this design ... and the deadly is next week .. now it's working :) :)

Perform sum, average, and multiplication across all columns in a table?

I have a table that looks like this:
id test1 test2 test3 test4 ... test200
1 90 87 85 86 70
2 100 95 83 92 80
.
.
18000
I know there are standard operations to perform sums and averages on a single column and multiply the value of two columns together but is it possible to do it across all columns in a row with a given id? If its not clear, I want to do something like this across rows instead of across columns. Thanks
You might be better off redesigning the table so that it does not have 200 columns.
e.g.
Id testnum score
1 1 90
1 2 87
...
2 1 100
2 2 95
...
180000
Now you can do a query like this:
select sum(score) as totalscore
from mynewtable
where id=1
How about:
select id, sum(test1 + test2 + ...) as summation
group by id
Is the problem that you have so many columns? This solution doesn't handle many columns smoothly.
There might be some contorted SQL to work on the contents of the whole row (I doubt that), but you still have to specify the column names, otherwise you'd have the ID numbers included in the calculation.