获取结果和帐单总数的一定百分比

时间:2016-03-05 13:21:43

标签: sql sql-server sql-server-2008 sql-server-2012

我需要所有应用程序中某些生产者的销售总数值的百分比结果是,例如,12%。

此代码计算订单数量:

SELECT COUNT(*)
FROM (
  SELECT id
  FROM OrderDetails
  WHERE Product IN ('p1','p9')
  GROUP BY id
  HAVING COUNT(DISTINCT Product) = 2) AS t

如果您阅读How to select two products where it is in same OrderDetails

,即可了解我

2 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题,您需要拥有这两种产品的订单比例。您可以这样做:

ValueError: Trying to store a string with len [5761] in [values_block_0]
column but this column has a limit of [4430]!
Consider using min_itemsize to preset the sizes on these columns

答案 1 :(得分:0)

USE [Northwind]

SELECT
    CONVERT
    (VARCHAR,
        CONVERT
        (DECIMAL(3,2),
            CONVERT
            (DECIMAL(3, 2),
                (SELECT COUNT(*)
                    FROM (
                        SELECT [OrderID]
                            FROM  [Northwind].[dbo].[Order Details]
                            WHERE [ProductID] IN (41,51)
                            GROUP BY [OrderID]
                            HAVING COUNT(DISTINCT [ProductID]) = 2
                    ) AS T
                )
            )
            /
            (SELECT COUNT(*)
                FROM [Northwind].[dbo].[Orders]
            )
            *
            100
        )
    )
    +
    '%'