private void LoadQueryWithDerivedTableAndCte()
        {
            UnionSubQuery unionSubQuery = _query.QueryRoot.FirstSelect();

            // Derived Table
            SQLFromQuery fq = new SQLFromQuery(_query.SQLContext)
            {
                Alias = new SQLAliasObjectAlias(_query.SQLContext)
                {
                    Alias = _query.QueryRoot.CreateUniqueSubQueryName()
                },
                SubQuery = new SQLSubSelectStatement(_query.SQLContext)
            };

            SQLSubQuerySelectExpression sqse = new SQLSubQuerySelectExpression(_query.SQLContext);

            fq.SubQuery.Add(sqse);
            sqse.SelectItems = new SQLSelectItems(_query.SQLContext);
            sqse.From        = new SQLFromClause(_query.SQLContext);

            DataSourceQuery dataSourceQuery = (DataSourceQuery)_query.AddObject(unionSubQuery, fq, typeof(DataSourceQuery));
            UnionSubQuery   usc             = dataSourceQuery.SubQuery.FirstSelect();
            DataSource      dsDerivedTable  = _query.AddObject(usc, "MyDB.MySchema.SalesOrderHeader");

            QueryColumnListItem ciDerivedTable1 = usc.QueryColumnList.AddField(dsDerivedTable, "OrderDate");

            ciDerivedTable1.Selected = true;

            QueryColumnListItem ciDerivedTable2 = usc.QueryColumnList.AddField(dsDerivedTable, "SalesOrderID");

            ciDerivedTable2.ConditionStrings[0] = "> 25";

            // CTE
            SQLQualifiedName qn = new SQLQualifiedName(_query.SQLContext);

            if (!unionSubQuery.QueryRoot.SQLContext.SyntaxProvider.IsSupportCTE())
            {
                return;
            }

            DataSource dataSourceCte = null;

            try {
                AstTokenIdentifier withClauseItemName = _query.QueryRoot.CreateUniqueCTEName("CTE");

                qn.Add(withClauseItemName);

                SubQuery parentSubQuery = unionSubQuery.ParentSubQuery ?? unionSubQuery.QueryRoot as SubQuery;

                if (parentSubQuery.IsMainQuery)
                {
                    _query.QueryRoot.AddNewCTE(null, withClauseItemName);
                }
                else
                {
                    if (parentSubQuery.IsSubQueryCTE())
                    {
                        int index = parentSubQuery.GetSubQueryCTEIndex();
                        parentSubQuery.InsertNewCTE(index, null, withClauseItemName);
                    }
                    else
                    {
                        parentSubQuery.AddNewCTE(null, withClauseItemName);
                    }
                }

                if (_query.IsUniqueAlias(unionSubQuery, withClauseItemName))
                {
                    dataSourceCte = _query.AddObject(unionSubQuery, qn, null);
                }
                else
                {
                    string withClauseItemNameStr = withClauseItemName.GetSQL(new SQLGenerationOptions());
                    using (AstTokenIdentifier alias = _query.CreateUniqueAlias(unionSubQuery, withClauseItemNameStr)) {
                        dataSourceCte = _query.AddObject(unionSubQuery, qn, alias);
                    }
                }
            } finally {
                qn.Dispose();
            }

            SubQuery cte = _query.QueryRoot.GetSubQueryCTEList().FirstOrDefault();

            if (cte == null)
            {
                return;
            }


            UnionSubQuery unionSubQueryCte = cte.FirstSelect();
            DataSource    ds1 = _query.AddObject(unionSubQueryCte, "MyDB.MySchema.Customers");

            // create output column
            QueryColumnListItem ci1 = unionSubQueryCte.QueryColumnList.AddField(ds1, "CustomerName");

            ci1.Selected = true;

            // create output column
            QueryColumnListItem ci2 = unionSubQuery.QueryColumnList.AddField(dataSourceCte, "CustomerName");

            ci2.Selected = true;
        }