0

100 個のランダムな行を選択する以下のクエリがあります。ただし、個別の名前も選択する必要があります。私は使用してみSELECT DISTINCT u.nameましGROUP BY u.nameたが、何も動作しません。

SELECT TOP (100) c.id, u.id u.name, b.sector, u.default
FROM cads AS c 
INNER JOIN users AS u ON c.cad = u.id 
INNER JOIN business AS b ON u.id = b.cad
WHERE (c.[public] = 'True') AND (c.valid = 'True') 
        AND (u.default = '$curr') 
        AND (c.expires IS NULL OR c.expires >= GETDATE()) 
ORDER BY NEWID() 

どうもありがとう

4

2 に答える 2

0

多分

SELECT TOP (100) min(c.id), u.id, u.name, min(b.sector), min(u.default)
FROM cads AS c 
INNER JOIN users AS u ON c.cad = u.id 
INNER JOIN business AS b ON u.id = b.cad
WHERE (c.[public] = 'True') AND (c.valid = 'True') 
        AND (u.default = '$curr') 
        AND (c.expires IS NULL OR c.expires >= GETDATE())
GROUP BY u.id, u.name 
ORDER BY NEWID() 
于 2013-11-10T05:58:03.480 に答える
0

あなたは狡猾な使用を介してそれを行うことができますROW_NUMBER()

;With Arbitrary AS (
SELECT c.id, u.id u.name, b.sector, u.default,
    ROW_NUMBER() OVER (PARTITION BY u.name ORDER BY newid()) as rn
FROM cads AS c 
INNER JOIN users AS u ON c.cad = u.id 
INNER JOIN business AS b ON u.id = b.cad
WHERE (c.[public] = 'True') AND (c.valid = 'True') 
        AND (u.default = '$curr') 
        AND (c.expires IS NULL OR c.expires >= GETDATE()) 
)
select top 100 * from Arbitrary ORDER BY newid()

ORDER BY( CTEの内側の句が実際にTOP演算子によって考慮される行の順序に影響を与えるという保証がないため、両方の句が必要です)

于 2013-11-08T15:16:01.320 に答える