SQL Server - 根据周数获取一周内的第一个日期?(SQL Server - Get first date in a week, given the week number?)
我有一个查询(用于bug tracker.net),按状态计算每周的错误数。 但查询返回周数,我真正想要的是一周的第一个日期
select datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))) as [week], bg_status , st_name as [status], count(*) as [count] from bugs inner join statuses on bg_status = st_id group by datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))), bg_status, st_name order by [week], bg_status
得到周数的部分是
datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))) as [week]
它返回此输出:
week bg_status status count ----------- ----------- --------------------------------------------- ------ 22 1 new 1 22 5 closed 32
但最好是说每周的第一个日期,例如01-01-2010,然后是08-01-2010等
问题不是重复如何从SQL Server中的周数获得“周开始日期”和“周结束日期”? (回答说如何从不是从一周的数字开始的一周开始)
不是从周数计算日期的重复(问题请求c#)
从提供的日期获取第一周的日期 (问题请求javascript)不重复
我做了搜索,但找不到这个问题的答案为SQL Server(2010如果重要)
I've got a query (for use in bug tracker.net) that calculates the number of bugs by week by status. But the query returns the week number, what I really want is the first date of the week
select datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))) as [week], bg_status , st_name as [status], count(*) as [count] from bugs inner join statuses on bg_status = st_id group by datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))), bg_status, st_name order by [week], bg_status
The part that gets the week number is
datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))) as [week]
It returns this output:
week bg_status status count ----------- ----------- --------------------------------------------- ------ 22 1 new 1 22 5 closed 32
But it would be better to say the first date of each week, eg 01-01-2010, then 08-01-2010, etc
Question is not a duplicate of How do you get the "week start date" and "week end date" from week number in SQL Server? (answer says how to get week start from a date not from a week number)
Not a duplicate of Calculate date from week number (question asks for c#)
Not a duplicate of Get first date of week from provided date (question asks for javascript)
I did search but couldn't find this question answered for SQL Server (2010 if it matters)
原文:https://stackoverflow.com/questions/3331579
最满意答案
首先,你需要一个函数whitch计算表中一个区间内的所有频率:
int countinterval( double *tab, int tabsize, double min, double max ) { int count = 0; for ( int i = 0; i < tabsize; i ++ ) { if ( tab[i] >= min && tab[i] < max ) // test if frequence is in interval [min,max[ // if ( tab[i] >= min && tab[i] <= max ) // test if frequence is in interval [min,max] count ++; } return count; }
接下来,您需要一个函数whitch汇总每个表的结果。
int counttabsinterval( double *tab[], int tabsize, int tabcount, double min, double max ) { int count = 0; for ( int i = 0; i < tabcount; i ++ ) count += countinterval( tab[i], tabsize, min, max ); // sums results return count; }
在您想知道频率数的每个间隔调用该函数。
double *tab[1024]; int tabsize = ???; // <- init with size of one table int count1 = counttabsinterval( tab, tabsize, 1024, -1.0, -0.9 ); int count2 = counttabsinterval( tab, tabsize, 1024, -0.9, -0.8 );
这是一个计算表格
tab
上的定量频率分布的函数,其中包含表格的长度,并将结果存储在具有长度distcount
的数组dist
。void QuantitativeFrequencyDistribution( double *tab, int tabsize, double *dist, int distcount ) { int count = 0; double intervallAmount = 2.0 / distcount; for ( int i = 0; i < distcount; i ++ ) { double min = -1.0 + i*intervallAmount; double max = min + intervallAmount; dist[i] = countinterval( tab, tabsize, min, max ); } } int sizeOfTab = ?;// <- init with size of table double *tab; // <- your table int distcount = 20; // for example double dist[distcount]; QuantitativeFrequencyDistribution( tab, sizeOfTab, dist, distcount );
如果您有不同长度的不同表,并且您想知道所有表的定量频率分布,则必须为每个表调用
QuantitativeFrequencyDistribution
并计算结果的总和。First you need a function which counts all frequences in an interval in on table:
int countinterval( double *tab, int tabsize, double min, double max ) { int count = 0; for ( int i = 0; i < tabsize; i ++ ) { if ( tab[i] >= min && tab[i] < max ) // test if frequency is in interval [min,max[ // if ( tab[i] >= min && tab[i] <= max ) // test if frequency is in interval [min,max] count ++; } return count; }
Next you need a function which sums the results of each table.
int counttabsinterval( double *tab[], int tabsize, int tabcount, double min, double max ) { int count = 0; for ( int i = 0; i < tabcount; i ++ ) count += countinterval( tab[i], tabsize, min, max ); // sums results return count; }
Call the function for each interval where you want to know number of frequencies.
double *tab[1024]; int tabsize = ???; // <- init with size of one table int count1 = counttabsinterval( tab, tabsize, 1024, -1.0, -0.9 ); int count2 = counttabsinterval( tab, tabsize, 1024, -0.9, -0.8 );
Here is a function which calculates the Quantitative Frequency Distribution for on table
tab
with lenght of tabletabsize
ans stores the result in an arraydist
with lengthdistcount
.void QuantitativeFrequencyDistribution( double *tab, int tabsize, double *dist, int distcount ) { int count = 0; double intervallAmount = 2.0 / distcount; for ( int i = 0; i < distcount; i ++ ) { double min = -1.0 + i*intervallAmount; double max = min + intervallAmount; dist[i] = countinterval( tab, tabsize, min, max ); } } int sizeOfTab = ?;// <- init with size of table double *tab; // <- your table int distcount = 20; // for example double dist[distcount]; QuantitativeFrequencyDistribution( tab, sizeOfTab, dist, distcount );
If you have different tables with different lengths and you want to know the Quantitative Frequency Distribution over all tables, you have to call
QuantitativeFrequencyDistribution
for each single table and calculate sum of the results.
相关问答
更多-
OpenCV不适合绘图。 但是,对于简单的直方图,您可以为每列绘制一个矩形(最终使用交替的颜色) 这是代码: #include
#include using namespace std; using namespace cv; void drawHist(const vector & data, Mat3b& dst, int binSize = 3, int height = 0) { int max_value = * ... -
尝试这个: h <- hist(logarea,breaks="FD") product <- with(h,mids*counts) 除了绘制直方图之外, hist(...)返回值列表。 h$counts是每个bin中的频率(观察次数), h$mid是bin的中点值。 所以我认为这就是你要找的东西。 有关更多信息,请参阅文档 。 Try this: h <- hist(logarea,breaks="FD") product <- with(h,mids*counts) In addition to ...
-
使用bar的BarWidth属性更改分隔。 设置为1表示没有分离。 bar(v,'BarWidth',1); 或者,您可以使用Style参数使用hist将样式从grouped更改为直方图格式。 即 bar(v,'hist'); 对于给定的数据,上述两种方法都给出: Use the BarWidth property of bar to change the separation. Setting to 1 means no separation. bar(v,'BarWidth',1); Altern ...
-
一种解决方案是保存直方图os对象。 如果查看此对象的结构,可以看到直方图条的高度存储在元素counts 。 r<-sample(1:25000,1000) hist.ob <- hist(r) str(hist.ob) List of 7 $ breaks : num [1:14] 0 2000 4000 6000 8000 10000 12000 14000 16000 18000 ... $ counts : int [1:13] 75 46 72 91 71 91 74 87 86 ...
-
使用matplotlib的频率直方图 - 如何摆脱阵列显示?(frequency histogram with matplotlib--how to get rid of the array display?)[2022-07-15]
你需要添加; 用hist结束你的行。 这通常会抑制输出 ,但特别是对于图形调用。 (具有讽刺意味的是,当绘图时,输出是无关紧要的;它是显示绘图的调用的副作用。) You need to add ; to the end of your line with hist. This will suppress output in general, but in particular for graphing calls. (When graphing, ironically, the output is irre ... -
R频率直方图网格(R Frequency Histogram Grid)[2023-06-19]
R,大多数非古老的编程语言都区分大小写。 你的变量名称是“长度”和“物种”。 你正在调用小写“长度”和“物种”。 大写,它会工作: ggplot(data, aes(x=Length)) + geom_histogram() + facet_grid(~Species) + theme_bw() R, as most non-ancient programming languages, is case sensitive. Your variable names are "Length" and "Spe ... -
这是解决这个问题的一种方法,它利用hist()函数完成大多数繁重的工作,并且具有y的累积和的条形图与x的直方图的条目和维相匹配的优点: set.seed(1) mydata <- data.frame(y = runif (100, min= 0, max = 1), x = rpois(100, 15) * 10) mx <- mydata$x my <- mydata$y h <- hist(mydata$x) breaks <- data.frame( "beg"=h$breaks[-le ...
-
重新评论:这里是一个使用基数R的公式快速 - 基于上面的代码 - 您不必手动定义轴。 显而易见的困难是密度或频率不是漂亮的/圆形的数字(如果你想要它们都是圆的那么蜱可能会在不同的地方)。 我认为以规定的间隔保持密度更为明智。 这也需要不断的休息。 对于绘图我将密度分配给LHS轴,但这很容易调整。 histfreq <- function(X, lines=TRUE, ...) { par(mar=c(5, 4, 4, 4) + 0.1) h <- hist(X, plot= ...
-
首先,你需要一个函数whitch计算表中一个区间内的所有频率: int countinterval( double *tab, int tabsize, double min, double max ) { int count = 0; for ( int i = 0; i < tabsize; i ++ ) { if ( tab[i] >= min && tab[i] < max ) // test if frequence is in interval [min, ...
-
hist(dataset, breaks=seq(min(dataset)-0.5, max(dataset)+0.5, by=1) ) 另一种选择(对于你知道这些是整数的情况,将是: require(lattice) barchart(table(dataset), horizontal=FALSE) 要么: barplot(table(dataset)) hist(dataset, breaks=seq(min(dataset)-0.5, max(dataset)+0.5, by=1) ) ...