Retrieve stock balance by category from the following table - php

I have the following table structure
id stock category created type
11 10 4 2015-06-05 13:30:56 a
12 5 4 2015-06-05 13:31:30 s
14 12 6 2015-06-05 15:58:41 a
15 10 6 2015-06-05 15:59:07 s
16 10 4 2015-06-05 16:00:18 a
17 10 4 2015-06-05 16:00:52 a
18 1 6 2015-06-05 16:01:23 a
20 10 6 2015-06-05 16:51:48 a
21 10 6 2015-06-05 17:00:23 s
22 10 4 2015-06-05 17:02:39 s
23 45 7 2015-06-05 20:48:31 a
Type 'a' is for stock added
Type 's' is for sale
I'm performing following query to get the stock balance, but I need to fetch the balance for each category.
Here is the query I tried so far:
SELECT (
select SUM(stock)
from stock
where type='a'
) - (
select SUM(stock)
from stock
where type='s'
) as balance

I think you can try that (not tested)
select category
, sum(if(type='a', stock, 0)) - sum(if(type='s', stock, 0)) as balance
from stock
group by category
Edit:
I just tested it and it works

If you are using PHP you can use this
SELECT SUM(stock) FROM stock WHERE type= 'a' AND category = 4
put the result in the $var1
SELECT SUM(stock) FROM stock WHERE type= 'b' AND category = 4
put the result in the $var2 and then do
$var = $var1 - $var2
you can loop the SQL calls and set the category number that you need.
There are more solution but I since I do not know the code that you use before and were you put this results this is the best that I can do.

Related

MYSQL query that contains 2 things I need in 1 query

I have built a live scoring system for a game. When the scores are entered into the MySQL database, they are entered like this...
ID userid sessionid sprintid pointvalue1 pointvalue2 pointvalue3
1 1 1 1 10 5 2
2 2 1 1 10 5 3
3 3 1 1 12 6 3
4 1 1 2 10 6 4
5 2 1 2 12 5 3
6 3 1 2 9 4 3
As you can see from the above, there is 1 session, 2 different sprints (iterations) and 3 different users. I want to display a leader board. The leader board would show (by iteration), user name (from another table), sprint, point value 1, point value 2, point value 3, sum of point value 1 cumulative for all iterations in that session. So on iteration 1, point value 1 and sum of point value 1 cumulative would be the same. But, in iteration 2, I should see a sum of point value 1 of 20 for user 1, 22 for user 2, and 21 for user 3, in the proper order (descending). I tried a subquery using a select sum, but couldn't quite get it right, and I tried a SUM(IF()), which seemed like it would be right, but it's not. I guess I need some guidance on which direction to go.
I think this query will get you the raw data you want. You will probably want to adjust the query with some form of ordering on total points, and possibly WHERE and LIMIT clauses to meet your exact needs.
SELECT s1.userid,
s1.sessionid,
s1.sprintid,
s1.pointvalue1,
s1.pointvalue2,
s1.pointvalue3,
SUM(s2.pointvalue1) AS sum_pointvalue1,
SUM(s2.pointvalue2) AS sum_pointvalue2,
SUM(s2.pointvalue3) AS sum_pointvalue3
FROM scores s1
JOIN (SELECT userid,
sessionid,
sprintid,
SUM(pointvalue1) AS pointvalue1,
SUM(pointvalue2) AS pointvalue2,
SUM(pointvalue3) AS pointvalue3
FROM scores
GROUP BY userid, sessionid, sprintid
) s2
ON s2.userid = s1.userid AND s2.sessionid = s1.sessionid AND s2.sprintid <= s1.sprintid
GROUP BY userid, sessionid, sprintid
ORDER BY sprintid
Output:
userid sessionid sprintid pointvalue1 pointvalue2 pointvalue3 sum_pointvalue1 sum_pointvalue2 sum_pointvalue3
2 1 1 10 5 3 10 5 3
3 1 1 12 6 3 12 6 3
1 1 1 10 5 2 10 5 2
2 1 2 12 5 3 22 10 6
3 1 2 9 4 3 21 10 6
1 1 2 10 6 4 20 11 6
SQLFiddle Demo

How can I get data from 2 table based on specific condition

