How to find distinct of date in sql 2000?
For example :i have a table "Dates".It contains different dates like 26-11-2009,
25-11-2009,26-11-2009.'26-11-2009' has two entries.When i select date from this table i need only two entries such as 25-11-2009 and 26-11-2009.
Edited section:
In the table 'Emplyoee' there are two fields-JoinDate and EmployeeName.
All Data contains in Emplyoee table is as follows:
JoinDate | EmployeeName
------------------------
02-12-2009 Vijay
03-12-2009 Binoy
03-12-2009 Rahul
My select query is as follows:
SELECT DISTINCT JoinDate,EmployeeName FROM Emplyoee
I got the Result as follows:
JoinDate | EmployeeName
------------------------
02-12-2009 Vijay
03-12-2009 Binoy
03-12-2009 Rahul
But i need the result as follows:
JoinDate | EmployeeName
------------------------
02-12-2009 Vijay
03-12-2009 Binoy(first employee joined on this date)
SELECT DISTINCT DateAdd(dd, DateDiff(dd, 0, MyDateField),0)
FROM MyTable
This will give you the unique dates for your table
SELECT DISTINCT YourDateTimeField
FROM dbo.YourTable
although; I'm guessing you want something like this:
SELECT DISTINCT CONVERT(DATETIME, CONVERT(CHAR(10), YourDateTimeField, 101))
FROM dbo.YourTable
If this is a large table, or if the double conversion would significantly slow things down, you'd want to look for another solution
Having no idea exactly what you require, i can only guess that you need the distinct date values from a filed containg datetime values including times.
So you can try this.
SELECT DISTINCT DATEADD(dd,0, DATEDIFF(dd,0,DateVal)) FROM YourTable
Jim B's answer describes how to get list where every date occurs exactly once.
Or maybe you need dates that occur only once in your table. Your question is unclear.
select your_date_field
from your_table
group by your_date_field
having count(1) = 1;
Related
I'm trying to find the range of date values for a date datatype column in my one of my views.
I've searched many oracle forums looking for similar bug and no luck.
Columns for EPAServiceReport view:
EPASERVICEREPORT_KEY NUMBER(10)
EQUIPMENT_KEY NUMBER(10)
STARIDNO VARCHAR2(60)
WORKORDERNUMBER NVARCHAR2(50)
REPORT_ID NVARCHAR2(50)
CREATEDDATE DATE
SQL:
SELECT DISTINCT createddate
FROM epaservicereport
ORDER BY createddate
Results:
12-OCT-15
12-OCT-15
19-OCT-15
19-OCT-15
27-OCT-15
30-OCT-15
04-NOV-15
05-NOV-15
12-NOV-15
12-NOV-15
I expected to only see 1 row per value i.e. "distinct" but instead got every row from the view returned with many duplicate values.
Your date column has time, too. So although it displays the same but actually it's different in time on a day.
You could use this to get distint date only:
SELECT DISTINCT TRUNC(createddate)
FROM epaservicereport
ORDER BY 1; --TRUNC(createddate)
You can also use this:
SQL Server:
select distinct CONVERT(varchar(12),createddate,105)
FROM epaservicereport
ORDER BY 1;
More on cast and convert here:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017
Oracle:
select distinct TO_CHAR(createddate,'YYYY-MM-DD')
FROM epaservicereport
ORDER BY 1;
I need a sql query to display name of employees who are present "only in january" but not in feb,mar and april.
Here are the table details...
create table Employee(Id numeric(18,0),Name varchar(255),Date date);
insert into Employee values(101,'Shiva','2018/01/01'),
(102,'Vamsi','2018/01/01'),
(103,'Rajesh','2018/01/01'),
(104,'Krishna','2018/01/01'),
(105,'Tarun','2018/02/01'),
(101,'Shiva','2018/02/01'),
(103,'Rajesh','2018/02/01'),
(106,'Kaala','2018/03/02'),
(107,'Azeez','2018/03/02'),
(103,'Rajesh','2018/03/02'),
(108,'Eswar','2018/04/02'),
(109,'Dora','2018/04/02'),
(110,'Akash','2018/04/02'),
(103,'Rajesh','2018/04/02');
Expected result should be as following:
Vamsi
Krishna
Hope i'll get answer from you very soon
Thanks.
try this query
select Name from Employee where MONTH(date)=1
more details
In PostgreSQL you could try something like this. Here I select all employees that do not have a row where the month is not january.
SELECT Name
FROM Employee
WHERE Id NOT IN (SELECT Id FROM Employee WHERE date_part('month', Date) != 1);
A simple way is to use aggregation. If you only have data from one year:
select name
from Employee
group by name
having month(min(date)) = month(max(date)) and
month(min(date)) = 1;
If you want only January 2018:
select name
from Employee
group by name
having min(date) >= '2018-01-01' and
max(date) < '2018-02-01';
Try this
SELECT Id,Name,[Date]
FROM
(
SELECT *,
COUNT(Name)OVER(Partition by Name,DATEPART(YEAR,[date]) ORDER BY id)
AS EmpOnlyOnceInMonth
FROM EmployeeData
)dt
WHERE EmpOnlyOnceInMonth=1 AND DATEPART(M,dt.[date])=1
ORDER BY Id
Result
Id Name Date
----------------------
102 Vamsi 2018-01-01
104 Krishna 2018-01-01
I have a table as shown in image. Here user 'A' has no outtime where Id=2. If I select UserId,Name,MIN(inTime) and MAX(outtime) from MyTable ,then i will get First InTime and Last OutTime . Instead of selecting like that, I want to set User 'A' last outtime as null. How is it possible?.
Thanks in Advance
I assume you are using SQL Server:
select *
from(select name, userid, dateatt
from table
group by name, userid, dateatt)t
cross apply(select top 1 intime from table
where userid=t.userid and dateatt=t.dateatt order by id)i
cross apply(select top 1 outtime from table
where userid=t.userid and dateatt=t.dateatt order by id desc)o
By default, MAX and MIN do not include NULL when evaluating data.
See the below query I have modified for you taken from article here.
We will use COALESCE to replace any NULL EndDate with a date that is in the future that will not be coming up in our data anywhere, December 31, 2099 seems like a reasonable date for this. Next we take the MAX of the dates, which if NULL will evaluate as 12/31/2099 and be greater than any other date in our table. Wrap that in a CASE statement to replace the date 12/31/2099 back to NULL and group our data by StoreID.
SELECT
Name,
CASE WHEN MAX(COALESCE(outtime, ’12/31/2099′)) = ’12/31/2099′ THEN NULL ELSE MAX(outtime) END AS outtime
FROM WorkSchedule
GROUP BY Name
I have a table called Leaves which has Employee ID, Leave Type and Date. For example, If an employee with ID = 1234 applies for a sick leave from 1-June-2014 to 5-June-2014, this will be stored in Leave tables day by day, means that the following records will be added:
1234 sick leave 1-June-2014
1234 sick leave 2-June-2014
1234 sick leave 3-June-2014
1234 sick leave 4-June-2014
1234 sick leave 5-June-2014
This is considered as one case. To clarify what I mean by the case: The total cases is how many leave request had been applied… for example:
What I need is to get the following information by SQL statement (I should determine a period: 1-January-2014 to 30-December-2014, for example):
Sick leave cases: 2
Escort leave cases: 2
Study leave cases: 1
I am using PostgreSQL 9.2.
This design is a bit strange, because of these daily rows, if the same person will have several escort leaves, then you have to figure out different cases.
For this certain case you can use something like this
SELECT COUNT(*), leavetype
FROM (
SELECT leavetype
FROM Leaves
GROUP BY employee_id, leavetype
)
GROUP BY leavetype;
My suggestion is to use case_start and case_end dates for one case row.
Perhaps you can try this:
select leave_type, count(*)
from (
select employee_id, leave_type
from leaves
where date between ...
group by employee_id, leave_type) t
group by leave_type;
select LeaveType, count(EmployeeID) as TotalCases from(
select EmployeeID, LeaveType, count(LeaveType) as count_LeaveType
from Leaves
where Date BETWEEN '2007-02-01' AND '2007-02-31';
group by EmployeeID, LeaveType) as A
group by LeaveType, A.EmployeeID
Please find the SQLFiddle below.
SQL FIDDLE
SELECT leave_type,COUNT(leave_type) FROM
(SELECT leave_type,count(leave_type)
FROM leaves
WHERE Date BETWEEN '2014-01-01' AND '2014-12-31'
GROUP BY leave_type,emp_id) t
GROUP BY leave_type
Hope this solves your issue.
I am no sure how this will be done in PostgreSQL but in MySQL it can be achieve by following command :
QUERY
SELECT Leave type, count(Leave type)
FROM Leave
WHERE (Date BETWEEN '2014-01-01' AND '2014-12-30' )
GROUP BY Leave type
I always seem to trip myself up during these types of SQL Statements. Here is what I'm attempting to accomplish.
My Example Table
Name Date Type
Bob 9/28/11 1
Bob 9/27/11 1
Bob 9/28/11 2
Debra 9/28/11 1
I'm trying to write a SQL Statement that would give me all the names, their total count occured, and then a Date Filter. I'll write a rought statement with completely wrong syntax, but I think it'll convey what I'm attempting to do...I think.
SELECT Name, Count(*) As Total
FROM Table
GROUP BY Name, Total
WHERE Date = Today (I'm aware you can't do a WHERE in a GROUP BY) AND Type = 1
Essentially, I would like to get back a set of data that would show the Name, how many instances of Type 1 for today.
I don't think I'm searching for the proper question to actually be able to effectively research this on my own as well, probably because I'm wording it improperly.
Any help would be appreciated!
Your WHERE was in the wrong place:
SELECT Name, Count(*) As Total
FROM Table
WHERE Date = Today AND Type = 1
GROUP BY Name
You just need to move your WHERE after your FROM.
SELECT Name, Count(*) As Total
FROM Table
WHERE [Date] = CAST(getdate() as Date) -- or 'Jan 1 1990'
AND [Type] = 1
GROUP BY Name
WHERE comes before GROUP
SELECT Name, Count(*) As Total
FROM Table
WHERE Date >= #Today --assuming your date passed in has no time
AND Date < DATEADD(d,1,#Today)
AND Type = 1
GROUP BY Name, Total
example you can run
SELECT LEFT(name,1) FROM sysobjects
WHERE name < 'p'
GROUP BY LEFT(name,1)