Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Unexpected Result - 2 Columns Returning Same Value

Author  Topic 

CreativeNRG
Starting Member

44 Posts

Posted - 2008-01-22 : 13:07:41
My tables are as follows:


****** Object: Table [dbo].[Results] Script Date: 22/01/2008 18:01:09 ******/
CREATE TABLE [dbo].[Results] (
[ResultId] [int] IDENTITY (1, 1) NOT NULL ,
[TournamentId] [int] NOT NULL ,
[UserId] [int] NOT NULL ,
[Points] [int] NOT NULL ,
[Position] [int] NOT NULL ,
[Bonus1] [bit] NOT NULL ,
[Bonus2] [bit] NOT NULL ,
[Bonus3] [bit] NOT NULL ,
[Created] [smalldatetime] NOT NULL ,
[Updated] [smalldatetime] NULL
) ON [PRIMARY]
GO

/****** Object: Table [dbo].[Tournaments] Script Date: 22/01/2008 18:01:09 ******/
CREATE TABLE [dbo].[Tournaments] (
[TournamentId] [int] IDENTITY (1001, 1) NOT NULL ,
[LeagueId] [int] NOT NULL ,
[VenueId] [int] NOT NULL ,
[Date] [smalldatetime] NULL ,
[Status] [int] NOT NULL
) ON [PRIMARY]
GO

/****** Object: Table [dbo].[Venues] Script Date: 22/01/2008 18:01:09 ******/
CREATE TABLE [dbo].[Venues] (
[VenueId] [int] IDENTITY (1, 1) NOT NULL ,
[Name] [varchar] (100) COLLATE Latin1_General_CI_AS NULL ,
[RegionId] [int] NOT NULL ,
[AddressId] [int] NULL ,
[ImageFilename] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[TournamentDay] [int] NOT NULL ,
[TournamentTime] [char] (5) COLLATE Latin1_General_CI_AS NULL ,
[Website] [varchar] (100) COLLATE Latin1_General_CI_AS NULL ,
[Longitude] [varchar] (20) COLLATE Latin1_General_CI_AS NULL ,
[Latitude] [varchar] (20) COLLATE Latin1_General_CI_AS NULL ,
[Status] [int] NOT NULL ,
[Agent] [int] NULL ,
[SalesExecutive] [int] NULL ,
[Created] [smalldatetime] NOT NULL ,
[CreatedBy] [int] NULL ,
[Updated] [smalldatetime] NULL ,
[UpdatedBy] [int] NULL
) ON [PRIMARY]
GO


My query I want to return the following for each Sales Executive:

1. Total Venues from a given period(based on 'created' field
2. Total Tournaments (based on tournament id field)
3. Total Results from tournaments

Here is my query but for some reason I am getting the same values for the tournaments and results:



SELECT v.SalesExecutive, COUNT(t.TournamentId) AS Tournaments, COUNT(r.ResultId) AS Results
FROM dbo.Tournaments t INNER JOIN
dbo.Results r ON t.TournamentId = r.TournamentId RIGHT OUTER JOIN
dbo.Venues v ON t.VenueId = v.VenueId
WHERE (t.Status = 2) AND (v.Created BETWEEN CONVERT(DATETIME, '2007-12-01 00:00:00', 102) AND CONVERT(DATETIME, '2008-01-31 00:00:00', 102)) AND
(t.[Date] BETWEEN CONVERT(DATETIME, '2007-12-01 00:00:00', 102) AND CONVERT(DATETIME, '2008-01-31 00:00:00', 102))
GROUP BY v.SalesExecutive
HAVING (v.SalesExecutive <> - 1)


Thanks in advance

www.creativenrg.co.uk

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-22 : 13:14:26
Why do you think result to be wrong? You are taking an inner join with results so will always get both totals as equal. Try replacing INNER JOIN with LEFT OUTER then you will be considering tournaments without results also.
Go to Top of Page

CreativeNRG
Starting Member

44 Posts

Posted - 2008-01-22 : 14:50:26
quote:
Originally posted by visakh16

Why do you think result to be wrong? You are taking an inner join with results so will always get both totals as equal. Try replacing INNER JOIN with LEFT OUTER then you will be considering tournaments without results also.



I know the results of the query are wrong as I know for a fact that there are a lot more results than tournaments as the average number of results for each tournament is about 10 so the total number of tournaments should be a lot less than the total number of results.

Changing the query as per your suggestion seemed to have no effect, is there anything else obvious that I am missing?

www.creativenrg.co.uk
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-22 : 18:45:47
Try put COUNT(DISTINCT ...) in the query


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -