Finding Records Created Within N Minutes Of Each Other

Feb 18, 2008

I have the following generic table structure.

CREATE TABLE tableName (
...
, datetime_created datetime DEFAULT GetDate()
, created_by_user char(12)
)

I want to find out which records were created within a given period of time per user. For example I want to know if the user "georgev" (or any users) has created more than one record in this table within a 2 minute period.

Hopefully thsi will warm your brains this cold Monday morning :p

Any more information needed, let me know!

View 8 Replies


ADVERTISEMENT

Finding Minutes In Datetime Intervals [SQL Server2005]

May 29, 2008

[SQL Server 2005]


In a report there are following in parameters: from date, to date, from time and to time.

I need to match these search criteria€™s against start time and stop time in assignments.
The trick: How to write SQL that meets all different cases that can arise.

The query need's to traverse through every assignment,
and see if its start and stop time is in the interval given by the user.

It should then return the minutes from the assignment that is within the interval.



I'm thinking of packaging the minute "extracting" logic in a Scalar-valued function.

It should then take the in parameters from the user, and start time and stop time from the assignment as arguments.

To consider: Crossing midnight.



E.g. the user puts in

From date: 2008-05-24

From time: 22:00:00

To date: 2008-05-26

To time: 02:00:00



Since the user wants to evaluate a time span that crosses midnight,

I'll have to look in to time spans like:

2008-05-24 22:00:00 - 2008-05-24 23:59:59

2008-05-25 00:00:00 - 2008-05-25 02:00:00

2008-05-25 22:00:00 - 2008-05-25 23:59:59

2008-05-26 00:00:00 - 2008-05-26 02:00:00



Furthermore the start- and stop time can be partially in the above interval.

E.g. an assignment starts 2008-05-24 21:46:11 and stops 2008-05-24 22:36:05.

This would then yield from the time spans above 36 minutes.

The combinations seem never-ending€¦

View 9 Replies View Related

All Records Within X Minutes Of Each Other

Aug 30, 2005

