Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 9th, 2008, 08:00 AM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL Server 2005 : Query problem

I'm a sql server beginner.

I've have these following table

tbllocation
Main_ID | Date_Taken | Time |Hit
-----------------------------------------
206 | 5/9/2008 | 100 | 2
206 | 5/9/2008 | 200 | 3
206 | 5/6/2008 | 300 | 6
201 | 5/1/2008 | 400 | 5
201 | 5/4/2008 | 500 | 9
201 | 5/7/2008 | 600 | 2
204 | 5/2/2008 | 700 | 2
204 | 5/3/2008 | 800 | 4
204 | 5/6/2008 | 900 | 2
203 | 5/7/2008 | 100 | 2
203 | 5/8/2008 | 200 | 3
203 | 5/9/2008 | 300 | 6
202 | 5/4/2008 | 400 | 5
202 | 5/3/2008 | 500 | 9
202 | 5/8/2008 | 200 | 3
205 | 5/2/2008 | 300 | 6
205 | 5/1/2008 | 400 | 5
205 | 5/9/2008 | 500 | 9

tblSetValue
Main_ID | Hit2
---------------
206 | 3
201 | 5
204 | 3
203 | 1
202 | 8
205 | 7
*Main_ID is a primary key

Condition
1. Let's say, the current date is 5/9/2008
2. Result only display the last 7 days data. From above data. it's mean only pickup from 5/3/2008 to 5/9/2008
3. Every Main_ID only pickup the MAX Hit
4. Diff (column on the fly) = Hit - Hit2

The expected result shown as follow
tblResult
Main_ID | Date_Taken | Time | Hit | Hit2 | Diff
-----------------------------------------------
206 | 5/6/2008 | 300 | 6 | 3 | 3
201 | 5/4/2008 | 500 | 9 | 5 | 4
204 | 5/3/2008 | 800 | 4 | 3 | 1
203 | 5/9/2008 | 300 | 6 | 1 | 5
....
....
....

Anyone can help me to show the query?


Reply With Quote
  #2 (permalink)  
Old May 11th, 2008, 08:30 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

well, I'm a mysql-ist so may not work in sql-server but you should be able to google equivalents for functions like CURDATE() and so on. Also I changed the main_id fields in my test database so the numbers don't correspond.

Code:
mysql> SELECT * FROM location;
+---------+------------+------+------+
| main_id | dateTaken  | time | hit  |
+---------+------------+------+------+
|       1 | 2008-05-09 |  100 |    2 | 
|       1 | 2008-05-09 |  200 |    3 | 
|       1 | 2008-05-06 |  300 |    6 | 
|       2 | 2008-05-01 |  400 |    5 | 
|       2 | 2008-05-04 |  500 |    9 | 
|       2 | 2008-05-07 |  600 |    2 | 
|       3 | 2008-05-02 |  700 |    2 | 
|       3 | 2008-05-03 |  800 |    4 | 
|       3 | 2008-05-06 |  900 |    2 | 
|       4 | 2008-05-07 |  100 |    2 | 
|       4 | 2008-05-08 |  200 |    3 | 
|       4 | 2008-05-09 |  300 |    6 | 
|       5 | 2008-05-04 |  400 |    5 | 
|       5 | 2008-05-03 |  500 |    9 | 
|       5 | 2008-05-08 |  200 |    3 | 
|       6 | 2008-05-02 |  300 |    6 | 
|       6 | 2008-05-01 |  400 |    5 | 
|       6 | 2008-05-09 |  500 |    9 | 
+---------+------------+------+------+
18 rows in set (0.00 sec)

mysql> SELECT * FROM setValue;
+---------+------+
| main_id | hit2 |
+---------+------+
|       1 |    3 | 
|       2 |    5 | 
|       3 |    3 | 
|       4 |    1 | 
|       5 |    8 | 
|       6 |    7 | 
+---------+------+
6 rows in set (0.00 sec)

mysql> SELECT location.main_id, dateTaken, time, MAX(hit) AS maxHit, hit2, (MAX(hit)-hit2) as diff 
         FROM location,setValue 
        WHERE dateTaken BETWEEN DATE_SUB(CURDATE(),INTERVAL 7 DAY) AND CURDATE() 
          AND location.main_id=setValue.main_id 
     GROUP BY main_id 
     ORDER BY main_id;
+---------+------------+------+--------+------+------+
| main_id | dateTaken  | time | maxHit | hit2 | diff |
+---------+------------+------+--------+------+------+
|       1 | 2008-05-09 |  100 |      6 |    3 |    3 | 
|       2 | 2008-05-04 |  500 |      9 |    5 |    4 | 
|       3 | 2008-05-06 |  900 |      2 |    3 |   -1 | 
|       4 | 2008-05-07 |  100 |      6 |    1 |    5 | 
|       5 | 2008-05-04 |  400 |      5 |    8 |   -3 | 
|       6 | 2008-05-09 |  500 |      9 |    7 |    2 | 
+---------+------------+------+--------+------+------+
6 rows in set (0.00 sec)

mysql>
Can't remember off the top of my head how to get rid of the - sign off your diff column. There will be a function though.

HTH,
Charlie

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
remote sql server 2005 problem navbingo20 ASP.NET 2.0 Basics 2 July 16th, 2008 03:08 AM
Query Sql Server 2005, geographical data itHighway SQL Language 0 June 3rd, 2008 11:25 PM
SQL Server 2005 query for last of file edx SQL Language 10 July 12th, 2007 07:50 AM
SQL Server 2005 Express Problem asped_man BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 3 November 29th, 2005 05:07 AM



All times are GMT -4. The time now is 08:35 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.