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
最满意答案
你根本不需要使用.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 ...
-
如何从VBA写入单元格(how to write to a cell from VBA)[2022-03-13]
根据此页面 ,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单元格中调用正则表达式(Call regex in an Excel cell)[2024-01-04]
在Excel VBA代码编辑器中,选择“工具/参考”。 在选择器中检查最新版本的Microsoft VBScript Regular Expresions。 这是一个简单的用户定义函数,可以帮助您入门。 如果你将它指向一个带有“foobar”或“fooooobar”的单元格,它将返回“foo”。 Function RegExtract(contents As String) Dim regx As RegExp Set regx = New RegExp With regx ... -
VBA条件单元格选择(VBA conditional cell selection)[2024-03-07]
使用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")