- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何获取已按 _id 分组并相加的列的 MAX 值。
所以像这样:
然后我想显示四组的最大值,以及四组的最小值。
像这样 Scratch High = 702/Scratch Low = 325
这是否可以通过 SQLite 的内置数学函数实现,或者我是否需要编写特定代码来实现?实际的组数将超过 4 个,这将取决于投球手实际投球的频率。
到目前为止,我还没有为此编写任何代码,我试图在尝试这样做之前弄清楚这是否可行。欢迎大家提出意见。我尝试整合到我的项目中:
数据库助手.java
public static final String DERIVEDCOL_MAXSCORE = "max_score";
public static final String DERIVEDCOl_MINSCORE = "min_score";
public Cursor getMaxMinScoresAllAndroid() {
SQLiteDatabase db = this.getWritableDatabase();
String tmptbl = "summed_scores";
String tmptblcol = "sum_score";
String crttmptbl = "CREATE TEMP TABLE IF NOT EXISTS " + tmptbl + "(" +
tmptblcol + " INTEGER" +
")";
String empttmptbl = "DELETE FROM " + tmptbl;
db.execSQL(crttmptbl);
db.execSQL(empttmptbl);
String[] columns = new String[]{"sum(score) AS " + tmptblcol};
Cursor csr = db.query(Game.TABLE_NAME,columns,null,null,Game.COLUMN_BOWLER_ID,null,null);
DatabaseUtils.dumpCursor(csr);
while (csr.moveToNext()) {
ContentValues cv = new ContentValues();
cv.put(tmptblcol,csr.getInt(csr.getColumnIndex(tmptblcol)));
db.insert(tmptbl,null,cv);
}
csr.close();
columns = new String[]{"max(" +
tmptblcol +
") AS " + DERIVEDCOL_MAXSCORE,
"min(" +
tmptblcol +
") AS " + DERIVEDCOl_MINSCORE};
return csr = db.query(tmptbl,columns,null,null,null,null,null);
}
public MaxMin getMaxAndminScoresAll() {
MaxMin rv = new MaxMin(0,0);
Cursor csr = getMaxMinScoresAllAndroid();
if (csr.moveToFirst()) {
rv.setMin(csr.getInt(csr.getColumnIndex(DERIVEDCOl_MINSCORE)));
rv.setMax(csr.getInt(csr.getColumnIndex(DERIVEDCOL_MAXSCORE)));
}
csr.close();
return rv;
}
BowlerProfileViewActivity.java
public class BowlerProfileViewActivity extends AppCompatActivity {
Bowler bowler;
private DatabaseHelper db;
private static final String PREFS_NAME = "prefs";
private static final String PREF_BLUE_THEME = "blue_theme";
private static final String PREF_GREEN_THEME = "green_theme";
private static final String PREF_ORANGE_THEME = "purple_theme";
private static final String PREF_RED_THEME = "red_theme";
private static final String PREF_YELLOW_THEME = "yellow_theme";
@Override
protected void onResume() {
super.onResume();
db = new DatabaseHelper(this);
//mAdapter.notifyDatasetChanged(db.getAllLeagues());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//Use Chosen Theme
SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
boolean useBlueTheme = preferences.getBoolean(PREF_BLUE_THEME, false);
if (useBlueTheme) {
setTheme(R.style.AppTheme_Blue_NoActionBar);
}
boolean useGreenTheme = preferences.getBoolean(PREF_GREEN_THEME, false);
if (useGreenTheme) {
setTheme(R.style.AppTheme_Green_NoActionBar);
}
boolean useOrangeTheme = preferences.getBoolean(PREF_ORANGE_THEME, false);
if (useOrangeTheme) {
setTheme(R.style.AppTheme_Orange_NoActionBar);
}
boolean useRedTheme = preferences.getBoolean(PREF_RED_THEME, false);
if (useRedTheme) {
setTheme(R.style.AppTheme_Red_NoActionBar);
}
boolean useYellowTheme = preferences.getBoolean(PREF_YELLOW_THEME, false);
if (useYellowTheme) {
setTheme(R.style.AppTheme_Yellow_NoActionBar);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bowler_profile_view);
Intent intent = getIntent();
Boolean shouldUpdate = getIntent().getExtras().getBoolean("shouldUpdate");
String savedLeagueId = intent.getStringExtra("leagueId");
String savedBowlerId = String.valueOf(getIntent().getIntExtra("bowlerId",2));
int bowlerId = Integer.valueOf(savedBowlerId);
getBowlerProfile(savedLeagueId, bowlerId);
// Get The min and max score
MaxMin bowlerMaxMin = db.getMaxAndminScoresAll();
Log.d("SCORES",
"\n\tMaximum Score is " + String.valueOf(bowlerMaxMin.getMax()) +
"\n\tMinimum Score is " + String.valueOf(bowlerMaxMin.getMin()));
}
public void getBowlerProfile(String savedLeagueId, int savedBowlerId) {
String bn, ba, bh;
SQLiteOpenHelper database = new DatabaseHelper(this);
SQLiteDatabase db = database.getReadableDatabase();
Cursor viewBowlerProfile = db.query( Bowler.TABLE_NAME,
new String[]{Bowler.COLUMN_ID, Bowler.COLUMN_LEAGUE_ID, Bowler.COLUMN_NAME, Bowler.COLUMN_BOWLER_AVERAGE, Bowler.COLUMN_BOWLER_HANDICAP, Bowler.COLUMN_TIMESTAMP},
Bowler.COLUMN_ID + "=?",
new String[]{String.valueOf(savedBowlerId)}, null, null, null, null);
if (viewBowlerProfile.moveToFirst()) {
//Prepare League Object
bowler = new Bowler(
viewBowlerProfile.getInt(viewBowlerProfile.getColumnIndex(Bowler.COLUMN_ID)),
viewBowlerProfile.getString(viewBowlerProfile.getColumnIndex(Bowler.COLUMN_LEAGUE_ID)),
bn = viewBowlerProfile.getString(viewBowlerProfile.getColumnIndex(Bowler.COLUMN_NAME)),
ba = viewBowlerProfile.getString(viewBowlerProfile.getColumnIndex(Bowler.COLUMN_BOWLER_AVERAGE)),
bh = viewBowlerProfile.getString(viewBowlerProfile.getColumnIndex(Bowler.COLUMN_BOWLER_HANDICAP)),
viewBowlerProfile.getString(viewBowlerProfile.getColumnIndex(Bowler.COLUMN_TIMESTAMP)));
final TextView bowlerName = (TextView) findViewById(R.id.tvBowlerName);
final TextView bowlerAverage = (TextView) findViewById(R.id.tvBowlerAverageValue);
final TextView bowlerHandicap = (TextView) findViewById(R.id.tvBowlerHandicapValue);
bowlerName.setText(String.valueOf(bn));
bowlerAverage.setText(String.valueOf(ba));
bowlerHandicap.setText(String.valueOf(bh));
//Close Database Connection
viewBowlerProfile.close();
}
//View League Profile Cancel Button
final Button cancel_button = (Button) findViewById(R.id.bCancel);
cancel_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("SAVEDLEAGUEID_VAL", ">>" + String.valueOf(savedLeagueId) + "<<");
Intent intent = new Intent(getApplicationContext(), BowlerActivity.class);
intent.putExtra("leagueId", savedLeagueId);
startActivity(intent);
finish();
overridePendingTransition(0, 0);
/*Intent intent = new Intent(getApplicationContext(), BowlerActivity.class);
intent.putExtra("leagueId", savedLeagueId);
Log.d("LEAGUEID VALUE","value of leagueId = " + String.valueOf(savedLeagueId));
startActivity(intent);
finish();
overridePendingTransition(0, 0);*/
}
});
//Edit League Profile Cancel Button
final Button edit_button = (Button) findViewById(R.id.bEdit);
edit_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int bowlerId = bowler.getId();
Intent intent = new Intent(getApplicationContext(), BowlerProfileEditActivity.class);
intent.putExtra("bowlerId", bowlerId);
intent.putExtra("leagueId", savedLeagueId);
startActivity(intent);
finish();
overridePendingTransition(0, 0);
}
});
}
}
日志
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'ca.rvogl.tpbcui.utils.MaxMin ca.rvogl.tpbcui.database.DatabaseHelper.getMaxAndminScoresAll()' on a null object reference
at ca.rvogl.tpbcui.views.BowlerProfileViewActivity.onCreate(BowlerProfileViewActivity.java:79)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
尝试按 bowlerId 和 seriesId 分组
public Cursor getMaxMinScoresAllAndroid(String bowlerId) {
SQLiteDatabase db = this.getWritableDatabase();
String tmptbl = "summed_scores";
String tmptblcol = "sum_score";
String tmpBowlerIdCol = "bowler_id";
String tmpSeriesIdCol = "series_id";
String crttmptbl = "CREATE TEMP TABLE IF NOT EXISTS " + tmptbl + "(" +
tmptblcol + " INTEGER," +
tmpBowlerIdCol + " TEXT," +
tmpSeriesIdCol + " TEXT)";
String empttmptbl = "DELETE FROM " + tmptbl;
db.execSQL(crttmptbl);
db.execSQL(empttmptbl);
String[] columns = new String[]{"sum(score) AS "};
Cursor csr = db.query(Game.TABLE_NAME,columns,null,null,Game.COLUMN_BOWLER_ID + " = '" + bowlerId + "'", Game.COLUMN_SERIES_ID,null,null);
DatabaseUtils.dumpCursor(csr);
while (csr.moveToNext()) {
ContentValues cv = new ContentValues();
cv.put(tmptblcol,csr.getInt(csr.getColumnIndex(tmptblcol)));
cv.put(tmpBowlerIdCol,csr.getInt(csr.getColumnIndex(tmpBowlerIdCol)));
cv.put(tmpSeriesIdCol,csr.getInt(csr.getColumnIndex(tmpSeriesIdCol)));
db.insert(tmptbl,null,cv);
}
csr.close();
columns = new String[]{"max(" +
tmptblcol +
") AS " + DERIVEDCOL_MAXSCORE,
"min(" +
tmptblcol +
") AS " + DERIVEDCOl_MINSCORE};
return csr = db.query(tmptbl,columns,null,null,null,null,null);
}
public MaxMin getMaxAndminScoresAll(String bowlerId) {
MaxMin rv = new MaxMin(0,0);
Cursor csr = getMaxMinScoresAllAndroid(bowlerId);
if (csr.moveToFirst()) {
rv.setMin(csr.getInt(csr.getColumnIndex(DERIVEDCOl_MINSCORE)));
rv.setMax(csr.getInt(csr.getColumnIndex(DERIVEDCOL_MAXSCORE)));
}
csr.close();
return rv;
}
最佳答案
您可以这样做(假设表名为 myscores 并且列为 _id 和 score)使用:-
WITH cte1 AS
(
sum(score) AS sum_score
FROM myscores
GROUP BY _id
)
SELECT max(sum_score) AS min_score, min(sum_score) FROM cte1;
使用它会导致以下结果:-
AS
用于重命名输出列
这利用 SQLite 聚合函数 max 和 min 以及 GROUP BY 子句根据 _id 列聚合列。
这也使用了公用表表达式(中间/临时表)。 SQL As Understood By SQLite - WITH clause
以下是一个示例应用程序,演示了如何将其整合到 Android 中:-
首先是最小值和最大值的简单类(在替代的 getMaxAndMinScores 方法中使用)
public class MaxMin {
private int min;
private int max;
public MaxMin(int min, int max) {
this.min = min;
this.max = max;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
}
SQLiteOpenHelper的子类(只有一个表名myscores)
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String TB_SCORE = "myscores";
public static final String COL_SCORE = "score";
public static final String COL_ID = BaseColumns._ID;
public static final String DERIVEDCOL_MAXSCORE = "max_score";
public static final String DERIVEDCOl_MINSCORE = "min_score";
private static final String crt_myscores_sql = "CREATE TABLE IF NOT EXISTS " + TB_SCORE + "(" +
COL_ID + " INTEGER," +
COL_SCORE + " INTEGER" +
")";
public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(crt_myscores_sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
public long addScore(long id, int score) {
ContentValues cv = new ContentValues();
cv.put(COL_ID,id);
cv.put(COL_SCORE,score);
return this.getWritableDatabase().insert(TB_SCORE,null,cv);
}
public Cursor getMaxMinScores() {
String sum_score = "sum_score";
String cte1 = "cte1";
String rawqry = " WITH " + cte1 +
" AS " +
"(" +
"SELECT sum(" +
COL_SCORE +
") AS " + sum_score +
" FROM " + TB_SCORE + " GROUP BY " + COL_ID +
") " +
"SELECT " +
" max(" +
sum_score +
") AS " + DERIVEDCOL_MAXSCORE +
"," +
" min(" +
sum_score +
") AS " + DERIVEDCOl_MINSCORE +
" FROM " + cte1 + ";";
return this.getWritableDatabase().rawQuery(rawqry,null);
}
public MaxMin getMaxAndMinScores() {
MaxMin rv = new MaxMin(0,0);
Cursor csr = getMaxMinScores();
if (csr.moveToFirst()) {
rv.setMin(csr.getInt(csr.getColumnIndex(DERIVEDCOl_MINSCORE)));
rv.setMax(csr.getInt(csr.getColumnIndex(DERIVEDCOL_MAXSCORE)));
}
csr.close();
return rv;
}
}
a) 添加一些行然后 b) 获得最大和最小分数的 Activity (使用替代方法两次):-
public class MainActivity extends AppCompatActivity {
DBHelper mDBHlpr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDBHlpr = new DBHelper(this);
// Add Some scores
mDBHlpr.addScore(1,112);
mDBHlpr.addScore(1,123);
mDBHlpr.addScore(1,144);
mDBHlpr.addScore(2,212);
mDBHlpr.addScore(2,190);
mDBHlpr.addScore(2,300);
mDBHlpr.addScore(3,234);
mDBHlpr.addScore(3,134);
mDBHlpr.addScore(3,122);
mDBHlpr.addScore(4,100);
mDBHlpr.addScore(4,111);
mDBHlpr.addScore(4,114);
// Get The min and max scores example 1
Cursor csr = mDBHlpr.getMaxMinScores();
if (csr.moveToFirst()) {
int max_score = csr.getInt(csr.getColumnIndex(DBHelper.DERIVEDCOL_MAXSCORE));
int min_score = csr.getInt(csr.getColumnIndex(DBHelper.DERIVEDCOl_MINSCORE));
Log.d("SCORES",
"\n\tMaximum Score is " + String.valueOf(max_score) +
"\n\tMinimum Score is " + String.valueOf(min_score)
);
}
//Alternative utilising the MaxMin object
MaxMin mymaxmin = mDBHlpr.getMaxAndMinScores();
Log.d("SCORES",
"\n\tMaximum Score is " + String.valueOf(mymaxmin.getMax()) +
"\n\tMinimum Score is " + String.valueOf(mymaxmin.getMin())
);
}
}
The
WITH
clause was introduced in SQL 3.8.3, some older versions of Android (below lollipop (but can be device independent)) will not support theWITH
clause.
以下方法与 getMaxMinScores
和 getMaxAndMinScores
等效,可用于任何 Android 版本:-
public Cursor getMaxMinScoresAllAndroid() {
SQLiteDatabase db = this.getWritableDatabase();
String tmptbl = "summed_scores";
String tmptblcol = "sum_score";
String crttmptbl = "CREATE TEMP TABLE IF NOT EXISTS " + tmptbl + "(" +
tmptblcol + " INTEGER" +
")";
String empttmptbl = "DELETE FROM " + tmptbl;
db.execSQL(crttmptbl);
db.execSQL(empttmptbl);
String[] columns = new String[]{"sum(score) AS " + tmptblcol};
Cursor csr = db.query(TB_SCORE,columns,null,null,COL_ID,null,null);
DatabaseUtils.dumpCursor(csr);
while (csr.moveToNext()) {
ContentValues cv = new ContentValues();
cv.put(tmptblcol,csr.getInt(csr.getColumnIndex(tmptblcol)));
db.insert(tmptbl,null,cv);
}
csr.close();
columns = new String[]{"max(" +
tmptblcol +
") AS " + DERIVEDCOL_MAXSCORE,
"min(" +
tmptblcol +
") AS " + DERIVEDCOl_MINSCORE};
return csr = db.query(tmptbl,columns,null,null,null,null,null);
}
public MaxMin getMaxAndminScoresAllAndroid() {
MaxMin rv = new MaxMin(0,0);
Cursor csr = getMaxMinScoresAllAndroid();
if (csr.moveToFirst()) {
rv.setMin(csr.getInt(csr.getColumnIndex(DERIVEDCOl_MINSCORE)));
rv.setMax(csr.getInt(csr.getColumnIndex(DERIVEDCOL_MAXSCORE)));
}
csr.close();
return rv;
}
WITH
关于java - 如何找出已添加在一起的组的最大值和最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52573525/
我有一个像 [3,10,4,3,9,15,6,13] 这样的列表,我想找到两个不重叠的系列/序列给出通过取最大-最小值可获得的最大值.它们必须是连续的,因此您不能从 1 中减去项目 3。但是您可以从
我正在尝试创建顶部列,这是几个列行的最大值。 Pandas 有一个方法 nlargest但我无法让它成行工作。 Pandas 也有 max和 idxmax这正是我想做的,但仅限于绝对最大值。 df =
我在使用 Android 时遇到了一点问题。 我有我的 GPS 位置,明确的经纬度,以及以米为单位的搜索射线(例如 100 米),可以吗? 想象一下我在射线形成的圆心的位置,我会知道如何在 Andro
假设我有一组最小值和最大值。我想要一个数据结构,在给定外部值的情况下,它会最有效地为我提供值 >= 最小值、值 = 最小值和值 <= 最大值?,我们在Stack Overflow上找到一个类似的问题:
我有以下 Maxima 代码: m:sum(x[i],i,1,N)/N; 然后我想计算 $m^2$。 m2:m^2, sumexpand; 然后我得到双重求和: sum(sum(x[i1]*x[i2]
如何从嵌套字典中获取一个值的最小值/最大值,该字典的缺失值也包含“Nan”? *这是供引用,我找到了一个解决方案,我想我应该在这里分享它,因为我在 stackoverflow 上的任何地方都找不到答案
在千里马 12.04.0 我有一个总和 mysum : sum(u[i]^2, i, 1, N); 现在我区分它 diff(mysum, u[i]); 现在我指定一个定义的索引 i=A 来区分它 at
是否可以根据时间轴获取最小和最大时间戳?我将在 parking 场示例中进行解释。 +---------------------+------+--------+-------+-----------
基本上在几个领域有几个日期 SELECT MAX(MAX(DATE_A),MAX(DATE_B)) from table DATE_A 和 DATE_B 是日期,我基本上想要日期 A 或日期 B 的最
我创建了一个小测试,其中一个 div 根据滚动深度滑动。 我只是想知道怎么设置 A) 起点 (scrolltop = x something) B) 如何设置最大值? var pxlCount = 0
由于达到最大值,clock_gettime() 何时会使用 CLOCK_MONOTONIC 返回一个较小的值?我不是指被描述为错误的小扭曲,而是类似于计数器重置的东西。 它是时间测量的,还是与滴答的绝
我正在使用 angularjs,尤其是 $timeout 服务(setTimeout 的包装器)。它的工作原理如下: angular.module('MyApp').controller('MyCo
是否有可能获得 MinValue - 或 MaxValue未知的 T 型?如 Int其中有 Int.MinValue和 Int.MaxValue ?? 谢谢 最佳答案 正如@mpilquist 在上面
我的数据为 员工: id Name -------- 1 xyz 2 abc 3 qaz Employee_A:(Eid - 员工表,title - 职称表) eid active
我有一个日期和时间行列表,每天有多行。 对于每个唯一日期,我想获取最小和最大时间值。 如何在 Excel v10(又名 2002)中执行此操作? 最佳答案 首先,您可以使用 Excel 函数 MIN(
我有以下 SQL 表 - Date StoreNo Sales 23/4 34 4323.00 23/4 23 5
我可能错过了一些微不足道的东西。我想我还没有完全理解一些基本的交叉过滤器概念 无论如何,我创建了一个带有几个维度的交叉过滤器,并在维度上使用过滤器。我现在想知道过滤值(不是键)的最小值/最大值。 我将
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我在这里错过了什么吗?我希望以下代码段中的 np.max 会返回 [0, 4] ... >>> a array([[1, 2], [0, 4]]) >>> np.max(a,
给定大小为 2 的列表列表,我试图找到通过索引确定最小/最大值的最快方法。目标是确定一系列 XY 点的边界/范围。 子列表未排序(按一个索引排序并不能保证另一个索引已排序)。 目前我正在做以下事情:
我是一名优秀的程序员,十分优秀!