For example, I have fruit come from different country and have different brand(note that the country and brand is independence).
+-------+-------+---------+-------+
| name | stock | Country | brand |
+-------+-------+---------+-------+
| apple | 40 | USA | Sun |
| apple | 38 | UK | Sun |
| apple | 33 | CN | Green |
| apple | 29 | UK | Air |
| apple | 15 | USA | Green |
+-------+-------+---------+-------+
Normally if we use this command
SELECT name, Country, sum(stock)
FROM table
GROUP BY Country
It will give the following result
+-------+---------+------------+
| name | Country | sum(stock) |
+-------+---------+------------+
| apple | CN | 33 |
| apple | UK | 67 |
| apple | USA | 55 |
+-------+---------+------------+
But what I want is this....
+-------+----------+-------+
| name | category | stock |
+-------+----------+-------+
| apple | USA | 55 |
| apple | UK | 67 |
| apple | CN | 33 |
| apple | Sun | 78 |
| apple | Green | 48 |
| apple | Air | 29 |
+-------+----------+-------+
For some result, the command must be one step command, which means only for one command it should able to directly output the above result just one click.
I would suggest doing this with a subquery:
select name, category, sum(stock)
from (select name, brand as category, stock, 1 as ordering
from table
union all
select name, country, stock, 0 ordering
from table
) t
group by name, category, ordering
order by ordering, name;
Note the use of the ordering column to ensure that the results appear in the same order as in your desired results. Also, this will keep two rows in cases where the brand and country have the same name. You can remove the ordering from the group by if you want them combined.
try reading into unions or left join
i think you need to make
SELECT name, Country as category, sum(stock)
into #temptable1 FROM table
GROUP BY Country
and
SELECT name, brand as category, sum(stock)
into #temptable2 FROM table
GROUP BY brand
then join the two temp tables together
this can be done using stored procedures
or simply union directly without making temp tables
SELECT name, Country as category, sum(stock)
into #temptable1 FROM table
GROUP BY Country
UNION
SELECT name, brand as category, sum(stock)
into #temptable2 FROM table
GROUP BY brand
Related
Guys let me make myself clear. I'm studying MYSQL and practicing the function "count()". I have a table called "City", where I have ID, name, CountryCode, district, and Population. My first idea was to know how many cities I have by country
SELECT *, Count(name) as "total" FROM world.city GROUP BY countrycode;
It worked, an extra column was created with the number of cities by each country. I would like to know how many countries I have by counting the number of distinct rows (I know that a have this information on the bottom of the WorkBench, but I would like to know to make this information appear on my query). I tried to add a Count(CountryCode), but it didn't work as I was expecting, a number 4079 appeared, which is the total number of cities that I have. I figured out that my "Count()" is calculating the number of rows inside each Country, not counting the number of codes that I have for each country. Is that possible to get this information?
(A mini-lesson for a Novice.)
The first thing to learn is that COUNT(*) is the usual way to use COUNT. And you get the number of rows. In contrast, COUNT(name) counts the number of rows with non-NULL name values.
Then comes the way to use DISTINCT. It is not a function. So COUNT(DISTINCT a,b) counts the number of different combinations of a and b. And COUNT(DISTINCT(a)) though it works 'fine' and 'correctly', the parens are redundant. So use COUNT(DISTINCT a).
Don't use * with GROUP BY. That is, SELECT *, ... GROUP BY ... is improper. The usual way to say something like your query is
SELECT countrycode, COUNT(*) AS "total"
FROM world.city
GROUP BY countrycode;
For provinces in Canada (which I happen to have a table of):
SELECT province, COUNT(*) AS "total" FROM world.canada GROUP BY province;
+---------------------------+-------+
| province | total |
+---------------------------+-------+
| Alberta | 573 |
| British Columbia | 716 |
| Manitoba | 299 |
| New Brunswick | 210 |
| Newfoundland and Labrador | 474 |
| Northwest Territories | 94 |
| Nova Scotia | 331 |
| Nunavut | 107 |
| Ontario | 891 |
| Prince Edward Island | 57 |
| Quebec | 1045 |
| Saskatchewan | 573 |
| Yukon | 114 |
+---------------------------+-------+
Note that a few cities show up in multiple provinces:
SELECT COUNT(DISTINCT city), COUNT(*) FROM world.canada;
+----------------------+----------+
| COUNT(DISTINCT city) | COUNT(*) |
+----------------------+----------+
| 5248 | 5484 |
+----------------------+----------+
Munch on this; there are some more lessons to learn:
SELECT city, COUNT(*) AS ct, GROUP_CONCAT(DISTINCT state)
FROM world.us
GROUP BY city
ORDER BY COUNT(*)
DESC LIMIT 11;
+-------------+----+----------------------------------+
| city | ct | GROUP_CONCAT(DISTINCT state) |
+-------------+----+----------------------------------+
| Springfield | 11 | FL,IL,MA,MO,NJ,OH,OR,PA,TN,VA,VT |
| Clinton | 10 | CT,IA,MA,MD,MO,MS,OK,SC,TN,UT |
| Madison | 8 | AL,CT,IN,ME,MS,NJ,SD,WI |
| Lebanon | 8 | IN,ME,MO,NH,OH,OR,PA,TN |
| Auburn | 7 | AL,CA,IN,ME,NH,NY,WA |
| Burlington | 7 | IA,MA,NC,NJ,VT,WA,WI |
| Washington | 7 | DC,IL,IN,MO,NC,PA,UT |
| Farmington | 7 | ME,MI,MN,MO,NH,NM,UT |
| Canton | 6 | GA,IL,MA,MI,MS,OH |
| Monroe | 6 | GA,LA,MI,NC,WA,WI |
| Lancaster | 6 | CA,NY,OH,PA,SC,TX |
+-------------+----+----------------------------------+
As for the number of cities in a country, that belongs in a the table Countries, not in the table Cities. Then use a JOIN when you want to put them together.
I'm using a GROUP BY to display general info from a table.
SELECT * FROM table GROUP BY Continent
Datas are the following ones:
|--------|-----------|-----------|
| Id | Continent | Fruits |
|--------|-----------|-----------|
| 1 | Africa | Banana |
| 1 | Africa | Cherry |
| 1 | Mexico | Apple |
| 1 | Mexico | Pear |
| 1 | Europa | Blueberry |
| 1 | Europa | Orange |
| 1 | Europa | Kiwi |
|--------|-----------|-----------|
Now how can I get the FIRST fruit from each continent ?
This is what I've tried:
SELECT *, MIN(Fruits) AS FirstFruit FROM table GROUP BY Continent
Thanks.
If you just want two columns in the resultset, then aggregation is sufficient:
select continent, min(fruits) as firstfruit
from mytable
group by continent
If, on the other hand, there are other columns you are interested it, then aggregation alone cannot give you the result you want. One option uses a correlated subquery to filter the table:
select t.*
from mytable t
where t.fruits = (select min(t1.fruits) from mytable t1 where t1.continent = t.continent)
I have table that looks like the following:
`units`
+----+------+-------+---------------+-------+
| id | tech | jobID | city | units |
+----+------+-------+---------------+-------+
| 1 | 1234 | 8535 | San Jose | 3 |
| 2 | 1234 | 8253 | San Francisco | 4 |
| 3 | 1234 | 2457 | San Francisco | 5 |
| 4 | 1234 | 8351 | Mountain View | 8 |
+----+------+-------+---------------+-------+
and a view that uses this data to do some computations:
`total`
+----+--------+------+-------+
| id | name | tech | total |
+----+--------+------+-------+
| 1 | Dan | 1234 | 12 |
| 2 | Dan SF | 1234 | 12 |
+----+--------+------+-------+ ...
My problem is that I am trying to sum up the amount of units Dan completed in San Francisco and the amount of units he did elsewhere (need to specifically track how many units were completed in SF). However, I'm unsure of how to do this within my select query and if you look at my current total table, you'll see that both total values are simply summing all of the units regardless of city.
I want to get the following:
`total`
+----+--------+------+-------+
| id | name | tech | total |
+----+--------+------+-------+
| 1 | Dan | 1234 | 11 |
| 2 | Dan SF | 1234 | 9 |
+----+--------+------+-------+ ...
I need help writing my SELECT because I'm unsure of how to use CASE to get the desired result. I've tried the following:
SELECT otherTable.name AS name, units.tech AS tech,
(CASE WHEN City = 'SAN FRANCISCO' THEN SUM(units)
ELSE SUM(units)
) AS total
FROM units, otherTable
GROUP BY name
but clearly this won't work since I'm not differentiating between cities in the two aggregates.
Any help is greatly appreciated.
EDIT: The SELECT query for my current view (with join info) is as follows:
SELECT otherTable.name, units.tech, SUM(units.units)
FROM units
LEFT JOIN otherTable ON otherTable.tech = units.tech
GROUP BY name
As for otherTable, it simply associates each tech ID with a name:
`otherTable`
+----+--------+------+-----------+
| id | name | tech | otherInfo |
+----+--------+------+-----------+
| 1 | Dan | 1234 | ...... |
+----+--------+------+-----------+
First off, it appears that your base query is wrong. There isn't nothing about the join between units and otherTable, but I don't know enough to put it in.
It seems strange to me that you would want it broken out into rows instead of columns, but you could do the following:
SELECT otherTable.name AS name, units.tech AS tech,
SUM(units) AS total
FROM units, otherTable
-- not sure if this section should exclude 'SAN FRANCISO' or not
GROUP BY name
UNION ALL
SELECT otherTable.name || ' SF' AS name, units.tech AS tech,
SUM(units) AS total
FROM units, otherTable
WHERE City = 'SAN FRANCISCO'
GROUP BY name
This would give you
+--------+------+-------+
| name | tech | total |
+--------+------+-------+
| Dan | 1234 | 11 |
| Dan SF | 1234 | 9 |
+--------+------+-------+
Or if you want separate columns, you could do this
SELECT otherTable.name AS name, units.tech AS tech,
SUM(units) AS total,
SUM(CASE WHEN City = 'SAN FRANCISCO' THEN units
ELSE 0
) AS sf_total
FROM units, otherTable
GROUP BY name
This would give you
+--------+------+-------+----------+
| name | tech | total | sf_total |
+--------+------+-------+----------+
| Dan | 1234 | 11 | 9 |
+--------+------+-------+----------+
I'm new to mySQL, and I'm trying to be able to either SELECT or CREATE A VIEW with the information I want to retrieve from two tables. SQLfiddle
People Table
| people_id | username | Password | Email |
----------------------------------------------
| 1 | username1 | Password1 | Email1 |
| 2 | username2 | Password2 | Email2 |
| 3 | username3 | Password3 | Email3 |
Profile Table
| people_id | id | age | location | hobbies | about |
----------------------------------------------------------------------------------------------------------------------------
| 1 | 1 | 22 | USA | skiing, snowboarding | I am from the US and I like to snowboard |
| 2 | 2 | 45 | Canada | curling, ice skating, reading | I like to ice skate! |
| 3 | 3 | 38 | USA | tv, movies, you name it | I am from the US and I like to watch the tube |
| 2 | 4 | 45 | Canada | curling, reading | I do not like to ice skate anymore |
| 2 | 5 | 46 | Canada | bowling | Bowling is my new favorite hobby! I just turned 46! |
| 1 | 6 | 22 | Brazil | skiing, snowboarding | I am orginally from the US but I just moved to brazil|
I would like to see/retrieve the data like this :
| people_id | username | age | location | hobbies | about |
------------------------------------------------------------------------------------------------------------------------------------
| 3 | username3 | 38 | USA | tv, movies, you name it | I am from the US and I like to watch the tube |
| 2 | username2 | 46 | Canada | bowling | Bowling is my new favorite hobby! I just turned 46! |
| 1 | username1 | 22 | Brazil | skiing, snowboarding | I am orginally from the US but I just moved to brazil|
So I need to select all the people_id and username from table People and then select the people_id row from Profile where the id is the largest number for each people_id
I've tried
SELECT People.people_id, People.username, Profile.age, Profile.location, Profile.hobbies, Profile.about
FROM People
INNER JOIN Profile
ON People.people_id=Profile.people_id
Which gets me closer to what I want, but I don't want to show duplicate rows, I only want to show the last row inserted into the Profile table for each people_id.
SQLfiddle
The most efficient way to get what you want is to use a not exists condition in the where clause. This definitely takes some getting used to. What the query is going to do is to get the matching row from Profile subject to the condition that no other row has a larger id. This is a round-about way of saying "get the biggest id for each person". But, it happens to produce an efficient query plan (and this is true in most databases, not just MySQL).
SELECT p.people_id, p.username, pr.age, pr.location, pr.hobbies, pr.about
FROM People p INNER JOIN
Profile pr
ON p.people_id = pr.people_id
WHERE NOT EXISTS (SELECT 1
FROM Profile pr2
WHERE pr2.people_id = pr.people_id AND
pr2.id > pr.id
);
SELECT People.people_id, People.username, Profile.age, Profile.location, Profile.hobbies, Profile.about
FROM People
INNER JOIN (SELECT * FROM Profile ORDER BY id DESC) AS Profile
ON People.people_id=Profile.people_id
GROUP BY People.people_id
ORDER BY people_id DESC
I am actually facing an issue with MySQL, I need to have a similar behavior of the GROUP BY but without grouping.
Here is an example for just one table named 'resident' with 2 columns: 'name', 'city'.
If i want to count the number residents for each city, i just need to do this :
SELECT resident.city, count(resident.name) as 'Nb residents'
FROM resident
GROUP BY resident.city
So the result would be like :
| city | Nb residents |
| NY | 3 |
| HK | 1 |
| Rome | 2 |
...
But i am looking for a way to display my result like that :
| name | city | Nb residents |
| Name1 | NY | 3 |
| Name2 | NY | 3 |
| Name3 | NY | 3 |
| Name4 | HK | 1 |
| Name5 | Rome | 2 |
| Name6 | Rome | 2 |
Is it possible to do that ?
Thanks for your help.
SELECT resident.name, resident.city, (select count(a.name) from resident a where a.city = resident.city) as 'Nb residents'
FROM resident
You could to this with the following query:
SELECT t1.`name`, t2.`city`, t3.`Nb residents`
FROM resident t1
JOIN ( SELECT resident.city, count(resident.name) as 'Nb residents' FROM resident GROUP BY resident.city ) t2
ON t1.`city`= t2.`city`
This extracts the amount of residents per city in a subquery and then joins this with the names of the very table.