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 7902841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:36:24+00:00 2026-06-03T09:36:24+00:00

I have an sqlite table log that looks like this: ID p_id viewer ———————-

  • 0

I have an sqlite table “log” that looks like this:

ID     p_id     viewer
----------------------
1      1        100
2      1        200
3      1        300
4      3        550
5      3        230
6      5        420
7      2        320
8      2        203
9      9        10
10     9        55

And I want to get the average viewers from each p_id. That’d be

SELECT avg(viewer) FROM log GROUP BY p_id

But I want to treat p_id 1 and 5 as the same, so I’d get the average viewers of p_id 1 and 5 combined. How do I do that? Note that the table is much larger, and I need to treat two p_id’s as the same multiple times. Can I still do a “group by” to achieve this, or are there other ways?

  • 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-06-03T09:36:26+00:00Added an answer on June 3, 2026 at 9:36 am

    I tried this and it works with mySQL at least.

    SELECT avg(viewer) FROM log GROUP BY CASE p_id WHEN 5 THEN 1 ELSE p_id END;
    

    Edit: When using an alias-table named ‘aliases’ with the fields ‘alias_from_id’ and ‘aliased_as’:

    SELECT
    CASE ISNULL((SELECT aliased_as FROM aliases WHERE (alias_from_id = log.pid))) 
    WHEN 1 THEN log.pid ELSE aliased_as END AS the_id,
    SUM(value) AS value_sum
    FROM log
    LEFT JOIN aliases ON (alias_from_id = pid)
    GROUP BY
    CASE ISNULL((SELECT aliased_as FROM aliases WHERE (alias_from_id = log.pid)))
    WHEN 1 THEN log.pid ELSE aliased_as END 
    

    I’ve tested this with mySQL and it works like a charm. It might be possible to simplify this SQL-query a bit but this is the best I can do at the moment 🙂

    Edit2: Changed ISNULL to the corresponding SQLite IFNULL function

    SELECT
    IFNULL((SELECT aliased_as FROM aliases WHERE (alias_from_id = log.pid)), log.pid) AS the_id,
    SUM(value) AS value_sum
    FROM log
    LEFT JOIN aliases ON (alias_from_id = pid)
    GROUP BY
    IFNULL((SELECT aliased_as FROM aliases WHERE (alias_from_id = log.pid)), log.pid)
    

    I don’t know if SQLite supports the AS keyword, if it doesn’t then just remove that keyword – the functionality should be the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an sqlite database on android created like this: sqlite> .schema CREATE TABLE
i have a table in sqlite database like following. id status ============= x 0
i am using this code to insert names to table,i have a problem that
Okay, so I have a (SQLite) table called log wherein one column (time) is
I have a SQLite table called posts . An example is shown below. I
So I have a local SQLite table breadcrumbs being created on the Android device.
I have a boolean (BOOL) type field in the SQLite table. In the SubSonic
I have a table in SQLite: CREATE TABLE test_results(timestamp TEXT, npass INTEGER, nfails INTEGER)
I have a DataTable which I want to save to a SQLite Database Table.
I have a table in SQLite database of Android which has a column RANK

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.