Solved

SyBase SQL Syntax

Posted on 2014-03-06
7
314 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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Date Query 28 44
Oracle - Stored Procedure Privilge access 3 39
data lookup in Oracle - need suggestions 55 103
Make query more efficient 1 17
In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now