473,657 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem selecting count, comparing 2 fields in same table

I have the following table structure.

group1 group2 group1_result group2_result
'One' 'Two' 3 2
'One' 'Two' 3 1
'One' 'Two' 2 5
'One' 'Two' 4 1
'One' 'Two' 0 5

I need to sum up the number of times 'One' is greater than 'Two', and
vice-versa. For example, the result I would like to achieve is as
follows.

group1 group2 group1_total group2_total
'One' 'Two' 3 2

I'm using the following SQL statement, but I get 5 rows returned,
giving me a '1' or '0' for each row.

select group1, group2
,sum(CASE WHEN group1_result > group2_result THEN 1 ELSE 0 END)
,sum(CASE WHEN group2_result > group12_result THEN 1 ELSE 0 END)
FROM table1
GROUP BY group1, group2

Any help would be greatly appreciated.
Jul 20 '05 #1
1 4827
The query you posted gives the result you say you want!

CREATE TABLE Table1 (group1 CHAR(3) NOT NULL, group2 CHAR(3) NOT NULL,
group1_result INTEGER NOT NULL, group2_result INTEGER NOT NULL /*, PRIMARY
KEY ??? */)

INSERT INTO Table1 VALUES ('One', 'Two', 3,2)
INSERT INTO Table1 VALUES ('One', 'Two', 3,1)
INSERT INTO Table1 VALUES ('One', 'Two', 2,5)
INSERT INTO Table1 VALUES ('One', 'Two', 4,1)
INSERT INTO Table1 VALUES ('One', 'Two', 0,5)

SELECT group1, group2,
SUM(CASE WHEN group1_result > group2_result THEN 1 ELSE 0 END),
SUM(CASE WHEN group2_result > group1_result THEN 1 ELSE 0 END)
FROM Table1
GROUP BY group1, group2

What is the primary key of Table1? Did you leave out some relevant
column(s)?

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2

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

Similar topics

7
1868
by: Rodney King | last post by:
Hi, I have developed an ASP page which dynamically displays a list of checkbox options based on a SQL statement. Here is my code: <div style="OVERFLOW:auto; Height: 150px"> <table> <% dim adOpenForwardOnly, adLockReadOnly dim adCmdTable, ctr, checkboxID
6
58175
by: Nimesh | last post by:
I need to find customer's names that repeat / occur more than once in the same table. I have treid many options and I have tried comparing the column to itself but Oracle gives me an error. SELECT a.customer_name, b.customer_name FROM dtb_customer a, dtb_customer b where a.dtb_customer = b.dtb_customer and b.customer > 1
4
1659
by: Catherine Jo Morgan | last post by:
This is still for the db for the recreational tree climbing business. I thought I'd solved the problem of clients often being part of the same household, with a tblHouseholds, with Household as a FK in the Persons table. The main reason for a Households table is to give an appropriate mailing name for the household address, e.g. "The Brown Family" or "Jim & Jean Smith." This looks good to me till I consider when and how the household...
5
4141
by: Gary Cobden | last post by:
I have a problem with the following code, which leaves an instance of Excel visible in Task Manager. By a process of elimination I have got it down to the fact that something in the DoCmd.Transfer Spreadsheet line is holding on to an Excel reference somewhere - can anybody assist (if I comment this line out, the instance is released, and not visible in Task Manager). Private Sub btnLoadData_Click() Dim xlApp2 As Object
22
12463
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient moConn.Mode = adModeReadWrite' or using default...same result
7
1868
by: wade.wall | last post by:
Hi all, I am having a problem appending data to an existing table. I have two tables with identical fields and I want to append the data from one table (T2) to the first (T1). T1 has 136 records and T2 has 209, for a difference of 73 records. When I run the append query, only 72 records are selected from T2, and there doesn't seem to be a pattern as to why the records selected were selected. They aren't the last 72 records or the...
9
2771
by: QCLee | last post by:
Sir can you help me to transfer my Access Query to MS excel? i have a command button on the form to export the parameter query named "HVACWindwardQuery" to excel spreadsheet and i got the codes from searching on the internet and books but the problem is when i run the command button "Export" it just only open the Blank Spreadsheet, no data at all that it came from my query named "HVACWindwardQuery" and there's an error on it...
2
3075
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography section: Discography --------------------- DiscID
58
8064
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also the parts section an i seem to be having trouble. When i try to insert into the parts section i get the error Invalid character value for cast specification. But not sure what i am doing wrong. Here is what i am using to insert. All the sections...
0
8399
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8312
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,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8504
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
7337
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
6169
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
4159
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
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.