CTE错误:逗号附近的语法不正确

时间:2015-11-06 13:23:09

标签: c# sql syntax common-table-expression

有人可以点灯:

我在逗号附近收到错误:

  ;with cteClaims as (
    select a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
      FROM [spd].[claims].[Population] a                    
      group by a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
      having a.ClaimStatus not in ('Closed', 'Denied', 'Paid')                  
      ), ctePopulation as (  
      select a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                 
      FROM [spd].[claims].[Population] a                    
      join cteClaims b on a.LoanId = b.LoanId                   
      group by a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription)  
       select loanid as cteLoanId 
        from [spd].[claims].[Population] where id in (select id from ctePopulation)
   /// Error is here near this comma  below 
     , cteLoanBase as (
        select 
               a.LoanId as [Loan#]
             , a.AcquisitionDt as [AcquisitionDt]
             , a.CorpRecoverableBalanceAmt as [CorpRecoverableBalanceAmt]
             , a.EscrowBalance as [EscrowBalance]
             , a.FirstPaymentDueDt as [FirstPaymentDueDt]
             , a.InvestorLoanId as [InvestorLoanId]
             , a.InvestorPoolId as [InvestorPoolId]
             , a.LoanStatusId as [LoanStatusId]

2 个答案:

答案 0 :(得分:0)

只有在select定义完成后才应cte。代码在cte定义之间有select

 ;with cteClaims as (
select a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
  FROM [spd].[claims].[Population] a                    
  group by a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
  having a.ClaimStatus not in ('Closed', 'Denied', 'Paid')                  
  ), ctePopulation as (  
  select a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                 
  FROM [spd].[claims].[Population] a                    
  join cteClaims b on a.LoanId = b.LoanId                   
  group by a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription)  
 , cteLoanBase as ( ...)

 select loanid as cteLoanId 
 from [spd].[claims].[Population] where id in (select id from ctePopulation)

这里还要提一点:group by正在使用cte,但在任何列上都没有使用聚合。这应该避免。

答案 1 :(得分:0)

如果您更好地缩进代码,也许会有所帮助......

您的代码:

 ;with 
    cteClaims as (
        SELECT a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
        FROM [spd].[claims].[Population] a                    
            GROUP BY a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                   
              HAVING a.ClaimStatus not in ('Closed', 'Denied', 'Paid')),
    ctePopulation as (  
        SELECT a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription                 
        FROM [spd].[claims].[Population] a                    
        JOIN cteClaims b on a.LoanId = b.LoanId                   
        GROUP BY a.LoanId, a.ID, a.ClaimType, a.ClaimStatus, a.ClaimTypeDescription),
    cteLoanBase as (
        SELECT 
                   a.LoanId as [Loan#]
                 , a.AcquisitionDt as [AcquisitionDt]
                 , a.CorpRecoverableBalanceAmt as [CorpRecoverableBalanceAmt]
                 , a.EscrowBalance as [EscrowBalance]
                 , a.FirstPaymentDueDt as [FirstPaymentDueDt]
                 , a.InvestorLoanId as [InvestorLoanId]
                 , a.InvestorPoolId as [InvestorPoolId]
                 , a.LoanStatusId as [LoanStatusId]
    FROM ???  )


    SELECT loanid as cteLoanId 
        FROM [spd].[claims].[Population] 
    WHERE id in (select id from ctePopulation)

我的猜测:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
  #tooltip {
    color: white;
    opacity: .9;
    background: #333;
    padding: 5px;
    border: 1px solid lightgrey;
    border-radius: 5px;
    position: absolute;
    z-index: 10;
    visibility: hidden;
    white-space: nowrap;
    pointer-events: none;
  }
  #circle circle {
    fill: none;
    pointer-events: all;
  }
  path.group {
    fill-opacity: .8;
  }
  path.chord {
    fill-opacity: .8;
    stroke: #000;
    stroke-width: .25px;
  }
  #circle:hover path.fade {
    display: none;
  }
</style>
    </head>
   <body>
