Running Multiple Selection and Statistics queries

830
5
10-08-2012 09:28 AM
AnthonyTimpson2
Occasional Contributor
Hello,

Im just starting to throw myself at a line query and statistics script and am a bit lost on a simple thing.

I have sorted out how to select various features and run a statistics export on that selection.

i am running into a problem trying to run multiple selections and append those statistics to the already created output DBF.

import arcpy
from arcpy import env

env.workspace = "C:/Users/X/Desktop/PNG HCA Program Dev/PNG_Planning_Map.mdb"
arcpy.SelectLayerByAttribute_management ("Testing", "NEW_SELECTION", " [PRESSURE1] = 300 and [CL_MAOP] = 3 ")
arcpy.Statistics_analysis("Testing", "C:/Users/X/Desktop/PNG HCA Program Dev/PNG_Planning_Map.mdb", [["Shape_Length", "SUM"]])
arcpy.SelectLayerByAttribute_management ("Testing", "NEW_SELECTION", " [PRESSURE1] = 0 and [CL_MAOP] = 4 ")
arcpy.Statistics_analysis("Testing", "C:/Users/X/Desktop/PNG HCA Program Dev/PNG_Planning_Map.mdb", [["Shape_Length", "SUM"]])


My error is

Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000725: Output Table: Dataset C:\Users\X\Desktop\PNG HCA Program Dev\PNG_Planning_Map.dbf already exists.

im sure it is something incredibly simple but i just need each query/stats result to be appended to the generated dbf.

Thanks
Tags (2)
0 Kudos
5 Replies
MarcinGasior
Occasional Contributor III
I think it's not possible to directly append rows to table. But you can do this using Append.
Try this code, where first table is created in geodatabase, second table is created in in_memory workspace (temporary) and then appended to first table:
import arcpy
from arcpy import env

env.workspace = "C:/Users/X/Desktop/PNG HCA Program Dev/PNG_Planning_Map.mdb"
arcpy.SelectLayerByAttribute_management ("Testing", "NEW_SELECTION", " [PRESSURE1] = 300 and [CL_MAOP] = 3 ")
arcpy.Statistics_analysis("Testing", "C:/Users/X/Desktop/PNG HCA Program Dev/PNG_Planning_Map.mdb/Stats", [["Shape_Length", "SUM"]])
arcpy.SelectLayerByAttribute_management ("Testing", "NEW_SELECTION", " [PRESSURE1] = 0 and [CL_MAOP] = 4 ")
arcpy.Statistics_analysis("Testing", "in_memory/temp_stats", [["Shape_Length", "SUM"]])

arcpy.Append_management(["in_memory/temp_stats"], "C:/Users/X/Desktop/PNG HCA Program Dev/PNG_Planning_Map.mdb/Stats", "TEST", "", "")
0 Kudos
AnthonyTimpson2
Occasional Contributor
I really appreciate the tip!

I am running into a another slight issue. If my query doesnt bring up any results, summary statistics return nothing and No data is appended to the table. is there a way to check that there is no result and have it mash in Zeros instead of nothing?

While i can probably get around this by simply populating zero for the queries which return nothing, it would be more useful if the output table already contained mention that nothing was found.
0 Kudos
ChristopherThompson
Occasional Contributor III
You can use the GetCount_management tool that returns the number of rows in a table, featureclass, or layer.  Use that value in an if, else statement to execute different processes based on there either being a count > 0 or count = 0.
0 Kudos
AnthonyTimpson2
Occasional Contributor
Thank you!

I appreciate you taking time to answer my ignorant questions.
0 Kudos
ChristopherThompson
Occasional Contributor III
"Ignorant" is sort of a harsh term... even if its an honest ignorance.. you're obviously moving in the right direction.. the problem with learning programming languages is the same as with any other language.. the vocabulary.  You might know how to put together the sentence, if you just knew the words 🙂  Don't be shy about asking - it can save you hours or days sometimes so ask away!
0 Kudos