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
Related
I have a table table1 where I want to do a case statement that selects an employee ID based on the most recent hire date. An employee can have 2 separate user ID's in the system, I wanted to grab the user ID that was most recent. I tried approaching this by joining the fica_nbr of the employee from another table (table2), that way if it shows up more than once, I know the employee has 2 different hire dates and I can go
SELECT
CASE
WHEN COUNT(table2.fica_nbr) > 1
THEN SELECT(table1.employeeID)
WHERE employeeID is MAX date /*->This is the line im having trouble on, how would I get the employee ID that is the most up to date using the where clause*/
Thank you
you need to do something like this
change colunms with your required colunms
SELECT report_id, computer_id, date_entered
FROM reports AS a
WHERE date_entered = (
SELECT MAX(date_entered)
FROM reports AS b
WHERE a.report_id = b.report_id
AND a.computer_id = b.computer_id
)
I am using SQL Server and wondering if it is possible to iterate through time series data until specific condition is met and based on that label my data in other table?
For example, let's say I have a table like this:
Id Date Some_kind_of_event
+--+----------+------------------
1 |2018-01-01|dsdf...
1 |2018-01-06|sdfs...
1 |2018-01-29|fsdfs...
2 |2018-05-10|sdfs...
2 |2018-05-11|fgdf...
2 |2018-05-12|asda...
3 |2018-02-15|sgsd...
3 |2018-02-16|rgw...
3 |2018-02-17|sgs...
3 |2018-02-28|sgs...
What I want to get, is to calculate for each key the difference between two adjacent events and find out if there exists difference > 10 days between these two adjacent events. In case yes, I want to stop iterating for that specific key and put label 'inactive', otherwise 'active' in my other table. After we finish with one key, we start with another.
So for example id = 1 would get label 'inactive' because there exists two dates which have difference bigger that 10 days. The final result would be like that:
Id Label
+--+----------+
1 |inactive
2 |active
3 |inactive
Any ideas how to do that? Is it possible to do it with SQL?
When working with a DBMS you need to get away from the idea of thinking iteratively. Instead you need to try and think in sets. "Instead of thinking about what you want to do to a row, think about what you want to do to a column."
If I understand correctly, is this what you're after?
CREATE TABLE SomeEvent (ID int, EventDate date, EventName varchar(10));
INSERT INTO SomeEvent
VALUES (1,'20180101','dsdf...'),
(1,'20180106','sdfs...'),
(1,'20180129','fsdfs..'),
(2,'20180510','sdfs...'),
(2,'20180511','fgdf...'),
(2,'20180512','asda...'),
(3,'20180215','sgsd...'),
(3,'20180216','rgw....'),
(3,'20180217','sgs....'),
(3,'20180228','sgs....');
GO
WITH Gaps AS(
SELECT *,
DATEDIFF(DAY,LAG(EventDate) OVER (PARTITION BY ID ORDER BY EventDate),EventDate) AS EventGap
FROM SomeEvent)
SELECT ID,
CASE WHEN MAX(EventGap) > 10 THEN 'inactive' ELSE 'active' END AS Label
FROM Gaps
GROUP BY ID
ORDER BY ID;
GO
DROP TABLE SomeEvent;
GO
This assumes you are using SQL Server 2012+, as it uses the LAG function, and SQL Server 2008 has less than 12 months of any kind of support.
Try this. Note, replace #MyTable with your actual table.
WITH Diffs AS (
SELECT
Id
,DATEDIFF(DAY,[Date],LEAD([Date],1,0) OVER (ORDER BY [Id], [Date])) Diff
FROM #MyTable)
SELECT
Id
,CASE WHEN MAX(Diff) > 10 THEN 'Inactive' ELSE 'Active' END
FROM Diffs
GROUP BY Id
Just to share another approach (without a CTE).
SELECT
ID
, CASE WHEN SUM(TotalDays) = (MAX(CNT) - 1) THEN 'Active' ELSE 'Inactive' END Label
FROM (
SELECT
ID
, EventDate
, CASE WHEN DATEDIFF(DAY, EventDate, LEAD(EventDate) OVER(PARTITION BY ID ORDER BY EventDate)) < 10 THEN 1 ELSE 0 END TotalDays
, COUNT(ID) OVER(PARTITION BY ID) CNT
FROM EventsTable
) D
GROUP BY ID
The method is counting how many records each ID has, and getting the TotalDays by date differences (in days) between the current the next date, if the difference is less than 10 days, then give me 1, else give me 0.
Then compare, if the total days equal the number of records that each ID has (minus one) would print Active, else Inactive.
This is just another approach that doesn't use CTE.
Need a help.
I have two timestamp columns, so basically I want to get the max and min value with a thirD column showing as timedifference. I am skipping any 12.am time so used the syntax below. ANy help how to achieve the third column, timedifference.. It is in DB2.
SELECT EMPID,MIN(STARTDATETIME),MAX(ENDDATETIME)
FROM TABLE
WHERE DATE(STARTDATETIME)= '2012-05-15' AND HOUR(STARTDATETIME)<>0 AND HOUR(ENDDATETIME)<>0
GROUP BY EMPID
You can use the results from that in an inner select, and use those values to define the TimeDifference column. My knowledge of DB2 is very limited, so I'm making some assumptions, but this should give you an idea. I'll update the answer if something is drastically incorrect.
Select EmpId,
MinStartDate,
MaxEndDate,
MaxEndDate - MinStartDate As TimeDifference
From
(
Select EMPID,
MIN(STARTDATETIME) As MinStartDate,
MAX(ENDDATETIME) As MaxEndDate
From Table
Where DATE(STARTDATETIME) = '2012-05-15'
And HOUR(STARTDATETIME) <> 0
And HOUR(ENDDATETIME) <> 0
Group By EMPID
) A
I have a table with 3 columns example below. The table is populated with few records. I am given a task to search through each debtorid below, and return the last occurrence of a specific status lets say PTP. I query through the table order by the followupdate as below:
SELECT TOP 50 [status],[FollowupDate],debtorid
FROM dbo.FollowUp
WHERE ( dbo.FollowUp.FollowUpDate >= '01-01-2014 00:00:00.000'
and dbo.FollowUp.FollowUpDate <= '01-10-2014 23:59:00.00')
ORDER BY FollowUpDate
From the table above, the last the [status] 'PTP' record for debtorid '589009' is at the first row and I want the first row returned, for [debtorid] 589006 is on the second row and finally for debtorid '589002' is third on the table from below/DESC order. The query should return those three rows presumably . Please how do I archive this.
I thought I should be able to return the status 'PTP' from the table by simply like
SELECT TOP 1 * FROM [tblname] WHERE date BETWEEN date1 and dat2 AND status='PTP'
HOWEVER this is not table wise. Its rather debtorid wise which is a column in the table and
The following should do the trick:
SELECT Status, DebtorId, MAX(FollowUpDate)
FROM dbo.FollowUp
WHERE Status = 'PTP'
GROUP BY
Status, DebtorId;
Note that e.g. debtor 589002 has more than one status of 'PTP' on the same day, hence the need for the GROUPING to ensure that duplicates are ignored.
try this..
SELECT DISTINCT Status,DebtorId,FollowupDate
FROM dbo.FollowUp
WHERE Status='PTP'
AND FollowupDate BETWEEN date1 AND date2
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)