selecting data which meets 3 criteria between certain dates - sql-server

I need to select all data for the jobs where it was requested by the same memberID in the same department (dept) and on the same day since the beginning of may. Below is my attempted code however i get:
arithmetic overflow error converting expression to data type datetime
I am unsure how to solve this? maybe there is a better query to execute?
select jobID,
memberID,
dept,
[BriefDate],
schemeID from (
Select jobID,
memberID,
dept,
[BriefDate],
schemeID,
RowCnt = Count(*) over(partition by MemberId, Dept, Convert(date, [briefdate]))
from [SSRSReports].[dbo].[corporatejobreportdetailed]
where [BriefDate]>20170501 ) a
Where a.RowCnt >= 3

Related

Distinct columns should have one date per month in SQL Server

My table has columns Name, EmpName, Date. For distinct Name and EmpName values, Date should be only one value per month
For example:
Name EmpName Date
-----------------------
abc emp1 3/19/2018
abc emp1 3/22/2018 (This record should be rejected)
xyz emp2 3/15/2018 valid record
I wrote something like this
SELECT
name, empname,
ROW_NUMBER() OVER (PARTITION BY YEAR(date), MONTH(date) ORDER BY date DESC)
I got stuck writing a CASE statement
You can use row_number() :
select top (1) with ties t.*
from table t
order by row_number() over (partition by name, empname, year(date), month(date) order by date);
However, based on sample data simple aggregation would also work :
select name, empname, min(date)
from table t
group by name, empname, year(date), month(date);

SQL Server: selecting distinct values per one column

I was wandering if it's possible to filter select results removing values that partially overlap
For example below, i have thousands of records, but i need the 'week date' value to be unqiue, and in case of duplicates the one with the highest value should remain.
emplo project_id Value week_Date week_ActualStart week_ActualEnd
A0001 project001 100 2015-12-28 2015-12-28 2016-01-03
A0001 project001 60 2015-12-28 2016-01-01 2016-01-03
So only the first row should remain.
I could really use someone's advice
Try something like the following:
;WITH WeekDateCte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY emplno, week_Date ORDER BY Value DESC) RowNo
FROM employee
)
SELECT *
FROM WeekDateCte
WHERE RowNo = 1
For more information about ROW_NUMBER function, check here.
NOTE: ROW_NUMBER() returns BIGINT.
You can use ROW_NUMBER for this:
SELECT emplno, project_id, Value, week_Date,
week_ActualStart, week_ActualEnd
FROM (
SELECT emplno, project_id, Value, week_Date,
week_ActualStart, week_ActualEnd,
ROW_NUMBER() OVER (PARTITION BY emplno, week_Date
ORDER BY Value DESC) AS rn
FROM mytable) AS t
WHERE t.rn = 1
The query picks the row having the greatest Value per emplno, week_Date slice.

SQL , how to delete the duplicate row and find the min(timeIn) and max(timeOut)

I just want to ask question on how to manage the duplicate row with the different timeIn and timeOut. I mean, the result should be no duplicate row with same id but at the same time the timeIn for that particular person should be the minimum timeIn and the timeOut should be the last timeOut. The attDate must be on same day. How the query should be done, any ideas?
Here some of my simple code:
SELECT
id, name, attend_date, Time_in, Time_out,
count(1) as Count_person
FROM
employee
WHERE
attDate = '12/01/2015' AND Time_out IS NOT NULL
GROUP BY
id, name, attend_date, Time_in, Time_out
HAVING
COUNT(1) > 1
//note. I'm just a beginner and trying to improve myself.
You could use ROW_NUMBER to get the latest time_in and last time_out:
WITH Cte AS(
SELECT *,
RN_Time_in = ROW_NUMBER() OVER(PARTITION BY id, attDate ORDER BY Time_in),
RN_Time_Out = ROW_NUMBER() OVER(PARTITION BY id, attDate ORDER BY Time_Out DESC)
FROM employee
)
SELECT *
FROM Cte
WHERE
RN_Time_in = 1
AND RN_Time_Out = 1

Get complete Datetime but group by Date only. SQL Server

I have data like this:
Value|Date
0,055|2015-01-01 12:30:00.000
0,024|2015-01-01 15:30:00.000
0,023|2015-01-02 13:30.00.000
I need to get the max value per day and the time when it happens. So far I can get the max value without problems but because of the group by I only get the day without time. How can I get the complete datetime grouping by day?
This should work:
;WITH CTE AS
(
SELECT Value,
[Date],
RN = ROW_NUMBER() OVER(PARTITION BY CONVERT(DATE,[Date]) ORDER BY Value DESC)
FROM dbo.YourTable
)
SELECT Value,
[Date]
FROM CTE
WHERE RN = 1;

In SQL Server how can I fetch all rows with a date greater than some set date or at least 20 rows

I'm looking for a way to fetch at least 20 rows, regardless of the date or all rows with a date greater than a set date (whichever of the 2 fetches the most rows).
For example:
SELECT *
FROM table1
WHERE the_date >= '2014-11-01'
ORDER BY the_date DESC
will give me what I want, but if this returns less than 20 rows then I want to keep going before that date until I get 20 (or until there are no more rows - whichever comes first).
I could just select all and take the ones I need programatically, but I'm trying to avoid that as that would be a major change to the actual code in many places.
Can this be done?
Note: I am using SQL Server 2008.
Here's an approach:
select *
from
(
select row_number() over (order by the_date desc) num, t.*
from table1 t
) i
where i.the_date >= '2014-11-01'
or i.num < 21
Very strange requirement but something like this should work.
DECLARE #Date date = '2014-11-01';
with MyResults as
(
SELECT *, 1 as RowNum
FROM table1
WHERE (the_date >= #Date )
UNION ALL
SELECT top 10 *, ROW_NUMBER() over(order by the_date desc)
FROM table1
order by the_date desc
)
Select *
from MyResults
where RowNum <= 20
The right answer was almost there, but the user has removed it, so here it is again:
SELECT *
FROM table1
WHERE the_date >= '2014-11-01'
union
SELECT top 20 *
FROM table1
ORDER BY the_date desc
The removed answer had union all
This answer has a problem with duplicate dates , but with the data I have that is not an issue

Resources