计算从两个表关联到第三个表的行

时间:2016-12-05 04:09:09

标签: sql-server

我有三个表:数据,网络和软件。数据表具有我的所有客户端名称,而网络和软件表具有服务票证。我们需要每个客户每月至少从一个表(网络或软件)中选择一张票。我需要一个查询来计算每个月数据表中每个客户的表(网络和软件)的票证。

my tables

欲望结果: Final query

我找到了使用此查询计算的方法,但我不知道如何从两个表中计算。

SELECT institucion
    ,(SELECT count(institucion) FROM network WHERE data.institucion = network.institucion and fecha like '2016-07%')as July
    ,(SELECT count(institucion) FROM network WHERE data.institucion = network.institucion and fecha like '2016-08%')as August
    ,(SELECT count(institucion) FROM network WHERE data.institucion = network.institucion and fecha like '2016-09%')as September
    ,(SELECT count(institucion) FROM network WHERE data.institucion = network.institucion and fecha like '2016-10%')as October
    ,(SELECT count(institucion) FROM network WHERE data.institucion = network.institucion and fecha like '2016-11%')as November
    ,(SELECT count(institucion) FROM network WHERE data.institucion = network.institucion and fecha like '2016-12%')as December
    ,(SELECT count(institucion) FROM network WHERE data.institucion = network.institucion and fecha like '2017-01%')as January
    ,(SELECT count(institucion) FROM network WHERE data.institucion = network.institucion and fecha like '2017-02%')as February
    ,(SELECT count(institucion) FROM network WHERE data.institucion = network.institucion and fecha like '2017-03%')as March
    ,(SELECT count(institucion) FROM network WHERE data.institucion = network.institucion and fecha like '2017-04%')as April
    ,(SELECT count(institucion) FROM network WHERE data.institucion = network.institucion and fecha like '2017-05%')as May
    ,(SELECT count(institucion) FROM network WHERE data.institucion = network.institucion and fecha like '2017-06%')as June
FROM data
where data.erate = 'y'
order by institucion

2 个答案:

答案 0 :(得分:1)

我认为您需要UNIONnetwork表一起software,然后使用条件聚合来获取每个感兴趣的月份的计数摘要。

SELECT t1.institucion,
       SUM(CASE WHEN t1.fetcha LIKE '2016-07%' THEN 1 ELSE 0 END) AS July,
       SUM(CASE WHEN t1.fetcha LIKE '2016-08%' THEN 1 ELSE 0 END) AS August,
       SUM(CASE WHEN t1.fetcha LIKE '2016-09%' THEN 1 ELSE 0 END) AS September,
       SUM(CASE WHEN t1.fetcha LIKE '2016-10%' THEN 1 ELSE 0 END) AS October,
       SUM(CASE WHEN t1.fetcha LIKE '2016-11%' THEN 1 ELSE 0 END) AS November,
       SUM(CASE WHEN t1.fetcha LIKE '2016-12%' THEN 1 ELSE 0 END) AS December,
       SUM(CASE WHEN t1.fetcha LIKE '2017-01%' THEN 1 ELSE 0 END) AS January,
       SUM(CASE WHEN t1.fetcha LIKE '2017-02%' THEN 1 ELSE 0 END) AS February,
       SUM(CASE WHEN t1.fetcha LIKE '2017-03%' THEN 1 ELSE 0 END) AS March,
       SUM(CASE WHEN t1.fetcha LIKE '2017-04%' THEN 1 ELSE 0 END) AS April,
       SUM(CASE WHEN t1.fetcha LIKE '2017-05%' THEN 1 ELSE 0 END) AS May,
       SUM(CASE WHEN t1.fetcha LIKE '2017-06%' THEN 1 ELSE 0 END) AS June
FROM
(
    SELECT institucion, fecha, service
    FROM network n
    UNION ALL
    SELECT institucion, fecha, service
    FROM software s
) t1
INNER JOIN data t2
    ON t1.institucion = t2.institucion
WHERE t2.erate = 'y'

答案 1 :(得分:0)

我和Tim一起玩,你也可以尝试Pivot

例如:

     SELECT * FROM (
            SELECT institucion
                ,LEFT(fecha,7) AS [month],COUNT(institucion) AS cnt
            FROM DATA AS d
            LEFT JOIN network AS n ON d.institucion=n.institucion
            where data.erate = 'y'
            GROUP BY institucion,LEFT(fecha,7)
     ) AS t
    PIVOT(MAX(cnt) FOR [month] IN ([2016-07],[2016-08],[2016-09],[2016-10],[2016-11],[2016-12],[2017-01],[2017-02],[2017-03],[2017-04],[2017-05],[2017-06])) p