SQL Left(column,6)函数 - 我做错了什么

时间:2016-12-20 01:10:42

标签: sql sql-server

所以我有这个查询

class ApiWrapper extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      response: {
        "title": 'nothing fetched yet'
      }
    };
  }

  componentDidMount() {
    this._makeApiCall(this.props.endpoint);
  }

  _makeApiCall(endpoint) {
    fetch(endpoint).then(function(response) {
      this.setState({
        response: response
      });
    }.bind(this))
  }

  render() {
    return <Child data = {
      this.state.response
    }
    />;
  }
}

class Child extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: props.data
    };
  }

  render() {
    console.log(this.state.data, 'new data');
    return ( < span > {
      this.state.data.title
    } < /span>);
  };
}

var element = < ApiWrapper endpoint = "https://jsonplaceholder.typicode.com/posts/1" / > ;

ReactDOM.render(
  element,
  document.getElementById('container')
);

我刚刚意识到我需要选择不同的帐户,但只需要前6个字母。此代码从2个不同的表中选择信息,并且我需要仅查看前6个字母组,然后显示结果。我尝试了一些事情,比如选择不同的左(帐号,6)作为帐户,但我得到一个无效的列名称错误与我尝试的任何组合。我缺少什么

我使用microsoft managment studio 2012,而sql server本身就是2000

2 个答案:

答案 0 :(得分:0)

试试这个:

Select              
    ACCT_SUMM.Account, ACCT_SUMM.TotalTrades,ACCT_SUMM.TotalRev, ACCT_SUMM.US_Options, ACCT_SUMM.CAD_Options, ACCT_SUMM.USStock, ACCT_SUMM.CADStock, ACCT.ACCT_OPEN_DATE, ACCT_SUMM.ACCTYPE,                                                                    
        CASE                
           WHEN Min(YEAR(GETDATE()) - YEAR(ACCT_OPEN_DATE)) <= 1 THEN 'New_Accounts'   
           WHEN Min(YEAR(GETDATE()) - YEAR(ACCT_OPEN_DATE)) > 1  THEN 'Old_Account'             
        END as 'ClientAgeSeg'   
From [dbo].[WF_PM_ACCT_DB] ACCT 
    Join (          
    Select LEFT(Account,6) AS Account, Sum(DB_12) as 'TotalTrades',Sum(DB_3) as 'TotalRev', Sum(DB_12_1_6) as 'US_Options', Sum(DB_12_1_5) as 'CAD_Options', Sum(DB_12_5) as 'USStock',Sum(DB_12_4) as 'CADStock',
            CASE 
                WHEN SUM(DB_12) = 0 THEN 'INACTIVE'
                WHEN SUM(DB_12) > 0 and SUM(DB_12) <=119  and Sum(DB_12_1_6) = 0 and Sum(DB_12_1_5) = 0   THEN 'Engaged Stock Pure'
                WHEN SUM(DB_12) > 119  and Sum(DB_12_1_6) = 0 and Sum(DB_12_1_5) = 0   THEN 'Active Stock Pure'
                WHEN (Sum(DB_12_1_6) > 120 OR Sum(DB_12_1_5) > 120) And (SUM(DB_12_4)<=0  and SUM(DB_12_5)<=0)   THEN ' Pure Active OPT'
                WHEN (Sum(DB_12_1_6) <= 119 OR Sum(DB_12_1_5) <= 119) And (SUM(DB_12_4)<=0  And SUM(DB_12_5)<=0) THEN 'Pure Engaged OPT'
                WHEN (Sum(DB_12_1_6) > 120 OR Sum(DB_12_1_5) > 120) And (SUM(DB_12_4)>0  or SUM(DB_12_5)>0)   THEN 'Mix Active OPT'
                WHEN (Sum(DB_12_1_6) <= 119 OR Sum(DB_12_1_5) <= 119) And (SUM(DB_12_4)>0  or SUM(DB_12_5)>0)   THEN 'Mix Engaged OPT'
                Else 'Unclassified'
            END as 'ACCTYPE'
    FROM WEALTHDB.DBO.WF_PM_SUM_DB
    Group by LEFT(Account,6)
    ) ACCT_SUMM
    On LEFT(ACCT.Account,6) = ACCT_SUMM.Account 
Group by ACCT_SUMM.Account, ACCT_SUMM.TotalTrades, ACCT_SUMM.US_Options, ACCT_SUMM.CAD_Options, ACCT_SUMM.USStock, ACCT_SUMM.CADStock, ACCT.ACCT_OPEN_DATE, ACCT_SUMM.TotalRev, ACCT_SUMM.ACCTYPE                                                         

答案 1 :(得分:0)

这不起作用,因为在使用LEFT()等功能时会丢失列名。您的加入结果需要帐户的列名称。就像在DVT的回答中一样,你应该使用LEFT(Account,6) AS Account

这将允许您从加入SELECT ACCT_SUMM.Account