Select clause inside fields list
Teradata Teradata Discussion Forums Teradata.com Discussion Forum
Visit Teradata.com
Home       Guidelines    Member List
Welcome Guest ( Login | Register )
        


This online forum is for user-to-user discussions of Teradata products, and is not an official customer support channel for Teradata. If you require direct assistance, please contact Teradata support.


Select clause inside fields list Expand / Collapse
Author
Message
Posted 7/3/2009 2:26:31 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 7/30/2009 7:25:08 AM
Posts: 9, Visits: 30
I've been trying to run an SQL statement at TeraData and it keeps on failing...

The error I receive is: "3706: Syntax error: expected something between '/' and the 'SELECT' keyword."

The SQL statement is:
"SELECT TOP 3 ID_1, QTD,
CAST (
Cast( (QTD * 100) AS NUMERIC) / SELECT COUNT(*) FROM Table_1
AS DEC(10, 2)
) AS Percent
FROM ( SELECT ID_1, COUNT(*) AS QTD FROM Table_1 GROUP BY ID_CDR
) AS MyTable
ORDER BY Percent DESC"

As you may see in the query, there is a select inside the fields' list... Would that be the reason of the error?
Can anyone help me?

Regards.
Rodrigo Duarte.
Post #16031
Posted 7/4/2009 8:21:23 PM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 7/29/2009 9:23:21 AM
Posts: 27, Visits: 34
I don't believe until TD13 can you embed a SELECT statement within a column, which is similar to what Oracle permits currently. Try a CROSS JOIN instead. Since it will only return a single row the penalty for the product join is minimal and should not cause you any problems.

SELECT TOP 3
ID_1
, QTD
, CAST (Cast( (QTD * 100) AS NUMERIC) / Denominator.MyCount
AS DEC(10, 2)) AS Percent
FROM ( SELECT ID_1, COUNT(*) AS QTD FROM Table_1 GROUP BY ID_CDR
) AS MyTable
CROSS JOIN
(SELECT COUNT(*) AS MyCount FROM Table_1) AS Denominator
ORDER BY Percent DESC
Post #16033
Posted 7/5/2009 9:38:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 7/30/2009 7:25:08 AM
Posts: 9, Visits: 30
Rob Paller, I appreciate your reply!
I still didn't have the chance to try your query in tera data 'cause my server is currently under maintenance... Too bad =/

Well, I liked your idea and I can't see why it shouldn't work. I'll let you know if anything fail.
Thank you so much!

Regards,
Rodrigo.
Post #16036
Posted 7/5/2009 10:35:21 AM
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 11/5/2009 4:40:02 PM
Posts: 717, Visits: 466
Hi Rodrigo.
you can rewrite it using an OLAP function, too:

SELECT ID_CDR, COUNT(*) AS QTD
,CAST (QTD * 100 / sum(QTD) OVER () AS DEC(10, 2)) AS Percent
FROM Table_1
GROUP BY ID_CDR
QUALIFY ROW_NUMBER() OVER ( ORDER BY QTD DESC) <= 3

Depending on your needs, you might replace ROW_NUMBER with a faster RANK, this will return a TOP WITH TIES.

Dieter
Post #16037
« Prev Topic | Next Topic »


Reading This Topic Expand / Collapse
Active Users: 0 ( 0 guests, 0 members, 0 anonymous members )
No members currently viewing this topic.


All times are GMT -5:00, Time now is 2:42am

Powered By InstantForum.NET v4.1.4 © 2009
Execution: 0.078. 8 queries. Compression Disabled.