SQL query with 2 nested to queries on same table - sql

This question is an extension capability related to my previous question here Update column with autonumber. Now with only one table this time:
Date Adds
6/1/18 0
6/5/18 1
6/7/18 0
...+60 records
10/1/18 0
I would like to create a table of Dates, 60 date records (for ex) beyond the Date with a number in the New in Field. Using the previous method, here is what I have:
Select t1.adds, t1.date from T1 where t1.adds > 0 AND
(select count(*)+1 from t1 as t2
where t2.Date <= t1.Date AND t2.date >=
(select date from t1 as t3 where t3.date > t2.date) = 60)
I think everything would work except for the 2nd conditional statement where I need the date to be greater than the corresponding date where Adds > 0. If executed I would expect my table to look like:
Date Adds
10/1/18 1

I think this works but unsure how efficient it is yet. I just made a tbltemp with Adds and Date where Adds > 0
SELECT q1.adds, t1.Date
FROM T1, tblTemp AS q1
WHERE (select count(*) from T1 as t2 where t2.date <= t1.date AND t2.date > q1.date)=60
I will do a little more testing with more records unless anyone has any better ideas?

Related

Give the count of the minimum number of days for the time when temperature reduced

Have a table of 3,903 rows. I want to get a count for the days in which the temperature drops from one day to the next.
This is a sample of what the table shows
SELECT t1.*
FROM weather_dataset t1
WHERE t1.avg_temperature > COALESCE(
(
SELECT t2.avg_temperature
FROM weather_dataset t2
WHERE t2.date = t1.date AND t2.date < t1.date
), -1)
This is what I tried, but it showed the entire table.

SQL: Select from another table (t2) without joining but referencing a column from t1

