2

当我在表上的多个连接之后运行选择时,我有 2 列的输出,我想为返回的行集选择 col1 和 col2 的不同组合。

我运行的查询将是这样的:

select a.Col1,b.Col2 from a inner join b on b.Col4=a.Col3

现在输出会有点像这样

Col1 Col2  
1   z  
2   z  
2   x  
2   y  
3   x  
3   x  
3   y  
4   a  
4   b  
5   b  
5   b  
6   c  
6   c  
6   d  

现在我希望输出应该如下所示

1  z  
2  y  
3  x  
4  a  
5  b  
6  d 

如果我随机选择第二列是可以的,因为我的查询输出就像一百万行,我真的不认为会有这样的情况我会得到 Col1 和 Col2 的输出是相同的,即使在这种情况下我可以编辑值..

你能帮我做同样的事情吗?我想基本上 col3 需要是一个行号,然后我需要根据随机行号选择两个 cols 。我不知道如何将其转换为 SQL

考虑情况 1a 1b 1c 1d 1e 2a 2b 2c 2d 2e 现在 group by 将在我想要 1a 和 2d 或 1a 和 2b 的情况下给我所有这些结果。任何这样的组合。

好的,让我解释一下我的期望:

with rs as(
select a.Col1,b.Col2,rownumber() as rowNumber from a inner join b on b.Col4=a.Col3)
select rs.Col1,rs.Col2 from rs where rs.rowNumber=Round( Rand() *100)

现在我不确定如何让行号或随机数正常工作!

提前致谢。

4

4 回答 4

6

如果您根本不在乎col2返回什么值

select a.Col1,MAX(b.Col2) AS Col2
from a inner join b on b.Col4=a.Col3 
GROUP BY a.Col1

如果你确实想要一个随机值,你可以使用下面的方法。

 ;WITH T
     AS (SELECT a.Col1,
                b.Col2
                ROW_NUMBER() OVER (PARTITION BY a.Col1 ORDER BY (SELECT NEWID())
                ) AS RN
         FROM   a
                INNER JOIN b
                  ON b.Col4 = a.Col3)
SELECT Col1,
       Col2
FROM   T
WHERE  RN = 1  

或者,也可以使用 CLR 聚合函数。这种方法的优点是它消除了按partition, newid()示例实现排序的要求,如下所示。

using System;
using System.Data.SqlTypes;
using System.IO;
using System.Security.Cryptography;
using Microsoft.SqlServer.Server;

[Serializable]
[SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize = 8000)]
public struct Random : IBinarySerialize
{
    private MaxSoFar _maxSoFar;

    public void Init()
    {
    }

    public void Accumulate(SqlString value)
    {
        int rnd = GetRandom();
        if (!_maxSoFar.Initialised || (rnd > _maxSoFar.Rand))
            _maxSoFar = new MaxSoFar(value, rnd) {Rand = rnd, Value = value};
    }

    public void Merge(Random group)
    {
        if (_maxSoFar.Rand > group._maxSoFar.Rand)
        {
            _maxSoFar = group._maxSoFar;
        }
    }

    private static int GetRandom()
    {
        var buffer = new byte[4];

        new RNGCryptoServiceProvider().GetBytes(buffer);
        return BitConverter.ToInt32(buffer, 0);
    }

    public SqlString Terminate()
    {
        return _maxSoFar.Value;
    }

    #region Nested type: MaxSoFar

    private struct MaxSoFar
    {
        private SqlString _value;

        public MaxSoFar(SqlString value, int rand) : this()
        {
            Value = value;
            Rand = rand;
            Initialised = true;
        }

        public SqlString Value
        {
            get { return _value; }
            set
            {
                _value = value;
                IsNull = value.IsNull;
            }
        }

        public int Rand { get; set; }

        public bool Initialised { get; set; }
        public bool IsNull { get; set; }
    }

    #endregion


    #region IBinarySerialize Members

    public void Read(BinaryReader r)
    {
        _maxSoFar.Rand = r.ReadInt32();
        _maxSoFar.Initialised = r.ReadBoolean();
        _maxSoFar.IsNull = r.ReadBoolean();

        if (_maxSoFar.Initialised && !_maxSoFar.IsNull)
            _maxSoFar.Value = r.ReadString();
    }

    public void Write(BinaryWriter w)
    {
        w.Write(_maxSoFar.Rand);
        w.Write(_maxSoFar.Initialised);
        w.Write(_maxSoFar.IsNull);

        if (!_maxSoFar.IsNull)
            w.Write(_maxSoFar.Value.Value);
    }

    #endregion
}
于 2011-03-06T13:16:21.837 回答
3

您需要 group bya.Col1以获得 distinct by only a.Col1,然后由于b.Col2不包含在组中,您需要找到一个合适的聚合函数来将组中的所有值减少到一个,MIN如果您只想要其中一个值就足够了。

select a.Col1, MIN(b.Col2) as c2
from a 
inner join b on b.Col4=a.Col3
group by a.Col1
于 2011-03-06T13:15:01.697 回答
0

您必须使用一个group by子句:

select a.Col1,b.Col2 
from a 
inner join b on b.Col4=a.Col3
group by a.Col1
于 2011-03-06T13:04:14.683 回答
0

如果我理解正确,您希望在第 1 列和第 2 列中的每个组合都有一行。这可以通过使用 GROUP BY 或 DISTINCT 轻松完成,例如:

选择 col1,col2

从您的加入

按 col1、col2 分组

于 2011-03-06T13:05:33.937 回答