Get the total of multiple column in sql - php

In database I have such table:
| id hotel_id | room_id | Ac_rooms | Non_ac_rooms | simple_rooms | Furnitured_room | other_rooms | added_by |
| 9 | 2 | 3 | 2 | 6 | 12 | 21 | raj |
I want to get the total numbers of room from SQL query (which is the total of room_id, Ac_rooms, Non_ac_rooms, simple_rooms, Furnitured_room, other_rooms).
What is the best way to get the total from SQL query? I need total number of rows.

Try this:
SELECT
SUM(Ac_rooms) as Ac_rooms,
SUM(Non_ac_rooms) as Non_ac_rooms,
SUM(Simple_rooms) as Simple_rooms,
SUM(Furnitured_rooms) as Furnitured_room,
SUM(Other_rooms) as Other_rooms,
SUM(Ac_rooms+Non_ac_rooms+Simple_rooms+Furnitured_room+Other_rooms) as Total_rooms,
FROM tbl_rooms
Or
SELECT
SUM(SUM(Ac_rooms)+SUM(Non_ac_rooms)+SUM(Simple_rooms)+SUM(Furnitured_room)+SUM(Other_rooms)) as Total_rooms,
FROM tbl_rooms

If I understood you correctly, you just need the sum
select (ac_rooms + Non_ac_rooms + simple_rooms + Furnitured_room + other_rooms) as total_rooms from YOUR_TABLE
Also you specified finding total number of rows, which you can get by using standart count function
select count(*) as number_of_rows from YOUR_TABLE
Or may be you are looking for the sum of types of the rooms through all the rows? In that case you will need
select sum(ac_rooms ), sum(Non_ac_rooms), sum(simple_rooms), sum(Furnitured_room), sum(other_rooms) from YOUR_TABLE
UPD: If I got you right, you need this
select sum(ac_rooms ) as ac_rooms_total,
sum(Non_ac_rooms) as non_ac_rooms_total,
sum(simple_rooms) as simple_rooms_total,
sum(Furnitured_room) as furnitured_room_total,
sum(other_rooms) as other_rooms_total,
sum(ac_rooms + Non_ac_rooms + simple_rooms + Furnitured_room + other_rooms) as TOTAL
from YOUR_TABLE

Related

Display only first of duplicate column values