In MySQL database, I have 3 table called supplier, vehicle, and assigned table.
vehicle table:
vid vehicleName noOfSeat sid
========================================
13 x 10 28
14 x 8 28
15 x 12 28
16 x 14 29
17 x 4 29
18 x 8 30
assigned table
asgid bid sid vid did seatBooked assigned_start assigned_end
============================================================================
56 15 28 13 17 3 06/01/2018 02:01 06/01/2018 04:02
57 15 28 14 15 2 06/01/2018 02:01 06/01/2018 04:02
58 15 28 15 16 3 06/01/2018 02:01 06/01/2018 04:02
In a reservation page, I need to assign a vehicle to a selected date which is 06/01/2018 02:01 To 06/01/2018 04:02
Now I want to show all vehicle of a selected supplier as well as the number of seat from the vehicle table
But the condition is:
Those vehicles will be shown which has the number of seats available for my selected date: 6/01/2018 02:01 To 06/01/2018 04:02
For example:
It's must be shown following vehicle with the number of seats:
vid Mp pf Seat available
===========================
13 7 ( why 7? because on my selected date 3 seats booked)
14 6 ( why 6? because on my selected date 2 seats booked)
15 9 ( why 9? because on my selected date 3 seats booked)
16 14
17 4
18 8
Its something like bus ticket system.
My Current Query:
Currently, I am using the following query but it just removes those vid which is in my selected date but it's incorrect because as you can see that on my selected date few seats are available!!!
// my selected date: From = '$timestart' and To = '$timeend'
foreach ($sid as $key => $value) {
$vehicle->rowQuery("select * from vehicle where vid not in (
select vid from assigned where assigned_start BETWEEN '$timestart' AND '$timeend' AND assigned_end BETWEEN '$timestart' AND '$timeend' ) AND sid = $value ");
}
I can't imagine how can I solve it: =(
Your help is highly appreciated.
You can do something like this:
select vehicle.*, (noOfSeat -
(select sum(seatBooked)
from assigned
where sid = 28
and (assigned_start between '2018-01-06 02:01' and '2018-01-06 04:02' )
and (assigned_end between '2018-01-06 02:01' and '2018-01-06 04:02')
)) as seatsAvailable
from vehicle
having seatsAvailable > 0
http://sqlfiddle.com/#!9/c6bcb6/15

array_push and array_search array_splice on Laravel query builder

Are there any other eloquent query method to deal with the gross process on laravel query builder?
product table
id A_product B_product
1 4,5,8,9 3,7,10,15
2 4,7,10,11 2,3,5,13
which is better does A_product column use array or json?
----search--------------------------------------
if I want to confirm if is there 3 in A_product column on id 1
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
if(in_array("3"){
$A_result='1';
}else{
$A_result='0';
};
------insert----------------------------
if I want to add new num 20 to the A_product column
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
array_push($querys,"20");
$quearorder=array_value($querys);
DB::table('product')
->where('id', 1)
->update(['A_product' => $queryorder]);
--delete--------------------------------------
if I want to delete num 3 from the A_product column
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
$key=array_search(3,$querys);
array_splice($querys,$key,1);
DB::table('product')
->where('id', 1)
->update(['A_product' => $querys]);
Its suggestion that you should store all product into separate column , with product type and the group or for user_id
id product product_type category(or for user)
1 4 A 1
2 5 A 1
3 8 A 1
4 9 A 1
5 4 A 2
6 7 A 2
7 10 A 2
8 11 A 2
1 3 B 1
2 7 B 1
3 10 B 1
4 15 B 1
5 2 B 2
6 3 B 2
7 5 B 2
8 13 B 2
Then you can easily apply CRUD operation

How to get price difference from same table in SQL

I have following table
slno date productid companyid price
88 2017-05-17 1 1 65.27
87 2017-05-17 1 2 72.94
86 2017-05-17 1 3 73.13
85 2017-05-17 2 1 73.73
84 2017-05-17 2 2 67.71
83 2017-05-16 1 1 65.40
82 2017-05-16 1 2 72.49
81 2017-05-16 2 1 73.31
80 2017-05-16 2 2 67.17
Now I want price of product 1 for 2017-05-17 and difference of price from yesterday for same company id.
e.g:
getPrice( productid = 1, date='2017-05-17')
and this should return :
companyid , productid , date, price, difference from yesterday:
1, 1,'2017-05-17', 65.27, -0.13
2, 1,'2017-05-17', 72.94, 0.45
...
or it should return:
companyid , productid , date, price, yesterday price:
1, 1,'2017-05-17', 65.27, 65.40
2, 1,'2017-05-17', 72.94, 72.49
...
How to get this in PHP SQL?
You should consider doing this with php, because php can tell you yesterdays and today date. So get yesterdays and today date with php and use it to manipulate the sql query
so the query should look something like this
yesterdays Price:
select companyid, productid , date, price from table where productid='1' and date='$yesterday';
Today price
select companyid, productid , date, price from table where productid='1' and date='$today';
now you can get both values and subtract them with php to get the difference

CodeIgniter table class loops through db records 3 times - why?

This is my controller code (using Colin Williams' Template class):
$this->load->library('table');
$table['records'] = $this->db->get_where('data', array('category_1' => 'weight'));
$this->template->write_view('content', 'vw/weight_vw', $table, TRUE);
And my view code:
<div class="grid_16">
<?php echo $this->table->generate($records); ?>
</div>
Look at what I get
2 1 29 2011-01-01 10 weight
4 1 29 2010-11-03 11 weight
5 1 29 2011-05-02 10 weight
6 1 42 2011-07-11 23 weight // the database only has records up to here
2 1 29 2011-01-01 10 weight // from here on it's repeated twice
4 1 29 2010-11-03 11 weight
5 1 29 2011-05-02 10 weight
6 1 42 2011-07-11 23 weight
2 1 29 2011-01-01 10 weight
4 1 29 2010-11-03 11 weight
5 1 29 2011-05-02 10 weight
6 1 42 2011-07-11 23 weight
Any ideas why this is happening? None of my code is inside a loop or anything like that.
Use
$this->output->enable_profiler()
to see the db queries.
Take the query and run it against your db directly.
It's most likey you have a bad join in your database model.

Categories