簡體   English   中英

在SQL中選擇2列的不同組合

[英]selecting a distinct combination of 2 columns in SQL

當我在我的表上有多個連接后運行一個select時,我有一個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)

現在我不知道如何讓rownumber或隨機正常工作!

提前致謝。

如果您根本不關心返回的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()partition, newid()排序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
}

你需要通過組a.Col1僅得到不同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

您必須使用group by子句:

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

如果我理解正確,您希望在第1列和第2列中為每個組合添加一行。例如,可以使用GROUP BY或DISTINCT輕松完成:

SELECT col1,col2

來自你的加入

GROUP BY col1,col2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM