合并两个计数日期名称查询

时间:2016-08-31 08:25:26

标签: sql-server tsql merge

查询

$(document).ready(function(){
    $.ajax({
        url : "http://192.100.1.100/content.php",
        type : "GET",
        success : function(data){
            console.log(data);

            var userid = [];
            var facebook_follower = [];
            var twitter_follower = [];
            var googleplus_follower = [];

            for(var i in data) {
                userid.push("UserID " + data[i].userid);
                facebook_follower.push(data[i].facebook);
                twitter_follower.push(data[i].twitter);
                googleplus_follower.push(data[i].googleplus);
            }

            var chartdata = {
                labels: userid,
                datasets: [
                    {
                        label: "facebook",
                        fill: false,
                        lineTension: 0.1,
                        backgroundColor: "rgba(59, 89, 152, 0.75)",
                        borderColor: "rgba(59, 89, 152, 1)",
                        pointHoverBackgroundColor: "rgba(59, 89, 152, 1)",
                        pointHoverBorderColor: "rgba(59, 89, 152, 1)",
                        data: facebook_follower
                    },
                    {
                        label: "twitter",
                        fill: false,
                        lineTension: 0.1,
                        backgroundColor: "rgba(29, 202, 255, 0.75)",
                        borderColor: "rgba(29, 202, 255, 1)",
                        pointHoverBackgroundColor: "rgba(29, 202, 255, 1)",
                        pointHoverBorderColor: "rgba(29, 202, 255, 1)",
                        data: twitter_follower
                    },
                    {
                        label: "googleplus",
                        fill: false,
                        lineTension: 0.1,
                        backgroundColor: "rgba(211, 72, 54, 0.75)",
                        borderColor: "rgba(211, 72, 54, 1)",
                        pointHoverBackgroundColor: "rgba(211, 72, 54, 1)",
                        pointHoverBorderColor: "rgba(211, 72, 54, 1)",
                        data: googleplus_follower
                    }
                ]
            };

            var ctx = $("#mycanvas");

            var LineGraph = new Chart(ctx, {
                type: 'line',
                data: chartdata
                });
            },
        error : function(data) {
            }
        });
    });

给出每个工作日每小时的订单总数

结果

SELECT 
       DATENAME(dw, O1.CreationDateTime) AS 'Day of the week', 
       DATENAME(hh, O1.CreationDateTime) AS hour, 
       COUNT(*) AS 'Nr. Orders'
FROM 
       Orders AS O1
WHERE 
       O1.CreationDateTime >= '2015-10-01' 
GROUP BY 
       DATENAME(dw, O1.CreationDateTime), DATENAME(hh, O1.CreationDateTime)
ORDER BY 
       [Day of the week], CAST(DATENAME(hh, O1.CreationDateTime) as int)

目标是获得当天每小时的平均订单数量。但有些日子没有订单那些日子不应该算在内。 为了解决这个问题,我在下面编写了select distinct查询。

查询

Weekday Hour    Nr.Orders
Friday  0   10
Friday  1   11
Friday  2   2

例如,它具有如下所示的订单周五的数量。

结果

SELECT 
       DATENAME(dw,Formatteddate.Date) AS "Day of the week",
       COUNT(*) AS "Weekdays in period"
FROM
       (SELECT DISTINCT
       ((CONVERT (date,O2.CreationDateTime,112))) AS Date
FROM Orders AS O2
WHERE 
       O2.CreationDateTime >= '2015-10-01' 
       ) AS Formatteddate
GROUP BY DATENAME(dw,Formatteddate.Dates) 
  

基本上我的问题是两个如何合并我使用外部的那两个查询   适用于变通方法。然而,由于事实它很慢   检查每一行。我想我需要使用over子句但我没有   想法如何使用datename函数。

最终结果看起来应该像平均一样容易计算。

Day of the week | Week days in period (with order)
    Friday            32
    Monday            30
    Saturday          21

2 个答案:

答案 0 :(得分:1)

您可以在一周的某一天将两张桌子加在一起:

SELECT t1.[Day of the week] AS Weekday,
       t1.hour,
       t1.[Nr. Orders],
       COALESCE(t2.[Weekdays in period], 0)
FROM
(
    SELECT DATENAME(dw, O1.CreationDateTime) AS 'Day of the week', 
           DATENAME(hh, O1.CreationDateTime) AS hour, 
           COUNT(*) AS 'Nr. Orders'
    FROM Orders AS O1
    WHERE O1.CreationDateTime >= '2015-10-01' 
    GROUP BY DATENAME(dw, O1.CreationDateTime),
             DATENAME(hh, O1.CreationDateTime)
) t1
LEFT JOIN
(
    SELECT DATENAME(dw,Formatteddate.Date) AS "Day of the week",
           COUNT(*) AS "Weekdays in period"
    FROM
    (
        SELECT DISTINCT CONVERT (date,O2.CreationDateTime, 112) AS Date
        FROM Orders AS O2
        WHERE O2.CreationDateTime >= '2015-10-01' 
    ) AS Formatteddate
    GROUP BY DATENAME(dw,Formatteddate.Dates)
) t2
    ON t1.[Day of the week] = t2.[Day of the week]

答案 1 :(得分:0)

尝试加入当天:

WITH DayO AS (
SELECT 
       DATENAME(dw,Formatteddate.Date) AS DayOfWeek,
       COUNT(*) AS PeriodDays
FROM
       (SELECT DISTINCT
       ((CONVERT (date,O2.CreationDateTime,112))) AS Date
FROM Orders AS O2
WHERE 
       O2.CreationDateTime >= '2015-10-01' 
       ) AS Formatteddate
GROUP BY DATENAME(dw,Formatteddate.Dates) 
)

, HourO AS (
SELECT 
       DATENAME(dw, O1.CreationDateTime) AS DayOfWeek, 
       DATENAME(hh, O1.CreationDateTime) AS OrdHour, 
       COUNT(*) AS NumOrd
FROM 
       Orders AS O1
WHERE 
       O1.CreationDateTime >= '2015-10-01' 
GROUP BY 
       DATENAME(dw, O1.CreationDateTime), DATENAME(hh, O1.CreationDateTime)
ORDER BY 
       [DayOfWeek], CAST(DATENAME(hh, O1.CreationDateTime) as int)
)

SELECT ho.DayOfWeek as Weekday, ho.OrdHour as Hour, ho.NumOrd as [Nr. Orders], DayO.PeriodDays as [Weekdays in period]
from HourO ho
inner join DayO
on ho.DayOfWeek = DayO.DayOfWeek