使用两个不同表格中的列获取平均时间

时间:2016-06-13 12:31:13

标签: sql sql-server

我试图找到特定类型设备的平均校准时间。设备的具体描述列于" Gages"表。校准每件设备所花费的时间列在"事件"表。我试图通过使用此代码来平均校准所有相关设备的总实际时间

Text := ReturnFromSQL('SELECT AVG(NULLIF(ACTUAL_TIME,0)) FROM EVENTS E INNER JOIN GAGES G ON E.CERT_TYPE = G.GAGE_TYPE WHERE GAGE_DESCR = ''' + Equipment['Description']   +  '''AND EVENT_TYPE = ''CALIBRATION''');

这两个表有共同的列" CERT_TYPE"和" GAGE_TYPE"这只是设备的广泛类型(即Caliper,Scale,Torque Wrench)。

平均时间位于"事件"表但我需要使用" GAGE_DESCR"来自" Gages"表格以获得设备的具体类型(即Caliper 0-12,Scale-Truck,Torque Wrench CCW)。

这是用于跟踪估计时间与实际时间的报告。

我们正在使用Microsoft SQL Server 2008.

Here is a screenshot of the report as of right now.

似乎我正在错误地连接设备,因为两个不同的卡钳似乎几乎相同的平均时间,即使我测试校准卡钳48的时间比0-12长得多。

更新信息

Here is sample date from Events (left) and Gages (right)

1 个答案:

答案 0 :(得分:0)

您是否检查过加入条件是否正确?尝试用IN替换JOIN并比较结果以消除交叉连接的可能性。

1)使用 JOIN

20:08:55.937
org.datanucleus.query.JPQLSingleStringParser <init>: JPQL Single-String with "select n from MyTable n where n.name = 'Name1'"
20:08:56.141
org.datanucleus.store.query.AbstractJPQLQuery compileInternal: JPQL Query : Compiling "SELECT n FROM MyTable n WHERE n.name = 'Name1'"
20:08:56.339
org.datanucleus.store.query.AbstractJPQLQuery compileInternal: JPQL Query : Compile Time = 198 ms
20:08:56.340
org.datanucleus.store.query.AbstractJPQLQuery compileInternal: QueryCompilation:
  [from:ClassExpression(alias=n)]
  [filter:DyadicExpression{PrimaryExpression{n.name}  =  Literal{Name1}}]
  [symbols: n type=com.vicparkhoney.backend.bean.MyTable]
20:08:56.342
com.google.appengine.datanucleus.query.JPQLQuery performExecute: JPQL Query : Executing "SELECT n FROM MyTable n WHERE n.name = 'Name1'" ...
20:08:56.342
com.google.appengine.datanucleus.MetaDataValidator validate: Performing appengine-specific metadata validation for com.vicparkhoney.backend.bean.MyTable
20:08:56.344
com.google.appengine.datanucleus.MetaDataValidator validate: Finished performing appengine-specific metadata validation for com.vicparkhoney.backend.bean.MyTable
20:08:56.628
org.datanucleus.store.StoreDataManager registerStoreData: Managing Persistence of Class : com.vicparkhoney.backend.bean.MyTable [Table : com.vicparkhoney.backend.bean.MyTable, InheritanceStrategy : new-table]
20:08:57.342
com.google.appengine.datanucleus.query.JPQLQuery performExecute: Query compiled as : Kind=com.vicparkhoney.backend.bean.MyTable Filter : __key__=mytable("Name1") [QUERY-TYPE=NORMAL]
20:08:57.439
com.google.appengine.datanucleus.query.DatastoreQuery performExecute: Executing query in datastore for SELECT n FROM MyTable n WHERE n.name = 'Name1'
20:08:58.930
com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl$DatastoreManagedConnection <init>: Created ManagedConnection using DatastoreService = com.google.appengine.api.datastore.DatastoreServiceImpl@9355b6
20:08:58.937
org.datanucleus.store.connection.ConnectionManagerImpl allocateConnection: Connection added to the pool : com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl$DatastoreManagedConnection@56c36c for key=org.datanucleus.ObjectManagerImpl@15f65a1 in factory=ConnectionFactory:nontx[com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl@3817d7]
20:08:58.938
com.google.appengine.datanucleus.query.JPQLQuery performExecute: JPQL Query : Execution Time = 2596 ms
20:08:59.150
org.datanucleus.store.connection.ConnectionManagerImpl closeAllConnections: Connection found in the pool : com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl$DatastoreManagedConnection@56c36c for key=org.datanucleus.ObjectManagerImpl@15f65a1 in factory=ConnectionFactory:nontx[com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl@3817d7] but owner object closing so closing connection
20:08:59.220
org.datanucleus.store.connection.ConnectionManagerImpl$1 managedConnectionPostClose: Connection removed from the pool : com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl$DatastoreManagedConnection@56c36c for key=org.datanucleus.ObjectManagerImpl@15f65a1 in factory=ConnectionFactory:nontx[com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl@3817d7]
20:08:59.220
org.datanucleus.ObjectManagerImpl disconnectObjectProvidersFromCache: Level 1 Cache cleared
20:08:59.223
org.datanucleus.ObjectManagerImpl close: Object Manager "org.datanucleus.ObjectManagerImpl@15f65a1" closed

2)使用 IN

SELECT AVG(NULLIF(ACTUAL_TIME,0)) 
FROM EVENTS AS E 
INNER JOIN GAGES AS G 
ON E.CERT_TYPE = G.GAGE_TYPE 
WHERE GAGE_DESCR = *Equipment['Description']*
AND EVENT_TYPE = 'CALIBRATION'