Question

Top Function in TeraData

Asked by: jjrr007

I am having trouble getting the top 10 rows in Teradata.  

This is been complicated by a couple things:
1. I don't have permissions to create a temporary table.
2. I am using a group by function in the query.
3. I am joining to other tables in the query.

I have tried to use sample but have had problems because of joining to other tables.  Maybe, I don't have the syntax correct- I just put it right before the from clause.  Also, I tried the cum function, but had problems because I am using a group by.  

I have asked a question similar to this before; however, this time I have the complicated factors mentioned above.

Thank you!!!

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-06-22 at 16:38:05ID21896402
Tags

teradata

,

function

Topic

Databases Miscellaneous

Participating Experts
3
Points
500
Comments
29

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Extra space at the top of a row
    Sometimes Excel insists on inserting extra space at the top of a row. I can of course manually resize the row, but this is only temporary, and is often reverted back to the extra space by other manipulations, such as double-clicking on the row separator bar. This usual hap...
  2. size of temporary table
    Hi: I am coding a web application that computes huge amount of data pulled out the database. For delegating part of calculation to a stored procedure, I would have to save the work-in-progress data in a temporary table in the database. The question is, there will be, say, ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: rockiroadsPosted on 2006-06-22 at 22:30:44ID: 16966380

I dont know if its possible for you but have u tried using querying your query?

 

by: kenpemPosted on 2006-06-23 at 02:39:42ID: 16967388

If the query is just too complex to limit effectively, try wrapping it inside a VIEW, then SELECT TOP 10 * FROM theView

 

by: jjrr007Posted on 2006-06-23 at 04:01:49ID: 16967737

rockiroads
How do I query the query?  Please let me know the syntax for this...

Kenpem,
I don't know how to do what you suggested either.  Please let me know the syntax for this...

 

by: kenpemPosted on 2006-06-23 at 04:14:07ID: 16967820

bear in mind I don't actually KNOW TeraData, so this is based on standard MS T-SQL Syntax:

if we represent your big complex query as "SELECT BLAH FROM BOO" (I know there must be a lot more!):

CREATE VIEW MakeItEasy AS SELECT BLAH FROM BOO
GO

then you should be able to

SELECT TOP 10 * FROM MakeItEasy

hope that TeraData is close enough to MS that this works for you!

 

by: jjrr007Posted on 2006-06-23 at 04:21:36ID: 16967868

BTW, I don't have permission to change my permissions in the Teradata database and the database is not local.

 

by: jjrr007Posted on 2006-06-23 at 04:23:19ID: 16967879

I don't think that I have permission to create a view.  I will double check and let you know.  

Maybe the rank function will work.  What is the syntax for thiat in Teradata?  Thanks again.

 

by: kenpemPosted on 2006-06-23 at 04:25:40ID: 16967895

Any chance your permissions to be extended to something more suited to a developer?

And can you paste the query at us here? Might be easier to solve this if we could see the context.

 

by: wstuphPosted on 2006-06-23 at 05:29:54ID: 16968352

Yeah - paste up the query and let us take a look.  I work with Teradata DBs every day, I can probably help you out.

 

by: jjrr007Posted on 2006-06-23 at 06:15:52ID: 16968658

I can't chage the permissions.  Kenpem, I don't have permission to create a view.

The query is below,  I replaced the propietary information with the corresponding column table/names.  It's:
Select
E.Column1
Count(E.Column1) AS Column2

From
Table1 F Inner Join Table2 A ON
F.Column3= A.Column3

Inner Join Table4 E ON
A.Column5 = E.Column5

Group By
E.Column1

Where
(A.Date Between '2006-01-01' AND '2006-01-31')

Order By 2 DESC

 

by: rockiroadsPosted on 2006-06-23 at 06:58:01ID: 16969144

What are u using to query Teradata?

Reason why I ask is that whatever u use may support querying a query
e.g.

SELECT TOP Column2
FROM (Select E.Column1, Count(E.Column1) AS Column2
          From Table1 F Inner Join Table2 A ON F.Column3= A.Column3
          Inner Join Table4 E ON A.Column5 = E.Column5
          Group By E.Column1
          Where (A.Date Between '2006-01-01' AND '2006-01-31'))
Order By Column2

 

by: rockiroadsPosted on 2006-06-23 at 06:58:51ID: 16969153

I forgot the number in TOP
select top 3


not sure if top is supported with what u are doing

this is access

oracle would be rownum

so not sure what teradata is

 

by: jjrr007Posted on 2006-06-23 at 07:08:27ID: 16969268

The top function is not supported in Teradata.

 

by: rockiroadsPosted on 2006-06-23 at 07:13:12ID: 16969308

