sql-server 在进行单元格计算后在新临时表中插入条目

pw9qyyiw  于 2022-10-31  发布在  其他
关注(0)|答案(2)|浏览(152)

我有一个SQL Server表(源表),其中有一个字符串类型的类比列。我试图实现的是,当类比为“1/1”时,对片段进行计算并插入新条目。
更具体地说:当类比为1/1时

  • 使用productName,
  • 将颜色设置为“白色”,-
  • 保持类比文本相同“1/1”
  • 将当前条目和新条目上的PSC的数量除以2。

到目前为止,我尝试使用SELECT INTO创建一个#temp表,然后在#temp表中插入一个新条目之前,尝试创建一个递归来检查类比。
我已经使用了这个Denis Lukichev的例子,但是我不确定这个方法是否适合我的解决方案。另外,Felix Pamittan的this方法更接近我想要的,但是我不知道如何将它集成到我的例子中。
任何关于如何实现解决方案的帮助或参考将不胜感激。

表:

| 产品名称|色彩|类比|psc系统|
| - -|- -|- -|- -|
| 字母|灰色|1分之1|千元|
| 测试版|灰色|1分之1|千元|
| 加马|灰色|二月一日|一千五百元|
如何在新的临时表上实现以下结果?
| 产品名称|色彩|类比|psc系统|
| - -|- -|- -|- -|
| 字母|灰色|1分之1| 500个|
| 字母|白色|1分之1| 500个|
| 测试版|灰色|1分之1| 500个|
| 测试版|白色|1分之1| 500个|
| 加马|灰色|二月一日|千元|
| 加马|白色|二月一日|500个|
此外还有任何机会使用其他类比和重新计算psc.例如:如果类比是2/1,意味着2个时隙用于灰色,一个时隙用于白色,则相应的类比将是500+500 =1000 psc用于灰色,500 psc用于白色。

更新

在使用了Dordi的有益建议后,它考虑了一个接近解决方案,直到使用另一种颜色。
更具体地说,我已经添加了“白色”和“黑色”的颜色,结果并没有预期的。
SQL Fiddle

MS SQL Server 2017架构设置

CREATE TABLE sourceTable (
    productName varchar(50),
    color varchar(50),
    analogy varchar(50),
    psc int
);

INSERT INTO sourceTable (productName, color, analogy, psc) VALUES ('Alpha', 'Gray', '1/1',1000);
INSERT INTO sourceTable (productName, color, analogy, psc) VALUES ('Gama', 'Black', '1/2',1500);
INSERT INTO sourceTable (productName, color, analogy, psc) VALUES ('Gama', 'White', '3/0',1500);

查询1

SELECT t.productName,
x.color,
t.analogy,
CASE x.color 
    WHEN 'Gray' THEN psc * CAST(LEFT(analogy,CHARINDEX('/',analogy) - 1) as int) / (CAST(LEFT(analogy,CHARINDEX('/',analogy) - 1) as int)  + CAST(RIGHT(analogy,CHARINDEX('/',analogy) - 1) as int) )
    WHEN 'Black' THEN psc * CAST(LEFT(analogy,CHARINDEX('/',analogy) - 1) as int) / (CAST(LEFT(analogy,CHARINDEX('/',analogy) - 1) as int)  + CAST(RIGHT(analogy,CHARINDEX('/',analogy) - 1) as int) )
    WHEN 'White' THEN psc * CAST(RIGHT(analogy,CHARINDEX('/',analogy) - 1) as int)  / (CAST(LEFT(analogy,CHARINDEX('/',analogy) - 1) as int)  + CAST(RIGHT(analogy,CHARINDEX('/',analogy) - 1) as int) )
END AS psc
FROM sourceTable t 
CROSS JOIN (VALUES ('Gray'),('White'),('Black')) AS x(color)

Results

