2

I have an SQLite table where I have a list of messages:

to  | from
==========
9999  ME
9999  ME
ME    9999
ME    8888

The result of the query should be in the following format:

number(number of records matching)

9999 (3)
8888 (1)

How can I write a query to give me this resultset? I'm working with Android.

2
  • How is Android relevant to the question? Can to and from be the same in one record? Would that count as 1 or 2? Commented Oct 6, 2011 at 23:21
  • Error messages in comments indicate sqlite, not postgresql. tag wrong? Commented Oct 7, 2011 at 21:40

4 Answers 4

2

Maybe you can do something using this http://www.postgresql.org/docs/current/static/functions-conditional.html

I guess something like this (didn't run it):

SELECT number, count(number) as `Count` FROM (
  SELECT 
    CASE
      WHEN to = 'ME' THEN from
      ELSE to
    END AS number
    FROM table
  ) GROUP BY number;
Sign up to request clarification or add additional context in comments.

Comments

2
SELECT value, count(*)  
FROM ( 
   SELECT to as value
   FROM your_table

   UNION ALL

   SELECT "from" as value
   FROM your_table
) t
GROUP BY value

To filter out unwanted values for to and from use an approriate WHERE clause (ideally in the inner select to reduce the number of rows that need to be processed.

6 Comments

I changed field names and the query is almost ok; it returns 8888(1), 9999(3) *AND ME(4)*`
@WeContest: it's a column alias: postgresql.org/docs/current/static/…
@a_horse_with_no_name Ok, and what should I put as "to" and "from"??
@WeContest: I have no idea what you are talking about.
@a_horse_with_no_name If I execute your query replacing your_table with my table name, I get the following error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to as value FROM table_name UNION ALL SELECT "from" as value ' at line 3
|
1
select  value, sum (count) as count, from 
(
    select count(*) as count , "from" as value from your_table 
    group by "from"
   union all 
   select count(*) as count , "to" as value from your_table 
   group by "to" 
) t
group by t.value

Thanks @a_horse_with_no_name & @razvi!

9 Comments

The [] brackets are invalid SQL syntax. What are they supposed to do? And it should be UNION ALL otherwise the duplicates will be removed.
I guess they were put there so that they won't be considered as keywords (like the from field) ... maybe back-quotes instead of bracket will work.
@a_horse_with_no_name the [] were my way of escaping the reserved words (from) in the column names. That's actually valid in SQL server, I thought it might be valid in posgresql. ;) thanks for the union catch, will ammend my answer immediately.
@razvi exactly! thanks for the back quote tip, I can't do them from my cell but I will put double quotes at least for now. Will ammend my answer.
@Razvi: backticks are invalid in PostgreSQL as well (they are a non-standard behaviour of MySQL)
|
0

If you are only looking to count the occurrences of numeric values this should work.

Use concatenation ("||") to form the output that you want.

SELECT to_from || ' (' || count(to_from) || ')' FROM (
  SELECT
    CASE 
      WHEN to ~ '^[0-9]+$' THEN to
      WHEN from ~ '^[0-9]+$' THEN from
    END to_from
  FROM
    testing
) a
GROUP BY to_from

Results in

 ?column? 
----------
 9999 (3)
 8888 (1)
(2 rows)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.