Print Page | Close Window

SQL Commands

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2022
Forum Name: Data Connectivity
Forum Discription: How to connect to data sources and export reports
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=17228
Printed Date: 21 Apr 2025 at 1:17am


Topic: SQL Commands
Posted By: Ross
Subject: SQL Commands
Date Posted: 07 Aug 2012 at 2:28am
Hello
Crystal Reports 11
MYSQL 5.2
 
Created a query that unions two areas together, one of the areas uses a interval dateadd that creates a date for every day in that month.
to get this to work I have used a set  @i = -1.
but crystal doesnt like this set variable.
Has anyone have any idea what I can use in SQL command instead
 
Thank you for all your time
query as follows
 
set @i = -1;
set @StartDate = '2012-07-01';
set @EndDate = '2012-07-31';
 
##Combined Data
SELECT HDPD.*,
p.idnam as `Surname` ,
p.idfnam as `Forename`,
date(p.iddob) as `DOB`,
p.idnhs as `NHSNo`,
p.idpcd as `Postcode`,
 ((date_format(`PbRDate`,'%Y') - date_format(  p.IDDOB ,'%Y')) - (date_format(`PbRDate`,'00-%m-%d') < date_format(  p.IDDOB ,'00-%m-%d'))) AS  `Age` ,
##GP Data Area Add in Practice PCT and Code only##
p.practicelnk,
concat_ws(', ', e_practice.practice_name) as Practice_Name,
e_practice.Practice_add1 as Prac_Postcode,
e_practice.Practice_code,
## BBV Data Area
(select BVHCV from p_bbv as hepc where hepc.fk_oid=P.oid and BVHCV is not null order by bvdat desc limit 1) as hcv,
(select BVHIV from p_bbv as hiv where hiv.fk_oid=P.oid and BVHIV is not null order by bvdat desc limit 1) as hiv,
(select bvbsag from p_bbv as bvbsag where bvbsag.fk_oid=P.oid and bvbsag is not null order by bvdat desc limit 1) as bvbsag
 
# PD DATA EXTRACT UNION HD DATA EXTRACT
FROM ((
##PD DATA EXTRACT
SELECT
pd.`fk_oid` as `PatientID`,
pd_temp.`PbRDate`,
Date(pd.`TVDATS`)as `TlDate`,
Date(pd.`TVDATF`) as `TlEndDate`,
Date(IF(@StartDate>TVDATS,@StartDate, TVDATS)) as `From Date`,
Date(IF(@EndDate>TVDATF, TVDATF,@EndDate)) as `End Date`,
'PD' as `Mode`,
sp_getcodecode(pd.`TVMOD`) as `ModeOfCare`,
sp_getcodecode(pd.`TVCEN`) as `Centre`,
'Home' as `Location`,
'PD Catheter' as `Access`
FROM p_newtimeline pd
INNER JOIN
 
(SELECT DATE(ADDDATE(@StartDate, INTERVAL @i:=@i+1 DAY)) AS `PbRDate`
FROM `p_chemistry1`
HAVING  @i < DATEDIFF(@EndDate, @StartDate)) as pd_temp
 
where (`TVMOD` IN (183204,183205,183206))
and (`TVDATF` is null or (`TVDATF` between @StartDate and @EndDate))
and `TVDATS`<`PbRDate`
and Date(IF(@EndDate>TVDATF, TVDATF,@EndDate))>=`PbRDate`
)
 
UNION
 
# HD DATA EXTRACT
(Select
ob.fk_oid as `PatientID`,
date(ob.DIDAT) as `PbRDate`,
(SELECT   date(max(p_newtimeline.tvdats)) as maxtl
  FROM p_anthropometry as obsq
  inner join p_newtimeline
  on p_newtimeline.tvdats<=obsq.didat
  and p_newtimeline.fk_oid=obsq.fk_oid
  where obsq.fk_oid=ob.fk_oid and obsq.didat=ob.didat
  group by obsq.didat ) as `TlDate`,
'' as `TlEndDate`,
'' as `From Date`,
'' as `End Date`,
'HD' as `Mode`,
sp_getcodecode((SELECT TVMOD FROM p_newtimeline as Cen
where cen.fk_oid=`PatientID` and TVDATS=(SELECT   date(max(p_newtimeline.tvdats)) as maxtl
  FROM p_anthropometry as obsq
  inner join p_newtimeline
  on p_newtimeline.tvdats<=obsq.didat
  and p_newtimeline.fk_oid=obsq.fk_oid
  where obsq.fk_oid=ob.fk_oid and obsq.didat=ob.didat
  group by obsq.didat ) order by TVDATS desc limit 1)) as `ModeOfCare`,
sp_getcodecode((SELECT TVCEN FROM p_newtimeline as Cen
where cen.fk_oid=`PatientID` and TVDATS=(SELECT   date(max(p_newtimeline.tvdats)) as maxtl
  FROM p_anthropometry as obsq
  inner join p_newtimeline
  on p_newtimeline.tvdats<=obsq.didat
  and p_newtimeline.fk_oid=obsq.fk_oid
  where obsq.fk_oid=ob.fk_oid and obsq.didat=ob.didat
  group by obsq.didat ) order by TVDATS desc limit 1)) as `Centre`,
loc.codetext as `Location`,
acc.codetext as `Access`
FROM p_anthropometry as ob
left join codes as loc on loc.proid=ob.haloc
left join codes acc on acc.proid=ob.access_used
 -- inner join p_newtimeline  on p_newtimeline.fk_oid=ob.fk_oid
 
Where
ob.didat >= @StartDate and
ob.didat <= @EndDate and
ob.prepost=200101
order by ob.fk_oid, ob.didat)) as HDPD
 
## Add Patient and GP Data
inner join p_person p on P.oid=HDPD.`PatientID`
LEFT JOIN e_practice ON p.PracticeLnk = e_practice.oid
 
 
order by `PatientID`, `PbRDate`, `ModeOfCare` DESC
;
 
 


-------------
newbie



Replies:
Posted By: hilfy
Date Posted: 08 Aug 2012 at 4:59am
Crystal doesn't usually handle variables in any flavor of SQL.  Would it be possible for you to add a view to the database to handle this?  Or a "calendar" table that has all of the dates?
 
-Dell


-------------
Proviti, Data & Analytics Practice
http://www.protiviti.com/US-en/data-management-advanced-analytics - www.protiviti.com/US-en/data-management-advanced-analytics



Print Page | Close Window