P
Peter Carlson
I've been baning my head with this query.
We have a table with the following fields:
Table:Incident
id: autonumber id
person: reference to Person.ID
role: reference to Roles.ID
action1: reference to Actions.Code
action2: reference to Actions.Code
action3: reference to Actions.Code
action4: reference to Actions.Code
.... many more fields not pertinent to the question
Table: Person
id, last, first, ...
3, Carlson, Peter
Table: Roles
code, descr
1, Major Role
2, Minor Role
....
Table: Actions
code, descr (code is 2 char)
1, Did something
2, Did something else
NN, Did nothing
So assuming the following entry in the Incident table:
1, 3, 2, 1, NN, , ,
when I retrieve the sql (for html report) I want the following
id, fullname (Last + First), Descriptive Role, Desriptive Action1-4
separated with <br> if they exist
1, "Carlson, Peter", Minor Role, Did Something<br>Did nothing
So far we are using the following SQL which pulls most everything together:
SELECT IncidentNumber, FORMAT(I.Arrived, 'hh:nn') as Arrived,
FORMAT(I.Cleared, 'hh:nn') as Cleared, A.name AS apparatus, r.descr AS role,
B.descr AS Actions1, I.Actions2, I.Actions3, I.Actions4, (Actions1) &
('<br>'+Actions2) & ('<br>'+Actions3) & ('<br>'+Actions4) as Actions FROM
Incidents_Apparatus AS I, Apparatus AS A, tApparatus_Use AS R,
tIncident_Actions AS B WHERE I.IncidentNumber='%id%' and I.Vehicle=A.id and
I.Role=R.code and I.Actions1=B.code and I.Actions2=B.code and
I.Actions3=B.code and I.Actions4=B.code;
However we have 2 problems:
1. the concat of Actions 1-4 always adds the <br> even if the field is
empty (doesn't appear to be null, but appears as LEN(0) ).
2. if Actions1-4 is empty, then it is not able to find an appropriate entry
in the Actions Table (because there is no empty action)
I could add an "empty" row to the Actions table because the code column is a
2 char field, however if the code column was an autonumber ID like it is in
most of our other tables then I couldn't add an empty field. I could also
spatter my SQL with IIF to test the LEN of Actions1-4 and then concat
together, but that's not ANSI sql and wont work outside of MS. Although I
already need to maintain 2 tables of SQL, our product on the "heavy" end
supports oracle and mySql. The lite version uses Jet to an access database.
access doesn't use DATE_FORMAT and mySQL doesn't use FORMAT.
Is there any way to accomplish this?
Thanks, and sorry for the lengthy post.
Peter
We have a table with the following fields:
Table:Incident
id: autonumber id
person: reference to Person.ID
role: reference to Roles.ID
action1: reference to Actions.Code
action2: reference to Actions.Code
action3: reference to Actions.Code
action4: reference to Actions.Code
.... many more fields not pertinent to the question
Table: Person
id, last, first, ...
3, Carlson, Peter
Table: Roles
code, descr
1, Major Role
2, Minor Role
....
Table: Actions
code, descr (code is 2 char)
1, Did something
2, Did something else
NN, Did nothing
So assuming the following entry in the Incident table:
1, 3, 2, 1, NN, , ,
when I retrieve the sql (for html report) I want the following
id, fullname (Last + First), Descriptive Role, Desriptive Action1-4
separated with <br> if they exist
1, "Carlson, Peter", Minor Role, Did Something<br>Did nothing
So far we are using the following SQL which pulls most everything together:
SELECT IncidentNumber, FORMAT(I.Arrived, 'hh:nn') as Arrived,
FORMAT(I.Cleared, 'hh:nn') as Cleared, A.name AS apparatus, r.descr AS role,
B.descr AS Actions1, I.Actions2, I.Actions3, I.Actions4, (Actions1) &
('<br>'+Actions2) & ('<br>'+Actions3) & ('<br>'+Actions4) as Actions FROM
Incidents_Apparatus AS I, Apparatus AS A, tApparatus_Use AS R,
tIncident_Actions AS B WHERE I.IncidentNumber='%id%' and I.Vehicle=A.id and
I.Role=R.code and I.Actions1=B.code and I.Actions2=B.code and
I.Actions3=B.code and I.Actions4=B.code;
However we have 2 problems:
1. the concat of Actions 1-4 always adds the <br> even if the field is
empty (doesn't appear to be null, but appears as LEN(0) ).
2. if Actions1-4 is empty, then it is not able to find an appropriate entry
in the Actions Table (because there is no empty action)
I could add an "empty" row to the Actions table because the code column is a
2 char field, however if the code column was an autonumber ID like it is in
most of our other tables then I couldn't add an empty field. I could also
spatter my SQL with IIF to test the LEN of Actions1-4 and then concat
together, but that's not ANSI sql and wont work outside of MS. Although I
already need to maintain 2 tables of SQL, our product on the "heavy" end
supports oracle and mySql. The lite version uses Jet to an access database.
access doesn't use DATE_FORMAT and mySQL doesn't use FORMAT.
Is there any way to accomplish this?
Thanks, and sorry for the lengthy post.
Peter