Consider a table that holds Internet browsing history for users/machines,date/timed to the minute. The object is to tag all times that are separatedby previous and subsequent times by x number of minutes or less (it couldvary, and wouldn't necessarily be a convenient round number). This willenable reporting "active time" for users (a dubious inference, but hey).There are a lot of derivative ways of seeing this information that might begood to get to. What's the fist and last of these sets of times? Whatpercentage of a given period is spanned by active times, and not? What isthe average duration of such periods? What is the average interval betweenweb hits during such periods? During other times?Blah, blah. The basic problem is my principal problem. I don't have muchexperience with cursors, but from what I understand it would be very goodindeed to spare them, given the number of records I anticipate workingwith.I'd be glad of any pointers.--Scott

View 29 Replies View Related

8000 Records In Over 12 Minutes?

May 28, 2014

I have the following SQL select query:

Code:
SELECT
inbound_emails.inbound_email_id,
inbound_emails.campaign_id,
campaign_header.campaign_name,
customers.customer_name,
inbound_emails.date_received,

[Code] ....

Without any indexes (other than PK's) this query would take 1min 21 secs to return all 8500 records.

I wasn't satisfied with that so I added indexes to the tables.

Indexes are;

inbound_emails:

PK: inbound_email_id
1. campaign_id (non-unique, non-clustered)
2. email_from (non-unique, non-clustered)
3. email_to (non-unique, non-clustered)

campaign_header:

PK: campaign_id
1. campaign_name (unique, non-clustered)

customers:
PK: customer_id
1. customer_name (unique, non-clustered)

contracts:
PK: contract_id
1. contract_name (unique, non-clustered)
2. customer_id (non-unique, non-clustered)

Have I made a hash of the indexes? Or is the query statement badly written? What is a reasonable amount of time to expect to retrieve 8500 records?

I think email_from and email_to on inbound_emails is the main culprit, but I don't understand why?

View 11 Replies View Related

SQL Server 2012 :: Finding What Table A User-created Statistics Is Located?

Feb 21, 2014

I can easily find user created stat in a databaseSELECT * FROM DB.sys.stats WHERE user_created=1But how do I determine what tables those stats are in? with over 6000 tables I don't feel like looking through all the tables.

View 2 Replies View Related

Records Newer Than 90 Minutes (smalldatetime)

Jul 20, 2005

Hi,I have a table in SQLServer2000 where e.g. the actual time is insert forevery record (smalldatetime). But how do I select every records newerthan 90 minutes?/Chris

View 4 Replies View Related

UPDATE Only Records That Were Created After A Date

Sep 5, 2007

I have a text file that I am importing into my database.

Within this textfile there is a field named "Order_Date". Date Format is like this: 20070905

I want to run this DTS once a day, updating only the records that were created on "today's" date.

For example something like this:

UPDATE NAME
WHERE Order_Date = ????? (I want a variable that is todays date in 20070905 format)

View 7 Replies View Related

T-SQL (SS2K8) :: Display Row As 2 Days Ago / 1 Hours 34 Minutes Ago / 11 Minutes Ago

Apr 21, 2015

My table as data as follow,

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[table_Data]') AND type in (N'U'))
DROP TABLE [dbo].[table_Data]
GO
/****** Object: Table [dbo].[table_Data] Script Date: 04/21/2015 22:07:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[table_Data]') AND type in (N'U'))

[code].....

View 6 Replies View Related

Finding "missing" Records

Mar 7, 2001

Hi,

OK, here's a question for all you SQL Gurus.

I want to find all the records that are in t2 but are not in t1.
(NOTE: table print outs at bottom of post)

I could write this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~``
select t2.t from t2 where t not in (select t from t1)
t
-----------
6

(1 row(s) affected)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~``

BUT it takes to long.

So I thought of this:
select t2.t , t1.t from t2 left join t1 on t2.t = t1.t where t1.t = null

BUT it doesn't work.

WHY NOT?
Is there anything similar that i can write???

It should work, heres the output for the simple left join:

select t2.t , t1.t from t2 left join t1 on t2.t = t1.t
t t
----------- -----------
1 1
2 2
3 3
4 4
5 5
6 NULL
7 7

(7 row(s) affected)



Thanks,
Benny Hill

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~``



select * from t1
t
-----------
1
2
3
4
5
7

(6 row(s) affected)



select * from t2
t
-----------
1
2
3
4
5
6
7

(7 row(s) affected)

View 4 Replies View Related

Finding Orphaned Records

Mar 10, 2004

I am trying to find records that are orphaned in a data base after thier parent record (in another table) has been deleted.

Something happened to the constraints and I had no idea until now this had happened.

I have the faint rustle of a query in query sort of thing but my knowledge needs expansion.

PS: sorry for the cross post - just noticed that I was in the wrong forum before.

Thanks,
Matt

View 11 Replies View Related

Finding Duplicate Records

Oct 29, 2007

Hello,

I searched for all the posts which covered my question - but none were close enough to answer what i'm trying to do. Basically, the scenario is thus;

Table1 contains values for UserID, Account code, and Date.

My query (below) is trying to find all the accounts assigned to a particular user ID, but also those duplicate account codes which belong to a second user ID. The date column would be appended to the result set.

The query I'm using is as follows;

select acccountcode, userid, date from dbo.table1
where exists (select accountcode from dbo.table1 where accountcode = table1.accountcode
group by accountcode
having count(*) > 1)
and userid = 'x-x-x'
order by accountcode

What I think this produces is a list of all files where a duplicate exists, but of course it leaves out the 2nd UserID...which is crucial.

Hopefully this makes sense. Any insight my fellow DBA's can share would be greatly appreciated!

Thanks,
D.

View 1 Replies View Related

Finding Changed Records

Jul 20, 2005

I need to create a table that would be the result set of a comparisonbetween table a and table b? Table a and b first 2 fields will always bethe same (CustomerName and CustomerNumber). But if the Address1 fieldchanges in table a, I would like to throw that whole row into mycomparison table. Almost like a Select Into with a sub query that wouldinclude a WHERE TableA.field <> TableB.field. I would need to do thiscomparison for about 8 fields. Help appreciated for my syntax is prettybad. Thanks.Steve*** Sent via Developersdex http://www.developersdex.com ***Don't just participate in USENET...get rewarded for it!

View 1 Replies View Related

Subquery With Operators - Finding Records?

Jun 19, 2012

I've got a subquery that keeps throwing up an error but I can’t think of another way of completing the query.

Firstly I need to find records where TestQty =3

(this would find 2 records with TestQty =3, TestNumber, 7171003 and 7088650)

The TestNumber is not unique so I would like the final set of records to include all the records with TestNumber, 7171003 and 7088650

TestNumberTestQty
7088650____________3
7088650____________1
7088650____________2
7088650____________1
7088650____________2
7171003____________1
7171003____________3
7171003____________2

Code below:
[Code SQL]
USE TestWarehouse
IF ('dbo.TestItems') IS NOT NULL
DROP TABLE [dbo].[TestItems];
GO
CREATE TABLE [dbo].[TestItems]
( TestItem int not null IDENTITY (1,1)

[Code] .....

View 5 Replies View Related

Finding And Grouping Records Off Of A Given Field Value

Jun 16, 2007

In the "tblEmailGroupLink" table...

I need to find all records with the "UnSubscribed" field having a "True" value. All these records will have a corresponding "Emailid" field.



In the "tblEmailAddress" table...

The same "Emailid" field has a corresponding "EmailAddress" field.



What needed is all the email addresses found in the "EmailAdddress" field of these records.





I'm very new at this so I hope I explained this right. I'd really appreciate any help I can get.



Thanks,

Bill

View 2 Replies View Related

Finding Identical Values For A Given Set Of Records??

Mar 13, 2008

Here's what I'd like to be able to do: I have a queue that holds any number tasks, so something like this here:


queue_1 task_a


task_b
task_c
task_d

Workers are assigned to teams, Red team, Blue team, Green team. What I need to do is identify instances where all tasks for a given queue have been handled by one team. Once a task has been assigned to a queue any team can work on it, but when only one team has completed every task in a queue a bonus should be awarded.

I'm looking for this to award a bonus:
queue_num task_num team
queue_1 task_a red
task_b red
task_c red
task_d red

No bonus for any team here
queue_num task_num team
queue_1 task_a red
task_b blue
task_c red
task_d green

So the red team earns a bonus. Now, I have thousands of queues each containing any number of tasks. Using T-SQL how can I find all queues where only one team was responsible for completeing every task assigned to the queue? Do I have to use a cursor and eval each task coming through or is there a faster, more efficient way to handle this in SQL?

View 4 Replies View Related

Visual Studio C# Finding Records On A SQL Database

Jul 9, 2007

I need to find the name of the user that signed in to the web page.  For this, I have a web page with a text box, the program should access a SQL database use, use the userID and return the name.  My code (working) does the following:string mySqlQuery = "";
string strCnn = "";SqlDataReader myDataReader = null;
mySqlQuery = "SELECT c_name FROM database WHERE c_user='demo'";
//connect to the databasestrCnn = "Data Source=" + connectionServer + "; Initial Catalog=" + connectionDatabase + "; user id=" + connectionUserID + "; password=" + connectionPassword + "; Integrated Security=false";cnn = new SqlConnection(strCnn);
 
try
{
cnn.Open();MessageBox.Show("Success Opening the Database");
}
catch
{MessageBox.Show("Problems with the database");
}SqlCommand sqlSelectCommand = new SqlCommand(mySqlQuery, cnn);
 
 tbName.Text = "me";
cnn.Close();
 
This works, I just need to retrieve the value of c_name.  Any help, thanks

View 2 Replies View Related

SQL Server - Finding The Different Records In Two Identical Tables

Jul 30, 2007

Does anyone have a good query that would return records from two tables that are found in one, but not it the other table?  In my situation I have 2 tables that are duplicate tables and I need to find an additional 3000 records that were added to one of the tables.  I also have a composite key so the query would have col1, col2 and col3 as the composite key.  So far I have tried concatenating the 3 columns and giving the result an alias and then trying to show the ones that were not in both tables, but have been struggling.  Thanks.. 

View 4 Replies View Related

SQL Server 2008 :: Reminder Emails - Records Created With Status

Jun 29, 2015

We have a difficult requirement.

When records are initially created, they are given statuses of New, Pending, Closed

By default, the status is New.

If no action has been taken after 15 days since the records were created, users are sent a reminder.

I have this part working.

The problem is that as long as the status is not equal to closed, start sending reminders every 10 days.

Here is what I am working with:

SELECT [ID] , [Name], [Email],
Status
FROMmyTable
WHERE Status_Name != 'Closed' AND Status_Name IS NOT NULL and Status_Name = 'New' and Status='Pending'
AND CONVERT(Char,DateAdd(day,-15,getdate()),101) = CONVERT(Char,Date_Entered,101)

View 2 Replies View Related

T-SQL (SS2K8) :: Finding Last Records Inserted Into All Tables In A Database

Jul 27, 2015

I have a CRM database that has a lot of tables and would like to be able to extract the last 'x' records in descending order from each table based on a common a field 'modifiedon' that is in every table and is auto populated by the system.

View 4 Replies View Related

Transact SQL :: Comparing Records - Finding Matches / Duplicates

Nov 20, 2015

I have this 40,000,000 rows table... I am trying to clean this 'Contacts' table since I know there are a lot of duplicates.

At first, I wanted to get a count of how many there are.

I need to compare records where these fields are matched:

MATCHED: (email, firstname) but not MATCH: (lastname, phone, mobile).
MATCHED: (email, firstname, mobile)
But not MATCH: (lastname, phone)
MATCHED: (email, firstname, lastname)
But not MATCH: (phone, mobile)

View 9 Replies View Related

Transact SQL :: Finding Multiple Records Based On Another Table

Nov 6, 2015

I have 2 tables A, B with 1 to many relationship

Table A(ProductID),  TableB(ProductID, FileID)

I need to find only the records in Table A that may have more than one FileIDs in Table B,  since some ProductIDS have multiple FileIDs in Table B...

View 8 Replies View Related

SQL Server 2012 :: Select Statement - Finding Duplicate Records

Feb 18, 2014

I am trying to build a select statement that will

1). find records where a duplicate field exists.
2.) show the ItemNo of records where the Historical data is ' '.

I have the query which shows me the duplicates but I'm stuck when trying to only show records where the Historical field = ' '.

Originally I thought I could accomplish this goal with a sub query based off of the original duplicate result set but SQL server returns an error.

Below you will find a temp table to create and try in your environment.

create table #Items
(
ItemNovarchar (50),
SearchNo varchar (50),
Historical int

[Code] ....

Ultimately I need a result set that returns 'ATMR10 MFZ N', and 'QBGFEP2050 CH DIS'.

View 3 Replies View Related

T-SQL (SS2K8) :: Finding 5 Most Recent Records For Each Customer With Abnormal Order Amounts

Nov 5, 2014

The database consists of the following tables:

create table dbo.customer (
customer_id int identity primary key clustered,
customer_name nvarchar(256) not null
)
create table dbo.purchase_order (
purchase_order_id int identity primary key clustered
customer_id int not null,
amount money not null,
order_date date not null
)

Implement a query for the report that will provide the following information: for each customer output at most 5 different dates which contain abnormally high or low amounts (bigger or less than 3 times SDTDEV from AVG), for each of these dates output minimum and maximum amounts as well.

Possible result: [URL] ......

View 7 Replies View Related

Get New Database Created Then Running Script To Created Tables / Relationships

Jun 29, 2015

trying to get a new database created then running a script to created the tables, relationships, indexes and insert default data. All this I'm making happen during the installation of my Windows application. I'm installing SQL 2012 Express as a prerequisite of my application and then opening a connection to that installed SQL Server using Windows Authentication. 

E.g.: Data Source=ComputerNameSQLEXPRESS;Initial Catalog=master;Integrated Security=SSPI; Then I run a query from my code to create the database eg: "CREATE DATABASE [MyDatabaseName]".

From this point I run a script using a Batch file containing "SQLCMD....... Myscriptname.sql". In my script I have my tables being created using "Use [MyDatabaseName]   Go   CREATE TABLE [dbo].[MyTableName] .....". So question is, should I have [dbo]. as part of my Create Table T-SQL commands? Can I remove "[dbo]."? Who would be the owner of the database? If I can remove the [dbo]., should I also remove dbo. from any query string from within my code?

View 3 Replies View Related

SQL Query: Finding Records Between Datetime Inside Datetime

Mar 17, 2007

Hey :)I'm facing a lot of troubles trying to create a new pause/break-system. Right now i'm building up the query that counts how many records that is inside 2 fields. Let me first show you my table:
ID (int)     |    stamp_start (Type: DateTime)        |      stamp_end (Type: DateTime)           |      Username (varchar)0             |      17-03-07 12:00:00                      |            17-03-07 12:30:00                     |     Hovgaard
The client will enter a start time and a end time and this query should then count how many records that are inside this periode of time.
 Example: The client enter starttime: 12:05 and endtime: 12:35.The query shall then return 1 record found. The same thing if the user enters 12:20 and 12:50.My current query looks like this:SELECT COUNT(ID) AS Expr1 FROM table WHERE (start_stamp <= @pausetime_start) AND (end_stamp >= @pausetime_end)But this will only count if I enter the exact same times as the one inside the table.Any ideas how I can figure this out?Thanks for your time so far :)/Jonas Hovgaard - Denmark

View 2 Replies View Related

SQL 2012 :: Query To Make Single Records From Multiple Records Based On Different Fields Of Different Records?

Mar 20, 2014

writing the query for the following, I need to collapse the continuity. If the termdate for an ID is one day less than the effdate of the next id (for the same ID) i need to collapse the records. See below example .....how should i write the query which will give me the desired output. i.e., get min(effdate) and max(termdate) if termdate is one day less than the effdate of next record.

ID effdate termdate
556868 1999-01-01 1999-06-30
556868 1999-07-01 1999-10-31
556869 2002-10-01 2004-01-31
556872 1999-02-01 2000-08-31
556872 2000-11-01 2004-01-31
556872 2004-02-01 2004-02-29

output should be ......

ID effdate termdate
556868 1999-01-01 1999-10-31
556869 2002-10-01 2004-01-31
556872 1999-02-01 2000-08-31
556872 2000-11-01 2004-02-29

View 0 Replies View Related

How To Re-set A Job To Run 10 Minutes Later

Aug 12, 2005

Hi,

I have a job where the first step starts and checks for a condition. If its not true, I want it to reset itself and start again in 10 minutes. I'm using sp_stop_job and sp_update_jobschedule and, initially, it looks like it works. But since it's a Daily job, the 'Next Run Date' increments to the following day. Even though I'm using sp_update_jobschedule to keep the active_start_date as the same day, it still increments. I've tried updating sysjobschedules directly, but get the same results.

Any thoughts much appreciated! Here's my code:
USE msdb


--This is the part that goes in the job step
--and increments the next_run_time if the condition is true.

If
(Select count('x') from mytable (NoLock)
Where PublicationDate > getdate()) < 1
BEGIN
Declare @ActiveStartDate int
Declare @ActiveStartTime int

Select @ActiveStartDate = active_start_date from msdb.dbo.sysjobschedules (NoLock)
Where schedule_id = 61
Select @ActiveStartTime = active_start_time from msdb.dbo.sysjobschedules (NoLock)
Where schedule_id = 61

EXEC msdb.dbo.sp_stop_job @job_name = 'Owners'
Select @ActiveStartTime = @ActiveStartTime + 1000

EXEC sp_update_jobschedule @job_id = '46C074D4-908D-46AE-8B0C-A23E3AD4A4F6',
@name = 'Daily',
@active_start_date = @ActiveStartDate,
@active_start_time = @ActiveStartTime
End

View 3 Replies View Related

Min/max Of X Minutes

Mar 27, 2006

I am trying to develop a sql statement that will create a recordset of the min (or max) values in x minute increments over a period of time.

e.g. over a period of 7 days, I have data that was collected in 1 minute intervals. I need to know the min (or max) value in each 10 minute interval over that same period of time.

Is there an efficient way of doing this?

View 3 Replies View Related

Group On Minutes

Mar 4, 2002

Hi,
I need to write a query whioch give me count on
0-15 minutes and then from 0-720 minutes.
I don't know how to group it.
Any help appreciated.
TIA

View 1 Replies View Related

Datetime To Minutes?

Aug 3, 2004

hey all, i need to find the ratio of difference in 2 datetime variables and the difference of another 2 datetime vars. I figured the best way to do it is to convert the difference in both numerator and denominator to number of minutes.

can anyone help ??

View 1 Replies View Related

CURRENT_TIMESTAMP - 5 Minutes

Sep 25, 2006

:shocked: hi,

I want to use CURRENT_TIMESTAMP - 5 minutes in a select and where clause.

I have tried using

CURRENT_TIMESTAMP - 0.004 AS [Time_-6]

But this is not a round off to a whole minute

Also tried

CURRENT_TIMESTAMP, dateadd(minute, datediff(minute, 0, CURRENT_TIMESTAMP) / 5 * 5, 0)

But this will not do wholes seconds e.g

CURRENT_TIMESTAMP = 10.03.33

CURRENT_TIMESTAMP, dateadd(minute, datediff(minute, 0, CURRENT_TIMESTAMP) / 5 * 5, 0) = 10.00.00

Can anyone help??

View 2 Replies View Related

MS SQL Performance From 10 To 3 Minutes

Sep 27, 2006

Hello all !

I am runing from .NET application an SQL Query
it normally return the rows in 10 seconds
but time to time the application turn 2 or 3 minutes and nearlly crash (or crash)

with exactly the same datas in database

what can be the reasons ?

thank you

View 8 Replies View Related

Got Seconds But Need Minutes

Apr 16, 2004

I have a result that comes out in number of seconds, but need to see it converted to minutes and hours and seconds. Is there a convert function that would do this?

Thanks,
Dan

View 3 Replies View Related







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