I have this Table A:
ITEM
DATE
COUNT
A
04.11.2021 13:38:51
1
A
04.11.2021 08:07:44
1
A
04.11.2021 08:07:42
1
A
04.11.2021 13:53:28
1
B
22.10.2021 21:14:19
1
B
22.10.2021 16:18:09
1
B
22.10.2021 18:15:11
1
C
06.11.2021 22:08:46
1
And i would like to Group and count the item by Date (no time) like this:
|ITEM|DATE|COUNT|
|----|----|-----|
|A|04.11.2021 |4|
|B|22.10.2021 |3|
|C|06.11.2021 |1|
That would be:
Select
[Item],
DateValue([Date]) As ItemDate,
Sum([Count])
From
[Table A]
Group By
[Item],
DateValue([Date]) As ItemDate
method1:
Change the DATE format to yyyy-mm-dd or each other Date format.
Then change in Values DATE from Don't summarize to Earliest or Lastest.
This method will change the DATE format from Datetime to Date and will also change in other Visuals formats. If you used Date base on datetime in other visuals, the problem is happened.
method2:
Create a new column(_date = 'Table A'[DATE].[Date]) or new measure(_date= max('Table A'[DATE].[Date])), and use it instead of DATE in the visual.
You can also use _COUNT=COUNT('Table A'[DATE]) instead of COUNT.
Related
I am attempting to plot data cumulatively from a MySQL table which logs a value, resetting to 0 every day. After selecting the values using select * from table where DateTime BETWEEN DateA AND DateB, the data looks like this: current data. I would like the output to look like this: preferred data, ignoring the daily resets.
As I am a novice in SQL I was unable to find a solution to this. I did, however, obtain the correct output in Matlab using a for loop:
output = data;
for k=1:(size(data, 1)-1)
% check if next value is smaller than current
if data(k+1)<data(k)
% add current value to all subsequent values
output = output + (1:size(data, 1)>k)'.*input(k);
end
end
I would like the final product to connect to a web page, so I am curious if it would be possible obtain a similar result using only SQL. While I have tried using SUM(), I have only been able to sum all values, but I need to add the last value each day to all subsequent values.
Using CTE and comparing dates, you can sum all values each date.
Let's say that table1 below is defined.
create table table1 (col_date date, col_value int);
insert into table1 values
('2020-07-15',1000),
('2020-07-15',2000),
('2020-07-16',1000),
('2020-07-16',3000),
('2020-07-16',4000),
('2020-07-17',1000),
('2020-07-18',2000),
('2020-07-19',1000),
('2020-07-19',1000),
('2020-07-19',2000),
('2020-07-19',3000),
('2020-07-20',4000),
('2020-07-20',5000),
('2020-07-21',6000)
;
In this case, the query looks like this:
with cte1 as (
select col_date, sum(col_value) as col_sum from table1
where col_date between '2020-07-16' and '2020-07-20'
group by col_date
)
select a.col_date, max(a.col_sum), sum(b.col_sum)
from cte1 a inner join cte1 b on a.col_date >= b.col_date
group by a.col_date;
The output is below:
col_date |max(a.col_sum) |sum(b.col_sum)
2020-07-16 |8000 | 8000
2020-07-17 |1000 | 9000
2020-07-18 |2000 |11000
2020-07-19 |7000 |18000
2020-07-20 |9000 |27000
The column of max() is just for reference.
Imagine the following table:
ID Name Type Start_Date End_Date
1 Cust_1 Retail 01/01/2010 21/07/2010
1 Cust_1 Oher 22/07/2010 17/05/2012
1 Cust_1 Corp 18/05/2012 31/12/9999
I would to know if there's any way to build a view that return the correct type, according to a date sent as parameter. For example:
SELECT Type WHERE date_to_analyse = '21/06/2010' should return 'Retail'
SELECT Type WHERE date_to_analyse = '18/06/2012' should return 'Corp'
Date_to_analyse is not a column of the table, I want to avoid using Stored Procedures.
My idea is making the query "clean", instead of using the following query
SELECT Type WHERE date_to_analyse >= Start_Date AND date_to_analyse < End_Date
Is this even possible?
Thanks
Got a table that holds dates and values, looks like this:
...Date........Value
2016-10-01 2.3
2016-10-01 1.6
2016-10-01 7.0
2016-10-02 2.4
2016-10-02 1.9
2016-10-02 7.3
etc, so multiple dates with multiple values for each date.
I can't figure out how to write the SQL to get to return a single row with the minimum value for each day.
Tried this but get error:Error in query (1054): Unknown column 'm.seldate' in 'on clause'
SELECT DISTINCT seldate
FROM mytable s
LEFT
JOIN
( SELECT MIN(myval) minval
FROM mytable
GROUP
BY seldate
) m
ON s.seldate = m.seldate
The following will return the minimum myvalue and the corresponding 'description' column (which I omitted in the original post) for each unique value of seldate within the table:
SELECT seldate, description, myvalue
FROM selections s1
WHERE myvalue=(SELECT min(s2.myvalue)
FROM selections s2
WHERE s1.seldate = s2.seldate);
Try:
select seldate, min(myval) as minval
from mytable
group by seldate
How do i calculate the percentage from the same column, between two dates?
I have column latest.
For example: 2015-11-16 the value was 159,4 and today the value is 160,1.
And between these dates there is other values, that i´m not interested in at the moment. How to i calculate the percentage difference from that specific date, compared to "todays" date.
EDIT
SELECT curr.latest * 100 / NULLIF(prev.latest, 0) as percentage
FROM myTable AS curr, myTable AS prev
WHERE date(prev.timestamp) = '2015-11-16'
AND date(curr.timestamp) = CURDATE()
AND curr.the_row_namn = 'apple'
Percentage of column latest of a specific date ex. 2015-11-16, with the the_row_namn of apple from the table myTable.
What is the percentage difference of column latest for apple of the day 2015-11-16 to today.
+--------------+--------+------------+
| the_row_namn | latest | timestamp |
+--------------+--------+------------+
| apple | 159,40 | 2015-11-16 |
| apple | 164,1 | 2015-11-17 |
+--------------+--------+------------+
Expected output: (rounded) percentage: 0,2864 or even better if that is possible 2,8%
Hope this will clarify things.
You could do this with the following SELECT statement:
SELECT curr.name,
curr.value * 100 / NULLIF(prev.value, 0) as percentage
FROM myTable AS curr
INNER JOIN myTable AS prev
ON curr.name = prev.name
WHERE prev.latest = '2015-10-26'
AND curr.latest = CURDATE()
AND curr.name = 'apple';
If you leave out the last condition, you'll get a result per possible value of name.
The main point of this query is that you use your table twice, once to select the current record, and once to retrieve the earlier record. You should just specify the date of your choice in the first line of the where clause.
Once you have both records, it is straightforward to calculate a percentage from the value columns of these two records.
The NULLIF is there to protect you from division by zero errors. Instead you'll get a NULL result.
This is the general form such a query can take:
SELECT a.value / b.value AS abPerc
FROM the_table AS a
INNER JOIN the_table AS b ON [pairing criteria]
;
I have a table code_prices that looks something like this:
CODE | DATE | PRICE
ABC | 25-7-2011 | 2.81
ABC | 23-7-2011 | 2.52
ABC | 22-7-2011 | 2.53
ABC | 21-7-2011 | 2.54
ABC | 20-7-2011 | 2.58
ABC | 17-7-2011 | 2.42
ABC | 16-7-2011 | 2.38
The problem with the data set is there are gaps in the dates, so I may want to look for the price of item ABC on the 18th however there is no entry because the item wasnt sold on this date. So I would like to return the most recent hisotrical entry for the price.
Say if I query on the date 19-7-2011, I would like to return the entry on the 17th then the next 10 avalaible entries.
If however I query for the price of ABC on the 20th, I would want to return the price on the 20th and the next 10 prices after that...
What is the most efficient way to go about this either in SQL statement or using a stored proc.
I can think of just writing a stored proc which takes the date as a param and then querying for all rows where DATE >= QUERY-DATE ordering by the date and then selecting the 11 items (via limit). Then basically I need to see if that set contains the current date, if it does then return, otherwise I will need to return the 10 most recent entires out of those 11 and also do another query on the table to return the previous entry by getting the max date where date < QUERY-DATE. I am thinking there might be a better way, however I'm not an expert with SQL (clearly)...
Thanks!
This is for one specific code:
SELECT code, `date`, price
FROM code_prices
WHERE code = #inputCode
AND `date` >=
( SELECT MAX(`date`)
FROM code_prices
WHERE code = #inputCode
AND `date` <= #inputDate
)
ORDER BY `date`
LIMIT 11
For ABC and 19-7-2011, the above will you give the row for 17-7-2011 and the 10 subsequent rows (20-7-2011, 21-7-2011, etc)
I'm not entirely clear on what you want to achieve, but I'll have a go anyway. This searches for the ID of the row that contains a date less than or equal to your specified date. It then uses that ID to return all rows with an ID greater than or equal to that value. It assumes that you have a column other than the date column on which the rows can be ordered. This is because you said that the dates are non-linear - I assume that you must have some other way of ordering the rows.
SELECT id, code, dt, price
FROM code_prices
WHERE id >= (
SELECT id
FROM code_prices
WHERE dt <= '2011-07-24'
ORDER BY dt DESC
LIMIT 1 )
ORDER BY id
LIMIT 11;
Alternative with code condition - thanks to #ypercube for highlighting that ;-)
SELECT id, code, dt, price
FROM code_prices
WHERE code = 'ABC'
AND id >= (
SELECT id
FROM code_prices
WHERE dt <= '2011-07-23'
AND code = 'ABC'
ORDER BY dt DESC
LIMIT 1 )
ORDER BY id
LIMIT 11;