Solved

SQl Display Query

Posted on 2012-03-29
6
313 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

685 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