如何计算年初至今的kdb小计

时间:2019-03-21 03:34:27

标签: q kdb

我有一张这样的桌子:

2019.03m Bolts  100
2019.03m Nuts    50
2019.02m Bolts   10
2019.02m Nuts   100 
2019.01m Bolts   50
2019.01m Nuts    10
2018.12m Bolts   10
2018.12m Nuts    10
2018.11m Bolts   20
2018.11m Nuts    30

我想介绍一个称为“年初至今”列的新列

2019.03m Bolts  100 160
2019.03m Nuts    50 160
2019.02m Bolts   10  60
2019.02m Nuts   100 110 
2019.01m Bolts   50  50
2019.01m Nuts    10  10
2018.12m Bolts   10  30
2018.12m Nuts    10  40
2018.11m Bolts   20  20
2018.11m Nuts    30  30

这将对上一年度至今的行求和,并在新的一年重新设置。

我有一个使用sums的想法,但是到新的一年该如何重置?

2 个答案:

答案 0 :(得分:6)

我相信以下是您所追求的。注意我已经颠倒了表的顺序,最初是按时间升序。

import UIKit
import MapKit

class MapViewController: UIViewController, MKMapViewDelegate {
    // MARK: - Variables

    // MARK: - IBOutlet
    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.delegate = self
    }

    @IBAction func addNewPin(_ sender: UIBarButtonItem) {
        addNewAnnotationPin(title: "Restaurant", subTitle: "Barbecue", lat: 35.6387264874361, lon: 139.99950350000003)
    }

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !(annotation is MKUserLocation) else {
            return nil
        }

        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView?.pinTintColor = UIColor.red
            pinView?.canShowCallout = true
        }
        else {
            pinView?.annotation = annotation
        }
        return pinView
    }

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        if view.annotation is MyPointAnnotation {
            if let selectedAnnotation = view.annotation as? MyPointAnnotation {
                if let id = selectedAnnotation.identifier {
                    for pin in mapView.annotations as! [MyPointAnnotation] {
                        if let myIdentifier = pin.identifier {
                            if myIdentifier == id {
                                print(pin.lat ?? 0.0)
                                print(pin.lon ?? 0.0)
                            }
                        }
                    }
                }
            }
        }
    }

    func addNewAnnotationPin(title: String, subTitle: String, lat: Double, lon: Double) {
        let myPin = MyPointAnnotation()
        myPin.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
        myPin.title = title
        myPin.subtitle = subTitle
        myPin.identifier = UUID().uuidString
        myPin.lat = lat
        myPin.lon = lon
        self.mapView.addAnnotation(myPin)
    }
}

答案 1 :(得分:0)

如果您的表按日期排序(在示例中降序),则可以使用以下查询。否则,您可以在运行查询之前使用date xdesc对其进行订购。

q) update ytd:reverse sums reverse num by date.year,name from t