| Author |
Topic |
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-25 : 02:07:03
|
| I would like to make the following data in my table (when I specify a date range)SID fromDate ToDate Price1 2006/01/01 2006/1/3 1001 2006/1/4 2006/1/5 150intoSID Adate Price1 2006/1/2 1001 2006/1/3 1001 2006/1/4 150Because this is used in my search engine so the second table must be compiled out efficiently and in real-time.Please help me to write a short SQL code(SQL Only) to help me to achieve this~~!! |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-25 : 02:12:12
|
| Why would you like to omit the first date in the range of 20060101 to 20060103, and omit the last date in the range for 20060104 to 20060105?PLEASE POST PROPER SAMPLE DATA AND PROPER EXPECTED OUTPUT BASED ON THE PROVIDED SAMPE DATA.Peter LarssonHelsingborg, Sweden |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-25 : 02:16:19
|
quote: Originally posted by Peso Why would you like to omit the first date in the range of 20060101 to 20060103, and omit the last date in the range for 20060104 to 20060105?PLEASE POST PROPER SAMPLE DATA AND PROPER EXPECTED OUTPUT BASED ON THE PROVIDED SAMPE DATA.Peter LarssonHelsingborg, Sweden
Hi, Thank you for your prompt reply.The first table is a simplified raw data and when the user specifies a data range, only those requested would be outputed, resulting in the second table. So the first date and the last date are omited to indicate that user might specify any range they desire.Regards. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-25 : 02:18:12
|
| Shouldn't the specified from date, and the to date, be inclusive?Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-25 : 02:28:52
|
| What will happen when there is a gap?SID fromDate ToDate Price1 2006/1/11 2006/1/13 1001 2006/1/18 2006/1/19 150And the user choose 2006/1/5 to 2006/1/21 ?Peter LarssonHelsingborg, Sweden |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-25 : 02:31:18
|
quote: Originally posted by Peso Shouldn't the specified from date, and the to date, be inclusive?Peter LarssonHelsingborg, Sweden
weather the fromdate and todate are inclusive depends on the user's request. The user might only want the price from 2006/1/2-2006/1/4, as shown in this case, then I have to extract the data out from the raw data and display it properly. |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-25 : 02:33:59
|
quote: Originally posted by Peso What will happen when there is a gap?SID fromDate ToDate Price1 2006/1/11 2006/1/13 1001 2006/1/18 2006/1/19 150And the user choose 2006/1/5 to 2006/1/21 ?Peter LarssonHelsingborg, Sweden
Good questionThanks for askingIf there is a gap then it will just display Null |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-25 : 02:37:23
|
quote: Originally posted by wangyc77
quote: Originally posted by Peso What will happen when there is a gap?SID fromDate ToDate Price1 2006/1/11 2006/1/13 1001 2006/1/18 2006/1/19 150And the user choose 2006/1/5 to 2006/1/21 ?Peter LarssonHelsingborg, Sweden
Good questionThanks for askingIf there is a gap then it will just display Null
it will still dislay all the dates but the price field is Null |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-25 : 02:43:22
|
| [code]-- prepare sample datadeclare @t table (sid int, fromdate datetime, todate datetime, price money)insert @tselect 1, '2006/1/11', '2006/1/13', 100 union allselect 1, '2006/1/18', '2006/1/19', 150-- initialize user requestdeclare @fromdate datetime, @todate datetimeselect @fromdate = '2006/1/5', @todate = '2006/1/21', @fromdate = @fromdate + 1, @todate = @todate - 1-- something to start withselect t.sid, x.date as adate, t.pricefrom f_table_date(@fromdate, @todate) as xleft join @t as t on t.fromdate <= x.date and t.todate >= x.date[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-25 : 02:52:22
|
quote: Originally posted by Peso
-- prepare sample datadeclare @t table (sid int, fromdate datetime, todate datetime, price money)insert @tselect 1, '2006/1/11', '2006/1/13', 100 union allselect 1, '2006/1/18', '2006/1/19', 150-- initialize user requestdeclare @fromdate datetime, @todate datetimeselect @fromdate = '2006/1/5', @todate = '2006/1/21', @fromdate = @fromdate + 1, @todate = @todate - 1-- something to start withselect t.sid, x.date as adate, t.pricefrom f_table_date(@fromdate, @todate) as xleft join @t as t on t.fromdate <= x.date and t.todate >= x.date Peter LarssonHelsingborg, Sweden
Thank you, I will give it a quick try and see if there is anymore question |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-25 : 03:05:53
|
quote: Originally posted by wangyc77
quote: Originally posted by Peso
-- prepare sample datadeclare @t table (sid int, fromdate datetime, todate datetime, price money)insert @tselect 1, '2006/1/11', '2006/1/13', 100 union allselect 1, '2006/1/18', '2006/1/19', 150-- initialize user requestdeclare @fromdate datetime, @todate datetimeselect @fromdate = '2006/1/5', @todate = '2006/1/21', @fromdate = @fromdate + 1, @todate = @todate - 1-- something to start withselect t.sid, x.date as adate, t.pricefrom f_table_date(@fromdate, @todate) as xleft join @t as t on t.fromdate <= x.date and t.todate >= x.date Peter LarssonHelsingborg, Sweden
Thank you, I will give it a quick try and see if there is anymore question
doesselect @fromdate = '2006/1/5', @todate = '2006/1/21', @fromdate = @fromdate + 1, @todate = @todate - 1 belongs to f_table_date?? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-25 : 03:24:06
|
| No, if you make this as a stored procedure, @fromdate and @todate are the parameters passed to the stored procedure from your client application.Peter LarssonHelsingborg, Sweden |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-25 : 03:59:11
|
quote: Originally posted by Peso No, if you make this as a stored procedure, @fromdate and @todate are the parameters passed to the stored procedure from your client application.Peter LarssonHelsingborg, Sweden
so I just create yourF_TABLE_DATE (found at http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519)into my DB right? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-25 : 04:23:54
|
| It is not mine, it is Michael's function. But yes, you copy and paste that function into QA or SSMS and run once. Then the function is stored in your database server.Peter LarssonHelsingborg, Sweden |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-26 : 01:03:41
|
quote: Originally posted by Peso It is not mine, it is Michael's function. But yes, you copy and paste that function into QA or SSMS and run once. Then the function is stored in your database server.Peter LarssonHelsingborg, Sweden
Hi, I am kind of new in this so I am having some problem.I created the function and it works great and I copied now your code and ran it. The whole thing runs great but when I use SELECT, shouldn't the data show in the SQL Query Analyzer grid? I know it is a dumb question....Thank youJon |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-01-26 : 01:06:40
|
quote: shouldn't the data show in the SQL Query Analyzer grid
press Ctrl-D and run you query. Ctrl-D will switch the result to GRID. Ctrl-T to textor click on the execute mode to change KH |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-26 : 01:23:50
|
quote: Originally posted by khtan
quote: shouldn't the data show in the SQL Query Analyzer grid
press Ctrl-D and run you query. Ctrl-D will switch the result to GRID. Ctrl-T to textor click on the execute mode to change KH
Hi,Sorry I didnt phrase my question well. I did run in result in Grid Mode, but the problem is that nothing is shown in the gridsThank you |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-26 : 01:30:16
|
| When I ran the function, it says Server: Msg 8152, Level 16, State 2, Procedure F_TABLE_DATE, Line 477Character or binary data will be truncatedIs this harmless? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-01-26 : 01:33:09
|
How do you run it ? can you post it here ? KH |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-01-26 : 01:37:22
|
quote: Originally posted by khtan How do you run it ? can you post it here ? KH
The results in Grid option is turned on.For the function, I just placed the compltet code and ran it by pressing the arrow button in QA and the function is created with the errors shown above. But it seems ok.For the join table, I just pasted the exact code and ran it.I didnt alter anything. |
 |
|
|
Next Page
|