473,774 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8094
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*****@hotmai l.com> wrote in message
news:39******** *************** ***@posting.goo gle.com...
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*****@hotmai l.com> wrote in message news:<40******* ***@news.estpak .ee>...
"Jim" <ji*****@hotmai l.com> wrote in message
news:39******** *************** ***@posting.goo gle.com...
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
3572
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? DB2 Novice
0
5386
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 info just entered for a particular record and print out multiple pages of forms, each page having a different image as background.
0
645
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 change something (for example, add a space) in the HeaderControl.ascx, it will work correctly. Here is the specific error message: ============== Compilation Error Description: An error occurred during the compilation of a
2
2822
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 datatable foreach (System.Data.DataRow drReportGroupsDetails in tableReportGroupsDetails.Rows) { // Get the needed values from the datatable string reportName = (string)drReportGroupsDetails; string reportPath = (string)drReportGroupsDetails;
1
2501
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
2502
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 statements using a single object be done. thanks in advance, arthy
0
2337
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> #include<conio.h> int top; int bot; void main() {
10
13093
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 using a account. How to do this? I had got a idea and implemented. 1. When a user logs in storing the username, ip, login time to db.
3
1897
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
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.