针对同一列中的另一个函数计算窗口函数

时间:2018-07-11 19:25:04

标签: sql sql-server window-functions

嗨,我正在使用SQL Server。我有一组数据,我正在尝试使用窗口函数来计算某事的实例。然后,我再次运行window函数,并将其与行计数window函数相除,以尝试获取平均值。但是,我一直得到0。是否可以针对同一列中的另一个函数运行窗口函数?

COUNT(city) OVER (partition by state)/Count(*) over (partition by total)*100 AS AVG

像这样可能吗?当我将其分解并分别运行时,它可以工作,但是当我将它们组合时,我在列中得到0

1 个答案:

答案 0 :(得分:3)

这是因为整数运算-与窗口函数无关。编写为:

namespace PrintServiceTest2
{
    using Android.Print;
    using Android.PrintServices;
    using System.Collections.Generic;

    public class TransferPrintService : PrintService
    {
        private object mFirstFakePrinter;
        private object mSecondFakePrinter;

        public TransferPrintService()
        {
            MainActivity.ShowToast("TransferPrintService");
        }

        public override void OnCreate()
        {
            mFirstFakePrinter = new PrinterInfo.Builder(GeneratePrinterId("Printer 1"),
                "SHGH-21344", PrinterStatus.Idle).Build();
            mSecondFakePrinter = new PrinterInfo.Builder(GeneratePrinterId("Printer 2"),
                    "OPPPP-09434", PrinterStatus.Idle).Build();
            MainActivity.ShowToast("OnCreate");
            base.OnCreate();
        }

        protected override void OnPrintJobQueued(Android.PrintServices.PrintJob printJob)
        {
            printJob.Start();
            printJob.Complete();
        }

        protected override PrinterDiscoverySession OnCreatePrinterDiscoverySession()
        {
            MainActivity.ShowToast("OnCreatePrinterDiscoverySession");
            return new TransferPrinterDiscoverySession(this);
        }

        protected override void OnRequestCancelPrintJob(Android.PrintServices.PrintJob printJob)
        {
        }

        protected override void OnConnected()
        {
            MainActivity.ShowToast("OnConnected");
            base.OnConnected();
        }

        protected override void OnDisconnected()
        {
            MainActivity.ShowToast("OnDisconnected");
            base.OnDisconnected();
        }

        public override void OnDestroy()
        {
            MainActivity.ShowToast("OnDestroy");
            base.OnDestroy();
        }
    }

    public class TransferPrinterDiscoverySession : PrinterDiscoverySession
    {
        private readonly TransferPrintService printService;
        private const string PRINTER = "Transfer Printer";

        public TransferPrinterDiscoverySession(TransferPrintService printService)
        {
            this.printService = printService;
        }

        public override void OnStartPrinterDiscovery(IList<PrinterId> priorityList)
        {
            PrinterId id = printService.GeneratePrinterId(PRINTER);
            PrinterInfo.Builder builder =
                    new PrinterInfo.Builder(id, PRINTER, PrinterStatus.Idle);
            PrinterInfo info = builder.Build();
            List<PrinterInfo> infos = new List<PrinterInfo>
            {
                info
            };
            AddPrinters(infos);
        }

        public override void OnStopPrinterDiscovery()
        {
        }

        public override void OnValidatePrinters(IList<PrinterId> printerIds)
        {
            MainActivity.mainToast.Show();
        }

        public override void OnStartPrinterStateTracking(PrinterId printerId)
        {
            PrinterInfo.Builder builder = new PrinterInfo.Builder(printerId, PRINTER, PrinterStatus.Idle);
            PrinterCapabilitiesInfo.Builder capBuilder =
                    new PrinterCapabilitiesInfo.Builder(printerId);

            capBuilder.AddMediaSize(PrintAttributes.MediaSize.IsoA4, true);
            capBuilder.AddResolution(new PrintAttributes.Resolution(
                    "Default", "Default", 360, 360), true);
            capBuilder.SetColorModes(3, 2);
            capBuilder.SetMinMargins(PrintAttributes.Margins.NoMargins);

            PrinterCapabilitiesInfo caps = capBuilder.Build();
            builder.SetCapabilities(caps);
            PrinterInfo info = builder.Build();
            List<PrinterInfo> infos = new List<PrinterInfo>
            {
                info
            };
            AddPrinters(infos);
        }

        public override void OnStopPrinterStateTracking(PrinterId printerId)
        {
            MainActivity.mainToast.Show();
        }

        public override void OnDestroy()
        {
        }
    }
}

COUNT(city) OVER (partition by state) * 100.0 / Count(*) over (partition by total) AS AVG 在算术中放置一个小数点。因此1/2是0。但是1.0 / 2是0.5。