用SQL显示表人的大孩子

时间:2017-05-26 12:32:08

标签: mysql

我有一张名为person(ID,fname,Age,Gender,parentID)的表格, 任何人都可以告诉如何在 SQL 中向没有 大孩子的人展示?

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题,但如果有很多人有很多孩子,可能会有点慢:

SELECT DISTINCT p.*
FROM person p 
LEFT JOIN person c ON c.parentID = p.ID
LEFT JOIN person gc on gc.parentID = c.ID
WHERE gc.ID IS NULL

答案 1 :(得分:0)

import numpy as np
import pandas as pd
import pandas_datareader.data as web
import matplotlib.pyplot as plt 
import datetime as dt
from datetime import datetime, timedelta
np.set_printoptions(threshold=np.inf)

symbols =[   
"   AMZN"   ,
"   BABA"   ,
"   WMT "   ,
"   CMCSA"  ,
"   HD  "   
]
noa = len(symbols)

data = pd.DataFrame()
for sym in symbols:
    data[sym] = web.DataReader(sym, 'google',  start ='2016-05-28', end='2017-05-28')['Close']
data.columns = symbols

print data

(data / data.ix[0]*100).plot(figsize=(8,5))

rets = np.log(data / data.shift(1))             # calculates log returns of the different securities
rets.mean() * 252                               # 252 trading days to annualize returns
rets.cov() * 252                                # covariance matrix of returns 

weights = np.random.random(noa)                 # generate five random numbers for the weights
weights /= np.sum(weights)                      # normalizes the the values so that the sum equals 1

np.sum(rets.mean() * weights) * 252             #Expected portfolio return

np.dot(weights.T,np.dot(rets.cov() * 252, weights)) #Expected portfolio variance 
np.sqrt(np.dot(weights.T,np.dot(rets.cov() * 252, weights))) # Expected portfolio standarddeviation 

创建临时表可能会有所帮助,但对于单个查询,这将有效。