I want for each projet to have the highest reach of etape.
select nometape, description
from etape
where idetape in(
select max(idetape) from etapexprojet)
where distinct(idprojet));
my tab look like
etape
idetape/nometape/description
1/debut/commencement
2/rédaction/avancement
3/fin/fin
etapexprojet
idetape/idprojet
1/1
2/1
1/2
2/2
3/2
1/3
I got an error on the line 5 but i don'T know how to overcome it
Related
This question already has answers here:
Define a variable within select and use it within the same select
(4 answers)
Closed 7 months ago.
I have the following tables:
Stocks
id
name
amount
1
Pen
35
2
Cd
21
3
Bag
15
StockUnits
id
name
include
stockid
1
onepen
1
1
2
dozen
6
1
3
pocket
24
1
4
onecd
1
2
5
pocket
5
2
6
onebag
1
3
So how do I get a result like the following with SELECT query?
stock
unit
qty
Pen
onepen
5
Pen
dozen
1
Pen
pocket
1
Cd
onecd
1
Cd
pocket
4
Bag
onebag
15
As we see, each stock has some units.
Now I want to know how many units do i have according to the amount field, For example:
We have 35 pens which is equal to 1 pocket and 1 dozen and 5 onepen because:
For pocket we have 1 pocket because (each pocket includes 24 pen)
35/24=1
35%24=11
For dozen we have 1 dozen because (each dozen includes 6 pen)
11/6=1
11%6=5
For onepen we have 5 onepen because
5 onepen because we don't have any smaller unit
Now we have:
1 pocket
1 dozen
5 onepenSame calculation for Cd and Bag...
I know to get the desired result a temporary variable should be used to store the result of the MOD and use the variable for the next row but how?
How to store a result of a row in a variable and use it in the next row in MySQL SELECT query?
A pattern for MySQL version 5.x:
SELECT .. ,
{an expression which uses #variable with the value from previous row} AS calculated_column,
.. ,
#variable := {an expression for current row which will be used for next row processing} AS dummy_column,
..
FROM {source single table or subquery}
CROSS JOIN ( SELECT #variable := {initial variable value} ) AS init_variable
ORDER BY {ordering expression which provides rows uniqueness}
On MySQL 8+ use window function(s).
First of all, your Stocks and StockUnits make no sense, or they are at least bad designed.
You show that you store the id of each stock-row in multiple unit-rows, but it really should be the other way around.
How Stocks table should be:
id
name
qty
unit_id
1
Pen
5
1
2
Pen
1
2
3
Pen
1
3
...
How StockUnits table should be:
id
name
include
1
onepen
1
2
dozen
6
3
pocket
24
...
Example
Once you fix the design mistake, all you would need to do is join to get unit's name, for example in Laravel do something like:
$stockTable = Stock::with('unit')->all();
echo 'stock | unit | qty';
foreach ($stockTable as $item) {
echo $item->name . ' | ' . $item->unit->name . ' | ' . $item->qty;
}
To get output:
name
unit
qty
Pen
onepen
5
Pen
dozen
1
Pen
packet
1
...
I keep on looking around StackOverflow for a similar question but it seems that I can't find one. I would like to know the difference between timestamps in different rows grouped by employee ID.
Time Logs table:
id timestamp log_type
1 2019-06-19 12:34:50 log_in
2 2019-06-19 13:12:46 start_break
3 2019-06-19 13:13:56 end_break
4 2019-06-19 17:23:40 start_break
5 2019-06-19 17:44:36 end_break
6 2019-06-19 19:00:04 start_break
7 2019-06-19 19:03:17 end_break
8 2019-06-19 20:05:54 log_out
What I'm trying to accomplish is to calculate all duration of breaks. In this case, 1st break (id #2 and #3) is 1 minute and 10 seconds, 2nd break (id #4 and #5) is 20 minutes and 56 seconds, 3rd break (id #6 and #7) is 3 minutes and 13 seconds thus with the total of 25 minutes and 19 seconds.
Thanks for helping out! Much appreciated.
You can try below -
DEMO
select SEC_TO_TIME(sum(diff)) as result from
(
select
timestampdiff(second,min(case when log_tpe='start_break' then timestamps end) ,
min(case when log_tpe='end_break' then timestamps end)) as diff
from t
group by date(timestamps),hour(timestamps)
)A
OUTPUT:
result
00:25:19
I am very new to Microsoft reporting. I have the following table in my database:
CategoryName Id
Normal 1
High 2
Normal 3
Low 4
Normal 5
Normal 6
Normal 7
Normal 8
Low 9
Low 10
Low 11
High 12
I want to group by Category and also show the count of each category. Here is what I did:
I inserted a two column table and I grouped by the categoryName in the first column and in the second column, I tried doing
=CountDistinct(Fields!CategoryName.Value)
This is what I am seeing in the report
High 1
1
Normal 1
1
1
1
1
1
1
Low 1
1
1
1
I want to see something like this:
Category Count
Normal 6
Low 4
High 2
any help will be highly appreciated
Delete the Detail row and put the Count epxression in the Group row.
You'd probably be better off doing this in the query. Easier and leaves less room for error. Something like the following should work.
SELECT categoryName, COUNT(*)
FROM your table
GROUP BY categoryName
I have been at this for a few days without much luck and I am looking for some guidance on how to get the lowest estimate from a particular group of sullpiers and then place it into another table.
I have 4 supplier estimate on every piece of work and all new estimates go into a single table, i am trying to find the lowest 'mid' price from the 4 newsest entries in the 'RECENT QUOTE TABLE' with a group id of '1' and then place that into the 'LOWEST QUOTE TABLE' as seen below.
RECENT QUOTE TABLE:
suppid group min mid high
1 1 200 400 600
2 1 300 500 700
3 1 100 300 500
[4] [1] 50 [150] 300
5 2 1000 3000 5000
6 2 3000 5000 8000
7 2 2000 4000 6000
8 2 1250 3125 5578
LOWEST QUOTE TABLE:
suppid group min mid high
4 1 50 150 300
Any help on how to structure this would be great as i have been loking for a few days and have not been able to find anything to get me moving again, im using MYSQL and the app is made in Python im open to all suggestions.
Thanks in advance.
If you really want to select only row with group 1, you can do something like
INSERT INTO lowest_quote_table
SELECT * FROM recent_quote_table
WHERE `group` = 1
ORDER BY `mid` ASC
LIMIT 1.
If you want a row with the lowest mid from every group, you can do something like
INSERT INTO lowest_quote_table
SELECT rq.* FROM recent_quote_table AS rq
JOIN (
SELECT `group`, MIN(`mid`) AS min_mid FROM recent_quote_table
GROUP BY `group`
) MQ ON rq.`group` = MQ.`group` AND rq.`mid` = MQ.min_mid
In mysql, I need a query that returns the quantity of repeated values in the field "Info" of my table "Log".
Table Log:
ID_Log User Info
1 1 3
2 1 3
3 1 3
4 1 5
5 1 6
6 1 6
7 1 7
8 1 8
9 1 8
The query should return "4" (Info 3 appears three times, Info 6 appears two times, Info 8 appears two times).
Any suggestions?
You can get the number of values that have already appeared by using a simple subtraction. Subtract the number of distinct values from the total number of rows:
select count(*) - count(distinct info)
from log;
The difference is the number that "repeat".
This should work. Group the values of info together and only keep the results where the number of occurrences minus 1 is greater than 0. Then sum the numbers of occurrences.
select sum(repeats)
from (SELECT Info, count(*) - 1 AS repeats
FROM Log
GROUP BY Info
HAVING repeats > 0)