LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2009, 12:28 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
SQL: do math in query


Hi,

I have a table with a list of votes.
Each vote has an item id (the item that the votes refers to) and a value (either 1 or -1).

I want to select all votes, group by item_id, and count all values together.

For example:

There's 51 votes for item_id=1 with value=1
There's 50 votes for item_id=1 with value=-1
There's 5 votes for item_id=2 with value=1
There's 2 votes for item_id=2 with value=-1
There's 4 votes for item_id=3 with value=-1

I want a query that gives me this:

Code:
item_id .. vote_sum

.. 2 ........ 3
.. 1 ........ 1
.. 3 ....... -4
 
Old 04-25-2009, 01:23 AM   #2
giftlftr_23
Member
 
Registered: Oct 2008
Location: I live where I'm currently standing on
Distribution: fedora, opensuse
Posts: 34

Rep: Reputation: 17
You can use the count() function of SQL.
For example:

SELECT COUNT(item_id) AS votesum, item_id FROM table_votes WHERE value=1 GROUP BY item_id

Hope it helps.
 
Old 04-25-2009, 01:41 AM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Quote:
Originally Posted by giftlftr_23 View Post
You can use the count() function of SQL.
For example:

SELECT COUNT(item_id) AS votesum, item_id FROM table_votes WHERE value=1 GROUP BY item_id

Hope it helps.
No, this is not what I was looking for. This will count all positive votes. I wanted a way to do the math (all positive votes minus all negative votes) inside the SQL statement.

Like "count(*) where value=1" - "count(*) where value=-1"

As I wrote above the result should be:

Code:
item_id .. vote_sum

.. 2 ........ 3 (5 - 2)
.. 1 ........ 1 (51 - 50)
.. 3 ....... -4 (0 - 4)
(I added the math to clarify)
 
Old 04-25-2009, 01:50 AM   #4
aspire1
Member
 
Registered: Dec 2008
Distribution: Ubuntu
Posts: 62

Rep: Reputation: 23
Use the sum() function.
 
Old 04-25-2009, 02:47 AM   #5
giftlftr_23
Member
 
Registered: Oct 2008
Location: I live where I'm currently standing on
Distribution: fedora, opensuse
Posts: 34

Rep: Reputation: 17
The sum will do job. try:

SELECT item_id, SUM(value) AS vote_sum FROM table_votes WHERE item_id=id GROUP BY item_id
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sql query issue talat Linux - Server 3 10-18-2007 08:59 AM
Query SQL error gabsik Linux - Software 37 07-22-2006 02:34 AM
SQL query performance smaida Programming 6 06-08-2005 09:22 AM
Massive SQL Query patpawlowski Programming 7 03-05-2004 04:24 PM
SQL Query question oulevon Programming 7 01-16-2004 01:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration