SQl摘要表:一列相对于另一列的部分和

时间:2018-08-01 14:41:25

标签: sql sql-server

我想基于一个看起来像这样的表在SQL中创建一个表

表1

row    cost
1       25
2       15
3       10
4       12
...     ...

在新表中,每一行的SUM列是其行小于或等于新表中所有行的所有成本的总和。

表2

row    SUM
1       25 
2       40 is the sum of row 1 and 2 from Table 1
3       50 is the sum of row 1, 2 and 3 from Table 1
4       62 is the sum of row 1, 2, 3 and 4 from Table 1
...     ...

您是否知道如何在不创建大量联接的情况下以最有效的方式执行此操作?

1 个答案:

答案 0 :(得分:0)

您需要累计金额:

public void performFirebaseLogin(GoogleSignInAccount acct, final Context context, final LoginSPrefRepositoryImpl loginSPrefRepositoryImpl) {
        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        final GoogleAccountCredential googleCredential = GoogleAccountCredential.usingOAuth2(
                    context, Collections.singleton(Scopes.PROFILE));
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener((Activity) context, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {

                            FirebaseUser user = mAuth.getCurrentUser();
                            if (user != null) {
                            //here i am calling profile detail of user
                                new GetProfileDetails(user, googleCredential,loginSPrefRepositoryImpl).execute();
                            } else {
                                    onLoginListener.onFailure("Login Failed");
                            }
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                        }
                    }
                });
    }


//here i am retrieving user's information  

class GetProfileDetails extends AsyncTask<Void, Void, Person> {

        private PeopleService ps;
        private int authError = -1;
        private FirebaseUser user;
        private LoginSPrefRepositoryImpl loginSPrefRepositoryImpl;

        GetProfileDetails(FirebaseUser user, GoogleAccountCredential credential, LoginSPrefRepositoryImpl loginSPrefRepositoryImpl) {

            this.loginSPrefRepositoryImpl = loginSPrefRepositoryImpl;
            this.user = user;
            credential.setSelectedAccount(
                    new Account(user.getEmail(), "com.google"));

            HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
            JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

            ps = new PeopleService.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                    .setApplicationName("Google Sign In Quickstart")
                    .build();
        }

        @Override
        protected Person doInBackground(Void... params) {
            Person meProfile = null;
            try {
                meProfile = ps
                        .people()
                        .get("people/me")
                        .setPersonFields("genders")
                        .execute();
            } catch (UserRecoverableAuthIOException e) {
                e.printStackTrace();
                authError = 0;
            } catch (GoogleJsonResponseException e) {
                e.printStackTrace();
                authError = 1;
            } catch (IOException e) {
                e.printStackTrace();
                authError = 2;
            }
            return meProfile;
        }

        @Override
        protected void onPostExecute(Person meProfile) {
            //mainAct.printBasic();
            if (authError == 0) { //app has been revoke, re-authenticated required.
                //mainAct.reqPerm();
            } else if (authError == 1) {
                Log.w(TAG, "People API might not enable at" +
                        " https://console.developers.google.com/apis/library/people.googleapis.com/?project=<project name>");
            } else if (authError == 2) {
                Log.w(TAG, "API io error");
            } else {
                if (meProfile != null) {
                    // Log.d("kkkk", "" + meProfile.getGenders());
                    JSONArray jsonArray = new JSONArray(meProfile.getGenders());
                    try {
                        JSONObject jsonObject = jsonArray.getJSONObject(0);
                        String gender = jsonObject.getString("formattedValue");
                        loginSPrefRepositoryImpl.isLoggedIn();
                        loginSPrefRepositoryImpl.setGender(gender);
                        onLoginListener.onSuccess(user);
                    } catch (JSONException e) {
                        e.printStackTrace();
                        onLoginListener.onFailure("Something went wrong !");
                    }
                }
            }
        }
    }