Question

I am trying to create a group name formula using a record from a different line in the table than the line that the records contained in this group are linked to.

First off, my data has the word group in it, so to avoid confusion I have italicized it to differentiate it from groups in Crystal Reports.

My table that I am pulling the records for this group name from has:

  • {GroupSection.Group} Groups of items in our inventory database
  • {GroupSection.Section} Sections (these are like subgroups nested within each inventory group).
  • {GroupSection.Description} The problems start here because the descriptions for {GroupSection.Group} and {GroupSection.Section} are both stored here.

This is a sample of my table:

    {GroupSection.Group}    {GroupSection.Section}     {GroupSection.Description}
    3.00                      0.00                      PRECAST CONCRETE PRODUCT
    3.00                     50.00                      MISC PRECAST CONCRETE PRODUCT
    3.00                     99.00                      *Z* MISC PRECAST CONCRETE PRODUC
    4.00                      0.00                      CEMENT SUPPLIES
    4.00                     50.00                      MISC CEMENT SUPPLIES
    4.00                     99.00                      *Z* MISC CEMENT SUPPLIES

The first and fourth line in this table are descriptions for {GroupSection.Group} (they have a 0.00 in the {GroupSection.Section} line) and the rest are descriptions for {GroupSection.Section}. The actual data that is contained in this report is in a different table and has the same two fields as the first two in this table, but not the third field, hence the need to link to and use this table to make the descriptions of the group names. The other table has no records that link to the lines with 0.00.

I want my Group Tree to look like this:

    3.  PRECAST CONCRETE PRODUCT
          50  MISC PRECAST CONCRETE
          99  *Z* MISC PRECAST CONCRETE
    4.  CEMENT SUPPLIES
          50  MISC CEMENT SUPPLIES
          99  *Z* MISC CEMENT SUPPLIES

This is the flawed formula I am using now in the Group Name formula for the top group:

ToText (left(Cstr({GroupSection.Group}),2))+ " " + ToText (If {GroupSection.Section} <> 0 then {GroupSection.Description} Else " ")

This is the Group Name formula for the inner nested group (working fine):

ToText (left(Cstr({GroupSection.Section}),2))+ " " + ToText ({GroupSection.Description})

This is what my Group Tree looks like now:

    3.  MISC PRECAST CONCRETE
          50  MISC PRECAST CONCRETE
          99  *Z* MISC PRECAST CONCRETE
    4.  MISC CEMENT SUPPLIES
          50  MISC CEMENT SUPPLIES
          99  *Z* MISC CEMENT SUPPLIES

As you can see, I need to get the name for the outer group from the first row in the table even though it's linking the records that are in the group to the second and third row in the table. Make sense?

I have tried using previous() in the formula but it gives the following error: This function cannot be used because it must be evaluated later.

Edit: I also just tried adding the same table again and linking it once to the group and once to the section and it worked for the group names, but now I have 1.9 million records, something really duplicated. So that won't work unless I can figure out how to fix the multiple records.

The "." in the group name description is an unrelated problem. It's the best I can do to get 3.00 to display as 3 and 50.00 to display as 50.

Thanks for any help!


Edit Dec 30, 2013 for user @Promethean:

Sorry I am new at SQL Command Tables. I took a command table from another report and did some changes to it. This is how far I got:

SELECT 
"GroupSection"."Group",
"GroupSection"."Section",
"GroupSection"."Description"

 FROM   ("SpruceDotNet"."dbo"."InventoryCommon" "InventoryCommon" with (nolock)

INNER JOIN 
"SpruceDotNet"."dbo"."GroupSection" "GroupSection" with (nolock)
ON "InventoryCommon"."Group"="GroupSection"."Group")

WHERE
"GroupSection"."Section"=0

ORDER BY 
"InventoryCommon"."Group"

Is there any chance you could use this info to modify your command table so a dummy like me can follow it? It looks like you are adding one of the fields twice, but I don't follow everything.

And then how would you join the command table to the bigger table that contains the more detailed? I would have to use the right kind of join so I don't end up duplicating the records?

Any help is greatly appreciated.

Was it helpful?

Solution

This may be very straightforward if you do this with SQL in a commmand table:

select a.Group, a.Section, a.Description,
       b.GroupName

from GroupSection a

left outer join
    (
       select concat( format(b.Group, 0), '. ', a.Description) as GroupName, b.Group
       from GroupSection b
       where b.Section = 0
    ) c
on a.Group = b.Group

The concat may change depending on your database provider. This reflects MySQL. perforing this against a table that reflects your sample data would produce the following:

{GroupSection.Group}|{GroupSection.Section}|{GroupSection.Description}       |{GroupSection.GroupName}
3.00                |  0.00                | PRECAST CONCRETE PRODUCT        | 3. PRECAST CONCRETE PRODUCT
3.00                | 50.00                | MISC PRECAST CONCRETE PRODUCT   | 3. MISC PRECAST CONCRETE PRODUCT
3.00                | 99.00                | *Z* MISC PRECAST CONCRETE PRODUC| 3. *Z* MISC PRECAST CONCRETE PRODUC
4.00                |  0.00                | CEMENT SUPPLIES                 | 4. CEMENT SUPPLIES
4.00                | 50.00                | MISC CEMENT SUPPLIES            | 4. MISC CEMENT SUPPLIES
4.00                | 99.00                | *Z* MISC CEMENT SUPPLIES        | 4. *Z* MISC CEMENT SUPPLIES

This can easily be grouped in Crystal to produce your desired results.

BEGIN EDITS:

Check this out. It's a working SQLfiddle using your table structure. Only GroupSection is necessary to accomplish this part, so I ignored the other. You can play around with different queries to see what hapens, but as-is it will pass to Crystal the necessary derived field to group on in Crystal. You'll need to still group on the new field, GroupName, in Crystal.

As for the other tables, you can treat the command table the same way you any other table. Just make sure you add key/linking field to the command if its not there in your mockup.

Here's the fiddle: http://sqlfiddle.com/#!2/aa88d/5/0

and just in case the comments there make it too confusing to see the code, here it is with my annotations stripped out:

SELECT gs2.GroupName,
       gs.sGroup, gs.Section, gs.Description

FROM GroupSection gs

left outer join
    (
     SELECT concat( format(inn.sGroup, 0), '. ', inn.Description) as GroupName, inn.sGroup

     FROM GroupSection inn

     WHERE inn.Section = 0
    ) gs2
    ON gs.sGroup = gs2.sGroup

OTHER TIPS

This is quite a handful! It seems that your problem is in the database, as you pointed out at the beginning. If you group by {GroupSection.Group} and put the {GroupSection.Description} in the Group Header next to it, you should get 3. MISC PRECAST CONCRETE. And then you put {GroupSection.Section} in the Detail Section and {GroupSection.Description} next to it, you should get 50 MISC PRECAST CONCRETE. So at this point I would not use a formula to group by, but use the fields. So only one group ({GroupSection.Group}) and the rest in the Detail Section. Please try that suggestion and llet me know how that works.

EDIT:

I don't think it's going to work. Because everything is in the same table you will always get the wrong description unless the first item in the second group happens to be the same item as the one in the first group. Two things come to mind to remedy that:

  1. Create a new table and split the descriptions up.
  2. Add the table to your report a second time and use one alias for the main group and the second alias for the second group.

CR will give you a message that you already have the table in your report, but you can ignore that. I would give #2 a shot and see how it works. It's definitely less work than creating a brand new table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top