Concat results by days ranges - mysql

I have an apartment table:
Id: name:
1 apartment1
2 apartment2
3 apartment3
3 apartment4
And an availabilities table:
Id apartment_id availability_date remaning_number_places
1 1 01/02/2017 3
2 1 02/02/2017 2
3 1 03/02/2017 2
4 1 04/02/2017 2
5 2 01/02/2017 2
6 2 03/02/2017 2
7 2 04/02/2017 2
8 3 12/02/2017 2
9 3 03/02/2017 1
10 3 04/02/2017 1
11 4 12/02/2017 2
12 4 02/02/2017 2
I would like to do a search by starting and ending date with a minimum number of places, for example if I search from 01/02/2017 to 04/02/2017 I should have this result:
Apartment_ids availabilities starting_date ending_date nbr_places
[1] [1,2,3,4] 01/02/2017 04/02/2017 2
[2,4] [5,6,7,12] 01/02/2017 04/02/2017 2
The result must be a concatenation of contiguous days corresponding to given dates.
Thank you at advance.
EDIT : Trying to give more details hereunder:
I need an SQL query to look for an apartment availability for a given period.
As a result, if an apartment is available for the whole period I'm expecting to get it. In addition, I also need to get if more than one apartment are required to cover this given period. In that case, I'm expecting a contiguous list of apartments as a result.
Please, can someone help me build this query?

Related

Count the duplicate values on multiple array and sort it to highest

I have a target where I have this DUPLICATE checker with COUNT for multiple arrays. As we can see. Any suggestions on how I make this work? Thank you and have a nice day.
DATA:
id
value
1
[5,6,8,4,2]
2
[2,3,4,1,8]
3
[9,3,2,1,10]
Normal result:
number
count
1
2
2
3
3
2
4
2
5
1
6
1
7
0
8
2
9
1
10
1
This is my target result with sorting (Highest count):
number
count
2
3
1
2
3
2
4
2
8
2
5
1
6
1
10
1
9
1

Sum of all child and assign to parent in hierarchical data in MySql

I have a 2 Mysql tables like 1.Product(This table is hierarchical or recursive) and 2.Sales table in this will store sale id and product id. I need to show parent product with the sum of all hierarchical child product.
Product table having data like,
id name parent_id
1 Necklace NULL
2 Ring NULL
3 Earing NULL
4 Choker 1
5 Long Necklace 1
6 Short Necklace 1
7 2-Line 5
8 3-Line 5
9 Mango 5
10 Green 7
11 Red 7
12 White 7
13 Stud 3
Sales table will have data like,
id product_id no_of_pcs weight rate
1 10 5 40 35000
2 12 8 50 50000
3 9 2 20 25000
4 6 1 8 25000
5 13 2 16 22000
Now I'm trying the result as,
Product sale_pcs tot_pcs sale_wt sale_rate
1 Necklace 16 118 135000
3 Earing 2 16 22000
Now I'm using mysql query inside php recursive function by using product table. By using this even recursive function all the products need to check with sales table. This will happening performance issue. Could you please help any one to solve this issue whether this possible by doing in query itself

How to get total of parents categories?

I have two tables, the first is the following
"Categories"
id name parent_id
1 Food NULL
2 Vegetable 1
3 Fruit 1
4 Tomatoes 2
5 onion 2
6 bananas 3
7 grapes 3
the second table is the following
"amounts"
id amount category_id
1 100 4
2 50 6
3 25 5
4 100 7
5 50 4
6 25 7
Currently categories table are three levels and I want to return all
the Categories along with total columns:
Means that sum of the category of level 2 is the total of level 3 and
the total of level 2 should was the sum of level 1. I would really
appreciate some help Beforehand thank you very much.

Is it possible to display this data horizontally in SSRS?

I have a table that that is being joined by another table that looks like this:
Id Score Total
1 10 30
1 7 30
1 13 30
2 14 27
2 10 27
2 3 27
I want to be able to display this data like this in SSRS:
Id 1 2 3 Total
1 10 7 13 30
2 14 10 3 27
Can this be done and how?
You can do this by using a matrix.
You can add a row identifier for each id in your dataset (assuming you can modify the dataset, as you joined 2 tables). Below code is for SQL Server (T-SQL).
Select Id, Score, row_number() over (partition by id order by score) ident
from table
Output:
Id Score Ident
1 10 1
1 7 2
1 13 3
2 14 1
2 10 2
2 3 3
No need of the Total field, you can add it in matrix (Right Click on ColumnGroup>Add Total>After).
Use the above query in Matrix as shown below.

MySQL query - need assistance creating this query

I need some direction on how to create this MySQL query.
I have a table that looks like this.
id group name value
1 1 user mike
2 1 setting 1
3 2 user joe
4 2 setting 2
5 3 user jill
6 3 setting 1
7 4 user mark
8 4 setting 1
9 4 other 22
I would like to format the query to group users that have identical settings (IE mike and jill would be grouped in this example, not mark because of "other")
At the end of the day I am trying to consolidate and make the table look like this. If I can figure out the query to properly group them ,I will use PHP to combine the values and save it back to the DB.
id group name value
1 1 user mike OR jill
2 1 setting 1
3 2 user joe
4 2 setting 2
7 4 user mark
8 4 setting 1
9 4 other 22
Thank you!