Sql query for retrieving member data - sql

I want to make a payment to members on 1st may 2012 and i want to retrieve the data from the date between 1st april 2011 to 31st march 2012. How is it possible thru a query if i am a table (xyz) which maintains that on which date that person has become a member

select * from xyz where datecoljoined between '2011-04-01' and '2012-03-31'
Complying with British settings:
select * from xyz where datecoljoined between '20110401' and '20120331'

Related

How to calculate Australian Financial Year from a date column in SQL?

I have a table called Sales with a date column called SaleDate. How can I write a SQL query that shows the Australian financial year in addition to the other columns?
More details:
I use Microsoft SQL Server
The Australian financial year starts on 1 July and ends the next year on 30 June.
Example 1: 10 June 2019 is FY 2019
Example 2: 5 July 2019 is FY 2020
For Microsoft SQL Server, the following query will show a new column called FY, which represents the Australian financial year.
SELECT
year(dateadd(MONTH, 6, SaleDate)) AS FY,
SaleDate,
Item
FROM Sales

What's the best way to find if a SQL record ceased to exist in a subsequent time period

How can I generate a list of items that existed in one time period, but don't exist in a following time period. For example, a list of items that existed in January 2019, but did not appear in February 2019. I posted an idea of the sample query below, but I don't know how to approach this.
select period, item
from table
where period = 'February 2019'
and item (exists) in period -1
and item (does not exist) in period
Sample Data would be as follows (if there were three records, for example)
Period Item
January 2019 Bob
January 2019 Steve
January 2019 Phil
February 2019 Steve
February 2019 Phil
Desired results would be as follows:
Period Item
January 2019 Steve
Thank you for the help.
Based on your sample query, you should be able to use aggregation:
select item
from table
where period in ('February 2019', 'January 2019')
group by item
having min(period) = max(period) and
min(period) = 'January 2019';
You can use MONTH(yourdate) to get the month number and use that to get the desired results.
DECLARE #Month INT
SET #Month=1
SELECT * FROM
(SELECT * FROM #table WHERE month_number=#Month)current_period
LEFT JOIN
(SELECT * FROM #table WHERE month_number=#Month+1)following_period
ON
current_period.item=following_period.item
WHERE
following_period.item IS NULL
The SELECT after LEFT JOIN can be modified to Period_ID>#Month to include all the following periods.

T-SQL: August is the beginning of next years financial year, code to dynamically to change the batch ids

I have a column called batch_id with a list of dates - 2016080184 ie date 2016 08 01 84(84, I believe a time part).
I need to update the batch_id (varchar(25)) to change to 2017010184, based on another column voucher_date (datetime) = 2016-08-01 00:00:00.000
So if the voucher date was 2016-08-02 00:00:00.000, then the batch_id needs to change from 2016080278 to 2017010278 (78 at the end here doesn't matter)
August is the first month for the financial year, so August would effectively become January, September would become February etc.. and the year from Aug needs to indicate the following year ie this year is 2016 therefore batch_id should start with 2017.
Next August batch_id should indicate 2018 etc..
The file I receive is always a day behind to make things more complicated.
I am a little confused about your requirement for the year to change, but this should give you all you need to get started:
declare #BatchID nvarchar(10) = '2016080184'
select convert(nvarchar(8),dateadd(month,-7,cast(left(#BatchID,8) as date)),112) + right(#BatchID,2) as NewBatchID_SameYear
,convert(nvarchar(8),dateadd(month,5,cast(left(#BatchID,8) as date)),112) + right(#BatchID,2) as NewBatchID_NextYear
You can apply the above date changes with a CASE statement on the voucher_date column as required.

MS Access grouping query spanning start and end dates

I would like to get a running tally of how many widgets were/are rented at any one time, by month, by year. Data is held in an MS Access 2003 db;
Table name: rent_table
Fields:
rentid
startdate
enddate
rentfee
rentcost
bookingfee
Something like; Count number of rentid's that fall between month/year, then group them?
e.g. if a widget was rented from 5th Jan 2014 to 8th April 2014 it would appear as a count in Jan, Feb, Mar and April tally's.
Many thanks.
EDIT
More details (sorry);
Access db is fronted by classic ASP.
If possible I don't want to create any new tables.
No input is required in order to run the report.
There are around 350-400 widgets that could be rented at any one time.
Each widget is rented exclusively.
Report output example;
Month | Year | NumRented
Jan 2014 86
Feb 2014 113
...
Can a query pick up dates within dates? So literally do a count of the table where date >Dec 31st 2013 AND <1st Feb 2014 (to grab a count for all of January 2014) and would that include the example of the rent starting on the 5th Jan? So I could just do twelve counts for each year?
create a calendar table, e.g.
table = cal_yyyymm with one column dt_yyyymm as numeric field
populate the table with ... say 5 or 10 years of data
201401 201402 201403 ... 60 or 120 rows, a small table
make a sql
Select
dt_yyyymm,
count(*) as cnt
From cal_yyyymm
Left Join rent_table
On format(startdate,"yyyymm") >= dt_yyyymm
And dt_yyyymm >= format(enddate,"yyyymm")
think about the complications in the data -- --
widget was rented from 5th Jan 2014 to 8th Jan 2014
and again rented from 11th Jan 2014 to 21st Jan 2014
does this count at 1 or 2 in the month?
if it is 1, then the sql gets more complicated because
the rent_table first needs to have its dates converted
to yyyymm format, and second needs to be de-duped on rentid,
and third then joined to cal_ On the dates...

SQL every month basis report.

I have a 2 year data in SQL and i want to create or write query will be pull out data monthly basis. How do i write query which will give me every month data in one shot.
Jan Feb Mar Apil ....etc
SELECT myField1,myField2,...MONTH(myDateField) as monthDate,YEAR(myDateField) as yearDate
FROM myTableName ORDER BY yearDate,monthDate