| productName | color | analogy |  psc |
|-------------|-------|---------|------|
|       Alpha |  Gray |     1/1 |  500 |
|       Alpha | White |     1/1 |  500 |
|       Alpha | Black |     1/1 |  500 |
|        Gama |  Gray |     1/2 |  500 |
|        Gama | White |     1/2 | 1000 |
|        Gama | Black |     1/2 |  500 |
|        Gama |  Gray |     3/0 | 1500 |
|        Gama | White |     3/0 |    0 |
|        Gama | Black |     3/0 | 1500 |

但优选的结果是:

| productName | color | analogy |  psc |
|-------------|-------|---------|------|
|       Alpha |  Gray |     1/1 |  500 |
|       Alpha | White |     1/1 |  500 |
|        Gama | Black |     1/2 |  500 |
|        Gama | White |     1/2 | 1000 |
|        Gama | White |     3/0 | 1500 |
|        Gama | White |     3/0 |    0 |

我在想CROSS JOIN (VALUES ('Gray'),('White'),('Black')) AS x(color)是这里的问题,也许它应该采取动态(选择不同)的颜色,或另一种情况下处理颜色名称。
有什么想法吗?

7uzetpgm

7uzetpgm1#

APPLY运算符和适当计算的组合是另一个选项:

SELECT t.productName, a. color, t.analogy, a.psc
FROM (  
   SELECT 
      productName, 
      color, 
      analogy, 
      psc,
      CONVERT(int, LEFT(analogy, CHARINDEX('/', analogy) - 1)) AS analogy1,
      CONVERT(int, STUFF(analogy, 1, CHARINDEX('/', analogy), '')) AS analogy2
   FROM sourceTable
) t 
CROSS APPLY (VALUES 
   (t.color, ROUND(t.analogy1 * 1.0 / (t.analogy1 + t.analogy2) * t.psc, 0)), 
   ('White', ROUND(t.analogy2 * 1.0 / (t.analogy1 + t.analogy2) * t.psc, 0))
) a (color, psc)
oxcyiej7

oxcyiej72#

根据你的解释,“类比”是psc在颜色上的重新划分。
下面是另一种计算方法:

SELECT t.productName,
x.color,
t.analogy,
CASE x.color 
    WHEN 'Gray' THEN psc * CAST(LEFT(analogy,CHARINDEX('/',analogy) - 1) as int) / (CAST(LEFT(analogy,CHARINDEX('/',analogy) - 1) as int)  + CAST(RIGHT(analogy,CHARINDEX('/',analogy) - 1) as int) )
    WHEN 'White' THEN psc * CAST(RIGHT(analogy,CHARINDEX('/',analogy) - 1) as int)  / (CAST(LEFT(analogy,CHARINDEX('/',analogy) - 1) as int)  + CAST(RIGHT(analogy,CHARINDEX('/',analogy) - 1) as int) )
END AS psc
FROM #TEMP t 
CROSS JOIN (VALUES ('Gray'),('White')) AS x(color)

编辑

是的,如果您有多种颜色,则可以添加distint,您的查询将变为:

SELECT t.productName,
x.color,
t.analogy,
CASE  
    WHEN x.color = 'White' AND x.IsSource = 0 THEN psc * CAST(RIGHT(analogy,CHARINDEX('/',analogy) - 1) as int)  / (CAST(LEFT(analogy,CHARINDEX('/',analogy) - 1) as int)  + CAST(RIGHT(analogy,CHARINDEX('/',analogy) - 1) as int) )
  ELSE psc * CAST(LEFT(analogy,CHARINDEX('/',analogy) - 1) as int) / (CAST(LEFT(analogy,CHARINDEX('/',analogy) - 1) as int)  + CAST(RIGHT(analogy,CHARINDEX('/',analogy) - 1) as int) )
END AS psc
FROM sourceTable t 
INNER JOIN (SELECT DISTINCT color AS Id,color AS color,1 IsSource FROM sourceTable
  UNION ALL
  SELECT DISTINCT color AS Id,'White' AS color,0 IsSource FROM sourceTable
  ) AS x ON t.color = x.Id

这是一个dbfiddle

相关问题