I have a table with columns date and net_sales. For each day, I want to get the sum of the net_sales for the last 30 days.
This is my query:
thirty_days_net_sales AS (
SELECT
t1.date,
t1.net_sales AS net_sales_on_date,
(SELECT SUM(t2.net_sales) FROM total_net_sales_per_day t2 WHERE t2.date >= DATE_SUB(t1.date, INTERVAL 30 DAY) AND t2.date <= t1.date)
FROM
total_net_sales_per_day t1)
When I run this query I get the error: LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.
I am using Google BigQuery. Thanks in advance for your help!
Consider rather below approach
select *, sum(net_sales) over win last_30_days
from total_net_sales_per_day
window win as (order by unix_date(date) range between 29 preceding and current row )
You would use window functions. If you have data for every day (as the name of the table implies:
SELECT tnspd.*
sum(netsales) over (partition by date
order by date
rows between -30 and current row
)
FROM total_net_sales_per_day tnspd;
As the error said, you have to add and equal condition. In case of using join you have to use ON keyword for one equal condition.
In your query, because you do not have join explicitly, you must have an equal condition something like I added below:
thirty_days_net_sales AS (
SELECT
t1.date,
t1.net_sales AS net_sales_on_date,
(SELECT SUM(t2.net_sales) FROM total_net_sales_per_day t2 WHERE t2.date >= DATE_SUB(t1.date, INTERVAL 30 DAY) AND t2.date <= t1.date
AND t1.id==t2.id)
FROM
total_net_sales_per_day t1)
This link might help for more information:
https://sql.info/d/solved-bigquery-left-outer-join-cannot-be-used-without-a-condition-that-is-an-equality-of-fields-from-both-sides-of-the-join

Access SQL Statement Calculate the Difference From Previous Month [duplicate]

This question already has answers here:
Query for getting value from another record in same table and filter by difference greater than a gap threshold
(2 answers)
Closed 3 years ago.
I'm using Microsoft Access 2016 to developing a new program, each month the data entry enter in a row the date, the total budget plan and cumulative expending amount. I am trying to create a query showing the monthly bases experiences. For example in this month August, the cumulative value 170000, the cumulative value for July 125000, so I need a query showing the difference is 45000.
Attached an Access file for short data examples.
https://www.dropbox.com/s/h9vlp84fzmjc93d/Minus%20Calculation.accdb?dl=0
Thanks for your support in advance!
Best Regards
Mahmoud
Assuming that you always want to see the data from exactly one month previous (whether or not such data exists), and that the data could be entered on any day in the month, you might consider the following:
select
t1.date,
t1.cumulative_expending_amount,
t2.cumulative_expending_amount,
t1.cumulative_expending_amount-t2.cumulative_expending_amount
from
YourTable t1 left join YourTable t2 on
t1.date >= dateserial(year(t2.date),month(t2.date)+1,1) and
t1.date <= dateserial(year(t2.date),month(t2.date)+2,0)
Change YourTable, date, and cumulative_expending_amount to suit the name of your table and fields respectively.
Following your comments, it would appear that the following SQL satisfies your requirements:
select
t1.itemid,
t1.date,
t1.cumulative_expending_amount,
(
select top 1 t3.cumulative_expending_amount
from YourTable t3
where t3.itemid = t1.itemid and t3.date < t1.date
order by t3.date desc
) as last_cumulative_expending_amount,
t1.cumulative_expending_amount-last_cumulative_expending_amount as diff
from
YourTable t1 inner join
(
select t.itemid, max(t.date) as mdate
from YourTable t
group by t.itemid
) t2 on t1.itemid = t2.itemid and t1.date = t2.mdate
order by
t1.itemid,
t1.date desc
You can use a correlated subquery. Based on your description, the code looks like this:
select t.*,
(select top (1) t2.cumulative_spending
from t as t2
where t2.date < t.date
order by t2.date desc
) as prev_cumulative_spending
from t;

Grouping by ID and time interval in ms sql

I am trying to write a query that groups like ids within a timespan.
Real world scenario:
I want to see rows created by the same ID within 5 seconds of each other.
SELECT top 10
Id,
CreatedOn
FROM Logs
where ((DATEPART(SECOND, CreatedOn) + 5) - DATEPART(SECOND, CreatedOn)) < 10
GROUP BY
DATEPART(SECOND, CreatedOn),
Id,
CreatedOn
order by CreatedOn desc
This isnt quite right but I feel like I am on the right track.
thanks in advance
You may try doing a query on the condition that the ID matches, and the seconds since epoch is within 5 seconds of the matching record:
SELECT
t1.Id,
t1.CreatedOn
FROM logs t1
WHERE EXISTS (SELECT 1 FROM logs t2
WHERE t1.Id = t2.Id AND
t1.CreatedOn <> t2.CreatedOn AND
ABS(DATEDIFF(SECOND, t1.CreatedOn, t2.CreatedOn)) <= 5)
ORDER BY
t1.CreatedOn DESC;
Could be further optimized this way:
SELECT t1.Id,
,t1.CreatedOn
FROM logs t1
WHERE EXISTS (
SELECT 1
FROM logs t2
WHERE t2.Id = t1.Id
AND t2.CreatedOn <> t1.CreatedOn
AND ABS(DATEDIFF(SECOND, t1.CreatedOn, t2.CreatedOn)) <= 5
)
ORDER BY
t1.CreatedOn DESC;

Querying for records on a date with an offset from a given date

Consider I have a date as 29/03/2011.
I want to query records for one day, 5 day and 10 day post this date.
Say, the sample fields are F1,F2,F3;
My code goes like this
SELECT F1,F2,F3 FROM T1 WHERE T1.Date = ...
I am using MS Access 2007, and I am not able to understand how to query for a date which is having an offset.
I have tinkered and experimented with DateAdd() but is giving me an error.
Would like to have a solution.
Soham
In Access, you can use:
SELECT F1,F2,F3
FROM T1
WHERE T1.Date >= #2011-07-11# + 10
To show all records which are 10 days and afterwards a certain date.
or this to show all records with are exactly 10 days from a certain date:
SELECT F1,F2,F3
FROM T1
WHERE T1.Date = #2011-07-11# + 10
There is a huge problem though, if your dates have a time part! The above will not catch records where date = #2011-07-11 11:43#. It will match only records having time part 00:00:00. So it's equivalent to having:
SELECT F1,F2,F3
FROM T1
WHERE T1.Date = #2011-07-21 00:00:00#
Which is probably not what you want. It's preferable to use this:
SELECT F1,F2,F3
FROM T1
WHERE DateValue(T1.Date) = #2011-07-11# + 10
or this, which can use an index on the Date field, so it's the best approach:
SELECT F1,F2,F3
FROM T1
WHERE T1.Date >= #2011-07-11# + 10
AND T1.Date < #2011-07-11# + 11