Update a table with conditions in specfic order - mysql

I have searched the web for my problem, tested some subqueries and derived table approaches with Case Statements, but didnĀ“t get the result. Perhaps you can help? Thanks.
The examples below are just an example.
# generate the table as it is
DROP TABLE IF EXISTS `IN`;
CREATE TABLE `IN`
(`Part` CHAR(1),
`Warehouse` INT(1),
`Percentage` INT(1),
`Update` INT(1));
#some values for the table
INSERT INTO `IN`
(Part, Warehouse, Percentage)
VALUES
('A' , 1, 80),
('A', 2, 100),
('A', 3, 50),
('B', 1, 100),
('B', 2, 50),
('B', 3, 100);
# generate table as it should be
DROP TABLE IF EXISTS `OUT`;
CREATE TABLE `OUT`
(`Part` CHAR(1),
`Warehouse` INT(1),
`Percentage` INT(1),
`Update` INT(1));
# values for the table
INSERT INTO `OUT`
(Part, Warehouse, Percentage, `Update`)
VALUES
('A' , 1, 80, 2),
('A', 2, 100, 2),
('A', 3, 50, 2),
('B', 1, 100, 3),
('B', 2, 50, 3),
('B', 3, 100, 3);
I would like to add the specific warehouse name in the column Update for the specific part if the percentage is 100.
The value of the warehouse should be filled to every row for the specific part.
The fill calculation of the update column should be in a specific order.
So first there should be a check if warehouse 3 has 100 and take this value. If warehouse 3 only has 50 then check warehouse 2, if it has 100.
Thank you very much!

Here's one way...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(Part CHAR(1)
,Warehouse INT NOT NULL
,Percentage TINYINT NOT NULL
);
INSERT INTO my_table
(Part, Warehouse, Percentage)
VALUES
('A' , 1, 80),
('A', 2, 100),
('A', 3, 50),
('B', 1, 100),
('B', 2, 50),
('B', 3, 100);
SELECT w1.*, COALESCE(w3.warehouse,w2.warehouse,w1.warehouse) warehouse
FROM my_table w1
LEFT
JOIN my_table w2
ON w2.part = w1.part
AND w2.warehouse = 2
AND w2.percentage = 100
LEFT
JOIN my_table w3
ON w3.part = w1.part
AND w3.warehouse = 3
AND w3.percentage = 100;
+------+-----------+------------+-----------+
| Part | Warehouse | Percentage | warehouse |
+------+-----------+------------+-----------+
| A | 1 | 80 | 2 |
| A | 2 | 100 | 2 |
| A | 3 | 50 | 2 |
| B | 1 | 100 | 3 |
| B | 2 | 50 | 3 |
| B | 3 | 100 | 3 |
+------+-----------+------------+-----------+

Related

How to advanced query based on join

