Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ArviL
Starting Member
25 Posts |
Posted - 2011-03-03 : 02:28:57
|
HiI have a table dbo.products, with structure:prod_id init (PK),prod_cl_date datetime,prod_cl_vl_code varchar(3),...prod_eu_course_init numeric(30,20),prod_eu_date_init datetimeI need to update fields prod_eu_course_init and prod_eu_date_init with according (latest at dbo.products.prod_cl_date) values (vl_eu_course and vl_date) from another table, dbo.valuta_log, with structure:vl_row init (PK),vl_date datetime,vl_vl_code varchar(3),vl_eu_course, where the dbo.valuta_log.vl_vl_code = dbo.products.prod_cl_vl_code and dbo.valuta_log.vl_date <= dbo.products.prod_cl_date.I can do this easily processing dbo.products row-wise, but is tehre a way to do this with a single SELECT clause?Thanks in advance!Arvi Laanemets |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2011-03-03 : 03:23:20
|
I don't understand your requirement completely but I guess that below update statement should give you some idea to start with:update productsset prod_eu_course_init = vl_vl_code, prod_eu_date_init = vl_datefrom products p inner join valuta_log von v.vl_vl_code = p.products.prod_cl_vl_code and v.vl_date <= p.prod_cl_date |
 |
|
ArviL
Starting Member
25 Posts |
Posted - 2011-03-03 : 03:59:25
|
quote: Originally posted by pk_bohra set prod_eu_course_init = vl_vl_code, prod_eu_date_init = vl_datefrom products p inner join valuta_log von v.vl_vl_code = p.products.prod_cl_vl_code and v.vl_date <= p.prod_cl_date
An example:We have a product, with current price in USD estabilished at 01.02.2011In valuta log, we have USD courses p.e.12.03.2005 course050312...27.01.2011 course11012728.01.2011 course11012830.01.2011 course11013002.02.2011 course11020205.02.2011 course110205...(where course###### are really some numeric values)The query must return for this product prod_eu_course_init=course110130andprod_eu_date_init =30.01.2011For another product, with current price in USD estabilished at 28.01.2011, the query must returnprod_eu_course_init=course110128 andprod_eu_date_init =28.01.2011etc. |
 |
|
ArviL
Starting Member
25 Posts |
Posted - 2011-03-03 : 11:23:24
|
quote: Originally posted by ArviL HiI have a table dbo.products, with structure:prod_id init (PK),prod_cl_date datetime,prod_cl_vl_code varchar(3),...prod_eu_course_init numeric(30,20),prod_eu_date_init datetimeI need to update fields prod_eu_course_init and prod_eu_date_init with according (latest at dbo.products.prod_cl_date) values (vl_eu_course and vl_date) from another table, dbo.valuta_log, with structure:vl_row init (PK),vl_date datetime,vl_vl_code varchar(3),vl_eu_course, where the dbo.valuta_log.vl_vl_code = dbo.products.prod_cl_vl_code and dbo.valuta_log.vl_date <= dbo.products.prod_cl_date.I can do this easily processing dbo.products row-wise, but is tehre a way to do this with a single SELECT clause?
The solution (I'm not sure about typos there - I had to edit to make the query more basic):---;WITH init_date AS ( SELECT pr.prod_id, pr.prod_cl_vl_code, MAX(vl.vl_date) AS InitDate FROM dbo.products pr LEFT JOIN dbo.valuta_log vl ON pr.prod_cl_vl_code = vl.vl_vl_code AND vl.vll_date <= pr.prod_cl_date GROUP BY pr.prod_id, pr.prod_cl_vl_code)UPDATE dbo.productsSET prod_eu_date = b.InitDate, prod_eu_course = c.vl_eu_courseFROM dbo.products a LEFT JOIN init_date b ON b.prod_id = a.prod_id LEFT JOIN dbo.valuta_log c ON c.vl_vl_code = b.prod_cl_vl_kod AND c.vl_date=b.InitDate--- |
 |
|
|
|
|
|
|