2

I'm having a bit of trouble with the Alias variable in MapBasic. In short, I have a table where I was to group by a specific column. Then I want to run through each unique value from the grouped table through the original one and combine each one. So I have a table of cities with a column of which country each city is in. My program gives the user to choose a table and a column within that table to gorup by. Then it basically goes like this:

Dim aSelection, aGrouped as Alias 

Selection = selectedTable & "." & selectedColumn

Select * from selectedTable into Temp_GroupedTable Group By selectedColumn

aGrouped = Temp_GroupedTable & "." & selectedColumn

So a) is this code correct? b) How can I fetch the data from each row in the Temp_GroupedTable so that I can compare it with the values in the original table? I currently have this general setup within a loop:

Fetch rec i from Temp_GroupedTable

Select * from selectedTable into New_Temp_TAB Where aSelection = aGrouped

Objects Combine

I know there is something wrong... But I don't know how to fix it. Any ideas?

2

Try this:

Dim aSelection, aGrouped as Alias
Dim sGrouped As String

aSelection = selectedTable & "." & selectedColumn
Select * from selectedTable 
   into Temp_GroupedTable 
   Group By aSelection

aGrouped = "Temp_GroupedTable." & selectedColumn

Fetch First from Temp_GroupedTable
Do Until EOT(Temp_GroupedTable)
   aGrouped = sGrouped
   Select * from selectedTable 
      into New_Temp_TAB 
      Where aSelection = sGrouped

‘**  Objects Combine ...

   Fetch First from Temp_GroupedTable
Loop

I changed your first selection a bit and also the assignment of the aGrouped alias variable. And then I made some changes to your loop. I’m not entirely sure what more you want to do within the loop.

I’m not entirely sure about this line: Where aSelection = aGrouped

You might need to read the value into a string or numeric value first. But give it a go and let us know

  • Thanks @peter. But for some reason I get an error message "Variable or Field Temp_GroupedTable.Region not defined" for the line of code you weren't sure about. I can assure you however that the column does exist. Any ideas on solving this? – KDawg Feb 4 '16 at 9:39
  • Yeah, that was what I thought. I have modified the code a bit to get around that issue. Notice that I'm assuming your column is a Char() column in my example – Peter Horsbøll Møller Feb 5 '16 at 9:20
2

Thanks for the help @Peter! I ended up using the following code which did what I needed:

Sub Process

Dim aGrouped as Alias

Print "Object Combiner Loaded!"

Select * from selectedTable into Temp_GroupedTable Group By selectedColumn
Print "Grouping Successful"

'*** Groups by user selected column
aSelection = selectedTable & "." & selectedColumn
Select * from selectedTable 
    into Temp_GroupedTable 
    Group By aSelection

aGrouped = "Temp_GroupedTable." & selectedColumn

Fetch First from Temp_GroupedTable  '*** Fetchs first row from grouped table
'*** Runs through table to combine objects by each individual value
Do Until EOT(Temp_GroupedTable)
    sGrouped = aGrouped
    Select * from selectedTable 
        Where aSelection = sGrouped
    Objects Combine Data selectedColumn = sGrouped 'Retains value of 
                                                   'selected column
    Fetch Next from Temp_GroupedTable
Loop

End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.