基于不同日期字段的SQL中的比率计算

时间:2015-05-15 07:58:32

标签: mysql sql

我想计算每天的联系人数/订单数量 这就是我的样本:

orderID orderdate   ContactID   contact date
    1   11-May        A          12-May
    2   12-May        B          12-May
    3   12-May        NULL       NULL
    4   12-May        C          13-May
    5   13-May        D          14-May

EX:我的比例12月5日将是2/3(1,2,3和A,B) 需要在没有任何连接的情况下进行。 你可以帮我解决这个

的SQL或Hive查询

3 个答案:

答案 0 :(得分:0)

这应该为您提供您正在寻找的比率:

Select isnull(orderdate, contactdate), concat(cast(isnull(contacts,0) as char),'/',cast(isnull(orders,0) as char)) as Ratio
from
(Select orderdate, count(orderdate) orders
from tbl
where orderdate is not null
Group by orderdate) o
left join
(Select contactdate, count(contactdate) contacts
from tbl
where contactdate is not null
Group by contactdate) c on o.orderdate = c.contactdate
UNION
Select isnull(orderdate, contactdate), cast(isnull(contacts,0) as varchar) + '/' + cast(isnull(orders,0) as varchar)
from
(Select orderdate, count(orderdate) orders
from @foobar
where orderdate is not null
Group by orderdate) o
right join
(Select contactdate, count(contactdate) contacts
from @foobar
where contactdate is not null
Group by contactdate) c on o.orderdate = c.contactdate

我已经为此添加了一个联盟,以便在联系日期中的日期不在订单日期时复制完整外部联接

答案 1 :(得分:0)

你需要计算按天分组的每个列的元素,然后进行除法。

这里你是带有两列的查询结果。十进制除法和他的字符串表示:

select A.orderdate, 
CONVERT(decimal(4,2),numOrders)/CONVERT(decimal(4,2),numContacts) as DecimalRate, 
CONVERT(varchar(max),numOrders) + '/' + CONVERT(varchar(max),numContacts) textualRate

from 
(
select orderdate, count(*) as numOrders from AtestTable
group by orderdate ) A
left join
(
select contactdate, count(*) as numContacts from AtestTable
group by contactdate
) B
on A.orderDate = B.contactDate

答案 2 :(得分:0)

我有一个快速而肮脏的解决方案,似乎正在获得您正在寻找的结果。

DECLARE @TestTable TABLE (
    OrderId int,
    OrderDate date,
    ContactID varchar(5),
    ContactDate date
)

INSERT INTO @TestTable
VALUES
    (1,'5/11/2015','A','5/12/2015'),
    (2,'5/12/2015','B','5/12/2015'),
    (3,'5/12/2015',NULL,NULL),
    (4,'5/12/2015','C','5/13/2015'),
    (5,'5/13/2015','D','5/14/2015')

DECLARE @Dates TABLE (
    TheDate DATE
)

INSERT INTO @Dates
SELECT DISTINCT OrderDate FROM @TestTable
INSERT INTO @Dates
SELECT DISTINCT ContactDate FROM @TestTable WHERE ContactDate NOT IN (SELECT * FROM @Dates)


SELECT
    D.TheDate,
    --Order Ratio
    CAST((SELECT COUNT(*) FROM @TestTable WHERE OrderDate = TheDate) AS VARCHAR(2)) + '/' + CAST((SELECT COUNT(*) FROM @TestTable) AS VARCHAR(2)) AS 'OrderRatio',
    CAST((SELECT COUNT(*) FROM @TestTable WHERE OrderDate = TheDate) AS DECIMAL)/CAST((SELECT COUNT(*) FROM @TestTable) AS DECIMAL) AS 'OrderDecimal',
    --Contact Ratio
    CAST((SELECT COUNT(*) FROM @TestTable WHERE ContactDate = TheDate) AS VARCHAR(3)) + '/' + CAST((SELECT COUNT(*) FROM @TestTable) AS VARCHAR(3)) AS 'ContactRatio',
    CAST((SELECT COUNT(*) FROM @TestTable WHERE ContactDate = TheDate) AS DECIMAL)/CAST((SELECT COUNT(*) FROM @TestTable) AS DECIMAL) AS 'ContactDecimal'
FROM
    @Dates D

结果集如下所示:

TheDate         OrderRatio     OrderDecimal             ContactRatio    ContactDecimal
2015-05-11      1/5            0.2000000000000000000    0/5             0.0000000000000000000
2015-05-12      3/5            0.6000000000000000000    2/5             0.4000000000000000000
2015-05-13      1/5            0.2000000000000000000    1/5             0.2000000000000000000
2015-05-14      0/5            0.0000000000000000000    1/5             0.2000000000000000000