由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - Help on SQL statement
相关主题
紧急请教SQL tuning高手一个问题急问一个关于T-SQL的问题,谢谢
sql questioninterview question (SQL)
SQL Server 2000: 计算时间差怎样精确到小时SQL combine two columns from two different tables no shared (转载)
How to write this loop SQL query?ask for help with a simple query!!!
how to see all the tables in SQL*PLUS?问个SQL问题
Oracle 求助日期格式初级问题
Can I create thousands table in one...A question of filling in missing value in SQL
急问: 问一个SQL的问题 在线等!请帮忙在PL/SQL里面执行下面的计算,一个笔记软件破解用的
相关话题的讨论汇总
话题: productid话题: createdate话题: price话题: sql
进入Database版参与讨论
1 (共1页)
C****n
发帖数: 2324
1
Hello,
I have a table, has:
ProductID, Price, CreateDate
I need to create a view, has:
ProductID, LatestPriceof1999,Latestpriceof2000,Latestpriceof2001
Latestpriceof1999 means the latest price in year 1999.
Assume there're only data for 1999,2000,2001.
Who can help me out?
Note: I am creating a view, if you come up with a stored procedure
solution, don't answer.
If you Select statement is more than 2 pages, don't answer.
Demo Data:
1 1
a****c
发帖数: 25
2
Try this.
Create or Replace View Latest_Price
as
select '1999' Year, ProductID, Price LatestPrice
from DEMO_TABLE a
where CreateDate = (
select max(CreateDate)
from DEMO_TABLE b
where b.ProductID = a.ProductID
and b.CreateDate between to_date('01-JAN-99') and to_date('31-DEC-99') )
union all
select '2000' Year, ProductID, Price LatestPrice
from DEMO_TABLE a
where CreateDate = (
select max(CreateDate)
from DEMO_TABLE b
where b.ProductID = a.ProductID
and b.CreateDate between to_date('01-JAN-00')

【在 C****n 的大作中提到】
: Hello,
: I have a table, has:
: ProductID, Price, CreateDate
: I need to create a view, has:
: ProductID, LatestPriceof1999,Latestpriceof2000,Latestpriceof2001
: Latestpriceof1999 means the latest price in year 1999.
: Assume there're only data for 1999,2000,2001.
: Who can help me out?
: Note: I am creating a view, if you come up with a stored procedure
: solution, don't answer.

C****n
发帖数: 2324
3
It will work.
But I need something like:
productID, price1999,price2000,price2001
4 columns
not 3 columns like:
productID, year, price.
Calvin

【在 a****c 的大作中提到】
: Try this.
: Create or Replace View Latest_Price
: as
: select '1999' Year, ProductID, Price LatestPrice
: from DEMO_TABLE a
: where CreateDate = (
: select max(CreateDate)
: from DEMO_TABLE b
: where b.ProductID = a.ProductID
: and b.CreateDate between to_date('01-JAN-99') and to_date('31-DEC-99') )

g****n
发帖数: 40
4
select productID,
max(LatestPriceof1999) LatestPriceof1999,
max(LatestPriceof2000) LatestPriceof2000,
max(LatestPriceof2001) LatestPriceof2001
from (
select productID,
decode(to_char(createdate, 'YYYY'), '1999', price, null) LatestPriceof1999,
decode(to_char(createdate, 'YYYY'), '2000', price, null) LatestPriceof2000,
decode(to_char(createdate, 'YYYY'), '2001', price, null) LatestPriceof2001
from product
where (createdate, productid) in (
select max(createdate

【在 C****n 的大作中提到】
: Hello,
: I have a table, has:
: ProductID, Price, CreateDate
: I need to create a view, has:
: ProductID, LatestPriceof1999,Latestpriceof2000,Latestpriceof2001
: Latestpriceof1999 means the latest price in year 1999.
: Assume there're only data for 1999,2000,2001.
: Who can help me out?
: Note: I am creating a view, if you come up with a stored procedure
: solution, don't answer.

s**********i
发帖数: 711
5
I believe you get max prices of 1999, 2000, 2001
instead of lastest prices.

【在 g****n 的大作中提到】
: select productID,
: max(LatestPriceof1999) LatestPriceof1999,
: max(LatestPriceof2000) LatestPriceof2000,
: max(LatestPriceof2001) LatestPriceof2001
: from (
: select productID,
: decode(to_char(createdate, 'YYYY'), '1999', price, null) LatestPriceof1999,
: decode(to_char(createdate, 'YYYY'), '2000', price, null) LatestPriceof2000,
: decode(to_char(createdate, 'YYYY'), '2001', price, null) LatestPriceof2001
: from product

D****N
发帖数: 430
6
this query is enlightening!..
i want go back to work right now and rewrite our
massive self-joins-that-doesn't-work.. :)

【在 s**********i 的大作中提到】
: I believe you get max prices of 1999, 2000, 2001
: instead of lastest prices.

C****n
发帖数: 2324
7
This is a real world question asked by my Ex-GF, she's a CPA. She has an EXCEL
file including ProductID, Price, and Date, and she's trying to get the latest
price of every fiscal year for every product.
The the actual question is even more complicated because the fiscal year is
different than our year, i.e., Fiscal year of 1999 is actually between
10/01/1998 - 09/30/1999 instead of simply year 1999.
I was driven mad because she works in ACCESS, and ACCESS SQL is different to
any other T-SQL or P

【在 D****N 的大作中提到】
: this query is enlightening!..
: i want go back to work right now and rewrite our
: massive self-joins-that-doesn't-work.. :)

s**********i
发帖数: 711
8

unlikely the prices for past years will change again thus
this look like a one time thing for me. why have to spend
tons of time to find out a SQL to do it rather than simply
pull some data out and use whatver your preferred language
to do some simple processing.

【在 C****n 的大作中提到】
: This is a real world question asked by my Ex-GF, she's a CPA. She has an EXCEL
: file including ProductID, Price, and Date, and she's trying to get the latest
: price of every fiscal year for every product.
: The the actual question is even more complicated because the fiscal year is
: different than our year, i.e., Fiscal year of 1999 is actually between
: 10/01/1998 - 09/30/1999 instead of simply year 1999.
: I was driven mad because she works in ACCESS, and ACCESS SQL is different to
: any other T-SQL or P

C****n
发帖数: 2324
9
Yea, this is one time thing.
You mean to write a program t od this? It's really over kill. A CPA won't
understand what you are talk about, and she won't know what to do.
a SQL is the simplest solution cause she can just copy and paste and got wgat
she wants.
Here is the SQL I worked out, no joins. :-)
select ProductID,
Max(IIF(Date > "1999-09-30" and Date <="2000-09-30", DateDiff("D",Date,
"1999-09-30") * 100000 + price, 0)) Mod 100000 as LatestPriceOf2000,
Max(IIF(Date > "2000-09-30" and Date <

【在 s**********i 的大作中提到】
:
: unlikely the prices for past years will change again thus
: this look like a one time thing for me. why have to spend
: tons of time to find out a SQL to do it rather than simply
: pull some data out and use whatver your preferred language
: to do some simple processing.

1 (共1页)
进入Database版参与讨论
相关主题
请帮忙在PL/SQL里面执行下面的计算,一个笔记软件破解用的how to see all the tables in SQL*PLUS?
SQL问题(有包子)Oracle 求助
BSO一下订购的微软证书Can I create thousands table in one...
求帮忙recover deleted rows in oracle急问: 问一个SQL的问题 在线等!
紧急请教SQL tuning高手一个问题急问一个关于T-SQL的问题,谢谢
sql questioninterview question (SQL)
SQL Server 2000: 计算时间差怎样精确到小时SQL combine two columns from two different tables no shared (转载)
How to write this loop SQL query?ask for help with a simple query!!!
相关话题的讨论汇总
话题: productid话题: createdate话题: price话题: sql