如何计算POD_PRDCD 它具有值 POD_DISC1 和 POD_DISC2 以获取值 POH_DISC2 使用从 tbl_detail 循环。
我试过这样
private double BOHDISC2() {
Double DISC2 = 0.0;
Double TDISC1 = 0.0;
ArrayList<POModels> data = dbController.getAllDBPO();
for(int i = 0 ; i < data.size(); i++) {
if (data.get(i).getPoddisc2() != null) {
DISC2 += (Double.parseDouble(data.get(i).getPrice()) * Double.parseDouble(data.get(i).getQty())) -
(Double.parseDouble(data.get(i).getPrice()) * Double.parseDouble(data.get(i).getQty()) * Double.parseDouble(data.get(i).getPoddisc1())/100) ;
Log.e("DISC 2", String.valueOf(DISC2));
TDISC1 += (DISC2) * Double.parseDouble(data.get(i).getPoddisc2())/100;
Log.e("TDISC 2", String.valueOf(TDISC1));
pohdisc2.setText(String.valueOf(TDISC1));
}
} return TDISC1;
}
如果您只是想计算每个 NOPO 的 POD_DISC1 + POD_DISC2 的总和,那么您可以使用 SQLite 简单地执行此操作,例如
SELECT sum(pod_disc1 + pod_disc2) AS pod_prodcd FROM tbl_detail GROUP BY pod_nopo;
WHERE
子句可用于限制结果,例如对于单个 nopo WHERE pod_nopo = 'po-0104-2023-02-00119'
(不需要 GROUP BY 因为只有一个组)演示
这是一个演示,它将获取每个 nopo 的总和,然后将查询合并到更新中,将 poh_qtypo 列更改为该值(仅作为示例):-
/* Cleanup the demo environment in case it is dirty */
DROP TABLE IF EXISTS tbl_detail;
DROP TABLE IF EXISTS tbl_header;
/* Create and populate the demo environment */
CREATE TABLE IF NOT EXISTS tbl_header (poh_nopo TEXT PRIMARY KEY, poh_qtypo REAL, poh_gross REAL, poh_disc1 REAL,poh_disc2 REAL);
INSERT INTO tbl_header VALUES('po-0104-2023-02-00119',15.00,20000,4000,485);
INSERT INTO tbl_header VALUES('po-9999-2023-02-00119',21.00,50000,9000,999);
CREATE TABLE IF NOT EXISTS tbl_detail (pod_nopo TEXT,pod_prodcd REAL,pod_qtypo REAL, pod_hrgbeli REAL,pod_disc1 REAL, pod_disc2 REAL);
INSERT INTO tbl_detail VALUES
('po-0104-2023-02-00119',103115,5.00,20000.00,0.00,0.00),
('po-0104-2023-02-00119',4911021,5.00,10000.00,5.00,0.00),
('po-0104-2023-02-00119',102894,5.00,10000.00,3.00,1.00)
;
/* Demo 1 get the sum of disc1 + disc 2 per nopo */
SELECT sum(pod_disc1 + pod_disc2) AS pod_prodcd FROM tbl_detail GROUP BY pod_nopo; /*<<<<< Result 1*/
/* Show the headers (before updating) */
SELECT * FROM tbl_header; /* Result 2 */
/* Example using the query above as a subquery in an update */
WITH
/* use a CTE (comon table expression to drive the update, so all nopo's can be updated) */
pkey(k) AS (SELECT DISTINCT poh_nopo FROM tbl_header) /* get ALL poh_nopo's (but no duplicates, shouldn't be if poh_nopo is Primary key) */
UPDATE tbl_header SET
poh_qtypo = coalesce(
(
/* The subquery */
SELECT sum(pod_disc1 + pod_disc2)
FROM tbl_detail
WHERE pod_nopo=poh_nopo), /* adapted to only get the applicable nopo rows for the header row being updated */
0.00 /* if no rows (e.g. the po0999-.... ) then instead of null use 0.00 */
)
WHERE poh_nopo IN (SELECT * FROM pkey)
;
/* show the headers (after the update) */
SELECT * FROM tbl_header; /* Result 3 */
/* Clean up the demo environment */
DROP TABLE IF EXISTS tbl_detail;
DROP TABLE IF EXISTS tbl_header;
结果
首先是每个 nopo 的 disc + disc2 的总和(结果 1):-
pod_prodcd
9.0
header更新之前的下一个:-
poh_nopo poh_qtypo poh_gross poh_disc1 poh_disc2
po-0104-2023-02-00119 15 20000 4000 485
po-9999-2023-02-00119 21 50000 9000 999
标题更新后:-
poh_nopo poh_qtypo poh_gross poh_disc1 poh_disc2
po-0104-2023-02-00119 9 20000 4000 485
po-9999-2023-02-00119 0 50000 9000 999
即0104 的 poh_qtypo 是 9 (5.00 + 3.00 + 1.00) 而 9999 是 0 因为没有详细信息行
如果你想要的是更新 pod_prdcd 以反映总和,那么这可以通过使用来完成:-
UPDATE tbl_detail SET pod_prodcd = round(pod_disc1 + pod_disc2,2);
例如:-
pod_nopo pod_prodcd pod_qtypo pod_hrgbeli pod_disc1 pod_disc2
po-0104-2023-02-00119 0 5 20000 0 0
po-0104-2023-02-00119 5 5 10000 5 0
po-0104-2023-02-00119 4 5 10000 3 1
然而,没有必要拥有 pod_prodcd 列,因为该值始终可以即时计算,例如
SELECT pod_nopo, round(pod_disc1 + pod_disc2,2) AS pod_prodcd, pod_qtypo, pod_hrgbeli, pod_disc1, pod_disc2 FROM tbl_detail;
产生:-
po-0104-2023-02-00119 0.0 5 20000 0 0
po-0104-2023-02-00119 5.0 5 10000 5 0
po-0104-2023-02-00119 4.0 5 10000 3 1
您不妨参考:-
总之SQLite的SQL功能非常强大,可以大大减少编程代码。
Android(Java)示例
假设目的是获取每个细节的 disc1 + disc(并且还进一步演示 disc1 和 disc 2 的百分比)然后考虑以下工作 Android 演示。
首先是 DatabaseHelper(即扩展 SQLiteOpenHelper 的类)即 DBHelper :-
class DBHelper extends SQLiteOpenHelper {
public static final String DATABSE_NAME = "the_database";
public static final int DATABASE_VERSION = 1;
private DBHelper(Context context) {
super(context,DATABSE_NAME,null,DATABASE_VERSION);
}
private static volatile DBHelper instance;
static DBHelper getInstance(Context context) {
if (instance==null) {
return new DBHelper(context);
}
return instance;
}
public static final String TABLE_HEADER = "tbl_header";
/* CREATE TABLE IF NOT EXISTS tbl_header (poh_nopo TEXT PRIMARY KEY, poh_qtypo REAL, poh_gross REAL, poh_disc1 REAL,poh_disc2 REAL); */
public static final String HEADER_COL_NOPO = "poh_nopo";
public static final String HEADER_COL_QTYPO = "poh_qtypo";
public static final String HEADER_COL_GROSS = "poh_gross";
public static final String HEADER_COL_DISC1 = "poh_disc1";
public static final String HEADER_COL_DISC2 = "poh_disc2";
private static final String HEADER_CRTSQL = "CREATE TABLE IF NOT EXISTS " + TABLE_HEADER + "(" +
HEADER_COL_NOPO + " TEXT PRIMARY KEY" +
"," + HEADER_COL_QTYPO + " REAL " +
"," + HEADER_COL_GROSS + " REAL " +
"," + HEADER_COL_DISC1 + " REAL " +
"," + HEADER_COL_DISC2 + " REAL " +
");";
/*CREATE TABLE IF NOT EXISTS tbl_detail (pod_nopo TEXT,pod_prodcd REAL,pod_qtypo REAL, pod_hrgbeli REAL,pod_disc1 REAL, pod_disc2 REAL);*/
public static final String TABLE_DETAIL = "tbl_detail";
public static final String DETAIL_COL_NOPO = "pod_nopo";
public static final String DETAIL_COl_PRODCD = "pod_prodcd";
public static final String DETAIL_COl_QTYPO = "pod_qtypo";
public static final String DETAIL_COL_HRGBELI = "pod_hrgbeli";
public static final String DETAIL_COL_DISC1 = "pod_disc1";
public static final String DETAIL_COL_DISC2 = "pod_disc2";
private static final String DETAIL_CRTSQL = "CREATE TABLE IF NOT EXISTS " + TABLE_DETAIL + "(" +
DETAIL_COL_NOPO + " TEXT " +
"," + DETAIL_COl_PRODCD + " REAL " +
"," + DETAIL_COl_QTYPO + " REAL" +
"," + DETAIL_COL_HRGBELI + " REAL" +
"," + DETAIL_COL_DISC1 + " REAL " +
"," + DETAIL_COL_DISC2 + " REAL" +
");";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(HEADER_CRTSQL);
db.execSQL(DETAIL_CRTSQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
public long insertHeader(String nopo, double qtypo, double gross, double disc1, double disc2) {
ContentValues cv = new ContentValues();
cv.put(HEADER_COL_NOPO,nopo);
cv.put(HEADER_COL_QTYPO, qtypo);
cv.put(HEADER_COL_GROSS, gross);
cv.put(HEADER_COL_DISC1, disc1);
cv.put(HEADER_COL_DISC2,disc2);
return this.getReadableDatabase().insert(TABLE_HEADER,null,cv);
}
public long insertDetail(String nopo, double prodcd, double qtypo, double hrgbeli, double disc1, double disc2) {
ContentValues cv = new ContentValues();
cv.put(DETAIL_COL_NOPO,nopo);
cv.put(DETAIL_COl_PRODCD,prodcd);
cv.put(DETAIL_COl_QTYPO,qtypo);
cv.put(DETAIL_COL_HRGBELI,hrgbeli);
cv.put(DETAIL_COL_DISC1,disc1);
cv.put(DETAIL_COL_DISC2,disc2);
return this.getWritableDatabase().insert(TABLE_DETAIL,null,cv);
}
public Cursor getAllDetailsWithDisc1AddedToDisc2() {
return this.getWritableDatabase().query(
TABLE_DETAIL,new String[]{
DETAIL_COL_NOPO,
"round(" + DETAIL_COL_DISC1 +" + " + DETAIL_COL_DISC2 + ",2) AS " + DETAIL_COl_PRODCD,
DETAIL_COl_QTYPO,
DETAIL_COL_HRGBELI,
DETAIL_COL_DISC1,
DETAIL_COL_DISC2,
"coalesce((" + DETAIL_COL_DISC1 + " / (" + DETAIL_COL_DISC1 + "+" + DETAIL_COL_DISC2 + ") * 100),0) AS disc1_percent",
"coalesce((" + DETAIL_COL_DISC2 + " / (" + DETAIL_COL_DISC1 + "+" + DETAIL_COL_DISC2 + ") * 100),0) AS disc2_percent",
},
null,null,null,null,null);
}
}
这样一些插入数据的活动代码(沿着问题中提供的数据行(可能存在拼写错误)):-
public class MainActivity extends AppCompatActivity {
DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = DBHelper.getInstance(this);
db.insertHeader("po-0104-2023-02-00119",15.00,20000,4000,485);
db.insertHeader("po-9999-2023-02-00119",21.00,50000,9000,999);
db.insertDetail("po-0104-2023-02-00119",103115,5.00,20000.00,0.00,0.00);
db.insertDetail("po-0104-2023-02-00119",4911021,5.00,10000.00,5.00,0.00);
db.insertDetail("po-0104-2023-02-00119",102894,5.00,10000.00,3.00,1.00);
DatabaseUtils.dumpCursor(db.getAllDetailsWithDisc1AddedToDisc2());
}
}
运行时数据被插入并且可以看到。通过 App Inspection,将是:-
和:-
提取物又名游标,为简洁起见,只是转储到日志中:-
023-02-23 15:25:12.388 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@98fd0f5
2023-02-23 15:25:12.388 I/System.out: 0 {
2023-02-23 15:25:12.389 I/System.out: pod_nopo=po-0104-2023-02-00119
2023-02-23 15:25:12.389 I/System.out: pod_prodcd=0
2023-02-23 15:25:12.389 I/System.out: pod_qtypo=5
2023-02-23 15:25:12.389 I/System.out: pod_hrgbeli=20000
2023-02-23 15:25:12.389 I/System.out: pod_disc1=0
2023-02-23 15:25:12.389 I/System.out: pod_disc2=0
2023-02-23 15:25:12.389 I/System.out: disc1_percent=0
2023-02-23 15:25:12.389 I/System.out: disc2_percent=0
2023-02-23 15:25:12.389 I/System.out: }
2023-02-23 15:25:12.389 I/System.out: 1 {
2023-02-23 15:25:12.389 I/System.out: pod_nopo=po-0104-2023-02-00119
2023-02-23 15:25:12.389 I/System.out: pod_prodcd=5
2023-02-23 15:25:12.389 I/System.out: pod_qtypo=5
2023-02-23 15:25:12.389 I/System.out: pod_hrgbeli=10000
2023-02-23 15:25:12.389 I/System.out: pod_disc1=5
2023-02-23 15:25:12.389 I/System.out: pod_disc2=0
2023-02-23 15:25:12.389 I/System.out: disc1_percent=100
2023-02-23 15:25:12.389 I/System.out: disc2_percent=0
2023-02-23 15:25:12.389 I/System.out: }
2023-02-23 15:25:12.389 I/System.out: 2 {
2023-02-23 15:25:12.389 I/System.out: pod_nopo=po-0104-2023-02-00119
2023-02-23 15:25:12.389 I/System.out: pod_prodcd=4
2023-02-23 15:25:12.390 I/System.out: pod_qtypo=5
2023-02-23 15:25:12.390 I/System.out: pod_hrgbeli=10000
2023-02-23 15:25:12.390 I/System.out: pod_disc1=3
2023-02-23 15:25:12.390 I/System.out: pod_disc2=1
2023-02-23 15:25:12.390 I/System.out: disc1_percent=75
2023-02-23 15:25:12.390 I/System.out: disc2_percent=25
2023-02-23 15:25:12.390 I/System.out: }
2023-02-23 15:25:12.390 I/System.out: <<<<<