corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » SQL question


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author SQL question
Baskey
Member

Registered: 31st May 06
User status: Offline
   18th Aug 11 at 15:02   View User's Profile U2U Member Reply With Quote

I have written the following query in terradata SQL assistant that gives me the results I am looking for. My issue is it returns over a million lines when all I need is a sum of value. If I remove the Distinct line it just returns the sum but I know I am picking up duplicates. How do I amend it so it is only returning the sum of value, but it is not including and duplicate reference numbers in the sum ?

Iv changed the database/table names so IT don't get pissey

Select
 
Sum (Value )
,Distinct (Reference number)
 
From Databasename.Tablename1 T1
 
Inner Join Databasename. Tablename2 T2
 
On T1.Primarykeycolumn = t2. Primarykeycolumn
 
Where Date between '2011/06/01' and '2011/06/30'
and Active = 'YES'
and Type = 'Type1'
Group by 1;
Dom
Member

Registered: 13th Sep 03
User status: Offline
18th Aug 11 at 15:31   View User's Profile U2U Member Reply With Quote

Isn't it 'SUM(DISTINCT tablename.fieldname)'?

So it'd be -
code:

SELECT
SUM(DISTINCT tablename.Value), tablename.Reference_Number
FROM
Databasename.Tablename1 T1
INNER JOIN
Databasename.Tablename2 T2 ON T1.Primarykeycolumn = t2.Primarykeycolumn
WHERE
tablename.date BETWEEN ('2011/06/01' AND '2011/06/30')
AND tablename.Active = 'YES'
AND tablename.Type = 'Type1'
GROUP BY
1



Don't really use MSSQL so this could possibly be wrong, although wouldn't you group by tablename.Reference_Number?

Edit - I'd also use an alias on the returned SUM() field.

[Edited on 18-08-2011 by Dom]
Baskey
Member

Registered: 31st May 06
User status: Offline
18th Aug 11 at 15:48   View User's Profile U2U Member Reply With Quote

Yes my bad Dom it should be grouped by reference number. It will need to be distinct on the reference number as there are many values the same.

Baskey
Member

Registered: 31st May 06
User status: Offline
18th Aug 11 at 15:57   View User's Profile U2U Member Reply With Quote

As I don't want it to return the reference numbers I think I need to remove them from the select statement, I'm just not sure where/how to put a condition in to ignore duplicate lines (ref number is the best for this as they should be unique)
Dom
Member

Registered: 13th Sep 03
User status: Offline
18th Aug 11 at 15:57   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by Baskey
It will need to be distinct on the reference number as there are many values the same.



Not sure you need the 'DISTINCT reference name' as the GROUP BY will be doing that anyways.


quote:
Originally posted by Baskey
As I don't want it to return the reference numbers I think I need to remove them from the select statement, I'm just not sure where/how to put a condition in to ignore duplicate lines (ref number is the best for this as they should be unique)


Remove the 'reference number' from the SELECT; as above GROUP BY will do the grouping of the reference numbers.

[Edited on 18-08-2011 by Dom]
Baskey
Member

Registered: 31st May 06
User status: Offline
18th Aug 11 at 16:01   View User's Profile U2U Member Reply With Quote

Just logged off mate. Will try in the morning and let you know how I get on. Cheers

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
FAO: Programmers barteh Geek Day 6 777
6th Nov 07 at 14:02
by barteh
 
System Center Configuration Manager 2007 Pablo Geek Day 13 353
6th Feb 08 at 14:39
by Pablo
 
Wedsite design Daimo B Geek Day 12 399
11th Jun 09 at 09:06
by Sam
 
Php help? (mysql_num_rows) Whittie Geek Day 17 1507
15th Jan 10 at 14:38
by Dom
 
SQL Experts Bart Geek Day 1 263
7th Jul 11 at 18:09
by Mike2k111
 

Corsa Sport » Message Board » Off Day » Geek Day » SQL question 29 database queries in 0.0132620 seconds