首页 \ 问答 \ 使用Case和Subquery分组(Group by with Case and Subquery)

使用Case和Subquery分组(Group by with Case and Subquery)

使用以下SQL执行分组时遇到问题:

SELECT 
    CASE 
         WHEN  t = '11' then 'V' 
         WHEN (t = '20' OR t = '25') THEN 'S' 
    END as [ty],

    CONVERT(CHAR(3), DateEx, 101) + CONVERT(CHAR(4), DateEx, 120) as [Fecha], COUNT(*) as [Total],  
    SUM (case when  CodLastEstado in (3,4,5,6,7,10,11,12,13) then 1 else 0 end ) as [C1],
    SUM (case when  CodLastEstado in (4,5,6) then 1 else 0 end ) as [C2],
    (
    select COUNT(*)
    from (SELECT t.Line, t.VTO, t.SubTypo
          FROM (SELECT c.Line, CONVERT(CHAR(3), c.DateEx, 101) + CONVERT(CHAR(4), c.DateEx, 120) as [VTO],
                       ROW_NUMBER() OVER (PARTITION BY a.numid ORDER BY a.FechaAccion desc) as Fila, SubTypo
                FROM Acciones a, contratos c
                WHERE A.numid = c.numid
                      AND (c.CodLastEstado in (10,11) and a.IDCodTipoEstado in (4,5,6)) 
                      and c.Brand = 'mouse'             
               ) t
          where t.Fila = 1
         )as q   
    where q.VTO = CONVERT(CHAR(3), contratos.DateEx, 101) + CONVERT(CHAR(4), contratos.DateEx, 120)
    and q.SubTypo = contratos.SubTypo
    ) as [CHco],    
    SUM (case when  CodLastSubEstado in (56,46,36,3,13,24) then 1 else 0 end ) as [N],
 FROM contratos
WHERE Brand = 'MINI' AND DateEx between '01/11/2014' and '30/11/2014'
GROUP BY SubTypo,
CONVERT(CHAR(3), contratos.DateEx, 101) + CONVERT(CHAR(4), contratos.DateEx, 120)

语法正确,显示如下:

ty||Total||C1||C2||CHco||N

S || 120 ||5 ||50||  3 ||0
S ||   1 ||0 ||0 ||  1 ||0
V ||   3 ||0 ||0 ||  0 ||0

我尝试了很多东西,如下所示,但我没有,有什么想法?

ty||Total||C1||C2||CHco||N

S || 121 ||5 ||50||  4 ||0
V ||   3 ||0 ||0 ||  0 ||0

I'm having problems when performing a grouping with the following SQL:

SELECT 
    CASE 
         WHEN  t = '11' then 'V' 
         WHEN (t = '20' OR t = '25') THEN 'S' 
    END as [ty],

    CONVERT(CHAR(3), DateEx, 101) + CONVERT(CHAR(4), DateEx, 120) as [Fecha], COUNT(*) as [Total],  
    SUM (case when  CodLastEstado in (3,4,5,6,7,10,11,12,13) then 1 else 0 end ) as [C1],
    SUM (case when  CodLastEstado in (4,5,6) then 1 else 0 end ) as [C2],
    (
    select COUNT(*)
    from (SELECT t.Line, t.VTO, t.SubTypo
          FROM (SELECT c.Line, CONVERT(CHAR(3), c.DateEx, 101) + CONVERT(CHAR(4), c.DateEx, 120) as [VTO],
                       ROW_NUMBER() OVER (PARTITION BY a.numid ORDER BY a.FechaAccion desc) as Fila, SubTypo
                FROM Acciones a, contratos c
                WHERE A.numid = c.numid
                      AND (c.CodLastEstado in (10,11) and a.IDCodTipoEstado in (4,5,6)) 
                      and c.Brand = 'mouse'             
               ) t
          where t.Fila = 1
         )as q   
    where q.VTO = CONVERT(CHAR(3), contratos.DateEx, 101) + CONVERT(CHAR(4), contratos.DateEx, 120)
    and q.SubTypo = contratos.SubTypo
    ) as [CHco],    
    SUM (case when  CodLastSubEstado in (56,46,36,3,13,24) then 1 else 0 end ) as [N],
 FROM contratos
WHERE Brand = 'MINI' AND DateEx between '01/11/2014' and '30/11/2014'
GROUP BY SubTypo,
CONVERT(CHAR(3), contratos.DateEx, 101) + CONVERT(CHAR(4), contratos.DateEx, 120)

The syntax is correct, showing the following:

ty||Total||C1||C2||CHco||N

S || 120 ||5 ||50||  3 ||0
S ||   1 ||0 ||0 ||  1 ||0
V ||   3 ||0 ||0 ||  0 ||0

I tried many things to be displayed as follows, but I have not got, any ideas?

ty||Total||C1||C2||CHco||N

S || 121 ||5 ||50||  4 ||0
V ||   3 ||0 ||0 ||  0 ||0

原文:
更新时间:2022-03-25 09:03

最满意答案

一些技巧:

  • {}括号是对象 ,您可以访问object.propertyobject.property
  • []括号是数组 ,您可以通过索引array[index]访问元素。

在您的特定情况下,这应该这样做:

var objects_array = JSON.parse(data); // This is an array of objects.
var total = objects_array.length;

