Solved

SQl Display Query

Posted on 2012-03-29
6
306 Views
Last Modified: 2012-04-03
Hi

I have been tasked with the following, We have alot of visitors visiting our company and the company would like some for of Welcome to display on a screen in reception, so what i am doing is collecting data from our CRM software which i will then display onto the monitor.

The query looks like this at the moment

SELECT     dbo.dmactiv.startdate, dbo.dmactiv.starttime, dbo.dmactiv.duration, dbo.dmcont.firstname, dbo.dmcont.lastname, dbo.dmcont.comp_name
FROM         dbo.dmactiv INNER JOIN
                      dbo.dmcont ON dbo.dmactiv.contid = dbo.dmcont.contid

and this output something along the lines of this

Startdate             StartTime         Duration      Firstname     LastName          Comp_Name

2012-04-18                780                    60                joe                bloggs             microsoft


so i deally what i would like to do is half an hour before the arrival the query returns only those that are visiting and once the duration has ended then they no longer are part of the query.


However as you can see from the above data the start Time and duration are produced as whole numbers and i am not sure how to acheive this

Can anybody help

John

Ps sorry for not posting the code properly but all the body functions always seem to be greyed out and i can never add anything not sure why :(
0
Comment
Question by:pepps11976
  • 3
  • 3
6 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 37782372
What do the whole numbers represent (e.g., minutes)?
0
 

Author Comment

by:pepps11976
ID: 37782444
yes that particular example that i posted above its start time is 13.00 so 780 mins is what it is representing

John
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 37782466
If it is in minutes, then here is an example of what I may do.
First, the main idea is to generate a start and end DATETIME value from the three columns you have listed.

e.g.,

DECLARE @StartDate DATE = '2012-04-18', @StartTime INT = 780, @Duration INT = 60;
SELECT StartTime = DATEADD(MINUTE, @StartTime-30, CONVERT(DATETIME, @StartDate))
     , EndTime = DATEADD(MINUTE, @StartTime+@Duration, CONVERT(DATETIME, @StartDate))
;

StartTime               EndTime
----------------------- -----------------------
2012-04-18 12:30:00.000 2012-04-18 14:00:00.000

Open in new window


Note: I subtracted 30 minutes to account for the "half an hour before the arrival" portion of the requirement. Without this, you would get the actual start time, which I believe is 1PM.

Putting this together with your query:

SELECT dbo.dmactiv.startdate, dbo.dmactiv.starttime, dbo.dmactiv.duration
     , dbo.dmcont.firstname, dbo.dmcont.lastname, dbo.dmcont.comp_name
FROM dbo.dmactiv
INNER JOIN dbo.dmcont ON dbo.dmactiv.contid = dbo.dmcont.contid
WHERE CURRENT_TIMESTAMP BETWEEN DATEADD(MINUTE, dbo.dmactiv.starttime-30, CONVERT(DATETIME, dbo.dmactiv.startdate)) AND DATEADD(MINUTE, dbo.dmactiv.starttime+dbo.dmactiv.duration, CONVERT(DATETIME, dbo.dmactiv.startdate))
;

I hope that helps!
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:pepps11976
ID: 37782490
Thats great

i assume with what you posted it gets rid of visits that have gone past there time etc?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 37782510
Yes, once the current timestamp greater than the end time of the appointment, the record will no longer show (tested). So, if there query is scheduled it should pick up new visitors and drop off old ones automatically.
0
 

Author Comment

by:pepps11976
ID: 37782543
Superb Thanks for your help
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now