Filter Data by Date - sql

I have question to ask. Currently, I'm developing a payslip application. However, I stuck at 1 part of the process. I'm managed to display salary but I need to filter certain date in order for the salary to be display.
For example, for this September, the company already key in the salary on 26th Sep but user can only see it start from 28th Sep and above. So, basically, the program can show previous month payslip except for September unless user start see it on 28th Sep.
Current Output :
EMPLOYEEID MONTH YEAR SALARY
E001 7 2017 2000
E001 8 2017 2000
E001 9 2017 2000
E002 7 2017 2100
E002 8 2017 2100
E002 9 2017 2100
Expectation output:
EMPLOYEEID MONTH YEAR SALARY
E001 7 2017 2000
E001 8 2017 2000
E002 7 2017 2100
E002 8 2017 2100
Current Query Progress :
SELECT EMPLOYEEID, MONTH, YEAR, SALARY
FROM DBO.Salary
WHERE day(getdate())>=28

Today is 2017-09-29, do Sep should appear. Add the OR for when it's less than 28th
select *
from dbo.Salary s1
where day(getdate())>=28
or month(getdate()) > s1.Month

Related

how to get the data monthly wise in oracle sql between dates

I have table of EMPLOYEE , in which I need to show the salary from this month to this month .
Suppose I have dates eg. from date 17/01/2020 and to date 18/02/2020 I need monthly data between these two dates like from 17th JAN to 30th JAN one data and from 1st FEB to 28th FEB.
Please suggest some query i trying it but not able to fetch between two dates.
select add_months (to_date(from_date,'dd/mm/yyyy' ), - (level-1)), 'Mon yy') as MONTH,SALARAY from EMPLOYE_BG where CREATED_DATE between TO_DATE('17/01/2020','dd/mm/yyyy')
and to_date('10/03/2020','dd/mm/yyyy')
O/P:
MONTH SALARY
----------------
JAN-20 30000
FEB-20 50000
MAR-20 60000
like this i am expecting the result
SELECT * FROM EMPLOYE_BG
SALARY EMPNAME CREATED_DATE
---------------------------------
30000 JACK 07/01/2020
30000 SWETA 08/01/2020
30000 RAM 08/01/2020
40000 JOHN 01/02/2020
60000 SIMON 10/03/2020
70000 KIRA 11/04/2020
this is table details

Count the number of records for each 1st of the month in SQL

I have a dataset where I would like to query and obtain output of a count of records for the first of every month.
Data
name date1
hello july 1 2018
hello july 1 2018
hello july 10 2018
sure august 1 2019
sure august 1 2019
why august 20 2019
ok september 1 2019
ok september 1 2019
ok september 1 2019
sure september 5 2019
Desired
ID MONTH Day YEAR
2 July 1 2018
2 August 1 2019
3 September 1 2019
We are only counting the records from the 1st of each month
Doing
USE [Data]
SELECT COUNT(*) AS ID , MONTH(date1) AS MONTH, YEAR(date1) AS YEAR
FROM dbo.data1
GROUP BY MONTH(date1), YEAR(date1)
ORDER BY YEAR ASC
This only outputs the year and month
Any suggestion is appreciated
Assuming you are using the implicit conversion for date
Example
SELECT COUNT(*) AS ID,
DATENAME(MONTH,date1) AS MONTH,
DATEPART(DAY,date1) as DAY,
YEAR(date1) AS YEAR
FROM dbo.data1
WHERE DAY(date1)=1
GROUP BY YEAR(date1),DATENAME(MONTH,date1),DATEPART(DAY,date1)
ORDER BY YEAR ASC
Results
ID MONTH DAY YEAR
2 July 1 2018
2 August 1 2019
3 September 1 2019

Determine the first occurrence of a particular customer visiting the store in a particular month

