I want a resultset for this table:
ID Number_of_posts Number_of_user
1 100 21
2 23 34
as
ID Number_of_posts Number_of_user Number_of_posts_AND_Number_of_user
1 100 21 178
2 23 34 178
-----------------------------------------------
123 55
Is it possible to get the sum of two colums as another column/ as output in mysql?
To get cross-tab totals (horizontal and vertical):
select id,
number_of_posts as p,
number_of_users as u,
number_of_posts+number_of_users as p_and_u
from tbl
union all
select 99999 as id,
sum(number_of_posts) as p,
sum(number_of_users) as u,
sum(number_of_posts+number_of_users) as p_and_u
from tbl
order by 1
This will give you:
id p u p_and_u
----- --- --- -------
1 100 21 121
2 23 34 57
99999 123 55 178
You're complicating your query needlessly and using more memory that you have to. Pull the records in one query, then make another query to get the aggregates.
I know it doesn't answer your question, but it's what you should be doing instead. =)
SELECT id, number_of_posts, number_of_user,
(
SELECT SUM(number_of_posts + number_of_user)
FROM mytable
)
FROM mytable
SELECT SUM(Number_of_posts), SUM(Number_of_user) FROM table;
SELECT *,
(SELECT SUM(Number_of_posts) + SUM(Number_of_user) FROM TABLE) AS total
FROM table;
(Edit: Didn't originally notice it was the total total in the last column.)
Does MySQL support ROLLUP?
SELECT id,
SUM(Number_of_posts) Number_of_posts,
SUM(Number_of_user) Number_of_user,
SUM(Number_of_posts) + SUM(Number_of_user) Number_of_posts_AND_Number_of_user
FROM table
GROUP BY ROLLUP(id)
Edit: based on a quick search, in MySQL the last line might be GROUP BY id WITH ROLLUP.
Related
My query is:
SELECT avg(result)
FROM `survey_result_full`
WHERE `comp_id`='$corow[id]'
AND rater_type='riwter'
AND survey_id='$survey'
AND rater_id in (
SELECT id
FROM raters
WHERE participate='$id'
AND `type`='$rt[id]'
)
Here I will get all the average of result:
id survey_id comp_id rater_id rater_type beh_id result
---- --------- ------- -------- ---------- ------ ------
6198 79 204 180 riwter 573 4
6576 79 204 181 riwter 573 4
6577 79 204 181 riwter 574 4
But I need to find the average of the averages for rows with identical beh_id.
If two rows have the same beh_id then I need to find the average of the result column of these two rows first then find average of all items.
If what you want is to get a result with [beh_id , average], then your query should be:
SELECT beh_id, avg(result)
FROM `survey_result_full`
WHERE `comp_id`='$corow[id]'
AND rater_type='riwter'
AND survey_id='$survey'
AND rater_id in (
SELECT id
FROM raters
WHERE participate='$id'
AND `type`='$rt[id]'
)
GROUP BY beh_id
maybe sth like
SELECT AVG(avg_results)
FROM (
SELECT srf.beh_id, AVG(srf.result) as avg_results
FROM `survey_result_full` srf, `raters` r
WHERE srf.`comp_id`= '$corow[id]'
AND srf.rater_type = 'riwter'
AND srf.survey_id = '$survey'
AND srf.rater_id = r.id
AND r.participate ='$id'
AND r.`type` = '$rt[id]'
GROUP BY srf.beh_id) AS avgs
since what you want seems to be an average of averages.
(I've also refactored a bit the query since what you want is a join on the rater table)
I have a database, db and in it a table, Table.
It looks somewhat like:
id | val
--------
1 | 45
2 | 35
3 | 23
4 | 49
5 | 67
6 | 12
7 | 0
8 | 87
9 | 46
(This is just an example data set. Actual data set is huge. And I need to work in least time possible.)
I need to find the median of the column val. Actually I need a php function to be used multiple times.
A similar question does exist: Simple way to calculate median with MySQL
I tried a few answers in this question, none of them worked for me. The accepted answer doesn't work since it used to work with an older version of SQL only.
PS: It should also work in the case of many duplicates.
just for fun i thought i try and do it all in MySQL, here's the sqlFiddle
SELECT
CASE
WHEN MOD((select count(*) as count from t),2)=1 THEN
(select val from
(select #row:=#row+1 as row,val
from t,(select #row:=0)r
order by val)t1
where t1.row = CEIL((select count(*) as count from t)/2)
)
ELSE
((select val from
(select #row:=#row+1 as row,val
from t,(select #row:=0)r
order by val)t1
where t1.row = (select count(*) as count from t)/2)+
(select val from
(select #row:=#row+1 as row,val
from t,(select #row:=0)r
order by val)t1
where t1.row = ((select count(*) as count from t)/2)+1))/2
END AS median
Just replace occurences of t with your table name, don't change t1.
Also if the table has no rows, it'll return NULL as median.
This query can be further reduced to the below (sqlFiddle)
SELECT #rowCount:=(select count(*) as count from t) AS rowcount,
(select AVG(val) from
(select #row:=#row+1 as row,val
from t,(select #row:=0)r
order by val)t1
where t1.row IN (FLOOR((#rowCount+1)/2),
CEIL((#rowCount+1)/2)
)
) as Median
It'll return 2 columns, a rowcount column and a median column. I put the rowcount column there because i didn't want to count from t multiple times like previous query.
Still very new to all of this so bear with me.
Have 3 tables
table 1: member
Mem_index, Mem_name
1 joe
2 Mark
Table 2: Course
Course_index, Course_Name
1 Math
2 Reading
Table 3 : data
Data index,Member,Course,Score
1 1 1 85
2 1 2 75
3 2 1 95
4 1 2 65
SO what I would like to do is create a table:
Do a query and gather all of the courses, find the max score for each course and attribute the member name to it.
Table result should look like:
Course, Max score,name
Math 95 Mark
Reading 75 Mark
I can do the query individually but unsure of how to loop it and then propogate the data into the table.
How about this query for SQL?
SELECT c.course_name, MAX( d.score ), m.mem_name
FROM members m
JOIN data d on m.mem_id = d.member
JOIN course c on c.course_id = d.course
GROUP BY d.course
ORDER BY d.score, m.mem_name, c.course_name
Not sure if the field names match up but you get the idea - tested this in sql with some dummy data.
Data
Index Member Course Score
1 1 1 60
1 1 1 85
Course
course_id course_name
1 Math
2 English
3 Science
Members
mem_id mem_name
1 Mark
2 James
You will get the following
Course Name Score Member
Math 85 Mark
Try this query :
SELECT c.course_Name , MAX(d.score),m.mem_name
FROM data d
JOIN course c ON d.course=c.course_index
JOIN members m ON m.mem_index = d.member
GROUP BY d.course
ORDER by MAX(d.score) DESC
I am recording a real time change of a given signal into my database table.
Then I draw line graph to visualize the change of the signal level.
I want to get (10n+1)th rows in the table to make a rough graph. 10 is also arbitrary. User may change it to another value.
Does someone know how to make this just using a MySQL Query
If no, I will go with PHP after selecting all the data.
Here my table structure is:
|id |signal1 |signal2 | signal 3 |
+----------+----------+----------+------------+
|1 |0.41452 | 1.32135 | 0.31231 |
...
If you have an auto_incrememt id column, you can select rows that are divisible by n
SELECT * FROM tableName1 WHERE MOD(id,10)=0;
// id divided by 10 with a remainder equal to 0 (exact)
or without sequential column id's
SELECT * FROM (
SELECT
#row := #row +1 AS rowNum, colName1
FROM (
SELECT #row :=0) r, tableName1
) ranked
WHERE rowNum % 10 = 1
If your table has auto increment id (intID), then you should try this code.There are only 26 records in my table.
select * from tablename where intId
in (select case (intId%10=0) when 1 then intId else 0 end as x from tablename )
Output :-
10 sdf KK201300010 123456 Regular
20 sdf KK201300020 123456 Regular
It will displaying each record in every 10 record.
Hope it will help you.
i have a database: database with a table: table and some fields:
id fname dphone count_pic dup_id
6055903 Karla 5126xxx798 1 57
6173767 Aaliyah 4082xxx534 4 39
5611411 Aaliyah 4082xxx534 15 39
5611211 Aaliyah 4082xxx534 18 39
4234798 Abby 3057xxx974 31 16
6166691 Walter 6178xxx280 1 74
3375576 Walter 6178xxx280 17 74
what i am trying to do is to select the fields that have the smallest count_pic and the ones that have the bigger count_pic and that have the same dup_id
any ideas how to do this in mysql?
thanks.
With this query you'll select the smallest and the biggest values of count_pic for every dup_id
SELECT MIN(count_pic) AS minpic,
MAX(count_pic) AS maxpic,
dup_id
FROM `table`
GROUP BY dup_id
If you also need corresponding rows, then you could use something like
SELECT *
FROM `table` t1
INNER JOIN (SELECT MIN(count_pic) AS minpic,
MAX(count_pic) AS maxpic,
dup_id
FROM `table`
GROUP BY dup_id) t2 ON t1.dup_id = t2.dup_id
AND (t1.count_pic = minpic
OR t1.count_pic = maxpic)