Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL排序不同/唯一值_Mysql_Sql_Database_Data Science - Fatal编程技术网

MySQL排序不同/唯一值

MySQL排序不同/唯一值,mysql,sql,database,data-science,Mysql,Sql,Database,Data Science,决定将我的查询分解成若干部分,以帮助传达我正在努力实现的目标 我有一些关于客户和他们使用的优惠券代码的信息,采用这种格式 顾客 优惠码 1. 弗里德尔 1. 弗里德尔 1. 弗里德尔 1. 1562733 1. 8842939 1. 847hr64 1. 83jd63j 1. 弗里德尔 1. 8eh33jr 1. AA-2637 1. AA-9837 1. 节省200 1. 节省200 您可以使用聚合和条件逻辑: (case when sum( code like 'AA-%' ) > 0

决定将我的查询分解成若干部分,以帮助传达我正在努力实现的目标

我有一些关于客户和他们使用的优惠券代码的信息,采用这种格式

顾客 优惠码 1. 弗里德尔 1. 弗里德尔 1. 弗里德尔 1. 1562733 1. 8842939 1. 847hr64 1. 83jd63j 1. 弗里德尔 1. 8eh33jr 1. AA-2637 1. AA-9837 1. 节省200 1. 节省200
您可以使用聚合和条件逻辑:

(case when sum( code like 'AA-%' ) > 0 then 'AA-'
      when min(code) = max(code) then 'UNIQUE'
      else 'NON-UNIQUE'
 end)
您可以将其合并到聚合查询中,如下所示:

select grp, count(*)
from (select c.customer,
             (case when sum( code like 'AA-%' ) > 0 then 'AA-'
                   when min(code) = max(code) then 'UNIQUE'
                   else 'NON-UNIQUE'
              end) as grp,
             count(*)
      from c
      group by customer
     ) x
group by grp;

您可以使用聚合和条件逻辑:

(case when sum( code like 'AA-%' ) > 0 then 'AA-'
      when min(code) = max(code) then 'UNIQUE'
      else 'NON-UNIQUE'
 end)
您可以将其合并到聚合查询中,如下所示:

select grp, count(*)
from (select c.customer,
             (case when sum( code like 'AA-%' ) > 0 then 'AA-'
                   when min(code) = max(code) then 'UNIQUE'
                   else 'NON-UNIQUE'
              end) as grp,
             count(*)
      from c
      group by customer
     ) x
group by grp;

您可以分别计算:

select sum(cnt), customer, 'Non-Unique'
from (
  select customer, coupon_code, count(*) as cnt
  from ccodes
  where coupon_code not like 'AA-%'
  group by customer, coupon_code
  having cnt>1
) as q
group by customer
union
select count(*), customer, 'AA-'
from ccodes
where coupon_code like 'AA-%'
group by customer
union
select sum(cnt), customer, 'Unique'
from (
  select customer, coupon_code, count(*) as cnt
  from ccodes
  where coupon_code not like 'AA-%'
  group by customer, coupon_code
  having cnt=1
) as q
group by customer

请参见

您可以分别计算它们:

select sum(cnt), customer, 'Non-Unique'
from (
  select customer, coupon_code, count(*) as cnt
  from ccodes
  where coupon_code not like 'AA-%'
  group by customer, coupon_code
  having cnt>1
) as q
group by customer
union
select count(*), customer, 'AA-'
from ccodes
where coupon_code like 'AA-%'
group by customer
union
select sum(cnt), customer, 'Unique'
from (
  select customer, coupon_code, count(*) as cnt
  from ccodes
  where coupon_code not like 'AA-%'
  group by customer, coupon_code
  having cnt=1
) as q
group by customer

请参见

我不明白-这将如何计算唯一和非唯一实例的数量?@UsmaanBhatti。您可以将逻辑合并到聚合查询中。我编辑了答案。我不明白-这将如何计算唯一和非唯一实例的数量?@UsmaanBhatti。您可以将逻辑合并到聚合查询中。我编辑了答案。