Sorry if this is a dumb question everyone, but I'm stuck on something that seems pretty simple.
If I create SQL like this:
Weights: [Volume]/2792
I get the row by row percent of the total.
However, there's no way I'm going to hard-code the total, so I really want to setup my query like this:
Weights: [Volume]/sum([Volume])
However, that gives me this.
Can someone show me how to dynamically sum the field 'Weights' so I can get the percent of each record?
Here is the SQL:
SELECT tblOffices.ServiceID, tblOffices.Branch, tblOffices.Volume, [Volume]/Sum([Volume]) AS Weights
FROM tblOffices
GROUP BY tblOffices.ServiceID, tblOffices.Branch, tblOffices.Volume;
Thanks!
What you need is 2 queries or a query with a sub query. if you are not familiar with sub queries, do 2 queries like this:
QRY1
SELECT Sum([VOLUME]) AS [Total Volume]
FROM yourTable
This will give you the sum of all the volumes.
Then build another query, that uses the result of QRY1 as a value for your calculation.
QRY2
SELECT Yourtable.[Volume] AS Vol, YourTable.[Volume]/QRY1.[Total Volume] AS Weight
FROM YourTable,TEST1;
to display as a percentage multiple by 100 or set the format as percentage.
Related
I am currently a student creating a system for our capstone project. I want to ask how can I display the difference of 2 result in sql. I am using phpmyadmin.
This was my query
SELECT *, sum(no_sec * hpw) AS total_teaching_hours from facultyload GROUP BY sy ORDER BY sy DESC
and displays like this
I was hoping to get something like this
that shows the differences of the data how much was increased or decreased
Thank you for the helppppppp
I have a very basic question which I cannot answer myself but shouldn't take much of your time.
The following query works, it lists all the exhibition_category_id and counts the total of objects that are assigned to each category.
My question is: Why does it do it? I don't understand the query. It says count(*) - why doesn't it give me the total of different exhibition_category_id's (79), but instead counts, how many objects are assigned to each category?
Here is the query in question, as well as a screen shot from the actual output:
SELECT eb.exhibition_category_id, count(*) AS total
FROM exhibition_brand eb
GROUP BY eb.exhibition_category_id
https://i.stack.imgur.com/6deMv.png
Hope its understandable what I am asking for, eager to improve my post based on feedback.
Cheers
Your query is a basic aggregation query:
SELECT eb.exhibition_category_id, count(*) AS total
FROM exhibition_brand eb
GROUP BY eb.exhibition_category_id;
The GROUP BY specifies that the result set will contain one row for each value of eb.exhibition_category_id. The result set consists of two columns, one is the value that defines the row. The other is a count of the number of rows in each group. That is what COUNT(*) does.
If you wanted the total count of different eb.exhibition_category_id, then you want one row and COUNT(DISTINCT):
select count(distinct eb.exhibition_category_id)
from exhibition_brand eb;
The GROUP BY function groups the COUNT() by eb.exhibition_category_id, so the query groups the records by eb.exhibition_category_id, then counts the corresponding records.
I have a MySQL table which has two columns : ID and count. It has an index on ID field.
Now if i have to get sum of all the count between two IDs, I can write a query like:
Select SUM(count) from table where id between x and y
or i can get
select count from table where id between x and y
And then loop through the result and calculate the sum of the count on my application code
Which one is better, considering the speed is the essential thing here. Will indexing on the count help?? Or can i write a different SQL?
Would indexing on the count column help in any way?
I have around 10000 requests per second coming in and I am using a load balancer and 5 servers for this.
The second one is the correct one. There's no need to sum a count, as the count comes back as a single value. It only needs to be run once.
Unless you have a column named count, in which you want to sum all the values...
EDIT
Because you are saying you have a column named Count, you would use the first query:
Select SUM(count) from table where id between x and y
Use approach 1 as you would save on fetching data from MySQL and iterating over it.
The time taken by MySQL to execute either of your queries would be nearly the same but the second approach would require looping through the results and summing them; unnecessary overhead.
I am looking to write a query that selects a percentage of records using something like
select top 30 percent * from people
with a random order.
But I want to select more people if certain criteria is met. So I may want to select 35% where the nationality is Brazilian and a few other conditions.
What's the easiest way to do this? Thought I could use a union to a second query, but there may be several of these exceptions so is there a better way?
You can use variables to do it. Try something like this:
DECLARE #RecordsToReturn AS Decimal(16,2);
SET #RecordsToReturn AS (SELECT COUNT(*) FROM YourTable WHERE "Your Column" = 'Some Criteria') * .3
SELECT TOP (#RecordsToReturn) *
FROM YourTable
I dont have SQL server in front of me, but that should get you the first 30% of records based on the where criteria
I have a big database of users.
i have a query to filter them out and i encode in JSON the results.
i want to have at the same time the total number of the query ("Result found: 507")
but i dont want to load them all 507 but just the first 25...
i guess i have to write two queries, one for calc the toal and one to laod the first 25 right? any better solution?
thanks!
select *, count(id) as c FROM table
UNION ALL
select *, 0 as c FROM table
LIMIT 25
something like this gives 26 records, first one being a duplicate but having the total count in field 'c'
i think this gives the right answer
SQL_CALC_FOUND_ROWS and FOUND_ROWS()
via http://www.arraystudio.com/as-workshop/mysql-get-total-number-of-rows-when-using-limit.html