Solved

SyBase SQL Syntax

Posted on 2014-03-06
7
316 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 142

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 40

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 40

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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 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