将多个查询聚合为一个

时间:2017-01-18 22:45:36

标签: sql sql-server

我正在尝试将多个查询合并到一个总体摘要查询中。我当前的尝试发出错误,因为我的子查询(SELECT (oitems.numitems * oitems.unitprice) AS LineTotal FROM oitems INNER JOIN orders ON oitems.orderid = orders.orderid) AS OrderTotal返回的结果超过1。

我的基本查询:

SELECT orders.invoicenum AS InvoiceId, customers.contactid AS CustomerId, FORMAT(orders.odate, 'yyyy-MM-dd') AS OrderDate, 
   orders.oshipfirstname AS FirstName, orders.oshiplastname AS LastName, orders.oshipcompany AS Company, 
   orders.oshipaddress AS Address1, orders.oshipaddress2 AS Address2, orders.oshipcity AS City, orders.oshipcountry AS Country, 
   orders.oshipstate AS State, orders.oshipzip AS Zip, 'Direct' AS Channel, orders.oemail AS Email
FROM orders LEFT JOIN customers ON orders.ocustomerid = customers.contactid
WHERE (orders.invoicenum > 0);

SELECT oitems.orderitemid AS OrderItemId, orders.invoicenum AS InvoiceId, oitems.itemid AS Sku, oitems.numitems AS Qty, oitems.unitprice AS Price 
FROM orders INNER JOIN oitems ON orders.orderid = oitems.orderid;

SELECT (oitems.numitems * oitems.unitprice) AS LineTotal FROM oitems;

现在我的组合尝试:

SELECT CustomerId, InvoiceId, OrderDate, FirstName, LastName, Company, Address1, Address2, City, Country, State, Zip, Channel, Email, OrderTotal, ShipFee, Tax 
FROM (SELECT orders.invoicenum AS InvoiceId, customers.contactid AS CustomerId, FORMAT(orders.odate, 'yyyy-MM-dd') AS OrderDate, 
         orders.oshipfirstname AS FirstName, orders.oshiplastname AS LastName, orders.oshipcompany AS Company, 
         orders.oshipaddress AS Address1, orders.oshipaddress2 AS Address2, orders.oshipcity AS City, orders.oshipcountry AS Country, 
         orders.oshipstate AS State, orders.oshipzip AS Zip, 'Direct' AS Channel, orders.oemail AS Email, orders.oshipcost AS ShipFee, orders.otax AS Tax, 
         (SELECT (oitems.numitems * oitems.unitprice) AS LineTotal FROM oitems INNER JOIN orders ON oitems.orderid = orders.orderid) AS OrderTotal 
      FROM orders LEFT JOIN customers ON orders.ocustomerid = customers.contactid
      WHERE (orders.invoicenum > 0)) BaseData;

如何构建此查询以返回预期数据?

预期数据:

CustomerId, InvoiceId, OrderDate, FirstName, LastName, Company, Address1, Address2, City, Country, State, Zip, Channel, Email, OrderTotal, ShipFee, Tax 

2 个答案:

答案 0 :(得分:2)

这个重新组织的查询应该更接近你想要的。有时,对派生表的JOIN更简单。

SELECT CustomerId, 
    InvoiceId, 
    OrderDate, 
    FirstName, 
    LastName, 
    Company, 
    Address1, 
    Address2, 
    City, 
    Country, 
    State, 
    Zip, 
    Channel, 
    Email, 
    OrderTotal, 
    ShipFee, 
    Tax 
FROM (
    SELECT 
        orders.invoicenum AS InvoiceId, 
        customers.contactid AS CustomerId, 
        FORMAT(orders.odate, 'yyyy-MM-dd') AS OrderDate, 
        orders.oshipfirstname AS FirstName, 
        orders.oshiplastname AS LastName, 
        orders.oshipcompany AS Company, 
        orders.oshipaddress AS Address1, 
        orders.oshipaddress2 AS Address2, 
        orders.oshipcity AS City, 
        orders.oshipcountry AS Country, 
        orders.oshipstate AS State, 
        orders.oshipzip AS Zip, 
        'Direct' AS Channel, 
        orders.oemail AS Email, 
        orders.oshipcost AS ShipFee, 
        orders.otax AS Tax, 
        OrderTotal.OrdTotal
    FROM orders 
    LEFT JOIN customers 
        ON orders.ocustomerid = customers.contactid
    LEFT JOIN (
        SELECT 
            oitems.orderid,
            Sum(oitems.numitems * oitems.unitprice) OrdTotal 
        FROM oitems
        GROUP BY oitems.Orderid
        ) AS OrderTotal 
        ON OrderTotal.OrderID = orders.orderid
    WHERE orders.invoicenum > 0
    ) BaseData;

答案 1 :(得分:1)

也许你应该写fill而不是df %>% group_by(Size, cut_Price = Hmisc::cut2(Price, cuts = seq(4, 13, .5), oneval = FALSE), Type) %>% summarise_at(c("Opps", "NumberofSales", "Revenue"), sum) %>% ungroup() %>% complete(Size, cut_Price, Type) ## # A tibble: 57 × 6 ## Size cut_Price Type Opps NumberofSales Revenue ## <fctr> <fctr> <fctr> <dbl> <dbl> <dbl> ## 1 LARGE [ 3.63, 4.00) desktop NA NA NA ## 2 LARGE [ 4.00, 4.50) desktop NA NA NA ## 3 LARGE [ 4.50, 5.00) desktop NA NA NA ## 4 LARGE [ 5.00, 5.50) desktop NA NA NA ## 5 LARGE [ 5.50, 6.00) desktop 477870 342455 2037.67 ## 6 LARGE [ 6.00, 6.50) desktop 842882 523309 3292.29 ## 7 LARGE [ 6.50, 7.00) desktop NA NA NA ## 8 LARGE [ 7.00, 7.50) desktop NA NA NA ## 9 LARGE [ 7.50, 8.00) desktop 283107 149878 1189.56 ## 10 LARGE [ 8.00, 8.50) desktop NA NA NA ## # ... with 47 more rows

sum(oitems.numitems * oitems.unitprice)