Visit Our UNIX and Linux User Community


A different kind of counting in SQL


 
Thread Tools Search this Thread
Top Forums Programming A different kind of counting in SQL
# 1  
Old 10-23-2009
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  
Old 10-23-2009
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

# 3  
Old 10-24-2009
Thanks, that is actually very straightforward.

Previous Thread | Next Thread
Test Your Knowledge in Computers #890
Difficulty: Easy
In sed, the REGEX dot (.) matches exactly one character.
True or False?

8 More Discussions You Might Find Interesting

1. Linux

Kind of reverse engineering

Hi everyone, I'm a linux novice , in a training purpose i have to reconstruct under windows an application running under fedora 14, the application communicates with an STB device through RS232 in a first time and then through ethernet for firmware loading purpose. All what i know is how to run... (2 Replies)
Discussion started by: nidal
2 Replies

2. Cybersecurity

What kind of hacking is going on here??

Connecting to the Internet with OpenVPN, the connection fails. Rerunning openvpn works second time round but the install is hacked at that point (e.g., a rogue 'java-security' update tries to install itself on 'yum update', yum however spots this and rejects the download, other basic things start... (3 Replies)
Discussion started by: GSO
3 Replies

3. Infrastructure Monitoring

sed help,,kind of urgent!!

Hello All, My problem is: I want to replace a line from a file with sed. The first word in that line is always the same in every server. The second line is server model, which of course will vary from platform to platform and I need to leave that word as it is. After the second word, I need to... (3 Replies)
Discussion started by: solaix14
3 Replies

4. Solaris

How to check the kind of file

Hi all, I'm working on Solaris 10 and I have to install some packets in which there are files of different kind. In this situation I need to know a command of UNIX/Solaris to check the kind of file. For example how can I know if the file has ISO or ASCII or BER or HEX format? Thank you... (4 Replies)
Discussion started by: Sunb3
4 Replies

5. UNIX for Dummies Questions & Answers

how to determine which kind of unix is used

how can i determine which type of unix (solaris ,AIX,HP-UX...) is installed on the machine that i am working? (2 Replies)
Discussion started by: gfhgfnhhn
2 Replies

6. UNIX for Dummies Questions & Answers

Kind of weird question

I recently purchased a book titled Hacking: The Art of Exploitation. When I got it home I read the preface and found out that i shouldnt have bought it. It says the code examples in this book were done on an x86 based computer (I have a mac). Is there anything I can do to make my mac run similar to... (2 Replies)
Discussion started by: Cyberaxe
2 Replies

7. UNIX for Dummies Questions & Answers

what kind of UNIX

ok, so i want to figure out what type of UNIX i have and in this book im reading about it, it says that i can figure out what type i have by typing the command uname in the prompt. So i did this and it came up saying Darwin?? is that part of System V UNIX or BSD or do i have LINUX? if anyone can... (2 Replies)
Discussion started by: hiei
2 Replies

8. UNIX for Dummies Questions & Answers

Some kind of message command

Hello All, hope someone here can help me with this. I am a new unix system administrator on the HP-UX machine. Every night, our operators back up our file system using one tape but as of recently, our files have gotten bigger and it now requires 2 tapes for a complete backup. Since the operators... (3 Replies)
Discussion started by: sanjit
3 Replies

Featured Tech Videos