从日志表中获取列及其关联值并更新Article表

时间:2014-09-17 17:45:48

标签: sql sql-server-2008 tsql

我有两个Log table S38T1和S39T1。我已经进行了查询以加入它们以获取相关数据(示例数据显示在最后),如下所述:

查询:

SELECT S38T1.datreg, S38T1.procid , S38T1.ma_table, S38T1.username,
        S39T1.termname, S39T1.oldvalue, S39T1.newvalue
FROM S38T1 inner join S39T1 on S39T1.datreg = S38T1.datreg
AND S38T1.procid = S39T1.procid
AND S38T1.username Like 'ALKA'
Where S39T1.termname IN('shortl62','partwght','suwght','suvol','suheight','sulength','suwidth','huheight','hulength','huwidth','huqty')
order by S38T1.datreg ASC

基于' datreg'我必须找到最新的日期。然后

  1. 获取具有值的S39T1.termname(' shortl62',' partwght',' suwght',' suvol',' suheight'' sulength'' suwidth'' huheight'' hulength'' huwidth&#39 ;, ' huqty&#39)
  2. 获取等效的S39T1.old值(' shortl62',' partwght',' suwght',' suvol',' suheight'' sulength'' suwidth'' huheight'' hulength'' huwidth&#39 ;, ' huqty&#39)
  3. 还可以获取相应的S39T1.newvalue(' shortl62',' partwght',' suwght',' suvol',' ; suheight'' sulength'' suwidth'' huheight'' hulength'' huwidth' ' huqty&#39)
  4. 在最后一步中,我必须使用上述步骤中提取的值更新表文章。 Article表有相同的列,即(' shortl62',' partwght'' suwght',' suvol'' suheight&# 39;,' sulength'' suwidth'' huheight'' hulength'' huwidth'&# 39; huqty&#39)
  5. 我很感激您的建议。

    
    DATA:
    
    ###     datreg         |procid| ma_table| username | termname | oldvalue |newvalue|
    -------------------------------------------------------------------------------------
    2014-09-13 16:35:57.00 | 1932 | Article | ALKA     | huheight | 0.000000 | 0.000000 | 
    2014-09-13 16:35:57.00 | 1932 | Article | ALKA     | hulength | 0.000000 | 0.000000 | 
    2014-09-13 16:35:57.00 | 1932 | Article | ALKA     | huqty    | 0.200000 | 0.200000 | 
    2014-09-13 16:35:57.00 | 1932 | Article | ALKA     | huwidth  | 0.000000 | 0.000000 | 
    2014-09-13 16:35:57.00 | 1932 | Article | ALKA     | partwght | 1.000000 | 1.000000 | 
    2014-09-13 16:35:57.00 | 1932 | Article | ALKA     | shortl62 | 81       |  81      |  
    2014-09-13 16:35:57.00 | 1932 | Article | ALKA     | suheight | 0.000000 | 5.000000 | 
    2014-09-13 16:35:57.00 | 1932 | Article | ALKA     | sulength | 0.000000 | 0.000000 | 
    2014-09-13 16:35:57.00 | 1932 | Article | ALKA     | suvol    | 0.100000 | 0.100000 | 
    2014-09-13 16:35:57.00 | 1932 | Article | ALKA     | suwght   | 1.000000 | 1.000000 | 
    2014-09-13 16:35:57.00 | 1932 | Article | ALKA     | suwidth  | 0.000000 | 0.000000 | 
    2014-09-13 16:38:07.00 | 1932 | Article | ALKA     | huheight | 0.000000 | 0.000000 | 
    2014-09-13 16:38:07.00 | 1932 | Article | ALKA     | hulength | 0.000000 | 0.000000 | 
    2014-09-13 16:38:07.00 | 1932 | Article | ALKA     | huqty    | 0.200000 | 0.200000 | 
    2014-09-13 16:38:07.00 | 1932 | Article | ALKA     | huwidth  | 0.000000 | 0.000000 | 
    2014-09-13 16:38:07.00 | 1932 | Article | ALKA     | partwght | 1.000000 | 1.000000 | 
    2014-09-13 16:38:07.00 | 1932 | Article | ALKA     | shortl62 | 81       |   81     | 
    2014-09-13 16:38:07.00 | 1932 | Article | ALKA     | suheight | 5.000000 | 5.000000 | 
    2014-09-13 16:38:07.00 | 1932 | Article | ALKA     | sulength | 0.000000 | 26.000000|
    2014-09-13 16:38:07.00 | 1932 | Article | ALKA     | suvol|   | 0.100000 | 0.100000 | 
    2014-09-13 16:38:07.00 | 1932 | Article | ALKA     | suwght   | 1.000000 | 1.000000 | 
    2014-09-13 16:38:07.00 | 1932 | Article | ALKA     | suwidth  | 0.000000 | 30.000000|
    2014-09-13 16:39:17.00 | 1932 | Article | ALKA     | huheight | 0.000000 | 14.000000|
    2014-09-13 16:39:17.00 | 1932 | Article | ALKA     | hulength | 0.000000 | 30.000000|
    2014-09-13 16:39:17.00 | 1932 | Article | ALKA     | huqty    | 0.200000 | 0.200000 | 
    2014-09-13 16:39:17.00 | 1932 | Article | ALKA     | huwidth  | 0.000000 | 27.000000|
    2014-09-13 16:39:17.00 | 1932 | Article | ALKA     | partwght | 1.000000 | 1.000000 | 
    2014-09-13 16:39:17.00 | 1932 | Article | ALKA     | shortl62 |       81 |    81    | 
    2014-09-13 16:39:17.00 | 1932 | Article | ALKA     | suheight | 5.000000 | 5.000000 | 
    2014-09-13 16:39:17.00 | 1932 | Article | ALKA     | sulength | 26.000000| 26.000000|
    2014-09-13 16:39:17.00 | 1932 | Article | ALKA     | suvol    | 0.100000 | 0.100000 | 
    2014-09-13 16:39:17.00 | 1932 | Article | ALKA     | suwght   | 1.000000 | 1.000000 | 
    2014-09-13 16:39:17.00 | 1932 | Article | ALKA     | suwidth  | 30.000000| 30.000000|
    2014-09-13 16:43:03.00 | 1932 | Article | ALKA     | huheight | 14.000000| 14.000000|
    2014-09-13 16:43:03.00 | 1932 | Article | ALKA     | hulength | 30.000000| 30.000000|
    2014-09-13 16:43:03.00 | 1932 | Article | ALKA     | huqty    | 0.200000 | 3.000000 | 
    2014-09-13 16:43:03.00 | 1932 | Article | ALKA     | huwidth  | 27.000000| 27.000000|
    2014-09-13 16:43:03.00 | 1932 | Article | ALKA     | partwght | 1.000000 | 0.300000 | 
    2014-09-13 16:43:03.00 | 1932 | Article | ALKA     | shortl62 | 81       |   81     | 
    2014-09-13 16:43:03.00 | 1932 | Article | ALKA     | suheight | 5.000000 | 5.000000 | 
    2014-09-13 16:43:03.00 | 1932 | Article | ALKA     | sulength | 26.000000| 26.000000|
    2014-09-13 16:43:03.00 | 1932 | Article | ALKA     | suvol    | 0.100000 | 0.100000 | 
    2014-09-13 16:43:03.00 | 1932 | Article | ALKA     | suwght   | 1.000000 | 1.000000 | 
    2014-09-13 16:43:03.00 | 1932 | Article | ALKA     | suwidth  | 30.000000| 30.000000|
    2014-09-13 17:03:56.00 | 1932 | Article | ALKA     | huheight | 32.500000| 32.500000|
    2014-09-13 17:03:56.00 | 1932 | Article | ALKA     | hulength | 22.000000| 22.000000|
    2014-09-13 17:03:56.00 | 1932 | Article | ALKA     | huqty    | 4.000000 | 4.000000 | 
    2014-09-13 17:03:56.00 | 1932 | Article | ALKA     | huwidth  | 36.500000| 36.500000|
    2014-09-13 17:03:56.00 | 1932 | Article | ALKA     | partwght | 0.000000 | 0.342000 | 
    2014-09-13 17:03:56.00 | 1932 | Article | ALKA     | shortl62 |   86069  |  86069   |
    2014-09-13 17:03:56.00 | 1932 | Article | ALKA     | suheight | 30.500000| 30.50000 |
    2014-09-13 17:03:56.00 | 1932 | Article | ALKA     | sulength | 5.000000 | 5.000000 | 
    2014-09-13 17:03:56.00 | 1932 | Article | ALKA     | suvol    | 5414.0000| 5414.0000| 
    2014-09-13 17:03:56.00 | 1932 | Article | ALKA     | suwght   | 0.000000 | 0.000000 | 
    2014-09-13 17:03:56.00 | 1932 | Article | ALKA     | suwidth  | 35.500000| 35.500000|
    2014-09-13 17:19:47.00 | 1932 | Article | ALKA     | huheight | 0.000000 | 31.500000|
    2014-09-13 17:19:47.00 | 1932 | Article | ALKA     | hulength | 0.000000 | 50.000000|
    2014-09-13 17:19:47.00 | 1932 | Article | ALKA     | huqty    | 0.200000 | 0.200000 | 
    2014-09-13 17:19:47.00 | 1932 | Article | ALKA     | huwidth  | 0.000000 | 40.000000|
    2014-09-13 17:19:47.00 | 1932 | Article | ALKA     | partwght | 1.000000 | 1.320000 | 
    2014-09-13 17:19:47.00 | 1932 | Article | ALKA     | shortl62 |     76   |    76    |
    2014-09-13 17:19:47.00 | 1932 | Article | ALKA     | suheight | 0.000000 | 30.000000|
    2014-09-13 17:19:47.00 | 1932 | Article | ALKA     | sulength | 0.000000 | 39.000000|
    2014-09-13 17:19:47.00 | 1932 | Article | ALKA     | suvol    | 0.100000 | 0.100000 | 
    2014-09-13 17:19:47.00 | 1932 | Article | ALKA     | suwght   | 1.000000 | 1.000000 | 
    2014-09-13 17:19:47.00 | 1932 | Article | ALKA     | suwidth  | 0.000000 | 12.000000|
    2014-09-13 17:28:26.00 | 1932 | Article | ALKA     | huheight | 0.000000 | 20.000000|
    2014-09-13 17:28:26.00 | 1932 | Article | ALKA     | hulength | 0.000000 | 26.000000|
    2014-09-13 17:28:26.00 | 1932 | Article | ALKA     | huqty    | 0.200000 | 6.000000 | 
    2014-09-13 17:28:26.00 | 1932 | Article | ALKA     | huwidth  | 0.000000 | 38.500000|
    2014-09-13 17:28:26.00 | 1932 | Article | ALKA     | partwght | 1.000000 | 0.302000 |  
    

2 个答案:

答案 0 :(得分:0)


Actually The query is based on two log tables. These log tables have information about Article table 
and its columns which were updated by a background program. The Log table has column
'ma_table' which has table name 'Article' as its value. The Log table also has column
'termname' which has column-names/feilds of 'Article' table as its values.
Log table has two more columns 'oldvalue' and 'newvalue' which shows the old value and new value
(after updating) for the field listed under 'termname' column and for the table listed in
'ma_table' column. The structure of Article table is: Create Table Article ( int shortl62,
float huheight, float hulength, float huqty, float huwidth, float partwght, float suheight,
float sulength, float suwght, float suvol )
Now I have to improve my query listed above. I have to fetch the table name from the log-data
above and find its column-name and then fetch its new value and update Article table. I have to
do this task in run time by reading log table , fetching Article table and its columns from it
and then saving it into Article table ... I hope now its give you a clear picture.. What I meant :)
Any thoughts ?

答案 1 :(得分:0)

I resolved it by this way: 

With cte1  ( datreg, procid, ma_table, username, termname, oldvalue, newvalue, RowNum)
 As (
                    SELECT S38T1.datreg, S38T1.procid , S38T1.ma_table, S38T1.username,
                            S39T1.termname, S39T1.oldvalue, S39T1.newvalue, ROW_NUMBER() OVER (PARTITION BY S38T1.datreg  ORDER BY S38T1.datreg)
                    FROM S38T1 inner join S39T1 on S39T1.datreg = S38T1.datreg
                    AND S38T1.procid = S39T1.procid
                    AND S38T1.ma_table = 'Article'
                    where (S39T1.termname IN('partwght','suwght','suvol','suheight','sulength','suwidth','huheight','hulength','huwidth','huqty') and S39T1.newvalue !=S39T1.oldvalue)

            ),

 cte2 (Maxdatreg, shortl62)
AS (            
            SELECT S38T1.datreg,  S39T1.oldvalue as Shortl62
            FROM S38T1 inner join S39T1 on S39T1.datreg = S38T1.datreg
            AND S38T1.procid = S39T1.procid
            AND S38T1.ma_table = 'ARticle'      
            Where S39T1.termname IN('shortl62')

            )
SELECT * FROM cte1 inner join cte2 ON cte2.Maxdatreg = cte1.datreg 
order by cte2.Maxdatreg

then using Cursor to read records in the resultset and update the table