Click here to Skip to main content
15,915,086 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
SQL
;WITH cte

AS

(
SELECT

id,

(((DATEPART(hour, CAST([time] AS DATETIME)) * 60) + (DATEPART(minute, CAST([time] AS DATETIME)) ) )) AS [time],

price,

ROW_NUMBER() OVER(PARTITION BY (((DATEPART(hour, CAST([time] AS DATETIME)) * 60) + (DATEPART(minute, CAST([time] AS DATETIME)) ) )) ORDER BY CAST(Time AS DATETIME) ASC) AS rn_1,

ROW_NUMBER() OVER(PARTITION BY (((DATEPART(hour, CAST([time] AS DATETIME)) * 60) + (DATEPART(minute, CAST([time] AS DATETIME)) ) )) ORDER BY CAST(Time AS DATETIME) DESC) AS rn_2

FROM
Table_1
WHERE

id='1';)


SELECT

max(id) as id,

MAX (LTRIM([Time] / 60) + ':' + LTRIM([Time] % 60)) AS [Time],

MAX(CASE WHEN rn_1 = 1 and rn_2 !=1 THEN price ELSE NULL END) AS [Open],

MAX(price) AS High,

MIN(price) AS Low,

MAX(CASE WHEN rn_2 = 1 THEN price ELSE NULL END) AS [Close]

FROM

cte

group by
id,
[time]
order by
[Time]

i want to save this query in a new table but i do not know how? and of course how i can to send a Parameter to this query for id ?
my table have to be like this for example:

id time open high low close
1 10:05 22 33 23 23

thanks a lot
Posted
Updated 17-May-12 20:00pm
v3
Comments
Zoltán Zörgő 27-May-12 13:45pm    
Any progress?

To store the results in a new table you can use INSERT INTO..SELECT structure.

For example:
SQL
INSERT INTO TargetTableName (Col1, Col2, ...)
SELECT Col1, Col2, ...
FROM SourceTable

For the actual situation you would use your query and the correct target table.
 
Share this answer
 
- You have two query statements there. What is the relation between these two?
- Replace your "ID" parameter with "@ID", and use the parameters property of the statement object to add value. See MSDN
- Storing result in a new table (this will create a new table):
SQL
SELECT field1, field2, field3
INTO newtable
FROM
( subquery ) as sq
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900