Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have two tables 'tbl_orders' and 'tbl_instore'. tbl_orders have 'sel_product_qty' which I want to date wise SUM and table(tbl_instore) have 'inst_prod_qty' and 'chln_amount' which I calculate and want to get purchased unit price. But when I use join query on those two tables, the SUM(sel_product_qty) produces double, triple and quadruple amount from the expected result.
Sample data tables are..
Table "tbl_order":
+----------+------------+------------+-----------------+---------------+--------------------+
| order_id | ord_det_id | product_id | sel_product_qty | selling_price | order_date_time |
+----------+------------+------------+-----------------+---------------+--------------------+
| 3 | 1 | 4 | 50 | 67.82 | 2019-03-21 21:52:21|
| 4 | 1 | 1 | 100 | 37.88 | 2019-03-21 21:52:21|
| 5 | 2 | 4 | 120 | 67.82 | 2018-03-23 00:02:36|
| 6 | 3 | 3 | 300 | 123.67 | 2019-03-23 00:04:38|
| 7 | 3 | 2 | 50 | 76.28 | 2019-03-23 00:04:38|
| 8 | 4 | 4 | 50 | 67.82 | 2019-03-24 12:13:06|
| 9 | 4 | 2 | 100 | 76.28 | 2019-03-24 12:13:06|
| 10 | 5 | 1 | 10 | 37.88 | 2019-03-25 12:56:40|
| 11 | 5 | 4 | 7 | 67.82 | 2019-03-25 12:56:40|
| 12 | 6 | 4 | 23 | 67.82 | 2019-03-29 00:29:14|
| 13 | 6 | 2 | 25 | 76.28 | 2019-03-29 00:29:14|
| 16 | 7 | 1 | 120 | 37.88 | 2019-04-14 16:51:10|
| 17 | 7 | 3 | 90 | 123.67 | 2019-04-14 16:51:11|
| 18 | 8 | 1 | 100 | 66.95 | 2019-04-22 23:30:39|
| 19 | 8 | 2 | 22 | 70.04 | 2019-04-22 23:30:39|
+----------+------------+------------+-----------------+---------------+--------------------+
Table "tbl_instore":
+----------+----------+------------+---------------+-------------+--------------------+
| in_st_id | s_inv_id | product_id | inst_prod_qty | chln_amount | instore_date_time |
+----------+----------+------------+---------------+-------------+--------------------+
| 1 | 1 | 1 | 1000 | 65852 | 2/14/2018 17:28 |
| 14 | 9 | 1 | 100 | 6400 | 4/26/2019 8:26 |
| 3 | 2 | 1 | 2000 | 58885 | 3/19/2019 17:32 |
| 5 | 3 | 1 | 100 | 3588 | 3/19/2019 17:35 |
| 11 | 7 | 1 | 1000 | 65000 | 4/22/2019 23:17 |
| 9 | 5 | 1 | 100 | 6345 | 4/20/2019 0:13 |
| 12 | 8 | 2 | 100 | 7800 | 4/22/2019 23:20 |
| 8 | 4 | 2 | 2000 | 144567 | 3/23/2019 0:04 |
| 7 | 4 | 3 | 1000 | 121665 | 3/23/2019 0:04 |
| 13 | 8 | 3 | 150 | 32000 | 4/22/2019 23:20 |
| 15 | 9 | 3 | 100 | 19000 | 4/26/2019 8:26 |
| 10 | 6 | 4 | 1000 | 88022 | 4/20/2019 0:16 |
| 6 | 3 | 4 | 100 | 6582 | 3/19/2019 17:35 |
| 4 | 2 | 4 | 1000 | 65882 | 3/19/2019 17:32 |
| 2 | 1 | 4 | 5000 | 359877 | 2/14/2018 17:28 |
+----------+----------+------------+---------------+-------------+--------------------+
The following query I have currently tried:
SELECT SUM(tbl_orders.sel_product_qty) AS `sel_prod_qty`,
(SUM(chln_amount) / SUM(inst_prod_qty)) AS `pur_uni_price`,
date_format(`order_date_time`, '%M-%Y') as `month`,
tbl_orders.product_id AS `product_id`
FROM tbl_orders
INNER JOIN tbl_instore ON tbl_instore.product_id = tbl_orders.product_id
WHERE YEAR(`order_date_time`)= '2019'
GROUP BY `month`, `product_id`;
Which return the following result:
+--------------+---------------+--------+------------+
| sel_prod_qty | pur_uni_price | month | product_id |
+--------------+---------------+--------+------------+
| 1320 | 47.923256 | Apr-19 | 1 |
+--------------+---------------+--------+------------+
| 44 | 72.555714 | Apr-19 | 2 |
+--------------+---------------+--------+------------+
| 270 | 138.132 | Apr-19 | 3 |
+--------------+---------------+--------+------------+
| 660 | 47.923256 | Mar-19 | 1 |
+--------------+---------------+--------+------------+
| 350 | 72.555714 | Mar-19 | 2 |
+--------------+---------------+--------+------------+
| 900 | 138.132 | Mar-19 | 3 |
+--------------+---------------+--------+------------+
| 520 | 73.290563 | Mar-19 | 4 |
+--------------+---------------+--------+------------+
If I run the query individually without JOIN, I will get SUM(sel_prod_qty) value as following (also my expected result should be):
+--------------+---------------+--------+------------+
| sel_prod_qty | pur_uni_price | month | product_id |
+--------------+---------------+--------+------------+
| 220 | 47.923256 | Apr-19 | 1 |
+--------------+---------------+--------+------------+
| 22 | 72.555714 | Apr-19 | 2 |
+--------------+---------------+--------+------------+
| 90 | 138.132 | Apr-19 | 3 |
+--------------+---------------+--------+------------+
| 110 | 47.923256 | Mar-19 | 1 |
+--------------+---------------+--------+------------+
| 175 | 72.555714 | Mar-19 | 2 |
+--------------+---------------+--------+------------+
| 300 | 138.132 | Mar-19 | 3 |
+--------------+---------------+--------+------------+
| 130 | 73.290563 | Mar-19 | 4 |
+--------------+---------------+--------+------------+
So, my question is, why does my query return SUM(sel_product_qty) double, triple and quadruple amount from the expected result?
Try this:
SELECT `sel_prod_qty`,
`pur_uni_price`,
`month`,
orders.product_id AS `product_id`
FROM
(SELECT product_id,SUM(sel_product_qty) AS `sel_prod_qty`,
DATE_FORMAT(`order_date_time`, '%M-%Y') AS `month`
FROM tbl_orders
WHERE YEAR(`order_date_time`)='2019'
GROUP BY `month`, `product_id`) orders
INNER JOIN
(SELECT product_id,(SUM(chln_amount) / SUM(inst_prod_qty)) AS `pur_uni_price`
FROM tbl_instore GROUP BY product_id) instore
ON orders.product_id=instore.product_id;
I created two sub-query from those tables that you joined because what doesn't work is that you are doing INNER JOIN ... ON tbl_instore.product_id = tbl_orders.product_id. Which if you refer back to your table, those value were repeated multiple times in both tables. Therefore, this sub-queries will perform the mathematical operation and the outer query will only return the results from it.
The pur_uni_price field is not an aggregate field anymore, therefore you need to add it on your GROUP BY. See code below
SELECT SUM(tbl_orders.sel_product_qty) AS `sel_prod_qty`,
(SUM(chln_amount) / SUM(inst_prod_qty)) AS `pur_uni_price`,
date_format(`order_date_time`, '%M-%Y') as `month`,
tbl_orders.product_id AS `product_id`
FROM tbl_orders
INNER JOIN tbl_instore ON tbl_instore.product_id = tbl_orders.product_id
WHERE YEAR(`order_date_time`)= '2019'
GROUP BY `month`, `product_id`, (SUM(chln_amount) / SUM(inst_prod_qty))
Related
I have column user and rating.
SELECT rating.idUser, user.nmUser, rating.idBengkel, rating.nilai FROM `rating`
JOIN user on rating.idUser = user.idUser
WHERE rating.idBengkel=1 or rating.idBengkel=2
Result :
+--------+---------------------------+-----------+-------+
| idUser | nmUser | idBengkel | nilai |
+--------+---------------------------+-----------+-------+
| 10 | Hudson mas77 | 1 | 5 |
| 11 | Vina Nurfadzilah | 1 | 5 |
| 12 | Angelica Amartya | 1 | 5 |
| 15 | Syahrul K | 1 | 4 |
| 27 | Ashar Murdihastomo | 1 | 5 |
| 28 | Eril Obeit Choiri | 1 | 2 |
| 29 | Ariyadi | 1 | 3 |
| 30 | Robertus Dwian Augusta | 1 | 4 |
| 31 | Irfan Setiaji | 1 | 4 |
| 33 | Baby Ayuna | 1 | 5 |
| 9 | Nur k hamid | 2 | 5 |
| 10 | Hudson mas77 | 2 | 5 |
| 13 | Yuana Putra | 2 | 4 |
| 14 | Nanda Aulia Irza Ramadhan | 2 | 4 |
| 26 | taufiq rahman | 2 | 5 |
| 27 | Ashar Murdihastomo | 2 | 5 |
| 28 | Eril Obeit Choiri | 2 | 5 |
| 30 | Robertus Dwian Augusta | 2 | 4 |
| 44 | halim budiono | 2 | 1 |
+--------+---------------------------+-----------+-------+
When i try to get similar records using this query
SELECT rating.idUser, user.nmUser FROM rating
JOIN user
ON rating.idUser = user.idUser
WHERE rating.idBengkel = 1 and rating.idUser
IN (SELECT rating.idUser from rating WHERE rating.idBengkel = 2)
ORDER by idUser
Result :
+-----------+------------------------+
| idUser | nmUser |
+-----------+------------------------+
| 10 | Hudson mas77 |
| 27 | Ashar Murdihastomo |
| 28 | Eril Obeit Choiri |
| 30 | Robertus Dwian Augusta |
+-----------+------------------------+
The result work fine, but I want show column 'nilai' as ItemX and ItemY. Those are user similar data. In this case I have 4 similar user who rate on idBengkel=1 and idBengkel=2 as the results above. I want it like the table below.
+--------+------------------------+-------+-------+
| idUser | nmUser | ItemX | ItemY |
+--------+------------------------+-------+-------+
| 10 | Hudson mas77 | 5 | 5 |
| 27 | Ashar Murdihastomo | 5 | 5 |
| 28 | Eril Obeit Choiri | 2 | 5 |
| 30 | Robertus Dwian Augusta | 4 | 4 |
+--------+------------------------+-------+-------+
I need solution for this and i was trying with this solution in https://stackoverflow.com/a/7976379/12396302 but it resulting more than one row. Please help me, I cant implement that query's solution. Regards!
I think you need below query -
SELECT rating.idUser,
user.nmUser,
MAX(CASE WHEN rating.idBengkel = 1 THEN rating.nilai END) ItemX,
MAX(CASE WHEN rating.idBengkel = 2 THEN rating.nilai END) ItemY,
FROM `rating`
JOIN user on rating.idUser = user.idUser
WHERE rating.idBengkel IN (1, 2)
GROUP BY rating.idUser,
user.nmUser
This question already has answers here:
MySQL - Selecting a Column not in Group By
(4 answers)
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 5 years ago.
I am running below queries on my table.
Table:
+----+-------------+--------------+----------------------------+------------+--------+
| id | Qty_holding | Qty_reserved | created | tokenid_id | uid_id |
+----+-------------+--------------+----------------------------+------------+--------+
| 1 | 10 | 0 | 2018-01-18 10:52:14.957027 | 1 | 1 |
| 2 | 20 | 0 | 2018-01-18 11:20:08.205006 | 8 | 1 |
| 3 | 110 | 0 | 2018-01-18 11:20:21.496318 | 14 | 1 |
| 4 | 10 | 0 | 2018-01-23 14:26:49.124607 | 1 | 2 |
| 5 | 3 | 0 | 2018-01-23 15:00:26.876623 | 11 | 2 |
| 6 | 7 | 0 | 2018-01-23 15:08:41.887240 | 11 | 2 |
| 7 | 11 | 0 | 2018-01-23 15:22:48.424224 | 11 | 2 |
| 8 | 15 | 0 | 2018-01-23 15:24:03.419907 | 11 | 2 |
| 9 | 19 | 0 | 2018-01-23 15:24:26.531141 | 11 | 2 |
| 10 | 23 | 0 | 2018-01-23 15:27:11.549538 | 11 | 2 |
| 11 | 27 | 0 | 2018-01-23 15:27:24.162944 | 11 | 2 |
| 12 | 7.7909428 | 0.11459088 | 2018-01-23 15:27:24.168643 | 1 | 2 |
| 13 | 3 | 0 | 2018-01-23 15:36:51.412340 | 14 | 2 |
| 14 | 7.5585988 | 0.11459088 | 2018-01-23 15:36:51.417177 | 1 | 2 |
| 15 | 6 | 0 | 2018-01-24 08:43:46.635069 | 14 | 2 |
| 16 | 7.3262548 | 0.11459088 | 2018-01-24 08:43:46.639984 | 1 | 2 |
| 17 | 9 | 0 | 2018-01-24 10:09:08.207816 | 14 | 2 |
| 18 | 7.0939108 | 0.11459088 | 2018-01-24 10:09:08.212842 | 1 | 2 |
| 19 | 6 | 3 | 2018-01-24 13:43:08.929586 | 14 | 2 |
| 20 | 3 | 6 | 2018-01-24 14:49:56.960112 | 14 | 2 |
| 21 | 0 | 9 | 2018-01-24 14:50:33.423671 | 14 | 2 |
| 22 | 30 | 9 | 2018-01-24 14:51:14.865453 | 14 | 2 |
| 23 | 4.7704708 | 0.11459088 | 2018-01-24 14:51:14.870256 | 1 | 2 |
| 24 | 27 | 12 | 2018-01-24 14:56:56.914009 | 14 | 2 |
| 25 | 24 | 15 | 2018-01-24 14:57:56.475939 | 14 | 2 |
| 26 | 21 | 15 | 2018-01-24 14:58:06.750903 | 14 | 2 |
| 27 | 18 | 15 | 2018-01-24 15:02:43.203878 | 14 | 2 |
| 28 | 4.7705074 | 0.11459088 | 2018-01-24 15:02:43.224901 | 1 | 2 |
| 29 | 24 | 0 | 2018-01-24 15:03:40.421943 | 11 | 2 |
| 30 | 4.9535074 | 0.11459088 | 2018-01-24 15:03:40.441552 | 1 | 2 |
| 31 | 1 | 0 | 2018-01-26 10:35:33.173801 | 18 | 2 |
| 32 | 10 | 15 | 2018-01-26 12:46:03.780807 | 14 | 2 |
+----+-------------+--------------+----------------------------+------------+--------+
Query 1:
select uid_id
, tokenid_id
, max(created) as max_created
from accounts_userholding
group
by uid_id
, tokenid_id
+--------+------------+----------------------------+
| uid_id | tokenid_id | max_created |
+--------+------------+----------------------------+
| 1 | 1 | 2018-01-18 10:52:14.957027 |
| 1 | 8 | 2018-01-18 11:20:08.205006 |
| 1 | 14 | 2018-01-18 11:20:21.496318 |
| 2 | 1 | 2018-01-24 15:03:40.441552 |
| 2 | 11 | 2018-01-24 15:03:40.421943 |
| 2 | 14 | 2018-01-26 12:46:03.780807 |
| 2 | 18 | 2018-01-26 10:35:33.173801 |
+--------+------------+----------------------------+
Query 2:
select uid_id
, Qty_holding
, Qty_reserved tokenid_id
, max(created) as max_created
from accounts_userholding
group
by uid_id
, tokenid_id
+--------+-------------+--------------+------------+----------------------------+
| uid_id | Qty_holding | Qty_reserved | tokenid_id | max_created |
+--------+-------------+--------------+------------+----------------------------+
| 1 | 10 | 0 | 1 | 2018-01-18 10:52:14.957027 |
| 1 | 20 | 0 | 8 | 2018-01-18 11:20:08.205006 |
| 1 | 110 | 0 | 14 | 2018-01-18 11:20:21.496318 |
| 2 | 10 | 0 | 1 | 2018-01-24 15:03:40.441552 |
| 2 | 3 | 0 | 11 | 2018-01-24 15:03:40.421943 |
| 2 | 3 | 0 | 14 | 2018-01-26 12:46:03.780807 |
| 2 | 1 | 0 | 18 | 2018-01-26 10:35:33.173801 |
+--------+-------------+--------------+------------+----------------------------+
The Qty_holding value in above is not corresponding to latest date. For instance for tokenid_id 14 and uid_id as 2 latest record is
| 32 | 10 | 15 | 2018-01-26 12:46:03.780807 | 14 | 2 |
But above query is giving qty_holding as 3.
Any insights in functioning of mysql will be helpful . Thanks!
As a rule of thumb: When you mix normal columns with aggregate functions in SELECT, you need to use GROUP BY. Do not use GROUP BY when you do not have normal columns and aggregate functions in SELECT.
The thing to put into the GROUP BY, is all from SELECT but the aggregate functions (and possible constants).
As an example if you have a query:
select a, substring(b,3), 'x', max(y)
from yourtable
You need to use GROUP BY. You leave out 'x' as it is a constant and you leave out the aggregate function. The rest goes to the GROUP BY.
select a, substring(b,3), 'x', max(y)
from yourtable
group by a, substring(b,3)
Previous MySQL versions allowed quite liberal use of GROUP BY resulting quite often just bad/incorrect code.
I have 3 tables :
60.000 record Invoice
100.000 record Exchange Rate based on current date
25 record Currency Symbol
table_invoice:
+-----------+-------------+----------+--------------+
| invoice_id | currency_id | amount | invoice_date |
+------------+-------------+----------+--------------+
| 1 | 2 | 10 | 4/28/2016 |
| 2 | 3 | 30 | 4/29/2016 |
| 3 | 4 | 50 | 4/30/2016 |
| 4 | 2 | 40 | 6/18/2016 |
| 5 | 6 | 25 | 6/20/2016 |
| 6 | 7 | 87 | 6/25/2016 |
| 7 | 4 | 100 | 6/29/2016 |
| 8 | 9 | 45 | 7/14/2016 |
| 9 | 2 | 71 | 9/27/2016 |
| 60000 | 3 | 430 | 1/18/2017 |
+------------+-------------+----------+--------------+
and
table_exchange_rate:
+-----------------+-------------+---------------+--------------------+
| exchange_rate_id | currency_id | exchange_rate | exchange_rate_date |
+------------------+-------------+---------------+--------------------+
| 1 | 2 | 13.352 | 4/25/2016 |
| 2 | 3 | 10.195 | 4/25/2016 |
| 3 | 4 | 14.390 | 4/25/2016 |
| 4 | 5 | 1.720 | 4/25/2016 |
| 5 | 6 | 118 | 4/25/2016 |
| 6 | 7 | 9.468 | 4/25/2016 |
| 7 | 2 | 13.125 | 6/15/2016 |
| 8 | 3 | 10.520 | 6/25/2016 |
| 9 | 4 | 14.800 | 6/25/2016 |
| 10 | 5 | 1.800 | 6/25/2016 |
| 11 | 6 | 120 | 6/25/2016 |
| 12 | 7 | 9.320 | 6/25/2016 |
| 100000 | 7 | 9.500 | 6/25/2016 |
+------------------+-------------+---------------+--------------------+
and
reference_currency:
+-----------------+---------------+-----------------------+
| currency_id |currency_symbol| currency_name |
+------------------+---------------+-----------------------+
| 1 | USD | US Dollar |
| 2 | AUD | Australian Dollar |
| 3 | EUR | Euro |
| 4 | HKD | Hong Kong Dollar |
| 5 | JPY | Japan Yen |
| 6 | SGD | Singapore Dollar |
| 7 | MYR | Malaysian Ringgit |
| 8 | CHF | Swiss Franc |
| 9 | THB | Thailand Baht |
| 10 | GBP | Great Britain Pounds |
| 11 | SEK | Swedish Krona |
| 12 | CNY | China Yuan |
| 25 | SAR | Saudi Arabian Riyal |
+------------------+-------------+-------------------------+
When I run:
SELECT
a.invoice_id AS 'INVOICE_ID',
a.currency_id AS 'CURRENCY_ID',
a.amount AS 'AMOUNT',
a.invoice_date AS 'INVOICE_DATE',
(
SELECT b.exchange_rate FROM table_exchange_rate b
WHERE b.exchange_rate_date <= a.invoice_date AND b.currency_id = a.currency_id
ORDER BY b.exchange_rate_date DESC LIMIT 1) AS 'EXCHANGE RATE'
)
FROM table_invoice a
Result:
`+------------+-------------+----------+--------------+--------------+
| INVOICE_ID | CURRENCY_ID | AMOUNT | INVOICE_DATE | EXCHANGE_RATE |
+------------+-------------+----------+--------------+---------------+
| 1 | 2 | 10 | 4/28/2016 | 13.352
| 2 | 3 | 30 | 4/29/2016 | 10.195
| 3 | 4 | 50 | 4/30/2016 | 14.390
| 4 | 2 | 40 | 6/18/2016 | 13.125
| 5 | 6 | 25 | 6/20/2016 | 118
| 6 | 7 | 87 | 6/25/2016 | 9.320
`
and the result works fine but it is very slow(approximately > 60sec) with 60k record(table_invoice) looping on 100k record(table_exchange_rate) finding exchange rate at the current date,
If invoice_date can't match with exchange_rate_date or the user doesn't input the exchange rate on the app, it will using exchange rate the latest record already in input before the current date (b.exchange_rate_date <= a.invoice_date AND b.currency_id = a.currency_id)
Can I speed up this query or there any other options? Thank you..
Try same thing with JOIN
select a.invoice 'invoice_id', a.currency_id 'curency_id', a.amount 'amount', a.invoice_date 'invoice_date', ter.exchange_rate ' exchange_rate'
from table_invoice ti
left join table_exchange_rate ter on ter.currency_id = ti.currency_id and ter.exchange_rate_date <= ti.invoice_date
I have three tables, mess_stock, mess_voucher, add_grocery.
Mess_stock table is below,
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
| sno | voucher_id | particular_name | opening_balance | inward | outward | balance | pay_amount | pay_type |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
| 49 | 5 | 4 | 100 | 10 | 100 | 10 | 10.00 | 1 |
| 50 | 17 | 5 | 111 | 10 | 20 | 101 | 60.00 | 1 |
| 51 | 7 | 3 | 123 | 2 | 1 | 124 | 300.00 | 1 |
| 52 | 7 | 1 | 123 | 20 | 20 | 123 | 500.00 | 2 |
| 53 | 14 | 8 | 100 | 5 | 95 | 10 | 60.00 | 2 |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
Mess_voucher table is below
+------------+--------------+--------------+
| voucher_id | voucher_name | voucher_date |
+------------+--------------+--------------+
| 5 | VG1001 | 2015-02-19 |
| 6 | VG1001 | 2015-02-20 |
| 7 | VG1002 | 2015-02-20 |
| 8 | VG1002 | 2015-02-19 |
| 9 | MS1001 | 2015-02-20 |
| 10 | VG10012 | 2015-02-19 |
| 11 | 0 | 2015-02-23 |
| 12 | 1 | 2015-02-24 |
| 13 | MS1001 | 2015-02-25 |
| 14 | MS1001 | 2015-02-28 |
| 15 | VG1003 | 2015-02-28 |
| 16 | MS1001 | 2015-02-19 |
| 17 | MS1001 | 2015-02-21 |
+------------+--------------+--------------+
Add_grocery table is below
+-----+-----------------+------------------+
| sno | particular_name | particular_price |
+-----+-----------------+------------------+
| 1 | Rice | 25.00 |
| 3 | Mango | 150.00 |
| 4 | Coconut | 22.00 |
| 5 | Banana | 6.00 |
| 6 | Raddish | 12.00 |
| 7 | Apple | 150.00 |
| 8 | Pumkin | 12.00 |
+-----+-----------------+------------------+
I want to group the sum of pay_amount of mess_stock table. I have used the below query
SELECT opening_balance AS ope_stock,
balance AS clo_stock,
SUM(IF(pay_type = 1, pay_amount, 0)) mess_pay,
SUM(IF(pay_type=2, pay_amount, 0)) est_pay
FROM mess_stock;
That works fine. The particular_name is the auto increment id of add_grocery table. I need the inward outward amount total. For example the inward amount 10 means it has to get the particular_price from add_grocery using the particular_name provided in the mess_stock table, similarly I need all the answer. And I want to sort that by date wise. The date of the entry is stored in the mess_voucher table that is connected to mess_stock table.
Try this it will work :
Use Inner Join :
SELECT t2.`particular_name`,t1.`inward`,t1.`outward`,t2.`particular_price`,t3.`voucher_date` from Mess_stock t1 JOIN Add_grocery t2 ON t1.`particular_name`=t2.`sno` JOIN Mess_voucher t3 ON t3.`voucher_id`=t1.`voucher_id` ORDER BY t3.`voucher_date` DESC
good morning. I have this table:
mysql> select * from Data;
+---------------------------+--------+-------+
| affyId | exptId | level |
+---------------------------+--------+-------+
| 31315_at | 3 | 250 |
| 31324_at | 3 | 91 |
| 31325_at | 1 | 191 |
| 31325_at | 2 | 101 |
| 31325_at | 4 | 51 |
| 31325_at | 5 | 71 |
| 31325_at | 6 | 31 |
| 31356_at | 3 | 91 |
| 31362_at | 3 | 260 |
| 31510_s_at | 3 | 257 |
| 5321_at | 4 | 90 |
| 5322_at | 4 | 90 |
| 5323_at | 4 | 90 |
| 5324_at | 3 | 57 |
| 5324_at | 4 | 90 |
| 5325_at | 4 | 90 |
| AFFX-BioB-3_at | 3 | 97 |
| AFFX-BioB-5_at | 3 | 20 |
| AFFX-BioB-M_at | 3 | 20 |
| AFFX-BioB-M_at | 5 | 214 |
| AFFX-BioB-M_at | 7 | 20 |
| AFFX-BioB-M_at | 8 | 40 |
| AFFX-BioB-M_at | 9 | 20 |
| AFFX-HSAC07/X00351_M_at | 3 | 86 |
| AFFX-HUMBAPDH/M33197_3_st | 3 | 277 |
| AFFX-HUMTFFR/M11507_at | 3 | 90 |
| AFFX-M27830_3_at | 3 | 271 |
| AFFX-MurIL10_at | 3 | 8 |
| AFFX-MurIL10_at | 5 | 8 |
| AFFX-MurIL10_at | 6 | 4 |
| AFFX-MurIL2_at | 3 | 20 |
| AFFX-MurIL4_at | 5 | 78 |
| AFFX-MurIL4_at | 6 | 20 |
| U95-32123_at | 1 | 128 |
| U95-32123_at | 2 | 128 |
| U98-40474_at | 1 | 57 |
| U98-40474_at | 2 | 57 |
+---------------------------+--------+-------+
37 rows in set (0.00 sec)
If I wanna look for the average expression level (level) of each array probe (affyId) across all experiments, I do SELECT affyId, AVG(level) AS average FROM Data GROUP BY affyId;
However, I can't figure out how to look for the average expression level of each array probe (affyId) for each experiment... It must be something similar to the last query, but I don't obtain good results... any help?
PD: someone told me I should give some reputation or click to some green button if somebody solves my question... Is it right? How do I do it? I'm pretty new on this website...
This shows the average for every affyId:
SELECT affyId, AVG(level) AS average FROM Data GROUP BY affyId
This the average for every exptId:
SELECT exptId, AVG(level) AS average FROM Data GROUP BY exptId
and this the average for every exptId in every affyId:
SELECT affyId, exptId, AVG(level) AS average FROM Data GROUP BY exptId, affyId
Just add that to the group by clause
SELECT affyId, exptId, AVG(level) AS average
FROM Data
GROUP BY affyId, exptId;