首页 \ 问答 \ MySQL查询计算每个组内的百分比(MySQL query to calculate percentage within each group)

MySQL查询计算每个组内的百分比(MySQL query to calculate percentage within each group)

如何转换结果:

week,day,hour,branch,sales
1,1,1,a,50
1,1,1,b,50
1,1,2,a,25
1,1,2,b,75

进入这个:

week,day,hour,branch,sales,percentage
1,1,1,a,50,50%
1,1,1,b,50,50%
1,1,2,a,25,25%
1,1,2,b,75,75%

请注意,百分比按每小时分组。 换句话说,它不是所有销售的百分比,而只是每小时内的销售额。 我尝试了以下查询,但这给了我所有销售的百分比:

SELECT week,day,hour,branch,sales,
(sales/(SELECT SUM(sales) FROM test))*100 AS Percentage
FROM test
GROUP BY week,day,hour,branch

How to convert this result:

week,day,hour,branch,sales
1,1,1,a,50
1,1,1,b,50
1,1,2,a,25
1,1,2,b,75

Into this:

week,day,hour,branch,sales,percentage
1,1,1,a,50,50%
1,1,1,b,50,50%
1,1,2,a,25,25%
1,1,2,b,75,75%

Note that the percentage is grouped by each hour. In other words, it is not the percentage of all the sales but just the sales within each hour. I have tried the following query but this gives me percentage of all the sales:

SELECT week,day,hour,branch,sales,
(sales/(SELECT SUM(sales) FROM test))*100 AS Percentage
FROM test
GROUP BY week,day,hour,branch

原文:https://stackoverflow.com/questions/33490620
更新时间:2023-10-03 22:10

最满意答案

你根本不需要使用.select -

工作表代码:

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Sheet1.Range("A1:B2"), Target) Is Nothing Then
    MsgBox "Hello"
End If

End Sub

模块代码:

Sub DisplayResult()

Sheet1.Range("A3").Value = Sheet1.Range("A1").Value + Sheet1.Range("A2").Value
Sheet1.Range("B3").Value = Sheet1.Range("B1").Value + Sheet1.Range("B2").Value

End Sub

如果将Call DisplayResult行放在Worksheet_Change子例程中而不限制应该触发的范围,那么它将最终进入无限循环(DisplayResult触发Worksheet_Change子)。 应该何时触发DisplayResult代码? 只有当您更改了范围A1:B2?


you don't need to use .select at all here -

Worksheet Code:

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Sheet1.Range("A1:B2"), Target) Is Nothing Then
    MsgBox "Hello"
End If

End Sub

Module Code:

Sub DisplayResult()

Sheet1.Range("A3").Value = Sheet1.Range("A1").Value + Sheet1.Range("A2").Value
Sheet1.Range("B3").Value = Sheet1.Range("B1").Value + Sheet1.Range("B2").Value

End Sub

If you put the Call DisplayResult line in your Worksheet_Change sub routine without limiting what range it should be triggered on, then it will end up going into an infinite loop (DisplayResult triggers the Worksheet_Change sub). When should the DisplayResult code be triggered? Only when you have changed Range A1:B2 ?

相关问答

更多
  • 细节 当您尝试进行超出赋值目标限制的赋值时会产生溢出。 此错误有以下原因和解决方案: 赋值,计算或数据类型转换的结果太大,无法在该类型变量允许的值范围内表示。 将该值分配给可以保存更大范围值的类型的变量。 对属性的分配超出了该属性可以接受的最大值。 确保您的任务符合所制作财产的范围。 您尝试在计算中使用数字,并将该数字强制为一个整数,但结果大于整数。 例如: Dim x As Long x = 2000 * 365 ' Error: Overflow 要解决这种情况,请输入数字,如下所示: Dim x ...
  • 正如你已经意识到的那样 看起来问题是任何UDF都不允许编辑工作表,只返回一个值...所以如果我想编辑另一个单元作为同一个进程的一部分,我需要使用一个子“ 标准 UDF无法更改工作表。 但就你的后续评论而言 这是正确的,如果是这样,我将如何进行 - sub在子函数或函数内? 我希望我的电子表格能够像输入功能一样自动对输入作出反应 - 无需按钮或特殊操作。 您可以使用活动 举个例子: 您想在特定工作表上跟踪A1:A10以获得输入 如果使用此区域,则需要设置Worksheets("Sheet1").Range(" ...
  • 你根本不需要使用.select - 工作表代码: Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Sheet1.Range("A1:B2"), Target) Is Nothing Then MsgBox "Hello" End If End Sub 模块代码: Sub DisplayResult() Sheet1.Range("A3").Value = Sheet1.Range("A1").Value ...
  • 尝试使用这样的循环条件: i = 1 'To loop through the no of days j = 10 'To start from row no 10 For i = 1 To NoDays 'Nodays = Enddate-Startdate Sheet1.Cells(j, 5).value = StartDate StartDate = StartDate + 1 j = j + 5 'To add a gap of 4 blank rows Next i ...
  • 根据此页面 ,Excel用户定义的函数不可能更改工作簿的其他部分。 看起来你可能不得不尝试另一种方式。 According to this page, it's not possible for an Excel user defined function to alter other parts of the Workbook. Looks like you may have to try another way.
  • $highestRow = $sheet->getHighestDataRow(); $highestColumn = $sheet->getHighestDataColumn(); $highestRow = $sheet->getHighestDataRow(); $highestColumn = $sheet->getHighestDataColumn();
  • 我不这么认为,如果你不想要VBA那么我能想到的就是锁定单元格以便它们无法更新。 如何锁定一系列单元格 I don't think so, if you dont want VBA then all I can think of is to lock the cells so they can't be updated. How to lock just a range of cells
  • 在Excel VBA代码编辑器中,选择“工具/参考”。 在选择器中检查最新版本的Microsoft VBScript Regular Expresions。 这是一个简单的用户定义函数,可以帮助您入门。 如果你将它指向一个带有“foobar”或“fooooobar”的单元格,它将返回“foo”。 Function RegExtract(contents As String) Dim regx As RegExp Set regx = New RegExp With regx ...
  • 使用A1至B4中的数据,使用: = VLOOKUP(RANDBETWEEN(1,4),A1:B4,2,FALSE)或其VBA等价物 编辑#1 : 使用存储在单元格D3中的表的上限: = VLOOKUP(RANDBETWEEN(1,D3),间接(“A1:B”和D3),2,FALSE) With your data in A1 thru B4, use: =VLOOKUP(RANDBETWEEN(1,4),A1:B4,2,FALSE) or its VBA equivalent EDIT#1 : With th ...
  • 您可以在VBA中使用IIF功能。 它类似于Excel IF [c1] = IIf([a1] = [b1], "Yes", "No") You can use the IIF function in VBA. It is similar to the Excel IF [c1] = IIf([a1] = [b1], "Yes", "No")

相关文章

更多

最新问答

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