SQL递归部分和

时间:2016-11-22 10:35:31

标签: sql sql-server sqlite

我有两张桌子:

MasterTable

...
startSubNr
endSubNr
...

步骤

...
nrFrom
nrTo
value
...

MasterTable包含例如:

startSubNr: 2
endSubNr: 16

步骤包含例如:

nrFrom: 2
nrTo: 19
value: 20

nrFrom: 1
nrTo: 2
value: 10

nrFrom: 19
nrTo: 5
value: 100

nrFrom: 5
nrTo: 16
value: 200

nrFrom: 4
nrTo: 5
value: 50

我需要的是一个sql查询(应该适用于SQL-Server和sqlite)来计算从startnr到endnr的总和,其中步数可以变化。步骤(从 - 到)是独一无二的。

在此示例中,它必须总结 20 2-19 ), 100 19-5 )和 200 5-16 ),同时忽略1-2和4-5的值。

有没有办法在sql中执行此操作而不必使用多个查询?

2 个答案:

答案 0 :(得分:1)

    CREATE TABLE #Steps(nrFrom INT,nrTo INT ,value INT) DECLARE @Start INT = 2,@End INT = 16

    INSERT INTO #Steps(nrFrom ,nrTo ,value ) SELECT 2,19,20 UNION ALL SELECT 1,2,10 UNION ALL SELECT 19,5,100 UNION ALL SELECT 5,16,200 UNION ALL SELECT 4,5,50

    ;WITH _SumCTE (_From , _To , _Value )AS ( SELECT nrFrom ,nrTo,value FROM #Steps WHERE nrFrom = @Start UNION ALL SELECT _To , nrTo , value FROM #Steps JOIN _SumCTE ON _To = nrFrom and nrTo != @End

    )

    SELECT SUM(_Value) FROM _SumCTE

答案 1 :(得分:1)

这个递归查询做了你想要的事情,只有几个附带条件

  • 如果无法到达所需的端点,将抛出最大递归错误(例如,如果@end为1000)
  • 它假设nrFrom是唯一的;如果给定nrFrom中有多个“路径”,则会得到奇怪的结果
  • 循环路径将导致最大递归错误
  • sql可能不是这个逻辑的地方!

create table #step(nrFrom int,nrTo int,value int);

var json = '{"reports":[{"columnHeader":{"dimensions":["ga:landingPagePath"],"metricHeader":{"metricHeaderEntries":[{"name":"ga:pageviews","type":"INTEGER"},{"name":"ga:sessions","type":"INTEGER"}]}},"data":{"rows":[{"dimensions":["/-chandigarh/axis-bank-sarsini-branch_chandigarh_chg_850458.html"],"metrics":[{"values":["1","1"]}]},{"dimensions":["/267249-1.compliance-alex.xyz"],"metrics":[{"values":["29","10"]}]},{"dimensions":["/267249-1.compliance-don.xyz"],"metrics":[{"values":["27","9"]}]},{"dimensions":["/267249-1.compliance-fred.xyz"],"metrics":[{"values":["20","7"]}]},{"dimensions":["/abohar/axis-bank-the-fazilka-central-cooperative-bank-ltd-branch_abohar_frp_135.html"],"metrics":[{"values":["1","1"]}]},{"dimensions":["/about-us/career.htm"],"metrics":[{"values":["8","5"]}]},{"dimensions":["/about-us/company-profile.htm"],"metrics":[{"values":["34","14"]}]},{"dimensions":["/about-us/infrastructure.htm"],"metrics":[{"values":["3","1"]}]},{"dimensions":["/adilabad/gk-hospital-multispeciality-care_adilabad_adi_399806.html"],"metrics":[{"values":["2","1"]}]},{"dimensions":["/ahmedabad/akhani-jagdish-kumar_ahmedabad_ahd_1124498.html"],"metrics":[{"values":["7","3"]}]}],"totals":[{"values":["3420452","1333496"]}],"rowCount":347614,"minimums":[{"values":["0","1"]}],"maximums":[{"values":["56660","49274"]}],"isDataGolden":true},"nextPageToken":"1000"}]}'

// Parse the JSON into the data variable
var data = JSON.parse(json);

data.reports.forEach(report => {
  report.data.rows.forEach(row => {
    // row.dimensions will contain your 'dimensions' array
    console.log(row.dimensions);
    
    row.metrics.forEach(metric => {
      // metric.values will contain your 'values' array
      console.log(metric.values);
    });
  });
});