Assuming I have something like this :
MySQL table
Date | Val
2013/11/22 | 2
2013/11/23 | 4
2013/11/25 | 12
2013/11/30 | 28
2013/12/02 | 2
I need a query to get on an other column the sum of the "current" row's value plus the previous row's value.
With the example, the result would be something like this :
Date | Value | Total
2013/11/22 | 2 | 2
2013/11/23 | 4 | 6 <--- Because 4+2 = 6
2013/11/25 | 12 | 16
2013/11/30 | 28 | 40
2013/12/02 | 2 | 30
The problem is that I can't use variables because I'm on a view.
How can I do this ?
Any help is appreciated.
SELECT
t.Date,
t.Val,
COALESCE((SELECT Val FROM Table1 sq WHERE sq.Date < t.Date ORDER BY sq.Date DESC LIMIT 1), 0) + t.Val AS whatever
FROM
Table1 t
ORDER BY t.Date
see it working live in an sqlfiddle
Related
Hello I have a table here
---------------------
ID | PARTY | Name.Id
---------------------
1 | IND | 12
2 | IND | 13
3 | CUST | 14
4 | CUST | 15
5 | CUST | 16
6 | IND | 17
---------------------
I want to return the whole table but filter 'CUST' which has value 15 and 16 in the column 'Name.Id'
The result should look something like this
---------------------
ID | PARTY | Name.Id
---------------------
1 | IND | 12
2 | IND | 13
3 | CUST | 14
4 | IND | 17
---------------------
I tried using where statement on the 'Name.Id' but it returns only those rows which has a value 15 and 16.
whole table but filter 'CUST' which has value 15 and 16 in the column 'Name.Id'
It sounds like you want a where clause like:
WHERE NOT (party = 'CUST' and name.id IN(15,16))
But CUST seems redundant from your sample data, i.e. you could get away with simplifying it to just WHERE name.id NOT IN (15,16)
Can you give this a whirl?
select *
from t
where (PARTY, Name.Id) not in (
select 'CUST', 15
union
select 'CUST', 16
)
select *
from t
where Party not in
(select Party from t
where Party="CUST"
and (Name.Id=15 or Name=16));
I think I got the solution here
select *
from t
where Party <> 'CUST'
or (Party = 'CUST'
and Name.Id = 14))
I am trying to do this with SQL. I have a transaction table which contain transaction_date. After grouping by date, I got this list:
| transaction_date |
| 2019-03-01 |
| 2019-03-04 |
| 2019-03-05 |
| ... |
From these 3 transaction dates, I want to achieve:
Average = ((4-1) + (5-4)) / 2 = 2 days (calculate DATEDIFF every single date)
Minimum = 1 day
Maximum = 3 days
Is there any good syntax? Before I iterate all of them using WHILE.
Thanks in advance
If your mysql version didn't support lag or lead function.
You can try to make a column use a subquery to get next DateTime. then use DATEDIFF to get the date gap in a subquery.
Query 1:
SELECT avg(diffDt),min(diffDt),MAX(diffDt)
FROM (
SELECT DATEDIFF((SELECT transaction_date
FROM T tt
WHERE tt.transaction_date > t1.transaction_date
ORDER BY tt.transaction_date
LIMIT 1
),transaction_date) diffDt
FROM T t1
) t1
Results:
| avg(diffDt) | min(diffDt) | MAX(diffDt) |
|-------------|-------------|-------------|
| 2 | 1 | 3 |
if your mysql version higher than 8.0 you can try to use LEAD window function instead of subquery.
Query #1
SELECT avg(diffDt),min(diffDt),MAX(diffDt)
FROM (
SELECT DATEDIFF(LEAD(transaction_date) OVER(ORDER BY transaction_date),transaction_date) diffDt
FROM T t1
) t1;
| avg(diffDt) | min(diffDt) | MAX(diffDt) |
| ----------- | ----------- | ----------- |
| 2 | 1 | 3 |
View on DB Fiddle
I have a problem with SQLcode
I have a table
id | content | id_user | id_store
1 | abc | 1 | 10
2 | xzy | 1 | 10
3 | abc | 1 | 10
4 | abc | 1 | 11
5 | abc | 1 | 12
My problem is how i got the result is the count of max (id_store) which is 2* value >= max(id_store)
This is a example, result will be
id_store | count(...)
10 | 3
because (3*2) > max of count = 3
Tks everyone
It's very difficult to understand your question. Try to use the next query
SELECT id_store,COUNT(*) CountOfStore
FROM `Your Table`
GROUP BY id_store
HAVING 2*COUNT(*) >= (
SELECT MAX(CountOfStore) -- max of all CountOfStore
FROM
(
SELECT COUNT(*) CountOfStore -- count of store for each id_store
FROM `Your Table`
GROUP BY id_store
)
)
Hope I understood you rightly.
I want to calculate the difference in unique date fields between different rows in the same table.
For instance, given the following data:
id | date
---+------------
1 | 2011-01-01
2 | 2011-01-02
3 | 2011-01-15
4 | 2011-01-20
5 | 2011-01-10
6 | 2011-01-30
7 | 2011-01-03
I would like to generate a query that produces the following:
id | date | days_since_last
---+------------+-----------------
1 | 2011-01-01 |
2 | 2011-01-02 | 1
7 | 2011-01-03 | 1
5 | 2011-01-10 | 7
3 | 2011-01-15 | 5
4 | 2011-01-20 | 5
6 | 2011-01-30 | 10
Any suggestions for what date functions I would use in MySQL, or is there a subselect that would do this?
(Of course, I don't mind putting WHERE date > '2011-01-01' to ignore the first row.)
A correlated subquery could be of help:
SELECT
id,
date,
DATEDIFF(
(SELECT MAX(date) FROM atable WHERE date < t.date),
date
) AS days_since_last
FROM atable AS t
Something like this should work :
SELECT mytable.id, mytable.date, DATEDIFF(mytable.date, t2.date)
FROM mytable
LEFT JOIN mytable AS t2 ON t2.id = table.id - 1
However, this imply that your id are continuous in your table, otherwise this won't work at all. And maybe MySQL will complain for the first row since t2.date will be null but I don't have the time to check now.
I'm trying to do something like 'select groupwise maximum', but I'm looking for groupwise order number.
so with a table like this
briefs
----------
id_brief | id_case | date
1 | 1 | 06/07/2010
2 | 1 | 04/07/2010
3 | 1 | 03/07/2010
4 | 2 | 18/05/2010
5 | 2 | 17/05/2010
6 | 2 | 19/05/2010
I want a result like this
breifs result
----------
id_brief | id_case | dateOrder
1 | 1 | 3
2 | 1 | 2
3 | 1 | 1
4 | 2 | 2
5 | 2 | 1
6 | 2 | 3
I think I want to do something like described here MySQL - Get row number on select, but I don't know how I would reset the variable for each id_case.
This will give you how many records are there with this id_case value and a date less than or equal to this date value.
SELECT t1.id_brief,
t1.id_case,
COUNT(t2.*) AS dateOrder
FROM yourtable AS t1
LEFT JOIN yourtable AS t2 ON t2.id_case = t1.id_case AND t2.date <= t1.date
GROUP BY t1.id_brief
Mysql is permissive about columns which can be queries using GROUP BY. With a more stric DBMS you may need GROUP BY t1.id_brief, t1.id_case.
I strongly advise you to have the right indexes on the table:
CREATE INDEX filter1 ON yourtabl (id_case, date)