Solved

sql expression field "having clause"

Posted on 2007-04-10
9
793 Views
Last Modified: 2013-12-18
how to use a "having clause" in sql expression fields for crystal report 10(vs.net 2005)?
0
Comment
Question by:vielkacarolina1239
9 Comments
 
LVL 4

Expert Comment

by:ramumorla
ID: 18887239
Hey This is Oracle 10.x group.............
0
 
LVL 26

Expert Comment

by:Kurt Reinhardt
ID: 18887458
If you don't already know, a SQL Expression is essentially a subquery (either correlated or uncorrelated) in the SELECT clause that Crystal generates as part of it's SQL (Database|Show SQL Query).  As such, you must return a single, distinct value for the SQL Expression field.  Here's a sample of syntax I would use (Oracle SQL Server databases require the parentheses around the SQL in the SQL Expression editor):

(
SELECT DISTINCT
  field
FROM
  table
WHERE
  condition
)

If you want a HAVING clause, then you would use a statement like this:

(
SELECT DISTINCT
  field
FROM
  table
WHERE
  condition
GROUP BY
  field
HAVING
  COUNT(field) > 1
)

~Kurt
0
 

Author Comment

by:vielkacarolina1239
ID: 18889669
so let say the first sql statement have been generated by crystal if i want a having clause i just need to write a subquery and include my where clause along with the having clause. I already provided a where clause which is weekending=(select max(weekending) from table ) now I need to include a having clasue after this, I cannot put it together because weekending=........ represent just the where clause, and i tried creating a subquery with the two sql statement and it gives me the error(to many values).

(select sum("DIGITAL_MARKETING"."CURRENT_WEEK_UNITS"),sum("DIGITAL_MARKETING"."PRIOR_WEEK_UNITS")
 from "DIGITAL_MARKETING"
 having sum("DIGITAL_MARKETING"."CURRENT_WEEK_UNITS")>0 or sum("DIGITAL_MARKETING"."PRIOR_WEEK_UNITS")>0)
0
 
LVL 42

Expert Comment

by:frodoman
ID: 18889838
vielkacarolina1239,

Can you clarify if this is really a sql expression field?  If so, then as rhinok pointed out you must return a single result.  You're selecting two aggregate values coming back - if you want to do that you'll need to use two sql expression fields and split this into two parts...

frodoman
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 7

Expert Comment

by:RainMan82
ID: 18889853
use a group by in order to use a having....
0
 

Author Comment

by:vielkacarolina1239
ID: 18889888
I already have a field expression for the weekending=(select max(weekending) from table), so i would have to split the above query in order to get what I want.
0
 
LVL 26

Accepted Solution

by:
Kurt Reinhardt earned 500 total points
ID: 18889889
The reason this query doesn't work is because it returns more than one column.  Once again, because a SQL Expression is a subquery in the SELECT clause of the main query generated by the report, it can only return a single distinct value.  Here's  a sample of the SQL that Crystal generates when a SQL Expression is created:

SELECT
  `ENTITY`.`ENTITY`,
  `PRACENT`.`NAME`,
  `PRACENT`.`PRACNO`,
  (
  SELECT DISTINCT
    'Y'
  FROM
    SUBSTAN
  WHERE
    PRACNO = PRACENT."PRACNO"
    AND PAST = 'N'
    AND ADDR1 <> 'INQY'
  )
FROM
  `database`.`dbo`.`ENTITY` `ENTITY`
  INNER JOIN `database`.`dbo`.`PRACENT` `PRACENT` ON `ENTITY`.`ENTITY`=`PRACENT`.`ENTITY`
ORDER BY
  `ENTITY`.`ENTITY`

The part in parentheses is the SQL Expression field.  Notice, it only returns a single, distinct value for each record.  If you need multiple values, then you need multiple SQL Expression fields.

~Kurt

0
 

Author Comment

by:vielkacarolina1239
ID: 18889995
Thanks for the help really appreciate it.
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

Suggested Solutions

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

743 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

11 Experts available now in Live!

Get 1:1 Help Now