How to query for erase the view below?
+-------------------+------------+
| Order_id | Weight |
| 20 | 4 |
| 21 | 5 |
| 22 | 2 |
| 22 | 2 |
+-------------------+------------+
To be like this:
+-------------------+------------+
| Order_id | Weight |
| 20 | 4 |
| 21 | 5 |
| 22 | 2 |
| 22 | |
+-------------------+------------+
When displaying results but not entered into the database.
A simple way is:
select DISTINCT order_id, weight from xyz
UNION
select order_id, null from xyz
group by order_id, weight
having count(*) > 1
Order by weight desc;
The 1st select statement will display all the unique values and 2nd one will retrieve only the repeated values.
In your required output table, it seems like you want to display all the non-repeated rows and the 1st column value of repeated rows but not 2nd column value. The above query will allow you to do that.
OK, here is how to do it:
SELECT
Order_id,
Weight,
if(#order_id = Order_id, '', Weight) as no_dup_weight,
#order_id := Order_id as dummy
FROM Table1
ORDER BY Order_id asc;
You basically need to check to see if the previous Order_id is the same as the current, and if they are, output an empty field.
Here is an SQLFiddle demonstrating the solution.
Do you actually need 2 rows for the dupes? Can't you just use the DISTINCT clause as per http://www.mysqltutorial.org/mysql-distinct.aspx
Or is it important to know what has duplicates. In which case you should look into the GROUP BY clause

MySQL temporary table

In the project I'm working I use php language and mysql database.
The reason I thought to use temporary tables was right is because I perform many calculations, and in javascript they would take me a long time.
I must show these tables at screen:.
Table 1:
Total data. | 6
Average. | 2984-99
Sum x-Media. | 5088.9
Sum (x-Media)^2. | 138092659.8396
Sum (x-Media)^4. | 1.09E+16
Maximum value. | 9812.12
Minimum value. | 18.23
Table 2:
Amount | x-Media | (x-Media)^2 | (x-Media)^4
839.12 | -2145.87 | 704122.3744 | 495788318130.694
18.23 | 18.23 | 332.3329 | 110445.15642241
9812.12 | 11957.99 | 96277698.8944 | 9.27E+15
23.93 | 5.7 | 572.6449 | 327922.18149601
863.21 | -11094.78 | 745131.5041 | 555220958402.328
6353.33 | 6347.63 | 40364802.0889 | 1.63E+15
All calculations are based on the amount column, this value. I get it from the table tbl_layout_c:
Amount decimal (18,2)
----------
839.12
18.23
9812.12
23.93
863.21
6353.33
So the query is:
1 SELECT amount
2 (SELECT COUNT(*) FROM tbl_layout_c ) AS total_data
3 (SELECT SUM(amount) FROM tbl_layout_c ) AS total_sum,
4 (SELECT total_sum / total_data AS average,
5 (SELECT amount - average ) AS x_media,
6 (SELECT SUM(x_media)) AS suma_x_media
7 FROM tbl_layout_c
The issue is at LINE 6: (SELECT SUM(x_media)) AS suma_x_media
because I get the same value as x_media column. Ex:
Amount | x-Media | Sum x-Media.
839.12 | -2145.87 | -2145.87
And I need:
Amount | x-Media | Sum x-Media.
839.12 | -2145.87 | 5088.9
I have trouble when I try to use function sum() in the temporary column (suma_x_media) of the temporary table.. It's not working, I just get the same value as x_media
Anyone have any idea what I'm missing?
Here you can see an example of the Code
try this
select *,sum(x_media) from (SELECT monto, #obtengo el campo monto
(SELECT COUNT(*) FROM tbl_layout_c ) AS total_datos, #contar los registros
(SELECT SUM(monto) FROM tbl_layout_c ) AS suma_total, #suma total
(SELECT MAX(monto) FROM tbl_layout_c ) AS valor_maximo, #valor maximo
(SELECT MIN(monto) FROM tbl_layout_c ) AS valor_minimo, #valor minimo
(SELECT suma_total / total_datos) AS promedio, #promedio
(SELECT monto - promedio) AS x_media #media artimetica
FROM tbl_layout_c)tablealias
I tried in sql Fiddle with your data and It works. your sum(x_media) is 0 with data in sql Fiddle you have if you change the data same as here in TABLE 2 then you can get expected output same mentioned in Table 1.

Column Calculator

I have the following table and want to know how to set up a column total to calculate the sum of the row. I want the total column to calculate first_bid - second_bid.
id | First Bid | Second Bid | Total
0 | 7 | 1 | (first_bid)-(second_bid)
1 | 8 | 2 |
2 | 5 | 3 |
3 | 4 | 4 |
4 | 5 | 5 |
5 | 5 | 6 |
I need to display to the user all previous bids and total on a page. The total should be in descending order also.
SELECT id, First_Bid, Second_Bid, First_Bid - Second_Bid AS Total
For the grand total, you'd have to run a second query with SUM() aggregate functions. Which is somewhat redundant, since you can just do the summation in your client as you retrieve the data. e.g.
$total = 0;
while($row = fetch_from_db($result)) {
$total += $result['Total'];
... display row data ...
}
... display total ...
You can just calculate the total when you are querying the database.
SELECT first_bid, second_bid, (first_bid - second_bid) as total FROM table ORDER BY 3 DESC
Something along these lines
Try this query:
SELECT *, (First Bid-Second Bid) as total from TABLE

mysql speed of query - selecting max value of every 3 rows

I have a table that holds price information. I need to select the max value of every three rows. EXAMPLE:
Table `daily_high`
____ _______
| ID | HIGH |
| 1 | 24.65 |
| 2 | 24.93 |
| 3 | 26.02 |
| 4 | 25.33 |
| 5 | 25.16 |
| 6 | 25.91 |
| 7 | 26.05 |
| 8 | 28.13 |
| 9 | 27.07 |
|____|_______|
Desired output to new table (ID will be auto-increment so don't assume an association exists between this ID 1 and the daily_high ID 1:
____ ___________
| ID | 3MaxHIGH |
|____|___________|
| 1 | 26.02 |
| 2 | 25.91 |
| 3 | 28.13 |
|____|___________|
I want to compare IDs 1,2, and 3 to determine the high value among them. Then once I have compared 1-3, I want to move on to 4 through 6, then 7 through 9, etc until I've done this for all values contained in the table (currently about 400,000 values). I have written code that uses
SELECT max(HIGH) FROM daily_high as dh1 JOIN (SELECT max(HIGH) FROM daily_high WHERE id >= dh1 AND id < (dh1.id + 3))
This works but is horribly slow. I've tried using the SELECT statement where I identify the column values to be pull for display, meaning between the SELECT and FROM parts of the query.
I've tried to use JOIN to join all 3 rows onto the same table for comparison but it too is horribly slow. By slow I mean just under 10 seconds to gather information for 20 rows. This means that the query has analyzed 60 rows (20 groups of 3) in 9.65879893303 seconds (I didn't make this up, I used microtime() to calculate it.
Anyone have any suggestions for faster code than what I've got?
Keep in mind that my actual table is not the same as what I've posted above, but it the concept is the same.
Thanks for any help.
If you ID it continous you can make this
SELECT floor(id/3) as range, max(HIGH) FROM daily_high GROUP BY range;
Why not to use DIV operator for grouping your aggregation:
SELECT (id-1) DIV 3 + 1 AS ID, MAX(high) AS 3MaxHIGH
FROM daily_high
GROUP BY (id-1) DIV 3
This query gives the same result.
ID 3MaxHIGH
1 26.02
2 25.91
3 28.13
I was unable to run your query, and I believe that this one is faster.
UPD: To ensure that you have valid groups for your ranges, use this query:
select id, high, (id-1) div 3 + 1 from daily_high
result:
id high (id-1) div 3 + 1
1 24.65 1
2 24.93 1
3 26.02 1
4 25.33 2
5 25.16 2
6 25.91 2
7 26.05 3
8 28.13 3
9 27.07 3
Fuller answer with an example. The following code will do what I think you want.
SELECT FLOOR((row - 1) / 3), MAX(Sub1.high)
FROM (SELECT #row := #row + 1 as row, daily_high.*
FROM daily_high, (SELECT #row := 0) r) Sub1
GROUP BY FLOOR((row - 1) / 3)
ORDER BY Sub1.ID
The below query worked for me on a test table. perhaps not the best, but the other solutions failed on my test table.
This does require the ID's to be sequential. Also be sure to put an index on High aswell for speed.
SELECT FLOOR(T1.Id/3)+1 AS Id, ROUND(GREATEST(T1.High, T2.High, T3.High),2) AS High FROM `daily_high` T1, `daily_high` T2, `daily_high` T3
WHERE T2.Id=T1.Id+1
AND T3.Id=T2.Id+1
AND MOD(T1.Id, 3)=1
logic: if(id is divisible by 3, id/3-1, id/3)
select if(mod(id,3) = 0,floor(id/3)-1,floor(id/3)) as group_by_col , max(HIGH)
FROM daily_high GROUP BY group_by_col;

How best to get someone's 'rank' from a scores table with php and mysql without looping

My table will hold scores and initials.
But the table wont be ordered.
I can get the total row count easy enough and I know I can get all of them and Order By and then loop through them and get the rank THAT way...
But is there a better way? Could this maybe be done with the SQL statement?
I'm not TOO concerned about performance so if the SQL statement is some crazy thing, then Ill just loop.
Sorry - Table has id as primary key, a string to verify unique app install, a column for initials and a column for score.
When someone clicks GET RANK... I want to be able to tell them that their score is 100 out of 1000 players.
SELECT s1.initials, (
SELECT COUNT(*)
FROM scores AS s2
WHERE s2.score > s1.score
)+1 AS rank
FROM scores AS s1
Do you have a primary key for the table or are you fetching data by the initials or are you just fetching all the data and looping through it to get the single record? How many results are you trying to fetch?
If you only want to fetch one record, as the title suggests, you would query the database using a WHERE conditional:
SELECT score FROM table WHERE user_id = '1';
See this answer: https://stackoverflow.com/a/8684441/125816
In short, you can do something like this:
SELECT id, (#next_rank := IF(#score <> score, 1, 0)) nr,
(#score := score) score, (#r := IF(#next_rank = 1, #r + 1, #r)) rank
FROM rank, (SELECT #r := 0) dummy1
ORDER BY score DESC;
And it will produce a result like this:
+------+----+-------+------+
| id | nr | score | rank |
+------+----+-------+------+
| 2 | 1 | 23 | 1 |
| 4 | 1 | 17 | 2 |
| 1 | 0 | 17 | 2 |
| 5 | 1 | 10 | 3 |
| 3 | 1 | 2 | 4 |
+------+----+-------+------+
Note that users with equal scores have equal ranks. :-)

Categories