This question already has answers here:
How can I return pivot table output in MySQL?
(10 answers)
Closed 5 years ago.
I have the following db table, and I would like to be able to count amount of each product_id per name.
---------------------
| name | product_id |
---------------------
| David | 1 |
|Charlie| 1 |
| David | 2 |
| David | 1 |
|Charlie| 2 |
|Charlie| 3 |
|Charlie| 2 |
|Charlie| 3 |
---------------------
I would like to able to create a result set like the following;
----------------------------------------------------------------------------
| name | count(product_id_1) | count(product_id_1) | count(product_id_1) |
----------------------------------------------------------------------------
| David | 2 | 1 | 0 |
|Charlie | 1 | 2 | 2 |
----------------------------------------------------------------------------
So, please help me how to query for the above problem, Thank's
If you wan't to group the count of products based on customers you can do something like the following if you are joining tables.
SELECT
NAME,
COUNT(product_id1),
COUNT(product_id2),
COUNT(product_id2)
FROM
Customers c
INNER JOIN Ordered_Products
ON c.id = customer.id
GROUP BY c.name
otherwise if you're not doing a join you can do the following
SELECT
NAME,
COUNT(product_id1),
COUNT(product_id2),
COUNT(product_id2)
FROM
Customers c
GROUP BY c.name
Related
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Retrieving the last record in each group - MySQL
(33 answers)
Closed 4 months ago.
I not figure out to evaluate the maximum creation date value in a join. Below the tables envolved:
Supervisor
SupervisorCompany
Company
A Supervisor can be related to many Company, and a Company can be related to many Supervisor.
The relation ManyToMany is represented by SupervisorCompany table that conatains the foreign key related.
--------------
| Supervisor |
-----------------------------------------------------------
IdSupervisor | Name | Surname | CreationTime |
------------------------------------------------------------
1 | Maximilian | Green | 2022-01-01 01:00:01 |
------------------------------------------------------------
2 | Josh | Nice | 2023-04-03 01:00:01 |
------------------------------------------------------------
3 | Albert | Cloud | 2022-03-01 01:32:01 |
------------------------------------------------------------
4 | Peter | Dark | 2022-03-01 01:32:01 |
------------------------------------------------------------
--------------
| Company |
--------------------------------------
IdCompany | Brand | Address |
--------------------------------------
1 | X | |
--------------------------------------
2 | Y | |
--------------------------------------
3 | Z | |
--------------------------------------
4 | J | |
--------------------------------------
-------------------------
| SupervisorCompany |
--------------------------------------
Id |Id_Supervisor | Id_Company |
--------------------------------------
8 | 1 | 1 |
--------------------------------------
9 | 2 | 1 |
--------------------------------------
10 | 3 | 1 |
--------------------------------------
11 | 4 | 3 |
--------------------------------------
I want return the newest CreationTime Supervisor of a Company for each Company.
I executed this query:
select *, MAX(Supervisor.CreationTime) from Company
inner join SupervisorCompany on Company.IdCompany = SupervisorCompany.IdCompany
inner join Supervisor on SupervisorCompany.IdSupervisor = Supervisor.IdSupervisor
GROUP BY Company.IdCompany;
But the result is:
| Maximilian | Green | 2022-01-01 01:00:01 | 1 |
insted
| Josh | Nice | 2023-04-03 01:00:01 | 1 |
I know that there is somethings wrong in the query but I don't know exactly what is the mistake.
I tried with a subquery or others approach but I don't figure out.
Thanks in advance
You should select only the company ID and the max creation time:
SELECT c.IdCompany, MAX(s.CreationTime) AS MaxCreationTime
FROM Company c
INNER JOIN SupervisorCompany sc ON c.IdCompany = sc.IdCompany
INNER JOIN Supervisor s ON sc.IdSupervisor = s.IdSupervisor
GROUP BY c.IdCompany;
The rough rule of thumb for an aggregation query is that we can select only columns which appear in the GROUP BY clause or columns which appear inside aggregate functions (such as MAX()).
I've been trying to get this going for hours and haven't figured this out yet.
Say I've got 2 tables - master and details.
master/detail has the following data
master table
+------+-------+
| id | name |
+------+-------+
| 1 | jeff |
| 2 | simon |
| 3 | andy |
| 4 | jerry |
+------+-------+
details table
+----+-----------+---------+
| id | master_id | tag |
+----+-----------+---------+
| 1 | 1 | WINDOWS |
| 2 | 1 | MAC |
| 3 | 2 | MAC |
| 4 | 3 | WINDOWS |
| 5 | 3 | MAC |
| 6 | 3 | LINUX |
| 7 | 4 | MAC |
+----+-----------+---------+
how do I select the master records which has both tags 'WINDOWS', 'MAC'.
So it should only return master_id 1 and 3 which is jeff and andy only.
If I do a
select distinct(master_id) from details where tag in ('WINDOWS', 'MAC')
it gives me all of them.
Sorry for the newbie question but if anyone can help, it'll be much appreciated.
You need simple GROUP BY with HAVING clause :
select master_id
from details
where tag in ('WINDOWS', 'MAC')
group by master_id
having count(*) = 2;
If details table has duplicate tags for master_id then you need count(distinct tag).
YOu could use a join with count having 2 value only for tag
select distinct master_id
from detail
inner join (
select master_id from detail
group by master_id
having count(distinct tag) = 2
) t on t.master_id = detail.master_id and detail.tag in ('WINDOWS', 'MAC')
This question already has answers here:
Select values that meet different conditions on different rows?
(6 answers)
Closed 4 years ago.
can I return a single instance of a row after using a join on a categories table.
Entries
| id | Name |
| 1 | Johnny |
| 2 | Steve |
| 3 | Bam |
Categories
| cat_id | Name |
| 1 | Season one |
| 2 | Season two |
| 3 | Season three|
Category Posts
| id | cat_id |
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 2 |
| 2 | 3 |
| 3 | 1 |
What I want to do is select all cast where members that have been in season 2 and 3, they must have been in both and I only want a single instance returned.
Expected output
| id | Name |
| 1 | Johnny |
| 2 | Steve |
How would I got about selecting these? I've thought about grouping the user based on their name however because I'm selecting IN ("2", "3") I get some users that have been in two but not three and the expected results are wrong.
Thanks
you can use sub query
SELECT id,name from Entries where id in (select a.id from (select id from
category_post WHERE cat_id=2) as a,(select id from category_post WHERE cat_id=3) as b where a.id=b.id)
This question already has answers here:
select from one table, count from another where id's linked
(2 answers)
Closed 4 years ago.
I have something like this:
TABLE: maindata
client_id | username | data | data_id
______________________________________
0 | rusty | xyz | 827
1 | rusty1 | xyz | 827
2 | rusty2 | xyz | 827
And then in another table I have:
TABLE: users:
client_id | username |
______________________
0 | rusty |
1 | rusty1 |
2 | rusty2 |
2 | rusty3 |
2 | rusty4 |
I would like to return:
client_id | username | data | data_id | count
______________________________________
0 | rusty | xyz | 827 | 1
1 | rusty1 | xyz | 827 | 1
2 | rusty2 | xyz | 827 | 3
The count here is the count for the number of clients in users. What I have tried has not gotten me where I would like at all:
SELECT * from stats AS s INNER JOIN users as u SELECT COUNT(*) WHERE
u.client_id=s.client_id GROUP BY client_id
Any idea where I am going wrong? Is INNER JOIN totally wrong?
Edit : highlight query.
I think you want
SELECT s.*, COUNT(*) as count from stats AS s
INNER JOIN users as u on u.client_id=s.client_id
GROUP BY client_id
This question already has an answer here:
MySQL UPDATE with SELECT SUM from different table
(1 answer)
Closed 7 years ago.
I'm trying to update SubTotal field in Transactions table by summing up results from bridge table TransactionRecords. Following is what my table contains.
| Transaction |
| ID | SubTotal |
| 1 | ??? |
| 2 | ??? |
| 3 | ??? |
| TransactionRecords |
| TransactionID | ProductID | QTY | SalePrice |
| 1 | 10 | 4 | 19.99 |
| 2 | 5 | 8 | 9.99 |
| 2 | 3 | 12 | 14.99 |
What I want is to have Transaction.SubTotal equal to TransactionRecords.SalePrice * TransactionRecords.QTY where TransactionRecords.TransactionID equals to Transaction.ID
This query :
select TransactionID, sum(UnitPrice * QuantitySold)
from TransactionRecord
group by TransactionID;
gives me each transaction and sale amount made in each transaction and that's the value I want Transaction table to have in it's appropriate ID.
Try this:
UPDATE Transaction t
SET SubTotal = (SELECT SUM(UnitPrice * QuantitySold) FROM TransactionRecord tr WHERE tr.TransactionId = t.ID);