My schema looks like this:
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
create table ads(
ad_id int,
ad_name varchar(10)
);
create table ads_insight(
id int,
ad_id int,
date date,
ad_clicks int
);
create table products(
id int,
name varchar(10)
);
create table products_insight(
id int,
product_id int,
sale int,
date date
);
create table ads_products(
ad_id int,
product_id int
);
insert into ads(ad_id, ad_name) values
(1,'ad1'),
(2,'ad2'),
(3,'ad3');
insert into ads_insight(id, ad_id, date, ad_clicks) values
(1, 1, '2021-04-25', 1),
(3, 1, '2021-04-23', 2),
(4, 1, '2021-04-22', 8),
(5, 2, '2021-04-25', 6),
(6, 2, '2021-03-03', 7);
insert into products(id, name) values
(1,'prod1'),
(2,'prod2'),
(3,'prod3'),
(4,'prod4'),
(5,'prod5');
insert into products_insight(id, product_id, sale, date) values
(1, 1, 10, '2021-04-25'),
(2, 1, 13, '2021-04-24'),
(3, 1, 15, '2021-04-23'),
(4, 1, 14, '2021-04-22'),
(5, 1, 17, '2021-04-21'),
(6, 1, 15, '2021-04-20'),
(7, 1, 13, '2021-04-19'),
(8, 2, 15, '2021-04-25');
insert into ads_products (ad_id, product_id) values
(1, 1),
(1, 2),
(2, 3),
(2, 4),
(2, 2),
(3, 1);
Here you have fiddle
A quick explanation of schema:
I have ads:
each ad has insights, which tell us when a certain ad was active(=> ad_clicks has to be > 0).
each ad has products(many2many - ads_products table). Each product has products_insight which tells us how many sales that product generated on a certain day.
Now I want to get all ads from the time range 2021-04-20 - 2021-04-25 which had ad_clicks > 0 (which I have done) AND count how many sales each ad has generated when it was active. So count sale only if the ad has ad_insight and ad_clicks > 0.
My query looks like this:
SET #from_date = '2021-04-20';
SET #to_date = '2021-04-25';
SELECT
ads.ad_name,
IFNULL(ad_clicks, 0) AS clicks,
IFNULL(product_sale, 0) AS product_sale,
IFNULL(products, '') AS products
FROM ads
LEFT JOIN (
SELECT ad_id, SUM(ad_clicks) AS ad_clicks
FROM ads_insight
WHERE date BETWEEN #from_date AND #to_date
GROUP BY ad_id
) AS ai ON ai.ad_id = ads.ad_id
LEFT JOIN (
SELECT ad_id, SUM(sale) AS product_sale
FROM ads_products AS ap
LEFT JOIN products_insight AS pi ON pi.product_id = ap.product_id
WHERE date BETWEEN #from_date AND #to_date
GROUP BY ad_id
) AS pi ON pi.ad_id = ads.ad_id
LEFT JOIN (
SELECT ap.ad_id, GROUP_CONCAT(DISTINCT p.name) AS products
FROM ads_products AS ap
JOIN products AS p ON ap.product_id = p.id
GROUP BY ap.ad_id
) AS p ON ads.ad_id = p.ad_id
WHERE ad_clicks>0;
And it generates the following result:
| ad_name | clicks | product_sale | products |
| ------- | ------ | ------------ | ----------------- |
| ad1 | 11 | 99 | prod1,prod2 |
| ad2 | 6 | 15 | prod2,prod3,prod4 |
But I want this(there is a difference in the product_sale column)
| ad_name | clicks | product_sale | products |
| ------- | ------ | ------------ | ----------------- |
| ad1 | 11 | 55 | prod1,prod2 |
| ad2 | 6 | 15 | prod2,prod3,prod4 |
54 because it counts only rows with id: 1,3,4 from products_insight because in these days ad with id 1 was active. (active means that there is a row in ads_insight table.

MySQL - selecting all rooms including partial date ranges

Im currently have room availability present which displays current open rooms for bookings given a specified date range.
I need to display the same availability but instead of displaying rooms for the FULL availability i need to show partial availability.
Eg: booking 1 is from dates 22nd to 25th (within room 4)
booking 2 is from dates 24th to 28th (within room 3)
queried booking is from 23rd till 25th
22nd 23rd 24th 25th 28th
|-----------------------|
|------------------|
|------| free space
query:
SELECT r.*
, CASE WHEN b.ref IS NULL THEN 'all' ELSE 'partial' END status
FROM roominfo r
LEFT JOIN bookroom br ON br.id = r.id
LEFT JOIN book b ON b.ref = br.ref
AND b.end_date >= '2019-11-23' AND b.start_date <= '2019-11-25'
ORDERBY r.id;
example structure & data:
CREATE SCHEMA TEST;
USE TEST;
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'),('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 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), (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);
desired output:
id (& some indication of partial availability?)
1 all
2 all
3 partial
4 partial
5 all
6 all
7 all
8 all
9 all
10 all
Ignoring the distressing naming policy...
DROP TABLE IF EXISTS book;
CREATE TABLE BOOK( Ref INT NOT NULL AUTO_INCREMENT, Start_Date DATE NOT NULL, End_Date DATE NOT NULL, PRIMARY KEY(Ref));
DROP TABLE IF EXISTS roominfo;
CREATE TABLE ROOMINFO( ID INT NOT NULL AUTO_INCREMENT, `Type` VARCHAR(10) NOT NULL, capacity TINYINT NOT NULL, PRIMARY KEY(ID));
DROP TABLE IF EXISTS bookroom;
CREATE TABLE BOOKROOM( Ref INT NOT NULL,ID INT NOT NULL);
INSERT INTO BOOK(Start_Date, End_Date) VALUES
("2019-11-03", "2019-11-10"), ("2019-11-05", "2019-11-13");
INSERT INTO ROOMINFO (ID, `Type`,capacity) 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);
SELECT r.*
, CASE WHEN b.ref IS NULL THEN 'all' ELSE 'partial' END status
FROM roominfo r
LEFT
JOIN bookroom br
ON br.id = r.id
LEFT
JOIN book b
ON b.ref = br.ref
AND b.end_date >= '2019-11-01' AND b.start_date <= '2019-11-13'
ORDER
BY r.id;
+----+--------+----------+---------+
| ID | Type | capacity | status |
+----+--------+----------+---------+
| 1 | Family | 4 | all |
| 2 | Family | 4 | all |
| 3 | Family | 4 | partial |
| 4 | Dual | 2 | partial |
| 5 | Dual | 2 | all |
| 6 | Dual | 2 | all |
| 7 | Dual | 2 | all |
| 8 | Dual | 2 | all |
| 9 | Dual | 2 | all |
| 10 | Dual | 2 | all |
+----+--------+----------+---------+

