Hi I have three tables here is all
First. count_internets
id| company_id | item_id | created_at
1 | 1 | 1 | 2020-10-14 |
2 | 1 | 2 | 2020-10-15 |
3 | 2 | 4 | 2020-10-16 |
4 | 2 | 5 | 2020-11-20 |
5 | 1 | 1 | 2020-11-22 |
6 | 1 | 1 | 2020-11-23 |
7 | 2 | 5 | 2020-11-23 |
Second compaies
id | name
1 | Company 1
2 | Company 2
Third items
id | name | company_id
1 | Product 1 | 1
2 | Product 2 | 1
3 | Product 4 | 2
4 | Product 5 | 5
I want to get infomartion how many items sold during one month grouping by company like below
| Company | Products | Month | Count
| Company 1 | Product 1 | OCT | 1
| Company 1 | Product 2 | OCT | 1
| Company 1 | Product 1 | NOV | 1
| Company 1 | Product 4 | OCT | 1
| Company 2 | Product 5 | NOV | 2
I tried many SQL queries, but I can not solve. Please help to solve this query.
This one should work
SELECT c.name as company, i.name as products, EXTRACT(MONTH FROM FROM_UNIXTIME(created_at)) as month, count(*)
FROM count_internets
JOIN companies as c on company_id = c.id
JOIN items as i on item_id = i.id
GROUP BY company, products, month
Be aware that if that the query extract only the month (so no difference between years). If you need the year too you have to extract it EXTRACT(YEAR FROM FROM_UNIXTIME(created_at)) as year.
But your example is not very clear, company 1 didn't sell product 3. Furthermore, what's the meaning of the company_id in items table?
Related
I have posts and products. Posts is related with Products.
I need count all posts updated at current month (2017-07), but related product update date also is important.
Post3 is also updated this month, because have a product Product3.
If i try GROUP BY Posts.date_update, Products.date_update, but get incorect number.
Posts
id | Name | date_update
1 | Post1 | 2017-07
2 | Post2 | 2017-08
3 | Post3 | 2017-08
Products
id | Name | date_update
1 | Product1 | 2017-07
2 | Product2 | 2017-08
3 | Product3 | 2017-07
4 | Product4 | 2017-09
PostsToProducts
id | post_id | product_id
1 | 1 | 2
2 | 1 | 3
3 | 1 | 4
4 | 2 | 4
5 | 3 | 2
6 | 3 | 3
Assuming you always have at least one Product related to an existing Post you could do something like :
SELECT COUNT(DISTINCT(Posts.Name)) FROM Posts, Products, PostsToProducts WHERE
Posts.id = PostsToProducts.post_id
AND
Products.id = PostToProducts.product_id
AND
(Posts.date_update = '2017-07' OR Products.date_update = '2017-07');
I'm kinda lost on what kind of SQL query I should do to achieve what I want.
Let's say I have three tables :
select * FROM trip;
| trip_id | title | description
----------------------------------
| 1 | title1 | desc1 |
| 2 | title2 | desc2 |
| 3 | title3 | desc3 |
| 4 | title4 | desc4 |
| 5 | title5 | desc5 |
| 6 | title6 | desc6 |
select * FROM weekly_report;
| report_id | trip_id| incident_id
----------------------------------
| 1 | 1 | (null) |
| 2 | 1 | (null) |
| 3 | 1 | 1 |
| 4 | 2 | 2 |
| 5 | 3 | 3 |
| 6 | 3 | (null) |
select * FROM incident;
| incident_id | error_code |
----------------------------------
| 1 | 22223 |
| 2 | 25456 |
| 3 | 25456 |
So for a little operationnal knowledge :
The trip table contains 1 record PER trip done by the customer.
The weekly_report contains A report per Week of the trip. (1 trip of 2 weeks will have 2 records, 1 trip or 5 weeks will have 5.. ).
The incident table contains 1 record per incident. (If an incident happened during a week : we create a record in the incident table, else we do nothing)
I'd like to find in a single query (or if it has to be, with subqueries) the number of trips where during at least a week there has been an incident declared for the error_code "25456".
Expected result from the sample data : 2 ( because for trip 2 and three there exist an incident with the error code 25456 ).
I can explain more if needed, is there anybody out there willing to help me ?
Thanks,
You need to take count of distinct trips for related incidents
select count(distinct w.trip_id)
from weekly_report w
inner join incident i
on w.incident_id = i.incident_id
where i.error_code = 25456;
Try this:
SELECT w.trip_id
FROM incident i
INNER JOIN weekly_report w ON i.incident_id=w.incident_id
WHERE error_code='25456'
and if you want the count,then
SELECT COUNT(w.trip_id)
FROM incident i
INNER JOIN weekly_report w ON i.incident_id=w.incident_id
WHERE error_code='25456'
I keep track of certain game results in a MySQL database. Now I want to print the latest results in a nice HTML table. I have three tables:
Table persons contains all participants of the game.
+-----+---------+
| id | name |
+-----+---------+
| 2 | Jon |
| 3 | Philip |
| 4 | Tom |
| 5 | Joey |
| 6 | Joanna |
+-----+---------+
The table rounds contains information about each round of the game. Among other things, the week in which the game was fought.
+-----+------+
| id | week |
+-----+------+
| 1 | 9 |
| 2 | 10 |
| 3 | 11 |
| 4 | 12 |
| 5 | 13 |
+-----+------+
And the table results contains the results for each person and round. The result column is a score in the game.
+------------+----------+--------+
| personId | roundId | result |
+------------+----------+--------+
| 2 | 1 | 2 |
| 4 | 1 | 6 |
| 5 | 1 | 6 |
| 3 | 1 | 10 |
| 2 | 2 | 16 |
| 4 | 2 | 14 |
| 5 | 2 | 5 |
| 3 | 2 | 11 |
+------------+----------+--------+
Now I want to print a table with the players scores each week. I want my output to look like the table below. Note that if a player did not participate one week, the cell should be empty.
+------+-----+--------+-----+------+--------+
| Week | Jon | Philip | Tom | Joey | Joanna |
+------+-----+--------+-----+------+--------+
| 9 | 2 | 10 | 6 | 6 | |
| 10 | 16 | 11 | 14 | 5 | |
+------+-----+--------+-----+------+--------+
So my question is: How do I do to get such output?
This is not a duplicate. See comments below.
So as stated in comments, you want to make a PIVOT, but MySQL does not support it.
However since your number of players is low and fixed, you can hardcode the players in a GROUP BY query like this :
SELECT R.Week,
SUM(CASE WHEN P.name = 'Jon' THEN S.result END) AS Jon,
SUM(CASE WHEN P.name = 'Philip' THEN S.result END) AS Philip,
SUM(CASE WHEN P.name = 'Tom' THEN S.result END) AS Tom,
SUM(CASE WHEN P.name = 'Joey' THEN S.result END) AS Joey,
SUM(CASE WHEN P.name = 'Joanna' THEN S.result END) AS Joanna
FROM persons P
LEFT JOIN results S ON P.id=S.personId
LEFT JOIN rounds R ON R.id=S.roundId
WHERE R.week IS NOT NULL
GROUP BY R.Week
SqlFiddleDemo
my customer table
id | name
==========
1 | mick
2 | george
3 | john
bakery transactions
id | customer | type | amount | date
============================================
1 | 3 | bread | 1 | 2016/03/10 10:00
2 | 1 | bread | 2 | 2016/03/10 11:00
3 | 1 | baguette| 1 | 2016/03/10 11:00
4 | 2 | bread | 2 | 2016/03/11 09:00
5 | 3 | cookie | 5 | 2016/03/11 09:30
greengrocery transactions
id | customer | type | amount | date
============================================
1 | 1 | banana | 1 | 2016/03/10 08:00
2 | 1 | apple | 3 | 2016/03/10 08:00
3 | 1 | orange | 5 | 2016/03/10 14:00
4 | 3 | apple | 8 | 2016/03/11 09:30
5 | 1 | apple | 8 | 2016/03/12 09:30
Is it possible to get customer transactions by date from these transaction tables by the customer?
More specific, I want to get customer Id:1 transactions, ordered by date;
This is what I want.
Id| transactionType|customer | type | amount | date
=====================================================================
1 | greengrocery | 1 | banana | 1 | 2016/03/10 08:00
2 | greengrocery | 1 | apple | 3 | 2016/03/10 08:00
2 | bakery | 1 | bread | 2 | 2016/03/10 11:00
3 | bakery | 1 | baguette| 2 | 2016/03/10 11:00
4 | greengrocery | 1 | orange | 5 | 2016/03/10 14:00
5 | greengrocery | 1 | apple | 8 | 2016/03/12 09:30
You basically need two joined queries unioned together, like this:
SELECT * FROM (
SELECT t.id,'bakery' as TransType,s.id as customer,t.type,t.amount,t.date
FROM customer s
INNER JOIN bakery t ON(s.id = t.customer)
UNION ALL
SELECT t.id,'greengrocery' as TransType,s.id as customer,t.type,t.amount,t.date
FROM customer s
INNER JOIN greengrocery t ON(s.id = t.customer)) tt
WHERE tt.customer = 1
order by tt.date
Basically, you don't even need to select from customer because your are not using the name value, so it can be done like this:
SELECT * FROM (
SELECT t.id,'bakery' as TransType,t.customer as customer,t.type,t.amount,t.date
FROM bakery t
UNION ALL
SELECT t.id,'greengrocery' as TransType,t.customer as customer,t.type,t.amount,t.date
FROM greengrocery t) tt
WHERE tt.customer = 1
order by tt.date
You'd be better off having one table for transactions with a category field
id | category_id | customer | type | amount | date
============================================
1 | 1 | 3 | bread | 1 | 2016/03/10 10:00
And then a category table like:
id | category_name |
====================
1 | bakery
You can then do a join like so:
SELECT transactions.id, category_name AS transactionType, customer.name, transactions.type, tranasactions.amount, transactions.date
FROM transactions
LEFT JOIN categories ON transactions.category_id=categories.id
LEFT JOIN customers ON transactions.customer=customer.id
Use UNION ALL to generate the rows and use a variable to generate the new id.
SET #id = 0;
( SELECT #id:=#id+1 id, 'bakery', customer, type, amount, date
FROM bakery_transactions
WHERE customer = 1 )
UNION ALL
( SELECT #id:=#id+1 id, 'greengrocery', customer, type, amount, date
FROM greengrocery_transactions
WHERE customer = 1 )
ORDER BY date
I have two tables.
Orders (o)
id | buyer
1 | Joe
2 | Ann
3 | Sue
And Order_Items (oi)
id | master_record_id | stock number | qty
1 | 1 | 1234 | 1
2 | 1 | 7890 | 1
3 | 1 | 4987 | 1
4 | 2 | 1234 | 1
5 | 2 | 7890 | 1
6 | 2 | 4987 | 1
7 | 3 | 1234 | 1
8 | 3 | 7890 | 1
9 | 3 | 4987 | 1
Table oi is dependable on orders by oi.master_record_id. Every record (id) in table oi is a unique one (there is no storing of which item this is on an order) so for the example I have 3 orders each with 3 lines and in table oi I have 9 records with ids 1 to 9.
My question is what query should I use to be able to export the orders (a line for each ordered item) where each oi has an index based on the order it is connected to:
Example output:
o.id | o.buyer | Required: Item on Order | oi.stock_number
1 | Joe | 1 | 1234
1 | Joe | 2 | 7890
1 | Joe | 3 | 4987
2 | Ann | 1 | 1234
2 | Ann | 2 | 7890
2 | Ann | 3 | 4987
3 | Sue | 1 | 1234
3 | Sue | 2 | 7890
3 | Sue | 3 | 4987
Many Thanks,
Dani
SELECT id,buyer,Item_on_Order,stock_number
FROM
(SELECT O.`id` as id,
O.`buyer` as buyer,
(#row_num := IF(#category=O.`id`,#row_num+1,1)) as Item_on_Order,
OI.`stock_number` as stock_number,
#category := O.`id` AS Temp
FROM
Orders O
INNER JOIN Order_Items OI ON O.`id` = OI.`master_record_id`)T
Hope this helps.
In MySQL, you can use "user variables" to generate row numbers. Generate row numbers for your oi records and join with your Orders table to get your results. Here is a good blog post on how to achieve row numbering in MySQL.