首页 \ 问答 \ 按“每30天”分组mysql结果(Group mysql results by “every 30 days”)

按“每30天”分组mysql结果(Group mysql results by “every 30 days”)

我有一个问题:

SELECT 
  COUNT(id) as amount,
  DATEDIFF(expire, buydate) as days
FROM `vw8mv_orders`
GROUP BY MONTH(expire)

结果是:

    amount  days
    1       22
    1       30
    1       105
    1       161

我想在一组(每30天)看到这些结果。 如果天数值在1到30天之间,则将其放入30天组,如果下注31-60,则放入60天组等。

例如:

    amount  time
      2     30 days
      0     60 days
      1     90 days

I have a query:

SELECT 
  COUNT(id) as amount,
  DATEDIFF(expire, buydate) as days
FROM `vw8mv_orders`
GROUP BY MONTH(expire)

The result is:

    amount  days
    1       22
    1       30
    1       105
    1       161

I'd like to see these results in a group (every 30 days). If days value is between 1 and 30 days, then put this in 30days group, if bet 31-60, put to 60days group, etc.

For example:

    amount  time
      2     30 days
      0     60 days
      1     90 days

原文:https://stackoverflow.com/questions/23545695
更新时间:2022-02-09 06:02

最满意答案

  1. 我的困惑是,我们如何不必明确初始化矢量

这是std :: unordered_map :: operator []的预期行为,如果该键不存在,将执行带有值初始化映射值的插入。

返回映射到与键等效的键的值的引用,如果此键尚不存在,则执行插入操作。

这意味着对于hashtable[s].push_back("bar"); ,首先插入一个值初始化的std::vector (即一个空的std::vector ),然后由std::unordered_map::operator[]引用返回该向量。 然后在vector上调用push_back("bar") (然后它的大小变为1并包含一个元素)。

  1. 是不是将“bar”推入带有垃圾值的向量?

不, std::vector与原始数组不同,它的大小是动态的。 如上所述,值初始化的std::vector为空,其大小为0 ,仍然不包含任何元素(以及任何“垃圾值”)。


  1. My confusion is how come we don't have to explicitly initialize a vector

This is the expected behavior of std::unordered_map::operator[], which will perform an insertion with value-initialized mapped value if the key does not exist.

Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.

That means for hashtable[s].push_back("bar");, a value-initialized std::vector (i.e. an empty std::vector) will be inserted at first, then the vector will be returned by reference by std::unordered_map::operator[]. Then push_back("bar") is called on the vector (then its size becomes 1 and contains one element).

  1. isn't pushing "bar" into a vector with garbage values?

No, std::vector is not same as raw array, its size is dynamic. As explained above, the value-initialized std::vector is empty, its size is 0, still doesn't contain any elements (and any "garbage values").

相关问答

更多
  • STL算法通常适用于迭代器,而不是容器,所以我会建议类似下面的内容。 template T join( const T sep, Iterator b, Iterator e) { T t; while (b != e) t = t + *b++ + sep; return t; } 然后,你需要一个迭代器来取出键或值。 这是一个例子: template
  • 该解决方案的灵感来自于Marcelo的解决方案,其中有以下几个变化: #include #include #include #include #include // This works similar to ostream_iterator, but doesn't print a delimiter after the final item template
  • 您还没有提到您正在使用哪个编译器,但在符合C ++ 11的编译器中,您可以使用auto和基于Ranged的for循环 for (auto i : divisors) { cout << i << endl; } i在这里不是一个迭代器,它是容器模板类型,在你的情况下是一个int 指针是一种迭代器,特别是随机访问迭代器 。 迭代器被设计为带有* , -> , ++ , --等运算符的指针抽象,用于访问容器 。 对于C ++程序员, cplusplus.com是你的朋友。 You haven't me ...
  • 您的比较运算符不对。 只要您可以假设没有添加空值,如果您检测到任何空值,则可以抛出: bool operator()(const string* s1, const string* s2) const { if (!s1 || !s2) thr ...
  • 这很简单,真的。 编译器会假装T是你传入的参数的类型,然后继续编译。 如果它遇到任何错误,那么它会报告这些错误。 只要你使用的参数类型能够工作,如果你对这种类型进行了硬编码,那么它就会起作用。 在你的情况下,它与int失败,因为int没有cbegin()方法。 它使用new std::string("")失败,因为参数类型变成了std::string * const & ,并且你不能在这个指针上调用.cbegin() 。 (你将不得不调用->cbegin()来代替。) 然而,你可以用std::string( ...
  • 创建一个DoubleRange类来存储双范围,并在其上实现比较运算符。 那样, std::map会为你做其余的事,以DoubleRange类为关键。 Create a class DoubleRange to store the double range, and implement the comparison operators on it. That way, std::map will do the rest for you, with the DoubleRange class as the ke ...
  • 您可以使用引用分配给对象 - 通过将循环变量作为引用: cout << "Increnting ... " << endl; for (MyObject &o : oset) o.value += 1000; *编辑* 但是因为set是一个有序容器,所以它返回const迭代器。 因此,如果您需要修改一个集合,您应该考虑使用另一个容器。 也许是地图? 但是,如果你真的需要改变一套 - 不要像下面的代码那样。 正如Matthieu指出的那样,这使得valgrind非常沮丧。 为什么你不能在迭代通过容器时绕过 ...
  • 我们可以为您提供指导,但没有明确的答案 - 您需要自己进行基准测试,因为它至关重要取决于您的集合和对象大小: 对于小对象和/或相对较小的集合, std::vector将更快,因为即使您需要复制更多数据,更好的随机访问时间(对于std::list O(1)vs O(n))和缓存局部将占主导地位。 对于大型对象和/或大型集合, std::list会更快,因为虽然您需要O(n)来选择随机对象,但由于复制许多大型对象非常慢,因此插入速度会快得多。 但究竟在这两种情景之间的截止位置我不能说。 此外,如果你可以逃避交换 ...
  • 看看template class TT 。 此参数适用于模板,任何接受任意数量类型参数的模板。 现在看看std::array 。 此模板不接受类型作为第二个参数。 它接受一个积分常数。 所以它不能成为你定义的模板函数的一个参数,并且模板参数推导失败了。 至于使其工作,最简单的方法是提供只接受std::array的(模板化)重载。 模板参数将是(推导的)数组参数。 template os ...
  • 我的困惑是,我们如何不必明确初始化矢量 这是std :: unordered_map :: operator []的预期行为,如果该键不存在,将执行带有值初始化映射值的插入。 返回映射到与键等效的键的值的引用,如果此键尚不存在,则执行插入操作。 这意味着对于hashtable[s].push_back("bar"); ,首先插入一个值初始化的std::vector (即一个空的std::vector ),然后由std::unordered_map::operator[]引用返回该向量。 然后在vector上 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)