SQL - get distinct values and its frequency count of occurring in a group

I have a table data as:
CREATE TABLE SERP (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
s_product_id INT,
search_product_result VARCHAR(255)
);
INSERT INTO SERP(s_product_id, search_product_result)
VALUES
(0, 'A'),
(0, 'B'),
(0, 'C'),
(0, 'D'),
(1, 'A'),
(1, 'E'),
(2, 'A'),
(2, 'B'),
(3, 'D'),
(3, 'E'),
(3, 'D');
The data set is as follows:
s_product_id | search_product_result
___________________________________________
0 | A
0 | B
0 | C
0 | D
-------------------------------------------
1 | A
1 | E
-------------------------------------------
2 | A
2 | B
-------------------------------------------
3 | D
3 | E
3 | D
I need to list all distinct search_product_result values and count frequencies of these values occurring in s_product_id.
Required Output result-set:
DISTINCT_SEARCH_PRODUCT | s_product_id_frequency_count
------------------------------------------------------------
A | 3
B | 2
C | 1
D | 2 [occurred twice in 3, but counted only once.]
E | 2
Here, A occurs in three s_product_id : 0, 1, 2, B in two : 0, 2, and so on.
D occurred twice in the same group 3, but is counted only once for that group.
I tried grouping by search_product_result, but this counts D twice in the same group.
select search_product_result, count(*) as Total from serp group by search_product_result
Output:
search_product_result | Total
------------------------------------
A | 3
B | 2
C | 1
D | 3 <---
B | 2
You can try below - use count(distinct s_product_id)
select search_product_result, count(distinct s_product_id) as Total
from serp group by search_product_result
use count(distinct()
select search_product_result, count(distinct s_product_id, search_product_result) as Total
from SERP
group by search_product_result
see dbfiddle

SQL Server finding percentage using already calculated total

I have a table that contains Salary values and totals for each company. I want to find percentage and not sure how?
The istotal flag represents whether it is total or not; 0 means not total and 1 means total
create table work.temp
(
empid int,
Salaryvalue float,
istotal smallint
)
insert into work.temp
values (1, 10.0, 0), (1, 20.0, 0), (1, 30.0, 0), (1, 60.0, 1)
This is the expected output:
create table work.output
(
empid int,
salaryvalue float,
issubtotal smallint,
percentage float
)
insert into work.output
values (1, 10.0, 16.6), --(10.0/60.0)*100
(1, 20.0, 33.3), --(20.0/60.0)*100
(1, 30.0, 50.0), --(30.0/60.0)*100
(1, 60.0, 100.0) --(60.0/60.0)*100
Not quite sure how to do it
Thanks mr
You can reference the SalaryValue total for each empid on every row using windowed aggregates (The isnull nullif pair is there to handle divide by zero errors):
declare #t table(empid int,SalaryValue float,istotal smallint);
insert into #t values(1, 10.0, 0), (1, 20.0, 0), (1, 30.0, 0), (1, 60.0, 1);
select empid
,SalaryValue
,istotal
,isnull(SalaryValue / nullif(sum(case when istotal = 1 then SalaryValue else 0 end) over (partition by empid),0),0) as Percentage
from #t;
Output:
+-------+-------------+---------+-------------------+
| empid | SalaryValue | istotal | Percentage |
+-------+-------------+---------+-------------------+
| 1 | 10 | 0 | 0.166666666666667 |
| 1 | 20 | 0 | 0.333333333333333 |
| 1 | 30 | 0 | 0.5 |
| 1 | 60 | 1 | 1 |
+-------+-------------+---------+-------------------+
Alternatively, you could join the table to itself with one version returning istotal = 1 and the other istotal = 0:
declare #t table(empid int,SalaryValue float,istotal smallint);
insert into #t values(1, 10.0, 0), (1, 20.0, 0), (1, 30.0, 0), (1, 60.0, 1);
with t as
(
select empid
,SalaryValue
from #t
where istotal = 1
)
select s.empid
,s.SalaryValue
,t.SalaryValue as Total
,isnull(s.SalaryValue / nullif(t.SalaryValue,0),0) as Percentage
from #t as s
left join t
on s.empid = t.empid
where s.istotal = 0;
Output:
+-------+-------------+-------+-------------------+
| empid | SalaryValue | Total | Percentage |
+-------+-------------+-------+-------------------+
| 1 | 10 | 60 | 0.166666666666667 |
| 1 | 20 | 60 | 0.333333333333333 |
| 1 | 30 | 60 | 0.5 |
+-------+-------------+-------+-------------------+

Selecting unique rows with maximum values (with conditions)

With the table and data below I am trying to get the highest effective_from values that are less than the current timestamp, per unique brand/model combination - effectively the current price per item.
CREATE TABLE things
(`id` int, `brand` varchar(1), `model` varchar(5), `effective_from` int, `price` int);
INSERT INTO things
(`id`, `brand`, `model`, `effective_from`, `price`)
VALUES
(1, 'a', 'red', 1402351200, 100),
(2, 'b', 'red', 1402351200, 110),
(3, 'a', 'green', 1402391200, 120),
(4, 'b', 'blue', 1402951200, 115),
(5, 'a', 'red', 1409351200, 150),
(6, 'a', 'blue', 1902351200, 140),
(7, 'b', 'green', 1402358200, 135),
(8, 'b', 'blue', 1902358200, 155),
(9, 'b', 'red', 1902751200, 200),
(10, 'a', 'red', 1908351200, 210),
(11, 'a', 'red', 1402264800, 660);
So far I have managed to get the row I'm looking for when I add conditions for a specific brand/model combination, but don't know how to fetch the current prices for all unique row combinations.
SELECT *
FROM things
WHERE effective_from<UNIX_TIMESTAMP()
AND brand='a'
AND model='red'
ORDER BY effective_from DESC
LIMIT 1;
If the current timestamp was 1402404432 the results should be as follows:
(1, 'a', 'red', 1402351200, 100),
(3, 'a', 'green', 1402391200, 120),
(2, 'b', 'red', 1402351200, 110),
(7, 'b', 'green', 1402358200, 135),
I guess you're after this. Advise if otherwise...
SELECT x.*
FROM things x
JOIN
( SELECT brand
, model
, MAX(effective_from) max_effective_from
FROM things
WHERE effective_from <= UNIX_TIMESTAMP()
GROUP
BY brand
, model
) y
ON y.brand = x.brand
AND y.model = x.model
AND y.max_effective_from = x.effective_from;
+------+-------+-------+----------------+-------+
| id | brand | model | effective_from | price |
+------+-------+-------+----------------+-------+
| 1 | a | red | 1402351200 | 100 |
| 2 | b | red | 1402351200 | 110 |
| 3 | a | green | 1402391200 | 120 |
| 7 | b | green | 1402358200 | 135 |
+------+-------+-------+----------------+-------+
SELECT UNIX_TIMESTAMP();
+------------------+
| UNIX_TIMESTAMP() |
+------------------+
| 1402404432 |
+------------------+