I haven't used MS access in over ten years. I've looked at various sites and while I can find information on min and max, I can't find the answer to this one.
I have two tables, one archival and one current. Each time it's run, I need to take the oldest month's records and append them on to the archival table, then delete them from the current table.
If I can just figure out the select statement to return the range of the oldest month, I can do the rest.
So, I need....
Select *
From MyTable
Where ????
something that qualifies it as the oldest month
order by product_Id
The following uses the fact that MS Access really does a top with ties:
select top 1 t.*
from mytable as t
order by datepart("yyyy", datecol) asc, datepart("m", datecol) asc
A more typical method would be an inner join:
select t.*
from mytable as t inner join
(select min(datecol) as mindc
from mytable
) as tt
on datepart("yyyy", t.datecol) = datepart("yyyy", tt.mindc) and
datepart("m", t.datecol) = datepart("m", tt.mindc);
Related
**Is there a way to count how many strings in a specific column are seen for
Since the value in the column 2 gets repeated sometimes due to the fact that some clients make several transactions in different times (the client can make a transaction in the 1st month then later in the next year).
Is there a way for me to count how many IDs are completely new per month through a group by (never seen before)?
Please let me know if you need more context.
Thanks!
A simple way is two levels of aggregation. The inner level gets the first date for each customer. The outer summarizes by year and month:
select year(min_date), month(min_date), count(*) as num_firsts
from (select customerid, min(date) as min_date
from t
group by customerid
) c
group by year(min_date), month(min_date)
order by year(min_date), month(min_date);
Note that date/time functions depends on the database you are using, so the syntax for getting the year/month from the date may differ in your database.
You can do the following which will assign a rank to each of the transactions which are unique for that particular customer_id (rank 1 therefore will mean that it is the first order for that customer_id)
The above is included in an inline view and the inline view is then queried to give you the month and the count of the customer id for that month ONLY if their rank = 1.
I have tested on Oracle and works as expected.
SELECT DISTINCT
EXTRACT(MONTH FROM date_of_transaction) AS month,
COUNT(customer_id)
FROM
(
SELECT
date_of_transaction,
customer_id,
RANK() OVER(PARTITION BY customer_id
ORDER BY
date_of_transaction ASC
) AS rank
FROM
table_1
)
WHERE
rank = 1
GROUP BY
EXTRACT(MONTH FROM date_of_transaction)
ORDER BY
EXTRACT(MONTH FROM date_of_transaction) ASC;
Firstly you should generate associate every ID with year and month which are completely new then count, while grouping by year and month:
SELECT count(*) as new_customers, extract(year from t1.date) as year,
extract(month from t1.date) as month FROM table t1
WHERE not exists (SELECT 1 FROM table t2 WHERE t1.id==t2.id AND t2.date<t1.date)
GROUP BY year, month;
Your results will contain, new customer count, year and month
I want to run a report from sales of any customer that has ordered in the last two years.
I can run a report of all invoices dated within two years then remove duplicates in excel, but I would rather do it directly within (Firebird) SQL
I can use a WHERE date < 1 Jan 2015 (2 years or thereabours), but how do I get it to only show the customer once? I thought if I used MAX(Date) therefore showing the most recent date in that two year period. Where am I going wrong? I believe I need to use a UNIQUE() function like UNIQUE(ORDERCUSTOMER) within the SELECT clause.
SELECT
FINANCIALSALESINVOICES.TRANSACTIONDATE,
FINANCIALSALESINVOICES.INVOICECUSTOMER,
FINANCIALSALESINVOICES.ORDERCUSTOMER,
FINANCIALSALESINVOICES.INVOICENUMBER,
FINANCIALSALESINVOICES.SOURCENUMBER,
MAX(FINANCIALSALESINVOICES.TRANSACTIONDATE)
FROM FINANCIALSALESINVOICES
WHERE (FINANCIALSALESINVOICES.TRANSACTIONDATE>={d '2015-01-01'})
ORDER BY FINANCIALSALESINVOICES.INVOICECUSTOMER, FINANCIALSALESINVOICES.TRANSACTIONDATE
I did having it showing the max date for each instance of invoice in the past two years, but now can't fine that file or replicate it.
One approach is to use a subquery in the WHERE clause which checks for the most recent invoice:
SELECT
t.TRANSACTIONDATE,
t.INVOICECUSTOMER,
t.ORDERCUSTOMER,
t.INVOICENUMBER,
t.SOURCENUMBER
FROM FINANCIALSALESINVOICES t
WHERE t.TRANSACTIONDATE >= date '2015-01-01' AND
t.TRANSACTIONDATE = (SELECT MAX(f.TRANSACTIONDATE)
FROM FINANCIALSALESINVOICES f
WHERE t.ORDERCUSTOMER = f.ORDERCUSTOMER AND
f.TRANSACTIONDATE >= date '2015-01-01')
ORDER BY t.INVOICECUSTOMER,
t.TRANSACTIONDATE
With Firebird 3 you can use row_number() to assign a unique value to each row within a group (partition), that value can then be filtered on:
select
a.TRANSACTIONDATE,
a.INVOICECUSTOMER,
a.ORDERCUSTOMER,
a.INVOICENUMBER,
a.SOURCENUMBER
from (
select
TRANSACTIONDATE,
INVOICECUSTOMER,
ORDERCUSTOMER,
INVOICENUMBER,
SOURCENUMBER,
row_number() over (partition by INVOICECUSTOMER, order by TRANSACTIONDATE desc) as rownr
from FINANCIALSALESINVOICES
where TRANSACTIONDATE >= date '2015-01-01'
) a
where a.rownr = 1
order by a.INVOICECUSTOMER, a.TRANSACTIONDATE
See also Window (Analytical) Functions in the Firebird 3 release notes.
My goal is to extract from my database all of the records that fall NOT on the end of the month, but on the last date that exists in each month within the data (although some of these may so happen to be the end of the month).
Currently, just to simply get the latest dates for each month, using the following code alone takes 26 minutes:
SELECT Max(DATE)
FROM Accounts
GROUP BY Year(Date), Month(Date);
The point being that for the 16M rows that we have, running this as a subquery for the FROM statement within a temp_table is just not the speed improvement we were looking for (currently it takes about 40 minutes to read in the whole table anyway).
Any suggestions?
One method is to use window functions:
select a.*
from (select a.*,
rank() over (partition by year(date), month(date)
order by day(date) desc) as seqnum
from accounts a
) a
where seqnum = 1;
Note: the use of rank() (or equivalently dense_rank()) will return all rows for the last day of each month. If you only wanted one record, you can use row_number() instead.
I'm trying to write a SQL query for Oracle SQL in order to retrieve the last records for a certain period frequency. For example, say the frequency is Quarterly, (I'd also like monthly and annually to work), I can provide the start dates and end dates for the quarters if necessary, but I need to retrieve the last entry within each quarter. How can I do this? I've had limited luck so far without writing lots of subqueries.
Try something like:
select * from
(select t.*,
row_number() over (partition by trunc(date_field, 'MON')
order by date_field desc) rn
from my_table t)
where rn = 1
for months. (Use 'Q' or 'Y' instead of 'MON' in the trunc clause for quarters or years.)
SELECT col1, col2, col3...colN
FROM TableA
WHERE (colX = (SELECT MAX(Date) AS LastDate
FROM TableA
WHERE QuarterDate Between (BeginningDate AND EndingDate)
colX is the date column that is your date you need to be checking if in the quarter.
I think that should work.
I would appreciate a little expert help please.
in an SQL SELECT statement I am trying to get the last day with data per month for the last year.
Example, I am easily able to get the last day of each month and join that to my data table, but the problem is, if the last day of the month does not have data, then there is no returned data. What I need is for the SELECT to return the last day with data for the month.
This is probably easy to do, but to be honest, my brain fart is starting to hurt.
I've attached the select below that works for returning the data for only the last day of the month for the last 12 months.
Thanks in advance for your help!
SELECT fd.cust_id,fd.server_name,fd.instance_name,
TRUNC(fd.coll_date) AS coll_date,fd.column_name
FROM super_table fd,
(SELECT TRUNC(daterange,'MM')-1 first_of_month
FROM (
select TRUNC(sysdate-365,'MM') + level as DateRange
from dual
connect by level<=365)
GROUP BY TRUNC(daterange,'MM')) fom
WHERE fd.cust_id = :CUST_ID
AND fd.coll_date > SYSDATE-400
AND TRUNC(fd.coll_date) = fom.first_of_month
GROUP BY fd.cust_id,fd.server_name,fd.instance_name,
TRUNC(fd.coll_date),fd.column_name
ORDER BY fd.server_name,fd.instance_name,TRUNC(fd.coll_date)
You probably need to group your data so that each month's data is in the group, and then within the group select the maximum date present. The sub-query might be:
SELECT MAX(coll_date) AS last_day_of_month
FROM Super_Table AS fd
GROUP BY YEAR(coll_date) * 100 + MONTH(coll_date);
This presumes that the functions YEAR() and MONTH() exist to extract the year and month from a date as an integer value. Clearly, this doesn't constrain the range of dates - you can do that, too. If you don't have the functions in Oracle, then you do some sort of manipulation to get the equivalent result.
Using information from Rhose (thanks):
SELECT MAX(coll_date) AS last_day_of_month
FROM Super_Table AS fd
GROUP BY TO_CHAR(coll_date, 'YYYYMM');
This achieves the same net result, putting all dates from the same calendar month into a group and then determining the maximum value present within that group.
Here's another approach, if ANSI row_number() is supported:
with RevDayRanked(itemDate,rn) as (
select
cast(coll_date as date),
row_number() over (
partition by datediff(month,coll_date,'2000-01-01') -- rewrite datediff as needed for your platform
order by coll_date desc
)
from super_table
)
select itemDate
from RevDayRanked
where rn = 1;
Rows numbered 1 will be nondeterministically chosen among rows on the last active date of the month, so you don't need distinct. If you want information out of the table for all rows on these dates, use rank() over days instead of row_number() over coll_date values, so a value of 1 appears for any row on the last active date of the month, and select the additional columns you need:
with RevDayRanked(cust_id, server_name, coll_date, rk) as (
select
cust_id, server_name, coll_date,
rank() over (
partition by datediff(month,coll_date,'2000-01-01')
order by cast(coll_date as date) desc
)
from super_table
)
select cust_id, server_name, coll_date
from RevDayRanked
where rk = 1;
If row_number() and rank() aren't supported, another approach is this (for the second query above). Select all rows from your table for which there's no row in the table from a later day in the same month.
select
cust_id, server_name, coll_date
from super_table as ST1
where not exists (
select *
from super_table as ST2
where datediff(month,ST1.coll_date,ST2.coll_date) = 0
and cast(ST2.coll_date as date) > cast(ST1.coll_date as date)
)
If you have to do this kind of thing a lot, see if you can create an index over computed columns that hold cast(coll_date as date) and a month indicator like datediff(month,'2001-01-01',coll_date). That'll make more of the predicates SARGs.
Putting the above pieces together, would something like this work for you?
SELECT fd.cust_id,
fd.server_name,
fd.instance_name,
TRUNC(fd.coll_date) AS coll_date,
fd.column_name
FROM super_table fd,
WHERE fd.cust_id = :CUST_ID
AND TRUNC(fd.coll_date) IN (
SELECT MAX(TRUNC(coll_date))
FROM super_table
WHERE coll_date > SYSDATE - 400
AND cust_id = :CUST_ID
GROUP BY TO_CHAR(coll_date,'YYYYMM')
)
GROUP BY fd.cust_id,fd.server_name,fd.instance_name,TRUNC(fd.coll_date),fd.column_name
ORDER BY fd.server_name,fd.instance_name,TRUNC(fd.coll_date)