Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7682705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:37:45+00:00 2026-05-31T18:37:45+00:00

I have a database with the columns: id, pdate, pvalue1, pvalue2. First I make

  • 0

I have a database with the columns: id, pdate, pvalue1, pvalue2. First I make a query with a cursor:

Cursor c = ourDatabase.query(DATABASE_TABLE, new String[] { "_id","pdate","pvalue1","pvalue2"},
                    "pdate >= ? AND pdate <= ?",  new String[] { datefrom, dateto }, null, null, null);   

This gives me some rows, for example if pdate = 20120318, then pvalue1 = 58, pvalue2=29. These are strings so I can give a value of “XX” to pvalue2. I would like to sum the pvalue1 between the given datefrom and dateto and group them by pdate where pvalue2 = XX. My problem is that I cannot put this condition into the query (with that its working, like “pvalue2 = XX”..), because I need the other datas too.

if (c.moveToFirst())
{
     do{
         if (c.getString(3).equals("XX")){
         Log.i("XX", c.getString(1) + " " + c.getString(2)) + " " + c.getString(3));

          }
         else {
         Log.i("NotXX",  c.getString(1) + " " + c.getString(2)) + " " + c.getString(3));

          }

     while (c.moveToNext());
     }
}

It is okay so far, so I can log the datas with this where pvalue2 = XX and NotXX and get something like this:
(pdate,pvalue1,pvalue2) 20120317,48,29;——;20120317,21,54;——-20120317,11,XX;—–20120318,79,71;——-20120318,21,XX;

What I would like to do?

First: Grouping the sums (pvalue1) by pdate and indicate it if pvalue2 is XX or notXX, so somethnig like this:
20120317,NotXX,69 (since 48+21=69) ——– 20120317,XX,11 ——– 20120318,NotXX,79 ——– 20120318,XX,21

After this I would like to substract the XX sum from the NotXX sum for every day. I would like to get:
20120317,58 (since 69-11) ——- 20120318,58 (since 79-21)

How sould I do this?
Thank you very much in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:37:46+00:00Added an answer on May 31, 2026 at 6:37 pm

    My problem is that I cannot put this condition into the query

    You are probably wrong. You can add something like (syntax may contain errors)

    “select sum(select pdate from DATABASE_TABLE where pdata > x and pdate < y) as sum”

    to the projection argument and you get that result as a column named sum. The only problem is that there is no support for ? in projection (at least I have not tried it but I guess it would not work)

    If that’s not what you want then there is very likely a different way. SQLite is very powerful.

    Edit:

    Would that be what you want? It’s not done in SQL but it would print the sum you want for each day.

    Cursor c = ourDatabase.query(DATABASE_TABLE, new String[] { "_id","pdate","pvalue1","pvalue2"},
            "pdate >= ? AND pdate <= ?",  new String[] { datefrom, dateto }, null, null, "pdate");
    boolean first = true;
    if (c != null) {
        String currentDate = null;
        int sum = 0;
        while (c.moveToNext()) {
            String date = c.getString(1);
            int value1 = c.getInt(2);
            String value2 = c.getString(3);
            if (!date.equals(currentDate)) {
                if (!first) {
                    Log.d("TAG", "The result for " + currentDate + " is: " + sum);
                } else {
                    Log.d("TAG", "Date has changed, but we don't have data yet.");
                }
                first = false;
                currentDate = date;
                sum = 0;
            }
            if ("XX".equals(value2)) {
                Log.d("TAG", "new line: " + date + ", " + value1 + ", " + value2 + " -");
                sum -= value1;
            } else {
                Log.d("TAG", "new line: " + date + ", " + value1 + ", " + value2 + " +");
                sum += value1;
            }
        }
        if (!first) {
            Log.d("TAG", "The last result: " + currentDate + " is: " + sum);
        }
        c.close();
    }
    

    Edit2: This might work when you want it done by the database.

    Cursor c = ourDatabase.rawQuery(
            "SELECT pdate, sum(sum2) AS sum1 FROM " +
            "(" +
            "   SELECT pdate, pvalue1, pvalue2, -sum(pvalue1) AS sum2 " +
            "      FROM " + DATABASE_TABLE +
            "      WHERE pvalue2='XX' GROUP BY pdate" +
            "   UNION " +
            "   SELECT pdate, pvalue1, pvalue2, sum(pvalue1) AS sum2 " +
            "      FROM " + DATABASE_TABLE + 
            "      WHERE pvalue2!='XX' GROUP BY pdate" +
            ") " +
            "   WHERE pdate>=? AND pdate<=? " +
            "   GROUP BY pdate",
            new String[] { datefrom, dateto });
    if (c != null) {
        while (c.moveToNext()) {
            String date = c.getString(0);
            int value1 = c.getInt(1);
            Log.d("TAG", "The result for " + date + " is: " + value1);
        }
        c.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a database table with columns a, b, and c. I plan
I have one database table which contains 8 columns. One of the columns is
I Have a DataBase in my project With Table named 'ProcessData' and columns named
I have a Database (SQL Server) with table named 'ProcessData' and columns named 'Process_Name'
i have one database, and it contains some columns. My requirement is that how
I have 3 database tables, all of them have the same 5 columns. They
I have an old database (with terribly named tables and columns) and an entity
Hi there in my database I have 3 columns, is_contract, is_permenant and is_temporary. Within
I have 3 columns in Oracle database having table mytable and i want records
I have a table in my database representing releases of software; the relevant columns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.