Transact SQL :: Find Duration From Two Records

Oct 26, 2015

I am working on one logic. I have a table as below.

Declare @Table table (ID int, StartDate datetime, EndDate datetime)
Insert Into @Table (ID, StartDate, EndDate)
Values (1, '2013-01-01', '2013-12-31') 
, (1, '2014-01-01', '2014-06-30') 
, (2, '2014-01-01', '2014-07-31') 
, (2, '2014-08-02', '2014-08-30') 
select * from @Table 

I need as below.

ID, StartDate, EndDate
1, '2013-01-01', '2014-06-30'
2, '2014-01-01', '2014-07-31'
2, '2014-08-02', '2014-08-30'

Means If ID is same for two or more than two records then difference between first row's EndDate and second row's StartDate is 1 day then we should get one record as output. How can we built this logic in T-SQL ?

View 12 Replies


ADVERTISEMENT

Transact SQL :: Get Total Request Duration From Multiple Task Duration?

Jun 4, 2015

I have the following SQL query

SELECT
[Req_ID]
,[Service_Name]
,[Req_Started_Date]
,[Task_Name]
,[Task_Status]
,[Performer_Full_Name]

[code]....

Which works fine, but what I need to calculate the total duration of a request based on the duration of the tasks completed in the request based on Req_ID. I would like to use the CASE statement I have to determine the SLA_Mins for each task and add them together to get total request SLA_Mins.

Below is the create table schema and data

