MySql SELECT DISTINCT(x)AS Column1,COUNT(DISTINCT(y))作为Column2 FROM sample.database GROUP BY Column1查询失败

时间:2017-09-19 07:27:48

标签: mysql

我需要有关mysql查询的帮助。也就是说,我想显示一个结果,即在第一列中包含唯一值,例如,让我们成为四分之一,在第二列中,包含唯一数量的产品。以下查询将引发与数据库的连接。我想征求意见和帮助。

  SELECT DISTINCT(Quarter) AS Column1,
    COUNT(DISTINCT(Product)) as Column2 FROM sample.database
    GROUP BY Column1;

下面的示例表:

id  Quarter     Month           Product
1   Q4 2016  2016-10-01 00:00   Sample 1
2   Q4 2016  2016-10-01 00:00   Sample 1
3   Q4 2016  2016-10-01 00:00   Sample 2
4   Q3 2016  2016-10-01 00:00   Sample 3
5   Q3 2016  2016-10-01 00:00   Sample 3
6   Q3 2016  2016-10-01 00:00   Sample 4
7   Q3 2016  2016-10-01 00:00   Sample 4
8   Q3 2016  2016-10-01 00:00   Sample 4

2 个答案:

答案 0 :(得分:0)

如果"唯一值的总和"表示可以使用的唯一值的不同计数:

select Quarter, count(distinct Product)
from data
group by Quarter

答案 1 :(得分:0)

第1列您不需要@RequestMapping(method = RequestMethod.GET) public ModelAndView viewMain(Locale locale) { LocaleContextHolder.setLocale(Locale.US); Locale currentLocale = LocaleContextHolder.getLocale(); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("locale", currentLocale); LOG.info("main " /* + name*/ + "page shown"); modelAndView.setViewName("main_page"); return modelAndView; } ,因为protected $signature = 'db:restore {dataName}' 会为您执行此操作,请尝试以下查询:

$this->argument('dataName');