简体   繁体   English

查找列的唯一组合

[英]Finding unique combinations of columns

I'm trying to write a select query but am having trouble, probably because I'm not familiar with SQL Server (usually use MySQL ). 我正在尝试编写一个选择查询,但遇到了麻烦,可能是因为我不熟悉SQL Server(通常使用MySQL )。

Basically what I need to do is find the number of unique combinations of 2 columns, one a Varchar and one a Double . 基本上我需要做的是找到2列的唯一组合的数量,一个是Varchar ,另一个是Double

There are less rows in one than another, so I've been trying to figure out the right way to do this. 一行中的行数少于另一行,所以我一直在努力找出正确的方法。

Essentially pretend Table.Varchar has in it: 基本上假装Table.Varchar在其中:

Table.Varchar
--------------
apple  
orange 

and Table.Float has in it: 和Table.Float在其中:

Table.Float
--------------
1   
2  
3.  

How could I write a query which returns 我怎么能写一个返回的查询

QueryResult
-------------
apple1
apple2
apple3
orange1
orange2
orange3

Long day at work and I think I'm just overthinking this what I've tried so far is to concat the two columns and then count but it's not working. 漫长的一天工作,我想我只是在思考我迄今为止尝试过的是将两列连接起来然后计算但是它不起作用。 Any ideas to better go about this? 有什么想法更好地解决这个问题?

Select T1.VarcharField +  CAST(T2.FloatField as Varchar(10)) as [Concat]
from Table.Varchar T1
CROSS JOIN Table.Float T2

this way, you are generating the fields 这样,您就是在生成字段

so, then group by and use Count 所以,然后分组并使用Count

select  T.Concat, count(*) from 
(Select T1.VarcharField + CAST(T2.FloatField as Varchar(10)) as [Concat]
    from Table.Varchar T1
    CROSS JOIN Table.Float T2) T
group by T.Concat order by count(*) asc

If they are in the same table: 如果它们在同一个表中:

SELECT a.Field1, b.Field2
FROM [Table] a
    CROSS JOIN [Table] b

or if they are in seperate tables: 或者如果他们在单独的表格中:

SELECT a.Field1, b.Field2
FROM [Table1] a
    CROSS JOIN [Table2] b

Keep in mind that the above queries will match ALL records from the first table with ALL records from the second table, creating a cartesian product . 请记住,上面的查询将匹配从第一个表所有记录与第二个表所有记录,创造了笛卡尔乘积

This will eliminate duplicates: 这将消除重复:

DECLARE @Varchar TABLE(v VARCHAR(32));

DECLARE @Float TABLE(f FLOAT);

INSERT @Varchar SELECT 'apple' 
  UNION ALL SELECT 'orange' 
  UNION ALL SELECT 'apple';

INSERT @Float SELECT 1 
  UNION ALL SELECT 2 
  UNION ALL SELECT 3;

SELECT v.v + CONVERT(VARCHAR(12), f.f)
 FROM @Varchar AS v
 CROSS JOIN @Float AS f
 GROUP BY v.v, f.f;

A cross join is a join where each record in one table is combined with each record of the other table. 交叉连接是一个连接,其中一个表中的每个记录与另一个表的每个记录组合在一起。 Select the distinct values from the table and join them. 从表中选择不同的值并加入它们。

select x.Varchar, y.Float
from (select distinct Varchar from theTable) x
cross join (select distinct Float from theTable) y

To find the number of combinations you don't have to actually return all combinations, just count them. 要查找组合的数量,您不必实际返回所有组合,只需计算它们即可。

select
  (select count(distinct Varchar) from theTable) *
  (select count(distinct Float) from theTable)

Try This 尝试这个

Possible Cominations. 可能的Cominations。

SELECT 
DISTINCT T1.VarField+CONVERT(VARCHAR(12),T2.FtField) --Get Unique Combinations
FROM Table1 T1 CROSS JOIN Table2 T2 --From all possible combinations
WHERE T1.VarField IS NOT NULL AND T2.FtField IS NOT NULL --Making code NULL Proof

and to just get the Possible Cominations Count 并获得可能的通缉计数

SELECT Count(DISTINCT T1.VarcharField + CONVERT(VARCHAR(12), T2.FloatField))
FROM Table1 T1  
CROSS JOIN Table2 T2 
WHERE T1.VarcharField IS NOT NULL AND T2.FloatField IS NOT NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM