Solved

SyBase SQL Syntax

Posted on 2014-03-06
7
309 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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