如何检查每个相同的值我们在Mysql中具有相同的唯一ID

时间:2018-03-25 16:38:03

标签: mysql sql

我有3个左连接的查询给我下面提到的输出:

T2      ID        value1     Type     value2       frq        Value3
L-1     AB-12     ARTY55A    X        [Null]       8          ARTY55A
L-1     AB-12     ARTy55A    X        [Null]       7          BHyT78B
L-2     AB-21     VGTY25E    Y        VGTY25E      8          VGTY25E
L-3     AB-14     [Null]     Z        FRTE20E      8          FRTE20E
L-5     AB-18     AURT10E    X        [Null]       8          AURT10E
L-5     AB-18     AURT10E    X        [Null]       7          AERT10E
L-6     AB-18     AURT10E    X        [Null]       8          AURT10E

现在,我想检查Value3 Type = frq = X的ID的计数,具有相同的唯一Value2且不计数为1。

对于TypeID同样的Value3 Value3_count ID_count Value2 Value2_count ID ARTY55A 1 1 VGTY25E 1 1 AURT10E 2 1 [Null] [Null] [Null] != X具有相同的唯一 domain = c(-100,1000,-100, 1000) 且具有不同的计数1.(此处我们不检查frq)

预期结果:

    Lon = -97.191391  #longtitude
    Lat = 36.055935   #latitude

2 个答案:

答案 0 :(得分:2)

您可以使用分组和计数

  select t.Value3, count(*)
  from (  select a.Value3 from table1 left join..... ) t 
      where Type = 'X'
      and frq = 8
  group by t.Value3

  select t.Value2, count(*)
  from ( select a.Value3 from table1 left join..... ) t 
      where Type != 'X'
  group by t.Value2 

答案 1 :(得分:0)

你可以试试这个:

select Value3, count(*) as Value3_count, count(distinct ID) as ID_count
from (  my query with 3 left join ) t
where Type = 'X' and frq = 8
group by Value3;

select Value2, count(*) as Value2_count, count(distinct ID) as ID_count
from (  my query with 3 left join ) t
where Type != 'X'
group by Value2;