is limit supported?

select f1
from table1
limit 3

 

by: jjrr007Posted on 2006-06-23 at 07:17:00ID: 16969347

If I can just get a row number added.  I can filter the dataset on the report level

 

by: jjrr007Posted on 2006-06-23 at 07:18:57ID: 16969366

No the limit function is not supported.  The rank function will not work because I am using an aggregate function- i.e. taking an aggregate of an aggregate.

 

by: rockiroadsPosted on 2006-06-23 at 07:23:12ID: 16969403

rank Im assuming is what u use to get your rows?
thats why I suggested if u can use a query within a query
this way the subquery acts as a table

 

by: wstuphPosted on 2006-06-23 at 07:27:28ID: 16969450

I'm still trying to figure out what the result you're looking for is, personally.  So you want, say, the top 10 Column2 counts with the Column1 names?

column1 Column2
High  17
Next  15
Next2 14
....
Ten   2

?

 

by: jjrr007Posted on 2006-06-23 at 07:41:59ID: 16969583

rockiroads
A subquery would be a good idea- the only problem is that you can't join in a subquery in Teradata

wstuph,
The query is suppose to get the top 10 counts-i.e. Count(E.Column1)


Again, having a row count function would be ideal...
I pasted my query here again, since this question is getting long:

Select
E.Column1
Count(E.Column1) AS Column2

From
Table1 F Inner Join Table2 A ON
F.Column3= A.Column3

Inner Join Table4 E ON
A.Column5 = E.Column5

Group By
E.Column1

Where
(A.Date Between '2006-01-01' AND '2006-01-31')

Order By 2 DESC

 

by: jjrr007Posted on 2006-06-23 at 09:43:01ID: 16970730

I found the function for row_number in Teradata.  It is still not working.  I just return 0's please advise in the third column.  What is wrong.  If we can get this last column to work- the query should be OK.  Thanks

ROW_NUMBER() OVER (PARTITION BY E.column1 ORDER BY 2)

 

by: jjrr007Posted on 2006-06-23 at 09:45:23ID: 16970750

The row function is above- it returns 1's.  Please advise.  This is needed urgently- thanks!

 

by: rockiroadsPosted on 2006-06-23 at 10:00:32ID: 16970874

ok, lets try this then


Select
E.Column1
Count(E.Column1) AS Column2

From
Table1 F Inner Join Table2 A ON
F.Column3= A.Column3

Inner Join Table4 E ON
A.Column5 = E.Column5

Group By
E.Column1

Where
(A.Date Between '2006-01-01' AND '2006-01-31')

QUALIFY ROW_NUMBER() OVER (ORDER BY E.Column1) < 4

 

by: rockiroadsPosted on 2006-06-23 at 10:01:25ID: 16970885

this may help u, I found a forum

http://www.teradataforum.com/l030103a.htm

 

by: jjrr007Posted on 2006-06-23 at 10:33:21ID: 16971128

I will try that forum.  Thanks.

 

by: rockiroadsPosted on 2006-06-23 at 10:35:57ID: 16971160

I take it that SQL didnt work then

 

by: jjrr007Posted on 2006-06-23 at 10:45:28ID: 16971232

No, I couldn't get the row count thing to work.  

 

by: rockiroadsPosted on 2006-06-23 at 11:07:29ID: 16971365

In your query where did u use RANk?

Select
E.Column1
Count(E.Column1) AS Column2

From
Table1 F Inner Join Table2 A ON
F.Column3= A.Column3

Inner Join Table4 E ON
A.Column5 = E.Column5

Group By
E.Column1

Where
(A.Date Between '2006-01-01' AND '2006-01-31')

HAVING RANK (E.Column1)

Order By 2 DESC


 

by: rockiroadsPosted on 2006-06-23 at 11:08:09ID: 16971371

Im trying, honest guv. Since I dont have teradata, its a touch tricky to try help u with regards to myself testing first

Ok, Im doing a google search
http://www.google.com/search?sourceid=navclient&ie=UTF-8&rls=GGLG,GGLG:2005-39,GGLG:en&q=ROW%5FNUMBER%28%29+teradata

and from there, this link looks promising
http://www.teradata.com/t/go.aspx/?id=44353

this is using a subquery like the one I mentioned, so perhaps u can use the example here and try?
on the outer query, use RANK

 

by: jjrr007Posted on 2006-06-23 at 15:50:26ID: 16973201

I got the answer through the Teradat forum.  Thanks a lot rockiroads.

 

by: rockiroadsPosted on 2006-06-23 at 23:49:03ID: 16974297

No probs, glad you managed it. Tough one eh.
Would u mind posting it here so I can see plus for the benefit of others

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...