<div id="tooltip"></div>
<script src="lib/d3.js"></script>
<script src="lib/underscore.js"></script>
<script src="js/mapper.js"></script>
<script>


 //*******************************************************************
  //  CREATE MATRIX AND MAP

 //*******************************************************************
  d3.csv('data/CNV.csv', function (error, data) {


    var mpr = chordMpr(data);

    mpr
      .addValuesToMap('chr_no')
      .addValuesToMap('MUT')

    mpr  .setFilter(function (row, a, b) {
        return (row.chr_no === a.name && row.MUT === b.name) ||
               (row.chr_no === b.name && row.MUT === a.name)

      })
      .setAccessor(function (recs, a, b) {
        if (!recs[0]) return 0;
          return recs[0].MUT === a.name ? +recs[0].chr_start :    
  +recs[0].chr_stop ; 
      });
    drawChords(mpr.getMatrix(), mpr.getMap());
  });


 //*******************************************************************
  //  DRAW THE CHORD DIAGRAM

//*******************************************************************
  function drawChords (matrix, mmap) {
    var w = 980, h = 800, r1 = h / 2, r0 = r1 - 110;

    var fill = d3.scale.ordinal()

  .range(['#c7b570','#c6cdc7','#335c64','#768935',
  '#507282','#5c4a56','#aa7455','#574109','#837722',
'#73342d','#0a5564','#9c8f57','#7895a4','#4a5456',
'#b0a690','#0a3542',]);

    var chord = d3.layout.chord()
        .padding(.04)
        .sortSubgroups(d3.descending)
        .sortChords(d3.descending);

    var arc = d3.svg.arc()
        .innerRadius(r0)
        .outerRadius(r0 + 20);

    var svg = d3.select("body").append("svg:svg")
        .attr("width", w)
        .attr("height", h)
      .append("svg:g")
        .attr("id", "circle")
        .attr("transform", "translate(" + w / 2 + "," + h / 2 + ")");

        svg.append("circle")
            .attr("r", r0 + 20);

    var rdr = chordRdr(matrix, mmap);
    chord.matrix(matrix);

    var g = svg.selectAll("g.group")
        .data(chord.groups())
      .enter().append("svg:g")
        .attr("class", "group")
        .on("mouseover", mouseover)
        .on("mouseout", function (d) {    
  d3.select("#tooltip").style("visibility", "hidden") });

    g.append("svg:path")
        .style("stroke", "black")
        .style("fill", function(d) { return fill(rdr(d).gname); })
        .attr("d", arc);

    g.append("svg:text")
        .each(function(d) { d.angle = (d.startAngle + d.endAngle) / 2;      
     })
        .attr("dy", ".35em")
        .style("font-family", "helvetica, arial, sans-serif")
        .style("font-size", "9px")
        .attr("text-anchor", function(d) { return d.angle > Math.PI ? 
       "end" : null; })
        .attr("transform", function(d) {
          return "rotate(" + (d.angle * 180 / Math.PI - 90) + ")"
              + "translate(" + (r0 + 26) + ")"
              + (d.angle > Math.PI ? "rotate(180)" : "");
        })
        .text(function(d) { return rdr(d).gname; });

      var chordPaths = svg.selectAll("path.chord")
            .data(chord.chords())
          .enter().append("svg:path")
            .attr("class", "chord")
            .style("stroke", function(d) { return 
       d3.rgb(fill(rdr(d).sname)).darker(); })
            .style("fill", function(d) { return fill(rdr(d).sname); })
            .attr("d", d3.svg.chord().radius(r0))
            .on("mouseover", function (d) {
              d3.select("#tooltip")
                .style("visibility", "visible")
                .html(chordTip(rdr(d)))
                .style("top", function () { return (d3.event.pageY - 
       170)+"px"})
                .style("left", function () { return (d3.event.pageX - 
           100)+"px";})
            })
            .on("mouseout", function (d) { 
         d3.select("#tooltip").style("visibility", "hidden") });


      function chordTip (d) {

  var p = d3.format(".0%"), q = d3.format("0d")
        return "Choromosome information:<br/>"
          + q(d.sname) + " overlap with " + d.tname +"  " +
 "<br/>chromosome starts at"+" "+ d.sdata +"  " +
 "<br/>chromosome ends at"+ " "+ d.tdata

      }


      }

      function mouseover(d, i) {
        d3.select("#tooltip")
          .style("visibility", "visible")
          .html(groupTip(rdr(d)))
          .style("top", function () { return (d3.event.pageY - 80)+"px"})
          .style("left", function () { return (d3.event.pageX - 130)+"px";})

        chordPaths.classed("fade", function(p) {
          return p.source.index != i
              && p.target.index != i;
        });
      }
  }

</script>

很难猜到你想要什么......

  • 你最后错过了一个括号。
  • 您应该在主要选择之前定义“With”子句(在主要选择之后定义cteLoadBase)。
  • 您错过了一个FROM