How to insert a record on next selected date if count of a date is full - sql

I have a table where I maintain working days in a week like 2nd and 4th day and number of records it can accept is 10 records per working day
usercode DaysofWeek NumberOfRecords
0623PO54 2 10
0623PO54 4 10
On insertion I have application date example 01-09-2017(dd/mm/yyyy) which is Friday.
Now I have to insert this record in closest working day from 01-09-2017 which is 05-09-2017 as it is 2nd working day. After inserting 10 records next records should be insert on 4th working day which is 07-09-2017.
I don't know how to get closest date from application date and insert record on it.

If you also want to exclude holidays than use a master table for Ex. CalenderMastr of dates which have holidays flag and day of week like Monday=2. and as you mansion that you are maintaining working days in a table for ex. workdaymaster. Now make a select query to get next date from CalenderMastr from current date and day of week stored in workdaymaster. now on output date check if in your transaction table count is smaller or not if count is small insert new record or if not than move to next date using while loop in your query. hope you can understand what i am trying to say.

Related

How to subtract specific fields in a row from 2 dates in SQL?

I have a table where the data updates every hour and I need to subtract the number of quantities as of today and what was 60 days back. How can that be done in SQL?

SQL date time Query

Need help to get the data of particular format
We have a table which have a data which of production now we need to select the data of each day with particular time period which is differentiate between three shift A,B,C.
In our table we have a datetime column which capture's each seconds data now that data we need in shiftwise like 6am to 2pm is of A shift production count and 2pm to 10pm of shift B and 10pm to 6 am of shift C.
here i am getting the data for single day where i have written the below query which is working good.
select distinct(count(PRD_SERIAL_NUMBER)),(select convert(date,getdate())) as date,'B' as shift_name
from table_name
where status=02
and LAST_UPDATED_DATE
between (SELECT FORMAT(GETDATE(),'yyyy-MM-dd 14:01:00.000')) and
(SELECT FORMAT(GETDATE()-26,'yyyy-MM-dd 22:01:00.000'))
refer below output image 1
Here i am getting the count for single day and for upcoming days i have solution but now the question arise is i have a past 4 Month data which i need to get in datewise and shiftwise count and for the column prd_serial_number have duplicate entries so it should be in distinct.
please refer below image 2 for required output format

extracting common data of current months and last two monnth sql

I have a table with more than 20000 rows, In one of column i have month from jan 2014 to Dec 2014, and in another column i have a loan number. Most of the loan Numbers are reapeting every months,now i need to get only the loan Number which are apperead in all three monthy consecutively. For eg if i am getting data for current months i also wanted get data which are common in two months before the current months. The database that i m using is Access DB. Any adivice will be more than a help, Thanks in Advance.
SELECT Loans.LoanID, Sum(IIf([period]=[month],1,0)) AS CM, Sum(IIf([period]=[month]-1,1,0)) AS [m-1], Sum(IIf([period]=[month]-2,1,0)) AS [m-2]
FROM Loans
GROUP BY Loans.LoanID
HAVING (((Sum(IIf([period]=[month],1,0)))>1) AND ((Sum(IIf([period]=[month]-1,1,0)))>1) AND ((Sum(IIf([period]=[month]-2,1,0)))>1));
I used month as an integer, and didn't make any adjustment for months 1 and 2 to loop back and look at prior year - you should be able to modify this based on the actual format you are using for the month.

Effecient way to compare values with different date range

I need to build a report which has week's sales data by department by date (which I have done using Matrix) and compare it to weeks sales last year. Report would be run weekly. I am wondering which approach would be most efficient:
1) generate separate data sets - 1) for 1 weeks data and 2) for 1
week a year ago and then compare these values;
2) create 1 data set for a period of 1 year + 1 week and insert
calculated fields in the data set;
3) create 1 data set for a period of 1 year + 1 week and insert
calculated fields with expressions inside the report
4) any other.
Thanks
If you tables are well indexed, then I'd suggest you go with the first approach - It'd reduce the data to be compared. I feel that pulling data from one year in past would be a less expensive than comparing 2 week's data out of one year's data.
You could use the week number of any given week to efficiently pull data for any week based on the date range.

Compare date and month in sql server for alert system

I have an a table with two columns birthday and anniversary. I want to get alerts about birthdays and anniversaries between a 7 day period of time but, that should not include year (obviously if I include year, it would always be less than the current date). I want to get the alerts 7 days in advance.
That is, the query should compare the birthday and anniversary with the current date and return a list if their birthday or anniversary falls between 7 days of the same month so that it alerts me in advance about the upcoming birthdays and anniversaries.
Subtract the year difference from now to the requested date and then use datediff to calculate the date difference of the result with the requested date.
SELECT *
FROM Table
WHERE DATEDIFF(dd,DATEADD(yyyy,-DATEDIFF(yyyy,Birthday,GETDATE()),GETDATE()),Birthday) BETWEEN 0 AND 7
OR DATEDIFF(dd,DATEADD(yyyy,-DATEDIFF(yyyy,Anniversary,GETDATE()),GETDATE()),Anniversary) BETWEEN 0 AND 7
Try This
SELECT Name,max(Table .birthdate)
FROM Table group by Table .Name having (datediff(day,max(birthdate),getutcdate())>7 and datediff(day,max(birthdate),getutcdate())<8)