for (var i = 0; i < total; i++) {
    var obj = objects_array[i]; // This is one object from the array.
    console.log( obj.latitude + ',' + obj.longitude ); // We access object properties using a dot.
}

Some tips:

  • {} parentheses are objects, you access properties like object.property.
  • [] parentheses are arrays, you access elements by indexes array[index].

In your particular case, this should do it:

var objects_array = JSON.parse(data); // This is an array of objects.
var total = objects_array.length;

for (var i = 0; i < total; i++) {
    var obj = objects_array[i]; // This is one object from the array.
    console.log( obj.latitude + ',' + obj.longitude ); // We access object properties using a dot.
}

相关问答

更多
  • 首先,你做的是落后的步骤。 你正在倒数index = index - 1; 所以一切都会逆转。 另一方面,您没有使用干净的结构化代码。 在您的情况下,您可以执行某些操作,然后尝试调试并更改它,只要您需要满足需求即可。 特别是不要改变链接。 这是一个重写代码的简单示例。 请密切关注功能的分离,使其成为简单的功能。 尽量不做一些不必要的DOM操作,比如在函数中创建元素,在一个循环中更改另一个函数中的一些链接和文本。 尽量保持干燥(不要自己重复)。 // encapsulate frontend stuff. h ...
  • 您必须更改将XML数据转换为JSON数组的方法。 现在它不是您尝试通过JavaScript处理的格式。 $items = array(); foreach ($nfl->gms->g as $game) { $row = array(); foreach ($game->attributes() as $a => $b) { $row[$a] = (string) $b; } $items[] = $row; } echo json_encode($item ...
  • 您需要将第二个参数设置为true ,以告诉它将结果作为关联数组返回。 $array = json_decode($array, true); 这在PHP手册中已经清楚地记录了下来 。 然后,您可以像这样访问value : $array['value'] You need to set the second parameter to true, to tell it to return the result as an associative array. $array = json_decode($ar ...
  • var data = [ {"Id": 10004, "PageName": "club"}, {"Id": 10040, "PageName": "qaz"}, {"Id": 10059, "PageName": "jjjjjjj"} ]; $.each(data, function(i, item) { alert(data[i].PageName); });​ $.each(data, function(i, item) { alert(item.PageName); ...
  • 也许你可以试试这个: $.each(data.list, function(i, item) { var listData = "
  • " + item.subscribe + "
  • "; $('#lists').append(listData); }); 只是改变function(i, data) function(i, item) 。 通过以您所使用的方式重用变量名称data ,您实际上并不像您期望的那样循环访问数组。 Maybe you can try this: $.eac ...
  • 你可以使用Array.prototype.forEach()并做: const data = {"records": [{"id": "recGWUWqwjUNLpekA","fields": {"playerSprints": 12,"playerDistanceCovered_km": 6.23},"createdTime": "2018-03-22T18:16:56.000Z"},{"id": "recx5pMFpxnRwR4La","fields": {"playerSprints": 12,"pl ...
  • 基本上你可以在这里使用3个机会: Array.forEach(如下所示) 通常用于循环( for(let t = 0; t < data.length; t++) { ... } ) 循环的ES7( for(const el of data) { ... } ) 假设你有这样的数据: const data = [ { "FIELD1": "name", "FIELD2": "id"}, { "FIELD1": "abc", "FIELD2": "12"}]; 你可以循环它: data.forEach(el ...
  • if (info.hasOwnProperty(key)) 你不需要那个检查。 你的值是普通的对象,没有人足够愚蠢地扩展具有可枚举属性的Object.prototype 。 for (prop in person) 这似乎是不必要的,如果没有错的话。 您只想访问gifts属性,因此无需枚举name , address和gifts 。 同样的事情 for (var key in info) 你只想得到donors并迭代他们 - 对象甚至没有任何其他属性。 我的JSON不必以这种方式构建 然后我建议使用数 ...
  • 一些技巧: {}括号是对象 ,您可以访问object.property等object.property 。 []括号是数组 ,您可以通过索引array[index]访问元素。 在您的特定情况下,这应该这样做: var objects_array = JSON.parse(data); // This is an array of objects. var total = objects_array.length; for (var i = 0; i < total; i++) { var obj ...
  • $.getJSON是异步的,处理程序将在收到响应时被调用。 但是for-loop不会等待处理程序被调用! 创建一个inner-function并传递i作为参数的值,因为i值对于inner-function是持久的。 $(function() { for (var i = 1; i < 4; i++) { var tobeCalled = function(i) { var box = "#t" + i; var id = "dQw4w9WgXcQ"; ...
  • 相关文章

    更多
  • Hadoop 0.21如何运行单个test case
  • 罗兰贝格case interview面经
  • 《无懈可击的WEB设计(第2版)》(Bulletproof Web Design: Improving flexibility and protecting against worst-case scenarios with XHTML and CSS (2nd Edition))扫描版[PDF]
  • 《CMMI+敏捷整合开发:更快改进性能的案例与实用技术》( Integrating CMMI and Agile Development: Case Studies and Proven Techniques for Faster Performance Improvement )扫描版[PDF]
  • solr分组排序实现group by功能
  • SQL中GROUP BY与HAVING的用法
  • 求一个group by后面字段顺序影响结果的例子
  • solr的分组和统计功能
  • mysql数据库查询分组求教
  • 用Lucene实现分组,facet功能,FieldCache
  • 最新问答

    更多
  • 您如何使用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)