The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed help,,kind of urgent!! solaix14 Shell Programming and Scripting 3 03-10-2009 02:12 PM
A Different Kind of DoS Attack iBot IT Security RSS 0 09-17-2008 11:30 AM
Kind of knapsack problem ngungo Shell Programming and Scripting 3 11-08-2007 09:35 AM
What kind of Linux for the newbies? sanlen UNIX for Dummies Questions & Answers 2 10-01-2007 01:38 PM
what kind of UNIX hiei UNIX for Dummies Questions & Answers 2 04-12-2004 08:15 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-23-2009
figaro figaro is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 268
A different kind of counting in SQL

I am looking to do a count on a database table where some of the elements count double. Say the data is as follows:
Code:
id value
1 X
2 Y
3 X
4 X
5 Y
A regular count (SELECT value, COUNT(*) FROM data GROUP BY value) would yield:
Code:
X 3
Y 2
However, Y happens to count double so the answer should be:
Code:
X 3
Y 4
How do I achieve this in SQL? I am using MySQL 5.2.

Last edited by figaro; 10-23-2009 at 06:51 PM.. Reason: Removed typo
  #2 (permalink)  
Old 10-23-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,057
Hi figaro.

Not sure if this is the best answer you'll get, but not sure how you'd do that in a simple group by statement.

Code:
SELECT value, COUNT(*) FROM data where value != 'Y' group by value
union
SELECT value, COUNT(*)*2 FROM data where value = 'Y' group by value

V   COUNT(*)
- ----------
X          3
Y          4
Bits Awarded / Charged to scottn for this Post
Date User Comment Amount
10-24-2009 figaro N/A 1,000
  #3 (permalink)  
Old 10-24-2009
figaro figaro is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 268
Thanks, that is actually very straightforward.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0