•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 426,451 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,350 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums
Views: 2324 | Replies: 2
![]() |
•
•
Join Date: Jun 2005
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
i have two tables
table1 has the folowing fields
lastprice
symbol
time
table2 has the same fields
now table1 stores only the last data of the corrosponding symbol
but table2 goeson adding the data as it comes in
now i want to write a query get the difference of the lastprice of table1 and the lastprice of table2 which was recorder 5 minutes back something like
table1.lastprice,table2.lastprice where
table2.time<(table2.time-5 min)
now the problem is that i have to get only the first data of table2 for the correspondig symbol for that time.
pls help
table1 has the folowing fields
lastprice
symbol
time
table2 has the same fields
now table1 stores only the last data of the corrosponding symbol
but table2 goeson adding the data as it comes in
now i want to write a query get the difference of the lastprice of table1 and the lastprice of table2 which was recorder 5 minutes back something like
table1.lastprice,table2.lastprice where
table2.time<(table2.time-5 min)
now the problem is that i have to get only the first data of table2 for the correspondig symbol for that time.
pls help
SELECT symbol, lastprice, time
FROM table1 INNER JOIN table2
ON table1.symbol = table2.symbol
WHERE table2.time < (table2.time - 5min)
Would probably give you the result.
Hope this helps

FROM table1 INNER JOIN table2
ON table1.symbol = table2.symbol
WHERE table2.time < (table2.time - 5min)
Would probably give you the result.
Hope this helps

•
•
•
•
Originally Posted by abhishekj
i have two tables
table1 has the folowing fields
lastprice
symbol
time
table2 has the same fields
now table1 stores only the last data of the corrosponding symbol
but table2 goeson adding the data as it comes in
now i want to write a query get the difference of the lastprice of table1 and the lastprice of table2 which was recorder 5 minutes back something like
table1.lastprice,table2.lastprice where
table2.time<(table2.time-5 min)
now the problem is that i have to get only the first data of table2 for the correspondig symbol for that time.
pls help
Assistant Manager, Regional Pharmacy Information Systems
TLC Services Website (Under Construction)
Updated : ASP.Net Login Code
TLC Services Website (Under Construction)
Updated : ASP.Net Login Code
•
•
Join Date: Jun 2005
Location: Nothamptonshire
Posts: 27
Reputation:
Rep Power: 4
Solved Threads: 1
This query depends upon the format that the time is stored in. MS SQL doesn't have a TIME data type.The DATETIME format can store both dates and times (obviously). The next point is that you cannot just subtract one DATETIME from another. You have to use DATEDIFF. So, asuming that the time column is in fact a DATETIME datatype the requested quiery should look something like this.
select t1.lastprice - t2.lastprice as price_difference
from table1 t1, table2 t2
where t2.symbol = t1.symbol
and t2.time = (select top 1 t3.time
from table2 t3
where t3.symbol = t1.symbol order by time desc)
This query expects that the relevant record in table2 was the last one inserted (the current record hasn't been transfered from table1 to table2 yet.)
select t1.lastprice - t2.lastprice as price_difference
from table1 t1, table2 t2
where t2.symbol = t1.symbol
and t2.time = (select top 1 t3.time
from table2 t3
where t3.symbol = t1.symbol order by time desc)
This query expects that the relevant record in table2 was the last one inserted (the current record hasn't been transfered from table1 to table2 yet.)
![]() |
•
•
•
•
•
•
•
•
DaniWeb MS SQL Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- TravelMate 3212NWXMi - Urgent RAM Upgradation...!!! (Motherboards, CPUs and RAM)
- How do i using a Query in crystal report?... and (Visual Basic 4 / 5 / 6)
- formulating a sql query.Its urgent ! (MS Access and FileMaker Pro)
- Urgent Help with SQL Query (MySQL)
- Need Urgent Solution (Visual Basic 4 / 5 / 6)
Other Threads in the MS SQL Forum
- Previous Thread: RPC server unavailable
- Next Thread: grouping unique name in table


Linear Mode