T SQL累积减法

时间:2019-06-04 09:23:22

标签: sql sql-server tsql running-total

我正在使用MS SQL Server。

我有下表:

SKU     Shop    WeekNum    ShopPrioirty    Replen   OpeningStock
111     100         1            1           10          5000
111     200         1            2           10          NULL  
111     300         1            3           5           NULL
111     400         1            4           8           NULL

222     100         2            1           20          6000
222     200         2            2           15          NULL
222     300         2            3           12          NULL
222     400         2            4           10          NULL

这是期望的结果:

SKU     Shop    WeekNum    ShopPrioirty    Replen   OpeningStock
111     100         1            1           10          5000
111     200         1            2           10          4990  
111     300         1            3           5           4980
111     400         1            4           8           4975

222     100         2            1           20          6000
222     200         2            2           15          5980
222     300         2            3           12          5965
222     400         2            4           10          5953

在给定的一周内,SKU存在于多家商店中,并被分配了优先级。在优先级1,分配期初存货。

但是,我需要更新期初存量(当前为NULL)以等于先前的期初存量减去先前的Replen。

在尝试更新之前,我尝试执行SELECT

SELECT SKU
      ,Shop
      ,WeekNum
      ,StorePriority
      ,Replen
      ,OpeningStock
      ,OpeningStock - Replen OVER (ORDER BY SKU,Shop,WeekNum ROWS UNBOUNDED PRECEDING) AS Opening
       FROM [table1] t

但是我收到错误消息:Incorrect syntax near the keyword 'OVER'.

运行总和是正确的方法吗?

最好创建一个由SKU \ Shop \ WeekNum \ Priority组成的密钥吗?

谢谢。

1 个答案:

答案 0 :(得分:5)

我在您的class HereMapViewController: UIViewController { @IBOutlet private weak var mapView: NMAMapView! fileprivate var navigationManager = NMANavigationManager.sharedInstance() var route : NMAMapRoute? // MARK: - life cycle override func viewDidLoad() { super.viewDidLoad() setupUI() } deinit { navigationManager.stop() } // MARK: - private private func setupUI() { mapView.copyrightLogoPosition = .center mapView.zoomLevel = 10 mapView.positionIndicator.isVisible = true mapView.gestureDelegate = self navigationManager.voicePackageMeasurementSystem = NMAMeasurementSystem.imperialUS navigationManager.backgroundNavigationEnabled = true navigationManager.isVoiceEnabled = true navigationManager.delegate = self setupRoute() } private func setupRoute() { guard let mapRoute = route, let routeStart = mapRoute.route.start else { return } let start = NMAGeoCoordinates(latitude: routeStart.navigablePosition.latitude, longitude: routeStart.navigablePosition.longitude) mapView.add(mapObject: mapRoute) mapView.set(boundingBox: mapRoute.route.boundingBox!, animation: NMAMapAnimation.none) startNavigation() mapView.set(geoCenter: start, animation: .none) } private func startNavigation() { guard let route = route else { return } navigationManager.map = mapView NMAPositioningManager.sharedInstance().dataSource = NMAHEREPositionSource() navigationManager.startTurnByTurnNavigation(route.route) navigationManager.mapTrackingEnabled = false navigationManager.mapTrackingAutoZoomEnabled = false navigationManager.mapTrackingOrientation = .dynamic navigationManager.isSpeedWarningEnabled = true } } //MARK: - NMANavigationManagerDelegate extension HereMapViewController : NMANavigationManagerDelegate { // Signifies that there is new instruction information available func navigationManager(_ navigationManager: NMANavigationManager, didUpdateManeuvers currentManeuver: NMAManeuver?, _ nextManeuver: NMAManeuver?) { print("didUpdateManeuvers") } // Signifies that the system has found a GPS signal func navigationManagerDidFindPosition(_ navigationManager: NMANavigationManager) { print("New position has been found") } func navigationManager(_ navigationManager: NMANavigationManager, didFindAlternateRoute routeResult: NMARouteResult) { print("didFindAlternateRoute") } func navigationManager(_ navigationManager: NMANavigationManager, didUpdateRoute routeResult: NMARouteResult) { print("didUpdateRoute") } } //MARK: - NMAMapGestureDelegate extension HereMapViewController : NMAMapGestureDelegate { func mapView(_ mapView: NMAMapView, didReceiveTwoFingerPan translation: CGPoint, at location: CGPoint) { navigationManager.mapTrackingAutoZoomEnabled = false } } PARTITION BY子句中做了两个假设,但这可以使您获得想要的结果。由于您在ORDER BY的第一行中只有一个OpeningStock的值,因此我使用SKU来获得第一个值,然后除去所有先前 > FIRST_VALUE的值:

Replen