Solved

sql expression field "having clause"

Posted on 2007-04-10
9
796 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

803 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