473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Multiple counts using grouping, ordering, and joins

Jim
I'm having trouble doing multiple counts with groups, ordering, and
joins. Can someone help me out? Below is two table samples and what
I'm trying to get my output to be:

TABLEA
ID CHIP
----------- ----------
1 Lays
1 Ruffles
1 Ruffles
1 Ruffles
1 Doritos
1 Doritos
2 Lays
2 Lays
2 Doritos
3 Ruffles
TABLEB
ID NAME
----------- ----------
1 Jim
2 Bob
3 MISSY
What I'm hoping to get is the following rows:

Bob 2 0 1
Jim 1 3 2
MISSY 0 0 1
This is the name, count(lays), count(ruffles), count(doritos) ordered
by name. I thought it would be rather easy but can't seem to get it
for some reason. Any help would be greatly appreciated!
Nov 12 '05 #1
4 8067
Jim wrote:
I'm having trouble doing multiple counts with groups, ordering, and
joins. Can someone help me out? Below is two table samples and what
I'm trying to get my output to be:

TABLEA
ID CHIP
----------- ----------
1 Lays
1 Ruffles
1 Ruffles
1 Ruffles
1 Doritos
1 Doritos
2 Lays
2 Lays
2 Doritos
3 Ruffles
TABLEB
ID NAME
----------- ----------
1 Jim
2 Bob
3 MISSY
What I'm hoping to get is the following rows:

Bob 2 0 1
Jim 1 3 2
MISSY 0 0 1
This is the name, count(lays), count(ruffles), count(doritos) ordered
by name. I thought it would be rather easy but can't seem to get it
for some reason. Any help would be greatly appreciated!


SELECT b.name,
( SELECT COUNT(*) FROM tablea AS a
WHERE a.id = b.id AND a.chip = 'Lays' ),
( SELECT COUNT(*) FROM tablea AS a
WHERE a.id = b.id AND a.chip = 'Ruffles' ),
( SELECT COUNT(*) FROM tablea AS a
WHERE a.id = b.id AND a.chip = 'Doritos' )
FROM tableb AS b
ORDER BY b.name
or:

SELECT b.name,
SUM(CASE WHEN a.chip = 'Lays' THEN 1 ELSE 0 END),
SUM(CASE WHEN a.chip = 'Ruffles' THEN 1 ELSE 0 END),
SUM(CASE WHEN a.chip = 'Doritos' THEN 1 ELSE 0 END)
FROM tableb AS b JOIN tablea AS a ON
( a.id = b.id )
ORDER BY b.name
Most probably, you have a whole bunch of other ways to express your query.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #2

"Jim" <ji*****@hotmail.com> wrote in message
news:39**************************@posting.google.c om...
What I'm hoping to get is the following rows:

Bob 2 0 1
Jim 1 3 2
MISSY 0 0 1
This is the name, count(lays), count(ruffles), count(doritos) ordered
by name. I thought it would be rather easy but can't seem to get it
for some reason. Any help would be greatly appreciated!


One way would be following:

SELECT b.name,
SUM(CASE WHEN a.chip = 'Lays' THEN 1 ELSE 0 END),
SUM(CASE WHEN a.chip = 'Ruffles' THEN 1 ELSE 0 END),
SUM(CASE WHEN a.chip = 'Doritos' THEN 1 ELSE 0 END)
FROM TableA a, TableB b
WHERE a.id = b.id
GROUP BY b.name

HTH,
Tramm66
Nov 12 '05 #3
Jim
"Tramm66" <tr*****@hotmail.com> wrote in message news:<40**********@news.estpak.ee>...
"Jim" <ji*****@hotmail.com> wrote in message
news:39**************************@posting.google.c om...
What I'm hoping to get is the following rows:

Bob 2 0 1
Jim 1 3 2
MISSY 0 0 1
This is the name, count(lays), count(ruffles), count(doritos) ordered
by name. I thought it would be rather easy but can't seem to get it
for some reason. Any help would be greatly appreciated!


One way would be following:

SELECT b.name,
SUM(CASE WHEN a.chip = 'Lays' THEN 1 ELSE 0 END),
SUM(CASE WHEN a.chip = 'Ruffles' THEN 1 ELSE 0 END),
SUM(CASE WHEN a.chip = 'Doritos' THEN 1 ELSE 0 END)
FROM TableA a, TableB b
WHERE a.id = b.id
GROUP BY b.name

HTH,
Tramm66


Thanks... I ended up using

select rtrim(name), count(case when chip='Lays' then 1 end) as Lays,
count(case when chip='Ruffles' then 1 end) as Ruffles, count(case when
chip='Doritos' then 1 end) as Doritos from tableb, tablea where
tableb.id = tablea.id group by name order by 1

Are any of these more efficient than the other? Thanks!
Nov 12 '05 #4
select rtrim(name), count(case when chip='Lays' then 1 end) as Lays,
count(case when chip='Ruffles' then 1 end) as Ruffles, count(case when
chip='Doritos' then 1 end) as Doritos from tableb, tablea where
tableb.id = tablea.id group by name order by 1

Are any of these more efficient than the other? Thanks!


Try an "explain" on each of the potential candidates and see which one
performs the least work. Explain is documented in SQL Reference V2.

Phil Sherman

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: DB2 Novice | last post by:
I am trying to use DB2 Control Centre (version 8.2) to load one flat file into multiple tables. However, I don't see the options in Control Centre that allows that. Anyone knows how to do this?...
0
by: ghadley_00 | last post by:
MS Access Create form / report with multiple pages using different background images Hi, Would like to have users fill out a multipage form, and then click a print button, which pulls up the...
0
by: jhe | last post by:
I have a user control HeaderControl which was shared by multiple Asp.Net Pages (.aspx), sometimes when I make some changes to our website and restart iis, it will have the CS1595 error, but when I...
2
by: kewl | last post by:
Hi All, We have an ASP.NET 2.0 (C#) intranet application that needs to spawn multiple browsers using RegisterClientScriptBlock. Here's what we got so far: // Go thru each datarow in the...
1
by: anna_john | last post by:
How do i link to multiple pages using the same link? i would like to get an answer related to jsp or javascript
1
by: arthy | last post by:
Hi, Is it possible to execute multiple statements on to the database using a single dbconnection object.what is the drawback in using .If not possible ,then how can the execution of multiple...
0
by: raghuveer | last post by:
i want to implement multiple stacks using arrays..I am able to create ,insert and print them but not poping an element form any of the stack..This is what i have #include<stdio.h>...
10
by: shankhar | last post by:
Hi all, In my project there is a requirement. If a user logged in at a time since he/she logged out others are not allowed to loggin using the same user name. That is to avoid multiple logins...
3
by: khairulfnd | last post by:
i want to select multiple rows using a checkbox in a gridview in which it will be posted in another gridview
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.