SQL where子句用于两个与某些条件匹配的无关系表

时间:2014-12-22 15:37:22

标签: sql sql-server join where-clause

我有两张桌子:

销售

+----+--------------+---------------+-----------+------------+------------+
| id |  date        |     type      |   city    |   value    |     GB     |
+----+--------------+---------------+-----------+------------+------------+
| 1  |  2014-12-20  |     type1     |   city1   |    1000    |    100     |            
+----+--------------+---------------+-----------+------------+------------+
| 2  |  2014-12-20  |     type2     |   city2   |    2000    |    200     |   
+----+--------------+---------------+-----------+------------+------------+
| 3  |  2014-12-20  |     type2     |   city1   |    3000    |    300     |
+----+--------------+---------------+-----------+------------+------------+
| 4  |  2014-12-19  |     type1     |   city1   |    4000    |    400     |       
+----+--------------+---------------+-----------+------------+------------+

注释

+----+--------------+---------------+-----------+------------+------------+
| id |  date        |     type      |   city    |   value    |     GB     |
+----+--------------+---------------+-----------+------------+------------+
| 1  |  2014-12-20  |     type1     |   city1   |    100     |     10     |            
+----+--------------+---------------+-----------+------------+------------+
| 2  |  2014-12-19  |     type2     |   city2   |    200     |     20     |   
+----+--------------+---------------+-----------+------------+------------+
| 3  |  2014-12-20  |     type1     |   city1   |    300     |     30     |
+----+--------------+---------------+-----------+------------+------------+

DECLARE @date DATE
SET @date = '2014-12-20'

我需要减去两个表的valueGB列的总和(),并显示按类型和城市分组的结果

select sales.type, sales.city, sum(sales.value) - sum(notes.value) as raw_value_sales, sum(sales.GB)-sum(notes.GB) as raw_GB_sales from sales,notes
where notes.date = @date and sales.date = @date
group by sales.type, sales.city

此查询无法正常工作,因为它跨越表格。

必需的输出

+---------- --------+---------------+--------------------+----------------+
|    sales_type     |  sales_city   |  raw_value_sales   |  raw_GB_sales  | 
+-------------------+---------------+--------------------+----------------+
|      type1        |    city1      |        600         |      60        |                
+-------------------+---------------+--------------------+----------------+
|      type2        |    city1      |        3000        |      300       |
+-------------------+---------------+--------------------+----------------+
|      type2        |    city2      |        2000        |      200       |
+-------------------+---------------+--------------------+----------------+

请注意,这些数据只是为了解释我的问题。对于type2,所需的@date中没有注释,因此raw_values_sales和raw_GB_sales保持不变。

我正在尝试使用此sql语句来测试where子句:

select notes.date, sales.date
from Notes, Sales
WHERE notes.date = @date and ventas.date = @date

它显示了20994个回复,但如上所述我知道有476个销售符合该标准和44个备注。

提前致谢。

1 个答案:

答案 0 :(得分:0)

现在你已经编辑了你的问题,你可能会更清楚你所追求的是什么。我认为您将需要使用子查询来实现您想要的结果。我不确定它的效率如何,但我认为这个测试脚本演示了一种可能的技术:

create table sales (
    id int primary key,
    [date] date,
    [type] varchar(max),
    city varchar(max),
    value int,
    GB int);

insert into sales
    (id, [date], [type], city, value, GB)
values
    (1, '2014-12-20', 'type1', 'city1', 1000, 100),            
    (2, '2014-12-20', 'type2', 'city2', 2000, 200),   
    (3, '2014-12-20', 'type2', 'city1', 3000, 300),
    (4, '2014-12-19', 'type1', 'city1', 4000, 400);

create table notes
    (id int primary key,
    [date] date,
    [type] varchar(max),
    city varchar(max),
    value int,
    GB int);

insert into notes
    (id, [date], [type], city, value, GB)
values
    (1, '2014-12-20', 'type1', 'city1', 100, 10),
    (2, '2014-12-19', 'type2', 'city2', 200, 20),
    (3, '2014-12-20', 'type1', 'city1', 300, 30);

declare
    @date date = '2014-12-20';

select
    [type], 
    [city],
    sum(value) - isnull((select sum(value) from notes n where n.date=@date and n.[type] = s.[type] and n.city = s.city),0) raw_value_sales,
    sum(GB) - isnull((select sum(GB) from notes n where n.date=@date and n.[type] = s.[type] and n.city = s.city),0) raw_GB_sales
from
    sales s
where
    [date]=@date
group by 
    [type], 
    [city]
;

drop table notes;
drop table sales;