GO
/****** Object: Table [dbo].[MidrangeOtherSourceControl] Script Date: 06/03/2015 18:13:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[MidrangeOtherSourceControl](
[Req_ID] [float] NULL,
[Service_Name] [nvarchar](255) NULL,

[code]....

View 9 Replies View Related

Sum Of Duration Between Time Records

Aug 6, 2013

In my example, i have a table as below:

Time|Machine
2013-08-05 10:12:00 |M1
2013-08-05 10:15:00 |M1
2013-08-05 10:20:00 |M1
2013-08-05 10:11:00 |M2
2013-08-05 10:15:00 |M2
2013-08-05 10:16:00 |M2
2013-08-05 10:17:00 |M2
2013-08-06 11:12:00 |M1
2013-08-05 11:42:00 |M1
2013-08-05 12:02:00 |M1

I want to have a report for every machine by time that will show how long each machine worked. The result is something like below:

M1|8 (minutes)
M2|6
M1|50

View 5 Replies View Related

Transact SQL :: Get Duration Of Each Event 0 Which Has Other Events Before And After?

Jun 22, 2015

I have a table that holds log event records, that keep getting appended. I need to get the duration of the each event 0 which has other events before and after. How can I do this - to get event duration of event 0 and the cumulative.

ID Event   Date
1    1        2015-06-21 21:01:44.457
2    1        2015-06-21 21:01:44.457
3    0        2015-06-21 21:02:04.780
4    1        2015-06-21 21:02:32.600
5    0        2015-06-21 21:02:57.967
6    1        2015-06-21 21:03:30.513

View 7 Replies View Related

Transact SQL :: Need To Get Average Duration Per Day For Every Month

Sep 23, 2015

I am storing duration of a lot of jobs in a column in a table per job. This duration is in seconds and an integer datatype.

Sample data:
Job        Duration    date
Job1       25          2015/9/23
Job2       30          2015/9/23
Job3       45          2015/9/23
Job4       1            2015/9/22

Now I need to get average duration per day for every month. Is this possible? I have a calendar table that has every single day month year microsecond millisecond  second minute and hour.

View 5 Replies View Related

Transact SQL :: Calculating Duration In Days For Generic From And To

Oct 7, 2015

I need the SQL to calculate the duration between a day and time, no specific date. 

For example:
Sun 00:00 > Mon 08:00 = 5.67 days

So there is no date, it's just the general duration  (in days), from a specific day and time to a specific day and time.

View 3 Replies View Related

Looping Thru Records To Find Related Records

Oct 31, 2007

Hi, I have had this problem for a while and have not been able solve it.

What im looking at doing is looping thru my patient table and trying to organise the patients in to there admission sequence, so when patient "A" comes in and is treated at my hospital and is discharged and admitted to another Hospital within one day then patient "A" will get a code of 1 being there first admission.

then if patient "A" is admitted again but there admission date is greater than one day they get a code of 2 being for there second admission but will need to loop thru table looking for other admissions and discharges.

The table name is Adm_disc_Match_tbl

Basically what i have 4 fields.
Index_key = which is the patient common link (text)
ur_episode = this wil change for each Hospital (text)
Admission_datetime = patient admission date and time (datetime)
Discharge_datetime = patient discharge date and time (datetime)

example of data


Code: ( text )
Index_key,ur_episode,Admission_datetime,discharge_ datetime
HERBERT-7/1929,513884-1686900,4/07/2006 10:58,17/07/2006 13:37
HERBERT-7/1929,C023092-1698859,17/07/2006 13:20,24/07/2006 0:30
ELSIE-5/1916,G148445-1720874,8/08/2006 11:00,30/08/2006 10:00
STANISLAWA-3/1918 ,G119981-1720045,8/08/2006 13:01,22/08/2006 12:13
FREDA-11/1925,183772-1998910,27/03/2007 9:53,3/04/2007 11:06
FREDA-11/1925,G147858-2007408,3/04/2007 10:49,26/04/2007 12:39
FREDA-11/1925,183772-2037727,28/04/2007 17:05,9/05/2007 11:41
FREDA-11/1925,G147858-2052082,9/05/2007 12:00,25/05/2007 11:17


If anyone could help it would be much appreciated.

View 6 Replies View Related

SQL Server 2008 :: Loop Through Date Time Records To Find A Match From Multiple Other Date Time Records?

Aug 5, 2015

I'm looking for a way of taking a query which returns a set of date time fields (probable maximum of 20 rows) and looping through each value to see if it exists in a separate table.

E.g.

Query 1

Select ID, Person, ProposedEvent, DayField, TimeField
from MyOptions
where person = 'me'

Table

Select Person, ExistingEvent, DayField, TimeField
from MyTimetable
where person ='me'

Loop through Query 1 and if it finds ANY matching Dayfield AND Timefield in Query/Table 2, return the ProposedEvent (just as a message, the loop could stop there), if no match a message saying all is fine can proceed to process form blah blah.

I'm essentially wanting somebody to select a bunch of events in a form, query 1 then finds all the days and times those events happen and check that none of them exist in the MyTimetable table.

View 5 Replies View Related

How To Find Last 10 Records Using T-SQL?

Sep 7, 2001

Hi all,

Anybody would tell me how can I find last 10 records in a table using T-SQL?

Thank you very much!

Lee

View 3 Replies View Related

Find Same Records With Same ID

Aug 1, 2007

Hello,

Having trouble describing my problem……

I have the table below, and I am trying to retrieve TileIDs that have the same ModelIDs.

ModelID TileID
HP DL380 G3 120v Dual15400
HP DL380 G3 120v Dual15400
HP DL380 G3 120v Dual15400
HP DL380 G3 120v Dual15400
HP DL380 G3 120v Dual15400
HP DL380 G3 120v Dual15400
Sun SF 280R 120v 15401
Sun SF 280R 120v 15401
Sun SF 280R 120v 15401
Sun SF 280R 120v 15401
Lantronix MSS4 15401



So TileID ‘15400’ would be a keeper, since all ModelIDs are the same.

Any help would be appreciated.

Thanks

View 4 Replies View Related

How To Find Avg Of Last N-1 Records In Sql?

Jun 24, 2008

Hello friends,
plza help meeeeeeee

View 3 Replies View Related

How To Find This Records?.

Mar 4, 2008

tbl_user Data
user Updatedate
Bill 08/02/2006
Bill 08/02/2008
Peter 11/02/2008
Liz 01/02/2008
sam 01/02/2007





select * from
where Updatedate < getdate()-30 days and (not in Updatedate > getdate())


i want to get all records

output i am looking....
user Updatedate
Liz 01/02/2008
sam 01/02/2007

View 4 Replies View Related

Transact SQL :: Retrieve All Records From Parent Table And Any Records From Child Table

Oct 21, 2015

I am trying to write a query that will retrieve all students of a particular class and also any rows in HomeworkLogLine if they exist (but return null if there is no row). I thought this should be a relatively simple LEFT join but I've tried every possible combination of joins but it's not working.

SELECT
Student.StudentSurname + ', ' + Student.StudentForename AS Fullname,
HomeworkLogLine.HomeworkLogLineTimestamp,
HomeworkLog.HomeworkLogDescription,
ROW_NUMBER() OVER (PARTITION BY HomeworkLogLine.HomeworkLogLineStudentID ORDER BY

[Code] ...

It's only returning two rows (the students where they have a row in the HomeworkLogLine table). 

View 3 Replies View Related

Find Records From 2 Situations?

Dec 3, 2007

Hi All, I am trying to find records when searchProductCount = 2 AND when searchProductCount < 2 BUT productID not in (select pid from TableB) ... I have the query below ... but is there any other better way to do this?
TableB has IDs: 100, 700 ...etc
eg: searchProduct ID is 50,100
-- means returns everything when we found ALL productID (50,100) from TableA, count =2
Select productName, productID from TableA where searchProductCount = 2 AND productID IN (50,100)
union all
-- when not all productID found in TableA, we only return productsID from TableA which ID found in TableB, count < 2
Select productName, productID from TableA where searchProductCount < 2 and productID IN (select pid from TableB) -- in this case, pid found in TableB from searchProductID will be 100
------------------------------------------------------------------
it comes out there are duplicates results (when first query is valid, we union all second query, so we have duplicates records). How can we eliminate the duplicates? Or is there better way to acheive this without using union all?

View 3 Replies View Related

Find Duplicate Records

Jan 12, 2000

Hi,

Does anybody know the SQL query to find the duplicate records?

Many Thanks in advance!

View 2 Replies View Related

How To Find Duplicate Records

Feb 4, 2003

Hello board,

I was wondering if anyone can tell me an easy way to find duplicate records on sql. The thing is this, at work we have a database (table) which includes tracking numbers, I need a easy way to be able to search this table for duplicate tracking numbers and print them out. I currently access this table to edit some data by using the following path “Start > Programs > Microsoft SQL Server > Enterprise Manager” then work my down the tree to “Databases > Master > Tables” on tables I do a right click and “open table/query”. Any help would be most appreciated. Believe me I’m very “SQL illiterate”

Bill
:confused:

View 2 Replies View Related

How To Find Duplicate Records

May 13, 1999

Hi,

As far as I know in SQL Server 6.5 there is no concept called rowid. How can I find duplicate records in a table and delete them.

Thanks,
Srini

View 2 Replies View Related

Find Duplicate Records

Apr 17, 2014

I have this query that I have been using to find duplicate records works great except for now. The logic I am adding is pcs_rreas <> 'NG'. When I add this it does take out the NG, but it also excludes the reocords that have NULL data in this field. I don't want that to happen. How can I fix this? I tried adding (pcs_rreas <> 'NG' or pcs_rreas is null) but nothing is pulling now.

SELECT pcs_id1, pcs_rreas, pcs_lname, pcs_fname, pcs_minit, pcs_degree, pcs_xtyp, pcs_office, pcs_dba, pcs_dob, pcs_sex, pcs_eff, pcs_trm, pcs_spec1, pcs_spec2, pcs_spec3,
pcs_spec4, pcs_tax1, pcs_ssn, pcs_altid, pcs_upin, pcs_medic, pcs_mcaid, pcs_ecs, pcs_npi, pcs_status, pcs_dir, pcs_den, pcs_www, pcs_hold, pcs_email,
pcs_misc1, pcs_misc2, pcs_newdt, pcs_newby, pcs_chgdt, pcs_chgby, pcs_malp, pcs_pf, pcs_ctl, pcs_sys, pcs_pay, pcs_ann, pcs_bcert, pcs_pass, pcs_w9,
pcs_taxex, pcs_tax2, pcs_type, pcs_rreas, pcs_routo, pcs_rtime, pcs_rdate, pcs_force, pcs_flag, pcs_v419, pcs_bcity, pcs_bstate,

[code]...

View 3 Replies View Related

Find Latest Records

Dec 16, 2007

Hi all,

I have a question regarding SQL Server Performance and would be grateful for a tip. Let's say I have a DB with 50.000 records. These records belong to 1.000 different datasets, so there is 1 actual and 49 historical data records. For example a company with 1000 employees has a database where each year a new record is created for each employee so after 50 years they have 50.000 records (50 years x 1000 employees). 1 record is actual, and 49 are historical. What is the best way to store this? Main target is performance for the enduser, so when an employee clicks "See all my records" it should be fast. But on the other hand the application mainly works only with the current year. Additionally it should also be fast for the boss of business unit who wants to see the latest records of his e.g. 100 employees. I have some ideas and would like to get your opinion:

1. Retrieve by latest date
Just store the records. To get the current year just select the record with the latest year. Disadvantage might be with larger databases: If the company switches to store the requests per month, each user would have 600 records (12 months x 50 years). Each time the latest record should be retrieved, 600 recards have to be compared regarding the latest date (or sorted by date descending using Top1, but this might be a problem for the boss then? Or could this be combined for a group if the boss wants to see all the latest records of his employees?).

2. Add a 'IsCurrent'-Flag
Each time a new record is stored it should be compared to the latest record. If it is newer, the 'IsCurrent'-Flag should be removed and then checked on the new record. This should be fast processed (because on saving a new record it only needs to be checked against the currently 'IsCurrent'-flagged record), and for retrieving the current record no further comparison is necessary. But how could I do this? I need to update the "AddRecord"-SP with this comparison, but I don't know how to do this.

Currently number 2 is my favorite, I just don't know how to do it ;-) What is your opinion about it, and could you include an example?

Thanks

View 20 Replies View Related

How To Find Records Containing Only Digits?

Mar 12, 2008

Hello,

I would like to find all the records that contain only digits. So far, I have this:


SELECT *
FROM tmp1
WHERE (word LIKE N'[0-9]')


It returns only ten results, each containing a single digit. What I need is to find all the records of any length containing only digits, like '378', '2005', etc. but not records containing both digits and other stuff (e.g. letters) like 'I95' or 'P2P'.

Any idea?

Cornelius

View 7 Replies View Related

How Can I Find 'broken' Records ?

Dec 5, 2006

Version: Microsoft SQL Server 2000 - 8.00.2040 (Intel X86)

Problem:

Server: Msg 8964, Level 16, State 1, Line 1
Table error: Object ID 515532920. The text, ntext, or image node at page (1:377289), slot 1, text ID 897099563008 is not referenced.
Server: Msg 8964, Level 16, State 1, Line 1
Table error: Object ID 515532920. The text, ntext, or image node at page (1:377447), slot 1, text ID 897100939264 is not referenced.
<... and 4 same errors>

DBCC results for 'CC_Document'.
The repair level on the DBCC statement caused this repair to be bypassed.
<same messages>

The repair level on the DBCC statement caused this repair to be bypassed.
There are 192 rows in 6 pages for object 'CC_Document'.
CHECKTABLE found 0 allocation errors and 8 consistency errors in table 'CC_Document' (object ID 515532920).
repair_allow_data_loss is the minimum repair level for the errors found by DBCC CHECKTABLE (crmproded.dbo.CC_Document repair_fast).

I won't run checktable with 'repair_allow_data_loss' before all records aren't find out

I try run DBCC PAGE on this pages. It print info

PAGE: (1:377289)
----------------

BUFFER:
-------

BUF @0x01249540
---------------
bpage = 0x2928A000 bhash = 0x00000000 bpageno = (1:377289)
bdbid = 8 breferences = 0 bstat = 0x9
bspin = 0 bnext = 0x00000000

PAGE HEADER:
------------

Page @0x2928A000
----------------
m_pageId = (1:377289) m_headerVersion = 1 m_type = 3
m_typeFlagBits = 0x0 m_level = 0 m_flagBits = 0x8000
m_objId = 515532920 m_indexId = 255 m_prevPage = (0:0)
m_nextPage = (0:0) pminlen = 0 m_slotCnt = 2
m_freeCnt = 3742 m_freeData = 4446 m_reservedCnt = 0
m_lsn = (19116:290571:12) m_xactReserved = 0 m_xdesId = (0:0)
m_ghostRecCnt = 0 m_tornBits = 856438469

Allocation Status
-----------------
GAM (1:2) = ALLOCATED SGAM (1:3) = NOT ALLOCATED
PFS (1:372048) = 0x42 ALLOCATED 80_PCT_FULL DIFF (1:6) = NOT CHANGED
ML (1:7) = NOT MIN_LOGGED


Blob fragment at: Page (1:377289) Slot 0 Length: 4266 Type: 3 (DATA)

Blob Id:897099563008


2928A06E: 7fe8108f 64a115ed 245a1a1a b68e502c .......d..Z$,P..
<blob fragment ... >

2928B0FE: 00006278 00006e87 00007ad1 xb...n...z..


Blob fragment at: Page (1:377289) Slot 1 Length: 84 Type: 4 (LARGE_ROOT_2)

Blob Id: 897099563008 Level: 0 MaxLinks: 5 CurLinks: 5

Child fragment at Page (1:377237) Slot 0 Offset: 8080

Child fragment at Page (1:377238) Slot 0 Offset: 16160

Child fragment at Page (1:377239) Slot 0 Offset: 24240

Child fragment at Page (1:377288) Slot 0 Offset: 32320

Child fragment at Page (1:377289) Slot 0 Offset: 36572



There are 5 links at Slot 1 , that's generate error. But why? What does it mean - "The text, ntext, or image node at page (1:377289), slot 1, text ID 897099563008 is not referenced" ???

I saw other pages (not 'broken') - there is: Blob Fragment don't contains links - links are in other pages.

What are these 'broken' records in my example - they are chunks of lost information, that i can't recover?

Or not? May be exists a way, that i find all 'broken' records? Because, my table contains 192 records. But there is BLOB field - and i can't check consistency of every file, that uploaded in my table. In addition I will fully appreciate this problem.

At last, if i find record, i run checktable with repair_allow_data_loss, then upload file again.

p.s. These errors contained in backup too.

View 7 Replies View Related

Transact SQL :: How To Find A Node

Nov 8, 2015

i have a table below like :
Id    ,    Request   

int     nvarchar(100)   
and in Request field i put below data :
1 <request><F3>353535</F3><F6></F6></request> 2 <request><F5>353535</F5><F6></F6></request>3 <request><F3>353535</F3><F6></F6></request>

now i need to a query that i can find records that exists <F3> and if exists , remove just the <F3> tag

 below like :
1 <request><F6></F6></request> 2 <request><F5>353535</F5><F6></F6></request>3 <request><F6></F6></request>

View 18 Replies View Related

Find Duplicate Records In Table

Oct 5, 2007

Hello friends,
I have a one problem, i have a table in that some reocrds are duplicate.i want to find which records are duplicate.
for exp. my table is as follows
emp_id              emp_name
1                          aa
2                          bb
3                          cc
1                          aa
3                          cc
3                          cc
and i want the result is like
emp_id              emp_name
1                       aa
1                       aa
3                       cc
3                       cc
3                       cc
 

View 6 Replies View Related

Find Records For X Previous Days

Jul 13, 2005

On a webform, I have three button ... [7 days]  [15 days]  [30 days]When the user clicks one of the buttons, I want to return their orders for the past X days. The WHERE clause would include something like this (for 7 days):WHERE (Order_Date BETWEEN CONVERT(DATETIME, GETDATE() - 7, 102) AND CONVERT(DATETIME, GETDATE(), 102))How do I parameterize the number of days?Thanks,Tim

View 2 Replies View Related

Find Records With A Blank Field

Mar 13, 2000

I want to be able to use a query to display all the records in the 6.5 database that have no data in the STATUS field. This is the query I thought would work....."SELECT * from travel_date WHERE status="''"

But, that is not working. Can someone please help me figure out the right way to wrtie this?

I appreciate your help!

View 2 Replies View Related

How To Find Records In Database Using Select

Apr 27, 2004

I need to be able to find certain data as the user has submitted that data twice and delete one record,except that have hundreds of tables and don't know the table where she would have submitted the data, but I have some other key info that I can start with.

My question is,how do i select a record from the database if I don't know the table it comes from?

Could somebody give me an expamle please?

View 2 Replies View Related

Find Time Ranges Records

May 25, 2008

I have a DateTime field, I need to find out how many records are in 8am-11am, 12pm-5pm, 6pm-7am regardless of date. How can I do this?

Kamran Shahid
Sr. Software Engineer(MCSD.Net)
www.netprosys.com

View 2 Replies View Related

Find Records Where There Is Difference Between 2 Columns

Jan 24, 2014

I am trying to produce a report in Application express. I want to find records where there is a difference between 2 columns eg

Debt_amount - billed_amount

where debt >billed amount

I only want to see records where the debt is more than the amount billed

View 1 Replies View Related

Find The Smallest Value From Multiple Records

Jun 14, 2007

I have a table with the following variables:

patid cddate nadcd4

Patients (patid) have multiple records. I am trying to find the lowest nadcd4 for each patient along with the associated cddate. I can do a min(nadcd4) and find the lowest nadcd4 by patid but I can't seem to get the associated cddate. Any suggestions? Thanks

wal

View 6 Replies View Related

Find Missing/deleted Records?

Jul 20, 2005

I have 2 tables say table1 and table2 with the same structure. Each recordis identified by a field 'SerialNo'. Now there should be a total of 500000records in both tables with serialno from 1 to 500000. Either a record is intable1 or table2. I want to find records (or SerialNo's) that are inneither table (if deleted by accident etc). What would be the sql query?I'm using SQL 6.5thx

View 2 Replies View Related

Transact SQL :: How To Find Duplicates For Different Fields

Oct 27, 2015

I have a table that has Serial numbers and MSDSID numbers and many other fields in the table.

What I need to figure out is if the table contains a distinct Serial number with different MSDSIDs.

So If I have in the table

Serial          MSDSID          Date          Size...
001                 20            1/1/2015        5
002                 21            1/1/2015        3
001                 22            2/1/2015        1
003                 21            3/1/2015        1
004                 23            1/15/2015      5
003                 22            1/20/2015      6
004                 23            2/21/2015      5
002                 21            4/25/2015      4

I would like the results to show:

Serial            Count
001                  2           the count is 2 because Serial 001 has an MSDSID of 20 and 22
002                  1           the count is 1 because Serial 002 only has MSDSID 21
003                  2           the count is 2 because Serial 003 has an MSDSID of 21 and 22
004                  1           the count is 1 because Serial 002 only has MSDSID 23

It would be even better if the results just showed where the count is greater than 1.

View 5 Replies View Related

Transact SQL :: How To Find Mail Is Queued

Jul 19, 2015

I want to send an email twice a day, from database. So I have planned to make a storedproce which will be called by a job (which will select some record from one table and put it in other table based on a flag) but I want to run it in a transaction so that if email is send successfully then only it should commit else it should rollback.

How  can i find that "Mail queued" now i should commit.

View 4 Replies View Related

Transact SQL :: How To Find If A Column Is Used Anywhere In Database

Oct 20, 2015

I have looked around quite a bit, but mostly what I have found is looking to see if a table is used or if a column is in a stored procedure and honestly most of what I have seen does not work.

I want to reduce our nightly import by removing any columns that are not being used. We insert into our staging tables, Stage1 for example. And say Stage1 has column1 and column2. If those columns are not being used, then I want to remove them from Stage1. The only catch is that every Stage1 table has a v_Stage1. v_Stage1 should have all the columns from Stage1, but doesn't always. So I need to know what columns from Stage1 are used somewhere other than v_Stage1 and what columns from v_Stage1 are not used. 

View 14 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved