SQL:如何为每行随机抽取多个值

时间:2017-07-08 09:05:18

标签: mysql sql hadoop

假设我有一个表elements.length,我需要创建一个表A (id string),以便从B (id1 string, id2 string)随机抽样B.id2A.idB.id1相同。例如:

A.id

A

id 1 2 3 4

B

也就是说,对于id1 id2 1 2 1 3 2 4 2 3 3 1 3 2 4 1 4 4 中的每个id,在A中随机抽样2 id作为新列A。我怎么能用SQL做到这一点?我更喜欢没有替换的样品。但如果id2等于id2,则可以。谢谢你的帮助!

3 个答案:

答案 0 :(得分:1)

一种方法是:

 select  id as id1, (select id from A order by rand() limit 1) as id2 from A  
 union all
 select  id as id1, (select id from A order by rand() limit 1) as id2 from A  

您可以使用insert into ... select ...将此值放入表B。

如果你想要超过2个id,例如几十个,那么这个方法是不好的选择。

答案 1 :(得分:1)

另一种方法是执行cross join然后随机行。例如:

select a1.id, a2.id
from A a1 cross join
     A a2
where a1.id <> a2.id and
      rand() <= 0.01;

这会相对有效。它会从交叉连接中拉出大约1%的行。不会保证每个id都有相同的行数。

如果你需要为每个id获得相同的数字,你可以使用变量扩展这个想法:

select id1, id2
from (select a1.id as id1, a2.id as id2,
             (@rn := if(@i = a1.id, @rn + 1,
                        if(@i := a1.id, 1, 1)
                       )
             ) as rn
      from A a1 cross join
           A a2 cross join
           (select @i := -1, @rn := 0) params
      where a1.id <> a2.id 
      order by a1.id, rand()
     ) aa
where rn <= 2;

这将没有非常好的性能特征。它应该可以在长达一千行左右的桌子上正常工作。

答案 2 :(得分:0)

试试这个(在SQL Server中完成):

  override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    //let duration = 0.01
    let touch: UITouch = touches.first!
    let newPosition = touch.location(in: self)
    let oldPosition = touch.previousLocation(in: self)
    let xTranslation = newPosition.x - oldPosition.x

    if centerPlayer!.frame.midX > size.width/2 {
        if (leftPlayer != nil) {
            let actualTranslation = leftPlayer!.frame.midX + xTranslation > leftGuide ? xTranslation : leftGuide - leftPlayer!.frame.midX
            movePlayerByX(player: leftPlayer!, x: actualTranslation)
        }
    } else {
        if (rightPlayer != nil) {
            let actualTranslation = rightPlayer!.frame.midX + xTranslation < rightGuide ? xTranslation : rightGuide - rightPlayer!.frame.midX
            movePlayerByX(player: rightPlayer!, x: actualTranslation)
        }
    }

    movePlayerByX(player: centerPlayer!, x: xTranslation)
    priceLabel.isHidden = true; selectButton.isHidden = true; lockedButton.isHidden = true; otherButton.isHidden = true
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    let touch: UITouch = touches.first!
    let location: CGPoint = touch.location(in: self)
    let node: SKNode = self.atPoint(location)
    let duration = 0.25

    if node == selectButton {

        setNewPlayerSprite(nameOfPlayer: (centerPlayer?.name)!)

    } else if node == lockedButton || node == priceLabel {

        unlockPlayer(nameOfPlayer: (centerPlayer?.name)!)

    } else if node == otherButton {

        getSpecialUnlockData()

    } else if node == purchaseButton || node == purchasePriceLabe {

        purchaseCharacter(ID: characterProductID)

    } else if node == buyButton {

        giveItemAmount(itemName: "Item2", giveAmount: 1)

    }

如果您想为每个组分配不同的行数,只需将两个FETCH NEXT FROM CURB替换为计数循环