I need to determine the counts breakdown to per month (and year) of customers [alias'ed as Patient_ID] which made their first visit to a store. The date times of store visits are stored in the [MDT Review Date] column of the table.
Customers can come to the store multiple times throughout the year and increase the total count-> but what I require is ONLY the first time a customer visited.
E.g. Tom Bombadil visited the store once in January 2019, so count increased to 1, then again 4 times in March, so count should be 1 for the month of March and 0 for febraury and 1 for January, then again 4 times in October, then again 2 times in December.
I require that Tom Bombadil should be counted one and only once for a particular month, his first occurrence which was per month
The output should be like :
rn1 YEAR Month_Number Month Total_Count
1 2010 6 June 2
1 2010 7 July 1
1 2010 8 August 5
1 2010 10 October 5
1 2010 11 November 3
1 2011 1 January 4
1 2011 2 February 6
1 2011 4 April 7
1 2011 5 May 4
1 2011 6 June 10
1 2011 7 July 10
1 2011 8 August 14
1 2011 9 September 4
1 2011 10 October 8
1 2011 11 November 11
1 2011 12 December 11
1 2012 1 January 8
1 2012 2 February 21​
Please refer to my query. What I have attempts to use the windowing function COUNT to count the store visits per month. Then the ROW_NUMBER function attempts to assign a unique number to each visit. What am I doing wrong?
select
*
from
(select distinct
row_number() over (partition by p.Patient_ID, p.PAT_Forename1, p.PAT_Surname
order by PAT_Forename1, p.Patient_ID, PAT_Surname) AS rn1,
datepart(year, [DATE_COLUMN]) as YEAR,
datepart(month, [DATE_COLUMN]) as Month_Number,
datename(month,[DATE_COLUMN]) as Month,
count(p.Patient_ID) over (partition by datepart(year,[DATE_COLUMN]),
datename(month, [DATE_COLUMN])) as Total_Count
from
Tablename m
inner join
TableName p on m.PK_ID = p.PK_ID
) as temp
where
rn1 = 1​

Find Employees having Pending Salary

There is a table - 'EmpSalary' - with Employee ID, Salary paid date and Salary amount. When a salary is paid for an employee, an entry will be posted in this table.
I want to find out the employees whose salary is pending (means either no salary is given or partially given) upto a given month in the current year.
The entire employees are available in 'Employee' table. I am using SQL Server 2005
EmpID SalDate Amount AmtPending
------ ------- ------- ----------
1 3 Jan 2019 5000 0
2 4 Jan 2019 3000 500
3 4 Jan 2019 4000 0
1 4 Feb 2019 4500 500
3 4 Feb 2019 4000 0
1 3 Mar 2019 5000 0
Expected Result - Pending upto February
EmpID Amount
------ ------
1 500
2 4000 --500 from Jan + 3500 from Feb
Since 3 has no pending upto Feb,no need to display 3
Do you just want group by and having?
select empid, sum(amtpending)
from empsalary
group by empid
having sum(amtpending) > 0;

SQL Server running balance with Partition by month

I have the following scenario where a user has some allowance taken every month up to a yearly capping.
I have successfully implemented this as shown here
I have stumbled into a problem for if the user gets promoted during the year the yearly capping needs to be ratified accordingly.
The following query gave these results. (Using sql server 2012)
SELECT *,
RemainingBalance = AnnualCapping - Sum(amount)
OVER (
partition BY userid, year, annualcapping
ORDER BY userid, year, month)
FROM exampleTx
WHERE userid = 1
AND year = 2015
data
userId year month monthname name surname annualCapping amount RemainingBalance
1 2015 1 January Joe Black 500,00 40,00 460,00
1 2015 2 February Joe Black 500,00 40,00 420,00
1 2015 3 March Joe Black 500,00 40,00 380,00
1 2015 4 April Joe Black 500,00 40,00 340,00
1 2015 5 May Joe Black 500,00 40,00 300,00
1 2015 6 June Joe Black 500,00 40,00 260,00
1 2015 7 July Joe Black 500,00 40,00 220,00
1 2015 8 August Joe Black 500,00 40,00 180,00
1 2015 9 September Joe Black 1000,00 40,00 **960,00**
1 2015 10 October Joe Black 1000,00 40,00 **920,00**
1 2015 11 November Joe Black 1000,00 40,00 **880,00**
1 2015 12 December Joe Black 1000,00 40,00 **840,00**
In September the monthly allowance should have been proportional to remaining of year.
4 months = 1000 * 4/12 = 333.33
and remaining balance of 293.33, 253.33, 213.33,173.33.
Could I achieve this without modifying the annual capping field. ie.e would have been simpler if annual capping was reduced to 333.33 but this is the data I have.
A change in capping of previous month would indicate a promotion has taken place. It can occur during any month. Hence the new capping should be proportional.
You could use following query
Select *,
RemainingBalance = AnnualCapping - SUM(amount) OVER (
partition by userid ,year ORDER BY userid, year,month)
from
exampleTx
where userid = 1 and year = 2015
Remove the Annual Capping from the Partition by clause.