Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SyBase SQL Syntax

Posted on 2014-03-06
7
Medium Priority
?
331 Views
Last Modified: 2014-03-08
Hey guys,

SyBase SQL Anywhere v10

I have 4 tables. POSDetail, product, promo and refundreasons.

When POSdetail.prodtype = 0 or 1 I want to return product.descript where posdetail.prodnum = product.prodnum

When POSdetail.prodtype = 100 I want to return promo.descript where posdetail.prodnum = promo.promonum

When POSdetail.prodtype = 101 I want to return 2 columns product.descript where posdetail.prodnum = product.prodnum AND refundreasons.descript where posdetail.howpaid = refundreaons.refnum

It's ok if the first two cases have a blank column next to them when there is no prodtype 101.

This is what I have so far.....

select t1.quan as Qty, 
case when t1.prodtype in (0,1, 101) then t3.descript 
when t1.prodtype = 100 then t2.descript 
else 'no desc avail' end as Description, 
t1.costeach as 'Cost Each', 
(t1.quan * t1.costeach) as 'Extended Cost'
from dba.posdetail as t1 left join dba.promo as t2 on t1.prodnum = t2.promonum
left join dba.product as t3 on t1.prodnum = t3.prodnum
where t1.transact = 899330

Open in new window



Also, I cant figure our how to round Cost Each and Extended Cost to two decimal places :/


Thank you in advance!
0
Comment
Question by:triphen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39911867
select t1.quan as Qty, 
case when t1.prodtype in (0,1, 101) then t3.descript 
when t1.prodtype = 100 then t2.descript 
else 'no desc avail' end as Description, 
case when t1.prodtype = 101 then refundreasons.descript end refundreason
round(t1.costeach, 2) as 'Cost Each', 
round(t1.quan * t1.costeach , 2) as 'Extended Cost'
from dba.posdetail as t1 
left join dba.promo as t2 on t1.prodnum = t2.promonum 
left join dba.product as t3 on t1.prodnum = t3.prodnum 
left join dba.refundreasons t4 on t4.refnum = t1.howpaid  
where t1.transact = 899330 

Open in new window

0
 

Author Comment

by:triphen
ID: 39912923
---------------------------
PixelPoint Service Utility
---------------------------
[Sybase][ODBC Driver][SQL Anywhere]Syntax error near 'ROUND' on line 6.
---------------------------
OK  
---------------------------

Also in line 2 it now needs to be modified to read:

case when t1.prodtype in (0,1) then t3.descript           Without the 101 because it is being accounted for in the case you made.
0
 

Author Comment

by:triphen
ID: 39913016
I modified to this and it works as expected....


select t1.quan as Qty, 
case when t1.prodtype in (0,1, 101) then t3.descript 
when t1.prodtype = 100 then t2.descript 
else 'no desc avail' end as Description, 
case when t1.prodtype = 101 then t4.descript end as 'Void/Comp',
t1.costeach as 'Cost Each', 
(t1.quan * t1.costeach) as 'Extended Cost'
from dba.posdetail as t1 
left join dba.promo as t2 on t1.prodnum = t2.promonum 
left join dba.product as t3 on t1.prodnum = t3.prodnum 
left join dba.refundreasons t4 on t1.howordered = t4.refnum
where t1.transact = 899330

Open in new window



Still need the rounding though.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 41

Expert Comment

by:Sharath
ID: 39913918
Apply ROUND function
select t1.quan as Qty, 
case when t1.prodtype in (0,1, 101) then t3.descript 
when t1.prodtype = 100 then t2.descript 
else 'no desc avail' end as Description, 
case when t1.prodtype = 101 then t4.descript end as 'Void/Comp',
t1.costeach as 'Cost Each', 
ROUND(t1.quan * t1.costeach,2) as 'Extended Cost'
from dba.posdetail as t1 
left join dba.promo as t2 on t1.prodnum = t2.promonum 
left join dba.product as t3 on t1.prodnum = t3.prodnum 
left join dba.refundreasons t4 on t1.howordered = t4.refnum
where t1.transact = 899330

Open in new window

0
 

Author Comment

by:triphen
ID: 39914121
You have a Round with a parenthesis open but no close. See first post.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39914270
I overlooked at Guy Hengel's post. What is the error you are getting with that code?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 39914659
that error sounds like your sybase version and/or odbc driver you use does not support the usage of the function ROUND...
you may want to try a CAST( .... as number(10,2)) instead, or try to check to get a newer odbc driver, and double-check that the ROUND function is really supported.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question