Build query 'average amount by month' with filling gaps from previous month - mysql

I have a problem. I need to build report query to calculate average amount by month.
If user from account does not buy anything in this month I should use amount from previous month.
This is my data: As you can see I have one account operation on July (2014-07) and one operation in August (2014-08).
CREATE TABLE IF NOT EXISTS `value_hist` (
`id` int(11) NOT NULL,
`account_id` int(11) DEFAULT NULL,
`data_channel_id` int(11) DEFAULT NULL,
`status` tinyint(1) NOT NULL,
`amount` decimal(19,4) NOT NULL COMMENT '(DC2Type:money)(DC2Type:money)',
`created_at` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=8 ;
INSERT INTO `value_hist` (`id`, `account_id`, `data_channel_id`, `status`, `amount`, `created_at`)
VALUES
(28, 19, 2, 0, 100.0000, '2014-08-22 14:59:47'),
(29, 19, 2, 0, 11.0000, '2014-09-22 15:05:36'),
(30, 19, 2, 0, 22.0000, '2014-09-22 15:10:59'),
(31, 19, 2, 0, 0.0000, '2014-09-22 15:14:19'),
(32, 19, 2, 0, 22.0000, '2014-09-22 15:15:58'),
(33, 19, 2, 0, 0.0000, '2014-09-22 15:17:29'),
(34, 1, 2, 0, 50.0000, '2014-07-22 15:20:10'),
(35, 1, 2, 0, 0.0000, '2014-09-22 15:23:37'),
(36, 1, 2, 0, 100.0000, '2014-09-22 15:23:39'),
(37, 1, 2, 1, 0.0000, '2014-09-22 15:30:53'),
(38, 19, 2, 0, 100.0000, '2014-09-22 15:30:53'),
(39, 28, 2, 1, 0.0000, '2014-09-22 15:36:44'),
(40, 19, 2, 1, 0.0000, '2014-09-22 15:45:24'),
(41, 6, 2, 0, 0.0000, '2014-09-22 15:48:54'),
(42, 6, 2, 0, 200.0000, '2014-09-22 15:49:34'),
(43, 6, 2, 1, 33.0000, '2014-09-22 15:50:18');
I have built a query. Which can get average value from each month
SELECT
data_channel_id,
test2.monthaa,
AVG(h1.amount) as amount
FROM
value_hist h1
JOIN (
SELECT MAX(dd.`id`) as id, EXTRACT(month from dd.created_at) as monthaa
FROM value_hist dd
where dd.created_at > '01.01.2014'
GROUP BY data_channel_id, dd.account_id, monthaa
) test2 ON test2.id = h1.id
WHERE
created_at > '01.01.2014'
GROUP BY data_channel_id, monthaa
The result return average value from each month
| channel | month | amount |
| 2 | 7 | 50.0000|
| 2 | 8 | 100.000|
| 2 | 9 | 8.25000|
But I should get this result, I should consider previous month amount. For example (100 + 50) / 2, because account 1 did not buy in August (2014-08)
| channel | month | amount |
| 2 | 7 | 50.0000|
| 2 | 8 | 75.0000|
| 2 | 9 | 8.25000|
Update: There was only one account in July(7) thus amout equal to 50.00; (50.00 / 1 = 50.00).
In August(8) second account has come therefore amount equal to 75.00; ((100.00 + 50) / 2 = 75.00) because I have two accounts in August(8).

Related

MySQL Query to get profit and loss on asset by order history

Hello everyone I am getting a problem with the MySQL query and had confusion. I want to achieve profit and loss on assets from order history.
IGNORE coin_current_price in output not needed.
resultBuy = TotalBuyingPrice / totalCoinQtyBuy
if sellingPrice_Per_Coin is greater than resultBuy then it will be profitable and vice-versa for loss.
Coin Name, coin_currency are the same then it will be called as a single entity. if any of these conditions don't meet then it will create a new row for that item like usdt and INR
admin_id will be the user id only of that user data will show.
MySQL Query - For Two Table buy_table and sell_table
CREATE TABLE buy_table (
id INTEGER PRIMARY KEY,
coin_name TEXT NOT NULL,
coin_qty TEXT NOT NULL,
coin_buy_price TEXT NOT NULL,
coin_current_price TEXT NOT NULL,
admin_id TEXT NOT NULL,
priority TEXT NOT NULL,
coin_currency TEXT NOT NULL
);
CREATE TABLE sell_table (
id INTEGER PRIMARY KEY,
coin_name TEXT NOT NULL,
coin_qty TEXT NOT NULL,
coin_buy_price TEXT NOT NULL,
coin_current_price TEXT NOT NULL,
admin_id TEXT NOT NULL,
priority TEXT NOT NULL,
coin_currency TEXT NOT NULL
);
INSERT INTO buy_table VALUES (1, 'BTC', '1', '$10', '$10', '11', '1','inr');
INSERT INTO buy_table VALUES (2, 'ETH', '1', '$10', '$10', '11', '1', 'inr');
INSERT INTO buy_table VALUES (3, 'BTC', '1', '$10', '$10', '11', '1', 'inr');
INSERT INTO buy_table VALUES (4, 'ETH', '4', '$10', '$10', '11', '1', 'inr');
INSERT INTO buy_table VALUES (5, 'BTC', '3', '$10', '$10', '11', '1', 'inr');
INSERT INTO buy_table VALUES (6, 'WRX', '1', '$1', '$1', '11', '1', 'inr');
INSERT INTO buy_table VALUES (7, 'WRX', '1', '$1', '$1', '11', '1', 'usdt');
INSERT INTO buy_table VALUES (8, 'WRX', '1', '$1', '$1', '11', '1', 'inr');
INSERT INTO buy_table VALUES (9, 'WRX', '1', '$1', '$1', '11', '1', 'usdt');
INSERT INTO sell_table VALUES (1, 'ETH', '1', '$50', '$50', '11' , '1', 'inr');
INSERT INTO sell_table VALUES (2, 'BTC', '1', '$50', '$50', '11' , '1', 'inr');
Final Output Will Be Like Below
| Coin Name | coin_currency | Buying Price | CoinQty | PricePerCoin | SellingPrice | SoldQty | ProfitLoss | Stock | admin_id |
| --------- | ------------- | ------------ | ------- | ------------ | ------------ | ------- | ---------- | ----- | -------- |
| BTC | INR | $50 | 5 | $10 | $50 | 1 | $40 | 4 | 11 |
| ETH | INR | $50 | 5 | $10 | $50 | 1 | $40 | 4 | 11 |
| WRX | INR | $2 | 2 | $1 | 0 | 0 | 0 | 1 | 11 |
| WRX | USDT | $2 | 2 | $1 | 0 | 0 | 0 | 1 | 11 |
This is the table structure if required with an online query run. Link for table
I would combine the two tables into one transactions table but for this answer I will leave the tables split as you have them.
Firstly, let's address the issues with the datatypes used in your two tables. TEXT is not the appropriate datatype for any of your columns. I have changed them to what I believe to be more appropriate types.
CREATE TABLE buy_table (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
coin_name VARCHAR(6) NOT NULL,
coin_qty SMALLINT UNSIGNED NOT NULL,
coin_buy_price DECIMAL(12, 2) NOT NULL,
coin_current_price DECIMAL(12, 2) NOT NULL,
admin_id INTEGER UNSIGNED NOT NULL,
priority TINYINT UNSIGNED NOT NULL,
coin_currency CHAR(3) NOT NULL
);
/* I am omitting the amended sell_table as it is identical in all but name */
INSERT INTO buy_table VALUES
(1, 'BTC', 1, 10, 10, 11, 1, 'inr'),
(2, 'ETH', 1, 10, 10, 11, 1, 'inr'),
(3, 'BTC', 1, 10, 10, 11, 1, 'inr'),
(4, 'ETH', 4, 10, 10, 11, 1, 'inr'),
(5, 'BTC', 3, 10, 10, 11, 1, 'inr'),
(6, 'WRX', 1, 1, 1, 11, 1, 'inr'),
(7, 'WRX', 1, 1, 1, 11, 1, 'usd'),
(8, 'WRX', 1, 1, 1, 11, 1, 'inr'),
(9, 'WRX', 1, 1, 1, 11, 1, 'usd');
INSERT INTO sell_table VALUES
(1, 'ETH', 1, 50, 50, 11, 1, 'inr'),
(2, 'BTC', 1, 50, 50, 11, 1, 'inr');
For this query I have chosen to aggregate the data on each side of the union in the derived table. This could be done in just the outer select list but it made some of the calculations more readable. As you suggested this will be run with the context of an admin_id I have included this as a predicate on the inner queries and not included the admin_id in the outer select list -
SELECT
`coin_name`,
UPPER(`coin_currency`) AS `coin_currency`,
SUM(`total_paid`) AS `buying_price`,
SUM(`num_bought`) AS `coin_qty`,
SUM(`avg_paid`) AS `price_per_coin`,
SUM(`total_received`) AS `selling_price`,
SUM(`num_sold`) AS `sold_qty`,
ROUND((SUM(`avg_received`) - SUM(`avg_paid`)) * SUM(`num_sold`), 2) AS `profit_loss`,
SUM(`num_bought`) - SUM(`num_sold`) AS `stock`
FROM (
SELECT
`coin_name`,
`coin_currency`,
SUM(`coin_qty`) AS `num_bought`,
0 AS `num_sold`,
SUM(`coin_qty` * `coin_buy_price`) AS `total_paid`,
0 AS `total_received`,
ROUND(SUM(`coin_qty` * `coin_buy_price`) / SUM(`coin_qty`), 2) AS `avg_paid`,
0 AS `avg_received`
FROM buy_table
WHERE `admin_id` = 11
GROUP BY `coin_name`, `coin_currency`
UNION ALL
SELECT
`coin_name`,
`coin_currency`,
0 AS `num_bought`,
SUM(`coin_qty`) AS `num_sold`,
0 AS `total_paid`,
SUM(`coin_qty` * `coin_buy_price`) AS `total_received`,
0 AS `avg_paid`,
ROUND(SUM(`coin_qty` * `coin_buy_price`) / SUM(`coin_qty`), 2) AS `avg_received`
FROM sell_table
WHERE `admin_id` = 11
GROUP BY `coin_name`, `coin_currency`
) tx
GROUP BY `coin_name`, `coin_currency`;
This would be cleaner if all the transactions were in a single table.
UPDATE - tables combined into single transactions table
CREATE TABLE transactions (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
type ENUM('buy', 'sell') NOT NULL,
coin_name VARCHAR(6) NOT NULL,
coin_qty SMALLINT UNSIGNED NOT NULL,
coin_buy_price DECIMAL(12, 2) NOT NULL,
coin_current_price DECIMAL(12, 2) NOT NULL,
admin_id INTEGER UNSIGNED NOT NULL,
priority TINYINT UNSIGNED NOT NULL,
coin_currency CHAR(3) NOT NULL
);
INSERT INTO transactions VALUES
( 1, 'buy', 'BTC', 1, 10, 10, 11, 1, 'inr'),
( 2, 'buy', 'ETH', 1, 10, 10, 11, 1, 'inr'),
( 3, 'buy', 'BTC', 1, 10, 10, 11, 1, 'inr'),
( 4, 'buy', 'ETH', 4, 10, 10, 11, 1, 'inr'),
( 5, 'buy', 'BTC', 3, 10, 10, 11, 1, 'inr'),
( 6, 'buy', 'WRX', 1, 1, 1, 11, 1, 'inr'),
( 7, 'buy', 'WRX', 1, 1, 1, 11, 1, 'usd'),
( 8, 'buy', 'WRX', 1, 1, 1, 11, 1, 'inr'),
( 9, 'buy', 'WRX', 1, 1, 1, 11, 1, 'usd'),
(10, 'sell', 'ETH', 1, 50, 50, 11, 1, 'inr'),
(11, 'sell', 'BTC', 1, 50, 50, 11, 1, 'inr');
SELECT
`coin_name`,
`coin_currency`,
`total_paid` AS `buying_price`,
`bought_qty` AS `coin_qty`,
ROUND(`total_paid` / `bought_qty`, 2) AS `price_per_coin`,
`total_received` AS `selling_price`,
`sold_qty`,
ROUND(IFNULL(((`total_received` / `sold_qty`) - (`total_paid` / `bought_qty`)) * `sold_qty`, 0), 2) AS `profit_loss`,
`bought_qty` - `sold_qty` AS `stock`
FROM (
SELECT
`coin_name`,
UPPER(`coin_currency`) AS `coin_currency`,
SUM(IF(`type` = 'buy', `coin_qty`, 0)) AS `bought_qty`,
SUM(IF(`type` = 'sell', `coin_qty`, 0)) AS `sold_qty`,
SUM(IF(`type` = 'buy', `coin_qty` * `coin_buy_price`, 0)) AS `total_paid`,
SUM(IF(`type` = 'sell', `coin_qty` * `coin_buy_price`, 0)) AS `total_received`
FROM transactions
WHERE `admin_id` = 11
GROUP BY `coin_name`, `coin_currency`
) tx;
db<>fiddle

MySQL - Daily overview only shows current bookings rather than all

I'm trying to get a view where I can select a specific day and it will return a list of ALL rooms with the current days breakdown.
Currently it shows the daily overview for a specific day but lacks showing rooms which DONT have bookings for the day selected?
(I need to display the daily overview for all rooms even without bookings)
How do I change this so it will show ALL rooms even when there is not a booking for the specific room on that day as well as rooms that do?
Current display (for 2019-11-24):
SELECT * FROM `Daily Overview` WHERE '2019-11-24' >= StartDate AND '2019-11-24' <= EndDate;
3 Family 2 2019-11-24 2019-11-28 4 2 2,3 Ahri,Blitz
4 Dual 1 2019-11-22 2019-11-25 2 1 1 Aatrox
Desired output:
1
2
3 Family 2 2019-11-24 2019-11-28 4 2 2,3 Ahri,Blitz
4 Dual 1 2019-11-22 2019-11-25 2 1 1 Aatrox
5
6
8
9
10
View script:
CREATE VIEW `Daily Overview` AS
SELECT ROOMINFO.ID as Room, ROOMINFO.`Type`,
BOOKROOM.Ref as Ref,
BOOK.Start_Date as StartDate,
BOOK.End_Date as EndDate
FROM ROOMINFO
JOIN BOOKROOM ON ROOMINFO.ID = BOOKROOM.ID
JOIN BOOK ON ROOMINFO.ID AND BOOK.Ref = BOOKROOM.Ref
GROUP BY ROOMINFO.ID, ROOMINFO.`Type`, ROOMINFO.Max, Ref;
table data & structure script:
CREATE SCHEMA CATTERY2;
USE CATTERY2;
CREATE TABLE BOOK( Ref INT NOT NULL AUTO_INCREMENT, Start_Date DATE NOT NULL, End_Date DATE NOT NULL, PRIMARY KEY(Ref));
CREATE TABLE ROOMINFO( ID INT NOT NULL AUTO_INCREMENT,`Type` VARCHAR(10) NOT NULL, Max TINYINT NOT NULL, PRIMARY KEY(ID));
CREATE TABLE BOOKROOM( Ref INT NOT NULL, ID INT NOT NULL, FOREIGN KEY (Ref) REFERENCES BOOK(Ref), FOREIGN KEY (ID) REFERENCES ROOMINFO(ID));
INSERT INTO BOOK(Start_Date, End_Date) VALUES
("2019-11-22", "2019-11-25"), ("2019-11-24", "2019-11-28");
INSERT INTO ROOMINFO (ID, `Type`,Max) VALUES
(1, "Family", 4), (2, "Family", 4), (3, "Family", 4), (4, "Dual", 2),
(5, "Dual", 2), (6, "Dual", 2), (7, "Dual", 2), (8, "Dual", 2),
(9, "Dual", 2), (10, "Dual", 2);
INSERT INTO BOOKROOM( Ref, ID ) VALUES
(1, 4), (2, 3);
Consider the following. Note that I've modified your structure slightly to aid comprehension:
DROP SCHEMA IF EXISTS cattery2;
CREATE SCHEMA CATTERY2;
USE CATTERY2;
CREATE TABLE cats
(cat_id SERIAL PRIMARY KEY
,name VARCHAR(30)
,sex CHAR(1) NOT NULL
);
CREATE TABLE bookings
(booking_id SERIAL PRIMARY KEY
,start_Date DATE NOT NULL
,end_date DATE NOT NULL
);
CREATE TABLE rooms
(room_id SERIAL PRIMARY KEY
,capacity TINYINT NOT NULL
);
CREATE TABLE room_booking
(room_id INT NOT NULL
,booking_id INT NOT NULL
,PRIMARY KEY(room_id,booking_id)
);
CREATE TABLE cat_booking
(cat_id INT NOT NULL
,booking_id INT NOT NULL
,PRIMARY KEY(cat_id,booking_id)
);
INSERT INTO bookings(start_date, end_date) VALUES
("2019-11-22", "2019-11-25"),
("2019-11-24", "2019-11-28"),
("2019-12-01", "2019-12-02"),
("2019-12-01", "2019-12-06"),
("2019-12-02", "2019-12-03"),
("2019-12-04", "2019-12-10"),
("2019-12-04", "2019-12-10"),
("2019-12-05", "2019-12-13"),
("2019-12-16", "2019-12-19"),
("2019-12-26", "2019-12-28"),
("2019-12-26", "2020-01-01"),
("2019-12-28", "2020-01-02"),
("2019-12-31", "2020-01-05"),
("2020-01-03", "2020-01-08"),
("2020-01-05", "2020-01-11"),
("2020-01-06", "2020-01-09"),
("2020-01-06", "2020-01-11"),
("2020-01-08", "2020-01-18"),
("2020-01-11", "2020-01-15"),
("2020-01-15", "2020-01-17"),
("2020-01-15", "2020-01-18");
INSERT INTO rooms (room_id,capacity) VALUES
( 1,4),
( 2,4),
( 3,4),
( 4,2),
( 5,2),
( 6,2),
( 7,2),
( 8,2),
( 9,2),
(10,2);
INSERT INTO room_booking (booking_id,room_id) VALUES
( 1, 4),
( 2, 3),
( 3, 4),
( 4, 5),
( 5, 6),
( 6, 7),
( 7, 3),
( 8, 2),
( 9, 1),
(10, 8),
(11, 3),
(12, 9),
(13, 2),
(14, 10),
(15, 4),
(16, 5),
(17, 6),
(18, 7),
(19, 2),
(20, 1),
(21, 10);
INSERT INTO cats (name,sex) VALUES
('Aatrox', 'm'),
('Ahri', 'f'),
('Blitz', 'm'),
('Curley', 'm'),
('Mandy', 'm'),
('Nami', 'f'),
('Kog', 'm'),
('Caitlyn', 'f'),
('Barney', 'm'),
('Gnar', 'm'),
('Charley', 'f'),
('Bundy', 'm'),
('Ringo', 'm'),
('Smiley', 'm'),
('Bentley', 'f'),
('Barney', 'm'),
('Yuumi', 'f'),
('Rammus', 'm'),
('Viktor', 'm'),
('Xerath', 'm'),
('Azir', 'm'),
('Reginald', 'm'),
('Harry', 'm'),
('Indie', 'f'),
('Dotty', 'f'),
('Wesley', 'm'),
('Karma', 'f'),
('Nami', 'f'),
('Nautalus', 'm'),
('Tristy', 'f'),
('Kaisa', 'f'),
('Baron', 'm'),
('Braum', 'm'),
('Alistar', 'm'),
('Ahri', 'f');
INSERT INTO cat_booking(booking_id,cat_id) VALUES
( 1, 1),
( 2, 2),
( 2, 3),
( 3, 1),
( 4, 2),
( 5, 3),
( 5, 4),
( 6, 5),
( 7, 6),
( 7, 7),
( 7, 8),
( 8, 9),
( 8, 10),
( 8, 11),
( 9, 12),
( 9, 13),
( 9, 14),
(10, 15),
(11, 16),
(11, 17),
(11, 18),
(12, 19),
(12, 20),
(13, 21),
(13, 22),
(13, 23),
(14, 24),
(14, 25),
(15, 26),
(16, 27),
(16, 28),
(17, 29),
(18, 30),
(19, 31),
(19, 32),
(20, 33),
(20, 34),
(21, 35);
And the query...
SELECT DISTINCT r.room_id
, r.capacity
, x.booking_id
, x.start_date
, x.end_date
FROM rooms r
LEFT
JOIN
( SELECT b.booking_id
, b.start_date
, b.end_date
, rb.room_id
FROM bookings b
JOIN room_booking rb
ON rb.booking_id = b.booking_id
WHERE '2019-11-24' BETWEEN b.start_date AND b.end_date
) x
ON x.room_id = r.room_id
ORDER
BY r.room_id
, r.capacity
, x.start_date
, x.booking_id;
+---------+----------+------------+------------+------------+
| room_id | capacity | booking_id | start_date | end_date |
+---------+----------+------------+------------+------------+
| 1 | 4 | NULL | NULL | NULL |
| 2 | 4 | NULL | NULL | NULL |
| 3 | 4 | 2 | 2019-11-24 | 2019-11-28 |
| 4 | 2 | 1 | 2019-11-22 | 2019-11-25 |
| 5 | 2 | NULL | NULL | NULL |
| 6 | 2 | NULL | NULL | NULL |
| 7 | 2 | NULL | NULL | NULL |
| 8 | 2 | NULL | NULL | NULL |
| 9 | 2 | NULL | NULL | NULL |
| 10 | 2 | NULL | NULL | NULL |
+---------+----------+------------+------------+------------+
Also, note that there's no VIEW here. This is deliberate. Queries against VIEWS in MySQL have limited access to underlying indexes, making them (in my VIEW) almost useless.

Sum of values of a field based on another field

I have a table as follows. I need to build a query to find the following.
(Sum of feeComponentValue where feeComponentCalc = 1) - (Sum of feeComponentValue where feeComponentCalc = 1) and it should be group by feeSettingId
Please help
CREATE TABLE `feevalues` (
`feevaluesId` int(11) NOT NULL,
`feeSettingId` int(11) NOT NULL,
`feeComponentId` int(11) NOT NULL,
`feeComponentValue` int(11) DEFAULT NULL,
`feeComponentCalc` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table feevalues
INSERT INTO `feevalues`
(`feevaluesId`, `feeSettingId`, `feeComponentId`, `feeComponentValue`, `feeComponentCalc`)
VALUES
(27, 29, 1, 1000, 1),
(28, 29, 12, 2000, 1),
(29, 29, 3, 3000, 1),
(30, 29, 4, 4000, 1),
(103, 30, 3, 1000, 1),
(104, 30, 1, 2000, 1),
(105, 30, 3, 3000, 1),
(106, 30, 1, 4000, 1),
(107, 30, 14, 5000, 2),
(108, 30, 2, 6000, 2),
(109, 30, 13, 7000, 2),
(110, 30, 2, 8000, 2),
(111, 31, 1, 1000, 1),
(112, 31, 3, 2000, 1),
(113, 31, 13, 4000, 2),
(114, 31, 2, 3000, 2),
(122, 32, 1, 1, 1),
(123, 32, 3, 3, 1),
(124, 32, 4, 4, 1),
(125, 32, 2, 5, 2),
(126, 32, 14, 6, 2),
(127, 32, 13, 7, 2);
--
I assume you mean ...where feeComponentCalc = 2... in the 2nd case, right?
With conditional aggregation:
select
feeSettingId,
sum(case feeComponentCalc when 1 then feeComponentValue else 0 end) -
sum(case feeComponentCalc when 2 then feeComponentValue else 0 end) as result
from feevalues
group by feeSettingId
See the demo.
Or:
select
feeSettingId,
sum(
case feeComponentCalc
when 1 then feeComponentValue
when 2 then -feeComponentValue
else 0
end
) as result
from feevalues
group by feeSettingId
See the demo.
Results:
| feeSettingId | result |
| ------------ | ------ |
| 29 | 10000 |
| 30 | -16000 |
| 31 | -4000 |
| 32 | -10 |

MYSQL - Count consecutive identical values

I have a MySQL table which basically stores a list of points with reference of the player id who made that point.
My setup is:
CREATE TABLE `points` (
`id` int(10) unsigned NOT NULL,
`player_id` int(10) unsigned NOT NULL,
`game_id` int(10) unsigned NOT NULL,
`score` int(11) NOT NULL,
`isScore` tinyint(1) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
);
INSERT INTO `points` (`id`, `player_id`, `game_id`,
`score`, `isScore`, `created_at`, `updated_at`)
VALUES
(16, 11, 60, 4, 0, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(17, 11, 60, 5, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(18, 12, 60, 6, 0, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(19, 12, 60, 7, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(20, 12, 60, 8, 0, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(21, 12, 60, 9, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(22, 8, 60, 10, 0, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(23, 8, 60, 11, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(24, 12, 60, 12, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(25, 12, 60, 13, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(26, 5, 60, 14, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(27, 12, 60, 15, 0, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(28, 12, 60, 16, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(29, 10, 60, 17, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(30, 10, 60, 18, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39');
Here's a SQLfiddle with this setup: http://sqlfiddle.com/#!9/f6ca9f8
I want to get the number of maximum consecutive point made by a player for each player.
So in the sqlfiddle the result will be something like:
+-----------+------------------------+
| player_id | max_consegutive_points |
+-----------+------------------------+
| 12 | 4 |
| 8 | 2 |
| 5 | 1 |
| 10 | 2 |
+-----------+------------------------+
Your question is missing some key points (no pun intended). For example: what is considered a consecutive identical value? You need to define if those values will be listed ordered by ID, created_at, ID+game_id, etc.
So let's say the order is given by the ID, because it may be autoincremental and it's only natural that points are inserted in the order they happened.
However, the answer will be to create a query with a counter variable (accum) that increases if the player is the same than the one in the former row, or resets to 1 if the player changes:
select
points.id,
points.player_id,
points.game_id,
points.created_at,
#counter:=IF(#playerid=points.player_id,#counter+1,1) as accum,
#playerid:=points.player_id as current_player_id
from points,
(select #counter:=1) c,
(select #playerid:=0) p
order by points.id
See SQLFiddle here
Having the result of that table, and putting it into a view or temporary table that we may call "accums", you just need to do
SELECT player_id, max(accum)
FROM accums
GROUP BY player_id
select distinct(player_id), MAX(score) from points group by player_id;

SELECT query for TableA and TableB IF TableB.col value (0 OR 1) I create new columns for 0 and 1

I have a Table pictures
create table pictures (
picture_id int(10) unsigned NOT NULL AUTO_INCREMENT,
mall_id float NOT NULL,
shop_id float NOT NULL,
picture_islogo int(11) NOT NULL ,
picture_path varchar(255) NOT NULL,
PRIMARY KEY (picture_id)
) ;
Sample Data for pictures
INSERT INTO pictures VALUES
(1, -1, 1, 1, 'photo.jpg'),
(2, -1, 2, 1, 'photo.jpg'),
(3, -1, 3, 1, 'photo.jpg'),
(4, -1, 4, 1, 'photo.jpg'),
(5, -1, 5, 1, 'photo.jpg'),
(6, -1, 6, 1, 'photo.jpg'),
(7, -1, 7, 1, 'photo.jpg'),
(8, -1, 8, 1, 'photo.jpg'),
(9, -1, 9, 1, 'photo.jpg'),
(10, -1, 10, 1, 'photo.jpg'),
(11, -1, 11, 1, 'photo.jpg'),
(12, -1, 12, 1, 'photo.jpg'),
(13, -1, 13, 1, 'photo.jpg'),
(14, -1, 13, 0, 'photo.jpg');
and other table malls
create table malls (
mall_id float NOT NULL AUTO_INCREMENT,
user_id float NOT NULL,
mall_displaysname varchar(255) NOT NULL,
mall_description text NOT NULL,
mall_contact varchar(14) NOT NULL,
mall_logo_picture_id int(11) NOT NULL,
mall_background_picture_id int(11) NOT NULL,
PRIMARY KEY (mall_id)
);
Sample Data for malls
INSERT INTO malls VALUES
(1, 2, 'mall', 'description', '+60 12 3456789', 14, 36),
(2, 5, 'mall 2', 'description', '+60 12 3456789', 15, 37),
(3, 6, 'mall 3', 'description ', '+60 12 3456789', 16, 38),
(4, 13, 'Multimedia University', 'description ', '+60 12 3456789', 17, 39),
(5, 18, 'Setia Walk', 'description ', '+60 12 3456789', 18, 40),
(6, 20, 'Ampang Point', 'description ', '+60 12 3456789', 19, 41),
(7, 21, 'Alamanda Plaza', 'description ', '+60 12 3456789', 20, 42),
(8, 22, 'Subang Parade', 'description ', '+60 12 3456789', 21, 43),
(9, 26, 'مجمع العرب - جدة', 'description ', '+60 12 3456789', 22, 44);
Where mall_background_picture_id and mall_background_picture_id reference to pictures.picture_id and Depend on the value of pictures.picture_islogo (0: Logo, 1:background or others)
i want a query to that returns the pictures.picture_path depends on the value of pictures.picture_islogo (0: Logo, 1:background or others)
the data that I need to get
mall_id , mall_displaysname, mall_logo, mall_background , mall_description, and mall_contact
Where mall_logo is the photo_path of the condition
pictures.picture_islogo = 1
and same thing with **mall_background** but
picture_islogo = 0
AND
mall_logo_picture_id = picture_id
Put the extra condition into the join condition:
select * -- you can chose what columns you want
from malls m
left join pictures pl on m.mall_logo_picture_id = pl.picture_id
and pl.picture_isLogo = 1
left join pictures pb on m.mall_background_picture_id = pb.picture_id
and pb.picture_isLogo = 0
To have left joins, the extra conditions must be in the join condition, which is evaluated as the join is being made.
If you put them in the where clause, which is evaluated after the join is made, you force the outer join to be an inner join, because the join must be successful fur the extra condition to be true.