SQL Update每个不同的一半

时间:2016-09-27 17:47:44

标签: sql sql-server

我有一些记录,我通过一些操作从一张桌子移动到另一张桌子。

我把它们放在临时表中,这样我就可以进行操作了。具体来说,我需要将列上不同值的一半更改为某些内容,然后将另一半更改为其他内容:

在我的临时表中我有

Animals | SeveralOtherColumns
dog
cat 
fish
dog
dog
dog
fish
cat 
cat 
cat

我需要

Animals | SeveralOtherColumns
dog_A
cat_A
fish_A
dog_A
dog_B
dog_B
fish_B
cat_A
cat_B
cat_B

我不知道Animals中有哪些不同的值,或者每个不同的值有多少。

所以我最初的想法是:

SET @count = (SELECT COUNT(DISTINCT(Animals)) FROM #temptable)

--this throws an error, doesn't like the assignment 
SET @animals = (SELECT DISTINCT(Animals) FROM #temptable)

WHILE (@count > 0)
BEGIN
    -- about here is where I'm lost
    -- update the first half of the distinct value
    UPDATE #temptable SET #temptable.Animals = #temptable.Animals + '_A'
    WHERE Animals = @animals[@count] --Pretty sure this won't work but gets the point across
    FROM (SELECT TOP 50 PERCENT FROM #temptable)

    -- update the remaining set of that distinct value
    UPDATE #temptable SET #temptable.Animals = #temptable.Animals + '_B'
    WHERE Animals = @animals[@count]
    FROM #temptable


    SET @count = @count - 1
END

2 个答案:

答案 0 :(得分:5)

您可以使用{ "conta_recebedora" : "MYEMAILHERE@gmail.com", "token_api" : "API_TOKEN_HERE", "url_retorno_success" : "localhost:8000/checkout/ok", "url_retorno_error" : "localhost:8000/checkout/error", "url_retorno_cancel" : "localhost:8000/checkout/cancel", "dados_transacao" : { "valor_total" : 100.00, "produtos" : [ { "descricao" : "NAME OF MY PRODUCT", "valor" : 100.00, "quantidade" : 1 } ] }, "dados_custom" : { "id_cliente" : 25 } }

NTILE()

答案 1 :(得分:3)

您可以使用CTE在一个ROW_NUMBER()中执行此操作,并检查mod (%2)将它们分成相等的组:

 public class Datum
{
public string data1 { get; set; }
public string data2 { get; set; }
public string data3 { get; set; }
public string connectorType { get; set; }
public string sourceUri { get; set; }
public string destinationUri { get; set; }
public string rails { get; set; }
public string data6 { get; set; }
public string groupId { get; set; }
public string failbackAction { get; set; }
public string normal { get; set; }
public string failoverAction { get; set; }
public string artifactId { get; set; }
public string normalState { get; set; }
public string instanceId { get; set; }
}

public class RootObject
{
public string appname { get; set; }
public string key { get; set; }
public List<Datum> data { get; set; }
public string updateId { get; set; }
public string updateTS { get; set; }
public string creationUser { get; set; }
}