I have data like this
table t_prod
+---------+----------+-------------+-----------+----------+
|t_prod_id|t_prod_lot|t_prod_sublot|t_prod_card|t_prod_qty|
+---------+----------+-------------+-----------+----------+
| 4 | R001 | 1 | 1 | 6000 |
| 5 | R001 | 1 | 2 | 6000 |
| 6 | R001 | 1 | 3 | 6000 |
| 10 | R001 | 1 | 4 | 6000 |
| 11 | R001 | 1 | 5 | 6000 |
| 12 | R001 | 1 | 6 | 6000 |
| 13 | R001 | 2 | 1 | 6000 |
| 34 | R001 | 2 | 2 | 6000 |
| 36 | R001 | 2 | 3 | 2000 |
+---------+----------+-------------+-----------+----------+
and i want result like this when i select t_prod_lot = R001. count t_prod_sublot when same value and sum t_prod_qty when t_prod_sublot same value
+---------+----------+-------------+-----------+----------+--------------------+---------------+
|t_prod_id|t_prod_lot|t_prod_sublot|t_prod_card|t_prod_qty|count(t_prod_sublot)|sum(t_prod_qty)|
+---------+----------+-------------+-----------+----------+--------------------+---------------+
| 4 | R001 | 1 | 1 | 6000 | 6 | 36000 |
| 5 | R001 | 1 | 2 | 6000 | 6 | 36000 |
| 6 | R001 | 1 | 3 | 6000 | 6 | 36000 |
| 10 | R001 | 1 | 4 | 6000 | 6 | 36000 |
| 11 | R001 | 1 | 5 | 6000 | 6 | 36000 |
| 12 | R001 | 1 | 6 | 6000 | 6 | 36000 |
| 13 | R001 | 2 | 1 | 6000 | 3 | 14000 |
| 34 | R001 | 2 | 2 | 6000 | 3 | 14000 |
| 36 | R001 | 2 | 3 | 2000 | 3 | 14000 |
+---------+----------+-------------+-----------+----------+--------------------+---------------+
what is the query to produce this result ?
Sql Fiddle
Here's one option using a join with a subquery to count and sum the values:
select *
from t_prod t join (
select t_prod_lot, t_prod_sublot,
count(t_prod_card) t_prod_card_cnt,
sum(t_prod_qty) t_prod_qty_sum
from t_prod
group by t_prod_lot, t_prod_sublot
) t2 on t.t_prod_lot = t2.t_prod_lot and t.t_prod_sublot = t2.t_prod_sublot
Updated SQL Fiddle
Related
I want to count repeated values in rows one by one without breaks. If it breaks with NULL value, then count it again from zero.
It is a simple table consists from action_id and event_code.
| id | action_id | event_code |
--------------------------------
| 1 | 1 | 100 |
| 2 | 1 | 200 |
| 3 | 1 | 300 |
| 4 | 2 | 100 |
| 5 | 2 | 300 |
| 6 | 3 | 100 |
| 7 | 3 | 200 |
| 8 | 3 | 400 |
Then it groups by action_id (with SQL query) to:
| action_id | c100 | c200 | c300 | c400 |
-----------------------------------------
| 1 | 1 | 1 | 1 | 0 |
| 2 | 1 | 0 | 1 | 0 |
| 3 | 1 | 1 | 0 | 1 |
and as a result:
| action_id | c100 | e100_r | c200 | e200_r | c300 | e300_r | c400 | e400_r |
-----------------------------------------------------------------------------
| 1 | 1 | 3 | 1 | 1 | 1 | 2 | 0 | 0 |
| 2 | 1 | 3 | 0 | 0 | 1 | 2 | 0 | 0 |
| 3 | 1 | 3 | 1 | 1 | 0 | 0 | 1 | 1 |
or
| id | action_id | event_code | rep_count |
--------------------------------------------
| 1 | 1 | 100 | 3 |
| 2 | 1 | 200 | 1 |
| 3 | 1 | 300 | 2 |
| 4 | 2 | 100 | 3 |
| 5 | 2 | 300 | 2 |
| 6 | 3 | 100 | 3 |
| 7 | 3 | 200 | 1 |
| 8 | 3 | 400 | 1 |
Is it possible?
EDIT 1:
c100 - couunter of event_code == 100 and e100_r - repeats of event_code == 100
EDIT 2:
Rows breaks by zero (0)
How to calculate opening and closing stocks for particular date range
I want to calculate opening and closing stocks for items for a provided date range.
I have tables as follows,
global_items:
+-----+--------+
| id | name |
+-----+--------+
| 1 | item 1 |
+-----+--------+
| 2 | item 2 |
+-----+--------+
| 3 | item 3 |
+-----+--------+
| 4 | item 4 |
+-----+--------+
| ... | ... |
+-----+--------+
my_items:
+-----+----------------+-----------+
| id | global_item_id | outlet_id |
+-----+----------------+-----------+
| 1 | 1 | 7 |
+-----+----------------+-----------+
| 2 | 2 | 7 |
+-----+----------------+-----------+
| 3 | 3 | 7 |
+-----+----------------+-----------+
| 4 | 4 | 7 |
+-----+----------------+-----------+
| ... | ... | ... |
+-----+----------------+-----------+
my_item_stocks:
+-----+------------+---------+---------------+-----------+---------------+-------+
| id | my_item_id | size_id | purchase_rate | sale_rate | opening_stock | stock |
+-----+------------+---------+---------------+-----------+---------------+-------+
| 1 | 1 | 10 | 100 | 200 | 0 | 0 |
+-----+------------+---------+---------------+-----------+---------------+-------+
| 2 | 1 | 11 | 100 | 200 | 0 | 5 |
+-----+------------+---------+---------------+-----------+---------------+-------+
| 3 | 2 | 10 | 100 | 200 | 1.05 | 1.05 |
+-----+------------+---------+---------------+-----------+---------------+-------+
| 4 | 3 | 12 | 100 | 200 | 10 | 10 |
+-----+------------+---------+---------------+-----------+---------------+-------+
| ... | ... | ... | 100 | 200 | 0 | 1 |
+-----+------------+---------+---------------+-----------+---------------+-------+
sizes:
+-----+--------+----------+
| id | name | quantity |
+-----+--------+----------+
| 10 | 750 ml | 750 |
+-----+--------+----------+
| 11 | 500 ml | 500 |
+-----+--------+----------+
| 12 | 350 ml | 350 |
+-----+--------+----------+
| ... | ... | ... |
+-----+--------+----------+
Then I have sales and purchases tables as follows, adding purchase updates the stock in my_item_stocks and adding sale reduces stock.
purchases:
+-----+------------+-----------+
| id | date | outlet_id |
+-----+------------+-----------+
| 1 | 2018-07-01 | 7 |
+-----+------------+-----------+
| 2 | 2018-07-10 | 7 |
+-----+------------+-----------+
| 3 | 2018-07-19 | 7 |
+-----+------------+-----------+
| ... | ... | ... |
+-----+------------+-----------+
purchase_items:
+-----+-------------+------------+---------+----------+
| id | purchase_id | my_item_id | size_id | quantity |
+-----+-------------+------------+---------+----------+
| 1 | 1 | 1 | 10 | 2 |
+-----+-------------+------------+---------+----------+
| 2 | 1 | 2 | 11 | 5 |
+-----+-------------+------------+---------+----------+
| 3 | 2 | 2 | 10 | 17 |
+-----+-------------+------------+---------+----------+
| 4 | 3 | 2 | 12 | 15 |
+-----+-------------+------------+---------+----------+
| 5 | 3 | 2 | 12 | 10 |
+-----+-------------+------------+---------+----------+
| ... | ... | ... | ... | ... |
+-----+-------------+------------+---------+----------+
sales:
+-----+------------+-----------+
| id | date | outlet_id |
+-----+------------+-----------+
| 1 | 2018-07-01 | 7 |
+-----+------------+-----------+
| 2 | 2018-07-10 | 7 |
+-----+------------+-----------+
| 3 | 2018-07-19 | 7 |
+-----+------------+-----------+
| ... | ... | ... |
+-----+------------+-----------+
sale_items:
+-----+-------------+------------+---------+------+
| id | sale_id | my_item_id | size_id | quantity |
+-----+---------+------------+---------+----------+
| 1 | 1 | 1 | 10 | 1 |
+-----+---------+------------+---------+----------+
| 2 | 1 | 2 | 11 | 2 |
+-----+---------+------------+---------+----------+
| 3 | 2 | 2 | 10 | 10 |
+-----+---------+------------+---------+----------+
| 4 | 3 | 2 | 12 | 5 |
+-----+---------+------------+---------+----------+
| 5 | 3 | 2 | 12 | 2 |
+-----+---------+------------+---------+----------+
| ... | ... | ... | ... | ... |
+-----+---------+------------+---------+----------+
Now for selected date range e.g. from 2018-07-01 to 2018-07-19 I want the output like below,
+-----------+--------------------------+--------------------------+--------------------------+--------------------------+
| Item Name | Opening Stock | Purchase | Sale | Closing Stock |
+ +--------------------------+--------------------------+--------------------------+--------------------------+
| | 750 ml | 500 ml | 350 ml | 750 ml | 500 ml | 350 ml | 750 ml | 500 ml | 350 ml | 750 ml | 500 ml | 350 ml |
+-----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| Item 1 | | | | 2 | 5 | | 1 | 2 | | 3 | 7 | |
+-----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| Item 2 | 1.05 | | | 17 | | | 10 | | | 8.05 | | |
+-----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
+-----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
I am not sql pro, how can I achieve this? I am confused because of lot of related tables, just want right direction.
I am doing this is laravel so please suggest even if there is any way to achieve this using eloquent.
Models: GlobalItem, MyItem, MyItemStock, Size, Purchase, PurchaseItem, Sale, SaleItem
Many thanks!
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.
first table :
table Name : ssc_course;
+---------------+-----------------+------------+
| ssc_course_id | ssc_course_name | qualify_id |
+---------------+-----------------+------------+
| 1 | HSC | 1 |
| 2 | Diploma | 1 |
| 3 | ITI | 1 |
+---------------+-----------------+------------+
second table :
table name :ssc_stream
+---------------+--------------------+-----------------+
| ssc_stream_id | ssc_stream_name | ssc_course_id(fk) |
+---------------+--------------------+-----------------+
| 1 | Science | 1 |
| 2 | Commers | 1 |
| 3 | Arts | 1 |
| 17 | Computer Engg | 2 |
| 18 | Mechanical Engg | 2 |
| 19 | Civil Engg | 2 |
| 20 | Electronics & TELE | 2 |
| 21 | E&TC | 2 |
+---------------+--------------------+-----------------+
third table :(master table)
table name : ssc_college
+------------+--------------+---------------+---------------+-
| ssc_clg_id | clg_login_id | ssc_stream_id | ssc_course_id |
+------------+--------------+---------------+---------------+-
| 9 | 2 | 1 | 1 |
| 10 | 2 | 1 | 1 |
| 11 | 2 | 2 | 1 |
| 12 | 2 | 2 | 1 |
| 13 | 2 | 3 | 1 |
| 14 | 2 | 3 | 1 |
| 15 | 3 | 1 | 1 |
| 16 | 3 | 1 | 1 |
| 17 | 3 | 2 | 1 |
| 18 | 3 | 2 | 1 |
| 19 | 3 | 3 | 1 |
| 20 | 3 | 3 | 1 |
| 21 | 4 | 1 | 1 |
| 22 | 4 | 1 | 1 |
| 23 | 4 | 2 | 1 |
| 24 | 4 | 2 | 1 |
| 25 | 4 | 3 | 1 |
| 26 | 4 | 3 | 1 |
| 27 | 5 | 17 | 2 |
| 28 | 5 | 17 | 2 |
| 29 | 5 | 18 | 2 |
| 30 | 5 | 18 | 2 |
| 31 | 5 | 19 | 2 |
| 32 | 5 | 19 | 2 |
| 33 | 5 | 20 | 2 |
| 34 | 5 | 20 | 2 |
| 35 | 5 | 21 | 2 |
| 36 | 5 | 21 | 2 |
| 38 | 6 | 17 | 2 |
| 39 | 6 | 17 | 2 |
| 40 | 6 | 18 | 2 |
*************************************************************
I am trying to save course,stream id's into ssc_college table.
1 course have multiple stream how can i insert this type of values
ex. course 1-diploma and this course have multiple stream like-
1.comp. engg , 2.mechanical ,3.e&tc i want to add course id and
stream id at a time of third (ssc_college) table.
and multiple stream for multiple course how i can add at a time
multiple parent and multiple child id in mysql table.
Note: stream are dependant of course table.
There are two tables, table1 and table2.
[ table1 ]
+----+------+
| no | time |
+----+------+
| 1 | 1111 |
+----+------+
| 2 | 2222 |
+----+------+
| 3 | 3333 |
+----+------+
| 4 | 4444 |
+----+------+
| 5 | 5555 |
+----+------+
[ table2 ]
+----+-----+----------+------+
| no | idx | name | rank |
+----+-----+----------+------+
| 1 | 1 | Apple | 1 |
+----+-----+----------+------+
| 2 | 1 | Banana | 2 |
+----+-----+----------+------+
| 3 | 1 | Car | 3 |
+----+-----+----------+------+
| 4 | 1 | Dragon | 4 |
+----+-----+----------+------+
| 5 | 1 | Eagle | 5 |
+----+-----+----------+------+
| 6 | 2 | Fire | 2 |
+----+-----+----------+------+
| 7 | 2 | God | 3 |
+----+-----+----------+------+
| 8 | 2 | Hippo | 4 |
+----+-----+----------+------+
| 9 | 3 | Icecream | 1 |
+----+-----+----------+------+
| 10 | 3 | Juice | 3 |
+----+-----+----------+------+
| 11 | 3 | Korea | 4 |
+----+-----+----------+------+
| 12 | 3 | Low | 5 |
+----+-----+----------+------+
| 13 | 4 | Mother | 2 |
+----+-----+----------+------+
| 14 | 4 | News | 3 |
+----+-----+----------+------+
| 15 | 5 | Object | 1 |
+----+-----+----------+------+
And, I want to make a view like follow image using mysql.
+----+-----+----------+------+------+
| no | idx | name | time | rank |
+----+-----+----------+------+------+
| 1 | 1 | Apple | 1111 | 1 |
+----+-----+----------+------+------+
| 2 | 1 | Banana | 1111 | 2 |
+----+-----+----------+------+------+
| 3 | 1 | Car | 1111 | 3 |
+----+-----+----------+------+------+
| 4 | 1 | Dragon | 1111 | 4 |
+----+-----+----------+------+------+
| 5 | 1 | Eagle | 1111 | 5 |
+----+-----+----------+------+------+
| 1 | 2 | Apple | 2222 | 1 |
+----+-----+----------+------+------+
| 6 | 2 | Fire | 2222 | 2 |
+----+-----+----------+------+------+
| 7 | 2 | God | 2222 | 3 |
+----+-----+----------+------+------+
| 8 | 2 | Hippo | 2222 | 4 |
+----+-----+----------+------+------+
| 5 | 2 | Eagle | 2222 | 5 |
+----+-----+----------+------+------+
| 9 | 3 | Icecream | 3333 | 1 |
+----+-----+----------+------+------+
| 6 | 3 | Fire | 3333 | 2 |
+----+-----+----------+------+------+
| 10 | 3 | Juice | 3333 | 3 |
+----+-----+----------+------+------+
| 11 | 3 | Korea | 3333 | 4 |
+----+-----+----------+------+------+
| 12 | 3 | Low | 3333 | 5 |
+----+-----+----------+------+------+
| 9 | 4 | Icecream | 4444 | 1 |
+----+-----+----------+------+------+
| 13 | 4 | Mother | 4444 | 2 |
+----+-----+----------+------+------+
| 14 | 4 | NEws | 4444 | 3 |
+----+-----+----------+------+------+
| 11 | 4 | Korea | 4444 | 4 |
+----+-----+----------+------+------+
| 12 | 4 | Low | 4444 | 5 |
+----+-----+----------+------+------+
| 15 | 5 | Object | 5555 | 1 |
+----+-----+----------+------+------+
| 13 | 5 | Mother | 5555 | 2 |
+----+-----+----------+------+------+
| 14 | 5 | News | 5555 | 3 |
+----+-----+----------+------+------+
| 11 | 5 | Korea | 5555 | 4 |
+----+-----+----------+------+------+
| 12 | 5 | Low | 5555 | 5 |
+----+-----+----------+------+------+
The range of rank column is 1 to 5.
And, If there is no data like (idx2 & rank5), then I want to use the data which has same rank and biggest idx and lower idx than that data.
I try to use following query.
select * from (`table1` JOIN (select 1 AS `rank` union select 2 union select 3 union select 4 union select 5) AS `x` )
But, I don't know how should I do in the future.
try this
select ifnull(t2a.no, t2b.no) no, t.idx, ifnull(t2a.name, t2b.name) name, t.rank
from (
select t3.idx, t1.no rank, (select max(idx) from table2 where rank = t1.no and idx<t3.idx) max_idx
from (select distinct idx from table2 ) t3
cross join table1 t1
) t
left join table2 t2a on t2a.rank=t.rank and t.idx = t2a.idx
left join table2 t2b on t2b.rank=t.rank and t.max_idx = t2b.idx
order by 2,4