在VBA中使用列名作为范围(Using column names as range in VBA)
我有下面的代码,使用col范围作为值在工作表中搜索列。 相反,我需要使用col名(在该表中)作为搜索条件并执行相同的功能。 我如何实现这一点?
Sub WBR() Dim Count1Criteria As Variant Dim Count3Criteria As Variant Dim test As Variant Dim wf As WorksheetFunction Set wf = Application.WorksheetFunction Filter1InSummary = Array(Array("AE4", "Latency", "O:O", "Pass"), _ Array("AE51", "TT", "G:G", "Yes"), _ Array("AE52", "TT", "G:G", "No"), _ Array("AE61", "Reactive", "R:R", "Item")) Filter3InSummary = Array(Array("AE43", "TT", "I:I", "<>Duplicate TT", _ "G:G", "<>Not Tested", _ "U:U", "Item")) For Each test In Filter3InSummary With Worksheets(test(1)) Range(test(0)) = wf.CountIfs(.Range(test(2)), test(3), _ .Range(test(4)), test(5), _ .Range(test(6)), test(7)) End With Next
I have the below code that searches a column in a sheet using the col range as value. Instead of that, I need to use the col name (in that sheet) as he search criteria and perform the same function. How do I implement this?
Sub WBR() Dim Count1Criteria As Variant Dim Count3Criteria As Variant Dim test As Variant Dim wf As WorksheetFunction Set wf = Application.WorksheetFunction Filter1InSummary = Array(Array("AE4", "Latency", "O:O", "Pass"), _ Array("AE51", "TT", "G:G", "Yes"), _ Array("AE52", "TT", "G:G", "No"), _ Array("AE61", "Reactive", "R:R", "Item")) Filter3InSummary = Array(Array("AE43", "TT", "I:I", "<>Duplicate TT", _ "G:G", "<>Not Tested", _ "U:U", "Item")) For Each test In Filter3InSummary With Worksheets(test(1)) Range(test(0)) = wf.CountIfs(.Range(test(2)), test(3), _ .Range(test(4)), test(5), _ .Range(test(6)), test(7)) End With Next
原文:https://stackoverflow.com/questions/41581155
最满意答案
你能用COALESCE吗?
例如:
SELECT COALESCE(COUNT(*),0) AS orders FROM `orders` GROUP BY DATE(`order_datetime`) ORDER BY DATE(`order_time`) ASC
它返回列表中的第一个非空值 - 如果它不为空则计数,如果不是则为0。
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
挂起..是没有这些日期的行的问题,所以你只是得到那些日期的结果?
如果是这样,您也可以返回订单日期,并且对于如何显示结果更加聪明:
SELECT order_datetime,COUNT(*) AS orders FROM `orders` GROUP BY DATE(`order_datetime`) ORDER BY DATE(`order_time`) ASC
然后在输出结果时可能会创建一个循环,遍历您感兴趣的日期,如果该日期没有行,则打印0:
for (datetime d = startdate; d <= enddate; datetime.adddays(1)){ if (recordset[order_datetime] == d){ output (d,recordset[orders]); recordset.movenext(); }else { output (d,0); } }
(注意我不知道会是什么编程语言;-)
如果你想在数据库中完成所有操作,那么请查看这个SO问题,了解如何动态创建日期表,然后你就可以将它的外部联接留给你查询日期了。
can you use COALESCE?
eg:
SELECT COALESCE(COUNT(*),0) AS orders FROM `orders` GROUP BY DATE(`order_datetime`) ORDER BY DATE(`order_time`) ASC
it returns first non null value in the list - count if its not null and 0 if it is.
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
hang on.. is the issue that there are no rows with those dates so you just get results for those dates?
if so you could return the order date too and be a bit more clever about how you display your result:
SELECT order_datetime,COUNT(*) AS orders FROM `orders` GROUP BY DATE(`order_datetime`) ORDER BY DATE(`order_time`) ASC
then when outputting your results maybe create a loop that iterates through the dates you are interested in and prints a 0 if there is no row for that date:
for (datetime d = startdate; d <= enddate; datetime.adddays(1)){ if (recordset[order_datetime] == d){ output (d,recordset[orders]); recordset.movenext(); }else { output (d,0); } }
(note i have no idea what programming language that would be ;-)
if you want to do it all in the DB then look at this SO question for how to create a table of the dates on the fly and then you can left outer join from it to you query on date..
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
MySQL组由null(MySQL group by with null)[2023-09-08]
处理此问题的一种概念上简单的方法是将您的原始查询(略微修改)与UNION一起用于获取group_id为null的所有记录: SELECT * FROM objects WHERE group_id IS NOT NULL GROUP BY group_id UNION SELECT * FROM objects WHERE group_id IS NULL SQLFiddle 顺便说一下,我认为在GROUP BY查询中SELECT列是不好的做法,它们不是聚合或者正在进行分组的列。 因此,最好避免使用GRO ... -
MySQL按日期分组,如果不存在,强制返回空值(MySQL group by date, force to return null value if does not exists)[2022-08-24]
下面的查询使用派生表(别名inr )将YearMonth / app_id组合在一起。 然后在left join使用它来获取数据,而不 applestore_ranking 是否存在于表applestore_ranking 。 如果您希望零出现而不是NULL,请使用ifnull() 。 ifnull(AVG(r.rank),0) as Rank变成ifnull(AVG(r.rank),0) as Rank 。 请注意,它可以帮助您简单地将这个分段放在一起,然后单独突出显示inr select的代码并查看其简 ... -
你能用COALESCE吗? 例如: SELECT COALESCE(COUNT(*),0) AS orders FROM `orders` GROUP BY DATE(`order_datetime`) ORDER BY DATE(`order_time`) ASC 它返回列表中的第一个非空值 - 如果它不为空则计数,如果不是则为0。 http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce 挂起. ...
-
MySQL Group By和Skip Grouping on Null Values(MySQL Group By And Skip Grouping On Null Values)[2023-01-13]
如果表中有唯一的列(或列集),则只需向GROUP BY添加另一个表达式即可。 当collection_id为null时,表达式需要为每一行返回唯一值,否则返回一个常量。 假设表中有一个唯一的id列,那么这样的话: ... GROUP BY collection_id, IF(collection_id IS NULL, id, 0) 当collection_id不为null时,GROUP BY中的第二个表达式返回一个常量值,但当collection_id为null时,它会为每一行返回一个唯一值。 (注意, ... -
MySQL GROUP_CONCAT与COALESCE有关的NULL值(MySQL GROUP_CONCAT vs. COALESCE concerning NULL values)[2022-08-28]
这是正常工作的查询(不剥离NULL ): SELECT `table1`.* GROUP_CONCAT(COALESCE(`table3`.`id`, '') SEPARATOR ';') AS `t3_id`, GROUP_CONCAT(COALESCE(`table3`.`note`, '') SEPARATOR ';') AS `t3_note`, FROM `table1` LEFT JOIN `table3` ON `table3`.`id` = `table1`.`id` ... -
它通过一个常量值对整个查询进行分组,这使得它可以随机有效地选择一行来返回。 使用LIMIT 1可以获得相同的结果。 请注意,此习惯用法不是标准SQL,如果在其他数据库中尝试,可能会导致错误。 在MySQL中,您可以通过指定ONLY FULL GROUP BY SQL模式来禁用此行为。 有关详细信息,请参阅文档 。 OTOH, LIMIT 1也是非标准的,但是得到了更广泛的支持,并且在符合SQL:2008和FETCH ... ONLY子句的数据库中可以实现相同的效果。 有关详细信息,请参阅此处。 It's g ...
-
你的time_milis都是秒,但是hour需要像'10:05:03'这样的time ,你可以使用sec_to_time 。 喜欢这个: SELECT concat(hour(sec_to_time(time_milis)), ' ', sec_to_time(time_milis)) FROM ( SELECT SUM(TIMEDIFF(logout_timestamp,login_timestamp)) AS time_milis FROM t_access_log WHERE logout_tim ...
-
不要使用select * with group by 。 它是一种反模式,在大多数情况下不受其他数据库的支持。 接受挑战。 弄清楚如何组合结果。 在这种情况下,简单的聚合函数有效: SELECT DATE_FORMAT(FROM_UNIXTIME(`time`), '%Y %m %d %H') as yyyymmddhh, AVG(temperature) as temperature, AVG(pressure) as pressure, AVG(humidity ...
-
你能用UNION试试吗? (SELECT SQL_CALC_FOUND_ROWS * FROM calls WHERE parent_id = '$term' where job_num != 0 GROUP BY (job_num) ORDER BY date_start DESC LIMIT $page_position, $item_per_page) UNION (SELECT SQL_CALC_FOUND_ROWS * FROM calls WHERE parent_id = '$term' ...