Get the sum of the numbers generated by sql - mysql

I have a table named Company which has categorized by several sector_id
SELECT sector_id,count(sector_id) FROM Company group by sector_id
I could get the company numbers like this
sector_id count(sector_id)
1 10
2 15
3 22
Then, I would like to get the sum of count(sector_id) 10 + 15 + 22
Is it possible to do this by sql only? or I need to make some code with php??

Just remove the group by:
SELECT count(*)
FROM Company ;
EDIT:
It occurs to me that you want a summary row as well as the original data. If so, you can use rollup or grouping sets in most databases. Something like this:
SELECT sector_id, count(sector_id)
FROM Company
GROUP BY sector_id WITH ROLLUP;

Yes, it is possible to calculate sum using sql by using SUM() method
Syntax :
SELECT SUM(column_name)
FROM Table_Name;
In your case it will be like this :
SELECT SUM(sector_id)
FROM Company ;
Hope it helps.

Related

How to find only id that has different values in MySQL?

I have a table with two columns:
id
num
1
2
2
8
1
7
7
3
I want to get as an answer to my query only ids that have more than 1 nums.
For example in my table I would want to get as a result:
id
1
How should I express my query?
Thanks in advance.
You might need something like this:
SELECT id
FROM your_table_name
GROUP BY id
HAVING count(DISTINCT num) > 1;
Google 'Aggregate functions'. Here the aggregate function is count() and it works always coupled with a GROUP BY clause. Pretty fun.

How to group by with 2 different field for the below criteria

Name date sellingAmt
Raju 08-02-2017 2000
Ravi 09-02-2017 5000
Ravi 09-02-2017 2000
Raju 09-02-2107 1000
Expected Result
Name date sellingAmt
Raju 08-02-2017 2000
Ravi 09-02-2017 7000
Raju 09-02-2107 1000
Let me know how to group by this in mysql select query
Seems like a pretty basic question. What part are you struggling with?
Always post what you've tried so we can look at your train of thought and try to correct the problem at it's root instead of giving you a baked solution!
Date (and I just see name is as well) is a keyword/reserved word so out of habit I use the ` to escape them.
I used sellingAmt Desc as the order by since it appears you want those within the same date; though you could just as easily want name desc.
I group by name, date to achieve the desired results. While mySQL extends the group by so you can group on fewer values than what is in the select, I find this feature is wrongly used more often than correctly. So I tend to error on the side of caution and always group by all fields not part of an aggregate; which is what other RDBMS systems would require of you. In your case date and name are the unique combination to sum sellingAmt correctly and achieve your expected results.
.
SELECT `Name`, `Date`, Sum(SellingAmt) sellingAmt
FROM {tblName}
GROUP BY `Name`, `Date`
ORDER BY `date`, sellingAmt Desc
Replace {tblname} with your table name.

Add sum of columns to chart SSRS

I have searched all over and cannot seem to find a definitive answer for this issue! I have a simple chat here grouped on the 5 categories below detailing the Sums of their SqFt.
I want to add a Total Column to the graph ~(Total = 11M sqft). Can this only be done in SQL? It is a bit puzzling for me to do this because the query already sums the sqft for each row (as a nested query). I would need to Sum(sum(sqft)) in order to produce what I want, however, I dont believe this will work on the group level.
Sample Data set:
ID| Type| Sqft|
12| OF| 500
14| IN| 1294
99| OF| 12042
24| ME| 92043
15| IN| 13945
16| OW| 2650
Can this be done in the report builder?
Thanks!
You can add a Total row in your query by using GROUPING SETS operator. Once the total is in the dataset it is trivial to show the column in the chart.
Based on the data sample you posted you can use a similar query to the below:
SELECT
CASE
WHEN GROUPING_ID(Type) = 1 THEN 'TOTAL'
ELSE Type
END [Type],
SUM(Sqft) Sqft,
GROUPING_ID(Type) [Grouping]
FROM your_table
GROUP BY GROUPING SETS ((Type), ())
Check this Live Demo
If you are confused by the above query you can simply use the union operator to add a row to the end of your current dataset.
SELECT
ID,
[Type],
Sqft
FROM your_table
UNION ALL
SELECT
NULL,
'Total',
SUM(Sqft)
FROM your_table
Now just create your chart using the produced dataset.
Let me know if this helps.

MySQL Sum and Case Query

I create a ReportViewer with VB.NET connecting to a MySQL database. The data appears like below.
IdProduct Quantity TotalPrice OrderDate
0001 1 10 29/09/2014
0002 2 40 29/09/2014
0001 4 40 29/09/2014
0001 2 20 29/09/2014
0001 2 20 29/09/2014
Based on the records above, I'd like the result to appear like below
0001 0002
9 2
90 40
What is Query Sum Case the best use here? Thanks in advance.
NOTE: It's not possible for a query to "dynamically" alter the number or datatype of the columns returned, those must be specified at the time the SQL text is parsed.
To return the specified resultset with a query, you could do something like this:
SELECT SUM(IF(t.IdProduct='0001',t.Quantity,NULL)) AS `0001`
, SUM(IF(t.IdProduct='0002',t.Quantity,NULL)) AS `0002`
FROM mytable t
UNION ALL
SELECT SUM(IF(t.IdProduct='0001',t.TotalPrice,NULL)) AS `0001`
, SUM(IF(t.IdProduct='0002',t.TotalPrice,NULL)) AS `0002`
FROM mytable t
Note that the datatypes returned by the two queries will need to be compatible. This won't be a problem if Quantity and TotalPrice are both defined as integer.
Also, there's no specific guarantee that the "Quantity" row will be before the "TotalPrice" row; we observe that behavior, and it's unlikely that it will ever be different. But, to have a guarantee, we'd need an ORDER BY clause. So, including an additional discriminator column (a literal in the SELECT list of each query), that would give us something we could ORDER BY.
Note that it's not possible to have this single query dynamically create another column for IdProduct '0003'. We'd need to add that to the SELECT list of each query.
We could do this in two steps, using a query to get the list of distinct IdProduct, and then use that to dynamically create the query we need.
BUT... with all that said... we don't want to do that.
The normative pattern would be to return Quantity and TotalPrice as two separate columns, along with the IdProduct as another column. For example, the result returned by this statement:
SELECT t.IdProduct
, SUM(t.Quantity) AS `Quantity`
, SUM(t.TotalPrice) AS `TotalPrice`
FROM mytable t
GROUP BY t.IdProduct
And then the client application would be responsible for transforming that resultset into the desired display representation.
We don't want to push that job (of transforming the result into a display representation) into the SQL.
select idproduct, sum(quantity), sum(totalprice)
from your_table
group by idproduct

Find lowest value and coresponding code on asingle query mysql

I have a user table as follows
id name age
1 John 21
2. Mathan 23
3. Raj 21
4. Manoj 50
5 Krishnan 91
I want to find minimum age and its corresponding name. How can I do it with rails?
Can I do it in a single query?
Note: More than one names can have single age.
Is there a specific reason why you want to do it in a single query ?
If you can write 2 queries, I think you can just write :
User.where age: User.minimum(:age)
select age, group_concat(name) from table group by age order by age asc limit 1
You will need to process the results later on in ruby, but this gives all you need in one single query. Also i am assuming mysql, so might differ on other rdbms.
It gives exact output in mysql that you want try this
SELECT concat("[",name," ",age,"]") AS name
FROM TABLE
WHERE age =
(SELECT min(age)
FROM TABLE);