Click here to Skip to main content
15,896,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a DataGridView. It is containing Two Columns which is

EMPId    |   DATEtiME
1        |   2019-04-20 07:30:00.000
1        |   2019-04-20 12:30:00.000
1        |   2019-04-20 13:00:00.000
2        |   2019-04-20 07:30:00.000
2        |   2019-04-20 07:30:00.000
2        |   2019-04-20 13:00:00.000
3        |   2019-04-20 07:30:00.000
3        |   2019-04-20 12:30:00.000


I want to remove second row value in EMPID 1 and I want to remove second value in EMPID 2 and EMPID will remain the same. Like.

EMPId    |   DATEtiME
1        |   2019-04-20 07:30:00.000
1        |   2019-04-20 13:00:00.000
2        |   2019-04-20 07:30:00.000
2        |   2019-04-20 13:00:00.000
3        |   2019-04-20 07:30:00.000
3        |   2019-04-20 12:30:00.000



Please help anyone

What I have tried:

..................................................................................................................
Posted
Updated 20-Apr-19 5:14am
Comments
[no name] 20-Apr-19 9:27am    
a.) I don't see the logic in your example, especially for EMPId == '1'.
b.) Assuming the data comes from a database think about SELECT DISTINCT
Member 12245539 20-Apr-19 9:36am    
For EMPID 1, in same date it has three entries such as 07.30, 12.30 and 13.00. Now I want keep only 1 entry and last entry...
[no name] 20-Apr-19 9:43am    
In other words: You like to show only first and last according to date for a given EMPId?
Member 12245539 20-Apr-19 9:47am    
Yes
[no name] 20-Apr-19 9:49am    
And the data comes from database which can be queried by SQL?

Please note: I'm not sure whether this is an elegant solution and more whether this can become a performance bottle neck. But at least it looks like it works.

a.) Create test table
CREATE TABLE Tbl
(
	EMPId integer,
	DATEtiME DateTime
);

b.) Insert Test data
INSERT INTO Tbl(EMPId, DATEtiME)
VALUES(1, '2019-04-20 07:30:00.000'),
(1, '2019-04-20 07:30:00.000'),
(1, '2019-04-20 12:30:00.000'),
(1, '2019-04-20 13:00:00.000'),
(2, '2019-04-20 07:30:00.000'),
(2, '2019-04-20 07:30:00.000'),
(2, ' 2019-04-20 13:00:00.000'),
(3, '2019-04-20 07:30:00.000'),
(3, '2019-04-20 12:30:00.000'),
(4, '2019-04-20 00:30:00.000')

c.) Test query
SELECT DISTINCT
	a.EMPId, 
	a.DATEtiME
FROM Tbl a
WHERE a.DATEtiME IN (SELECT MIN(minEmp.DATEtiME) FROM Tbl minEmp WHERE minEmp.EMPId = a.EMPId)
   OR  a.DATEtiME IN (SELECT MAX(maxEmp.DATEtiME) FROM Tbl maxEmp WHERE maxEmp.EMPId = a.EMPId)

d.) Result
1	2019-04-20 07:30:00.000
1	2019-04-20 13:00:00.000
2	2019-04-20 07:30:00.000
2	2019-04-20 13:00:00.000
3	2019-04-20 07:30:00.000
3	2019-04-20 12:30:00.000
4	2019-04-20 00:30:00.000


Most probably the SQL experts @CHill60 and @losmac can give you a better answer ;)
 
Share this answer
 
v4
Comments
Member 12245539 20-Apr-19 12:13pm    
for this I am rating 5..
[no name] 20-Apr-19 12:15pm    
Thank you
[no name] 20-Apr-19 12:25pm    
Just came into my mind IN vs. =: Instead of
SELECT DISTINCT
a.EMPId,
a.DATEtiME
FROM Tbl a
WHERE a.DATEtiME IN (SELECT MIN(minEmp.DATEtiME) FROM Tbl minEmp WHERE minEmp.EMPId = a.EMPId)
OR a.DATEtiME IN (SELECT MAX(maxEmp.DATEtiME) FROM Tbl maxEmp WHERE maxEmp.EMPId = a.EMPId)

we can write
SELECT DISTINCT
a.EMPId,
a.DATEtiME
FROM Tbl a
WHERE a.DATEtiME = (SELECT MIN(minEmp.DATEtiME) FROM Tbl minEmp WHERE minEmp.EMPId = a.EMPId)
OR a.DATEtiME = (SELECT MAX(maxEmp.DATEtiME) FROM Tbl maxEmp WHERE maxEmp.EMPId = a.EMPId)
Select and group on the "duplicate columns / fields".

GROUP BY EMPID, DateTime.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900