基于联接表的计数和/或标志的SQL选择标志

基于联接表的计数和/或标志的SQL选择标志,sql,sql-server,tsql,sql-server-2000,Sql,Sql Server,Tsql,Sql Server 2000,我有一个客户表和一个地址表 地址表有一个标志,即发票、信函或交货 一个客户可以有0到多个地址记录 我希望能够查询这两个表,并根据地址数据为每个客户生成一个标志-无地址记录=无,1个或多个发票记录=HASINVOICE,无发票但1个或多个其他=HASOTHER 因此,对于以下数据: +------------+---------+ | CustomerID | Name | +------------+---------+ | 1 | Peter | |

我有一个客户表和一个地址表

地址表有一个标志,即发票、信函或交货

一个客户可以有0到多个地址记录

我希望能够查询这两个表,并根据地址数据为每个客户生成一个标志-无地址记录=无,1个或多个发票记录=HASINVOICE,无发票但1个或多个其他=HASOTHER

因此,对于以下数据:

+------------+---------+
| CustomerID |  Name   |
+------------+---------+
|          1 | Peter   |
|          2 | Ray     |
|          3 | Egon    |
|          4 | Winston |
|          5 | Dana    |
+------------+---------+

+-----------+------------+----------------+
| AddressID | CustomerID |  AddressType   |
+-----------+------------+----------------+
|         1 |          1 | INVOICE        |
|         2 |          1 | DELIVERY       |
|         3 |          2 | DELIVERY       |
|         4 |          2 | CORRESPONDENCE |
|         5 |          4 | INVOICE        |
|         6 |          5 | CORRESPONDENCE |
+-----------+------------+----------------+
我预计会有以下产出:

+------------+---------+-------------+
| CustomerID |  Name   | AddressFlag |
+------------+---------+-------------+
|          1 | Peter   | HASINVOICE  |
|          2 | Ray     | HASOTHER    |
|          3 | Egon    | NONE        |
|          4 | Winston | HASINVOICE  |
|          5 | Dana    | HASOTHER    |
+------------+---------+-------------+

对于SQL 2000,是否可以使用单个查询而不使用游标?

我手头没有2000实例(您真的应该升级,您落后于4-5个版本),但我认为这应该可以:

declare @Customers table (CustomerID int,Name varchar(10))
insert into @Customers (CustomerID,Name)
select 1,'Peter' union all    select 2,'Ray' union all
select 3,'Egon' union all    select 4,'Winston' union all
select 5,'Dana'

declare @Addresses table (AddressID int, CustomerID int,
                          AddressType varchar(30))
insert into @Addresses (AddressID,CustomerID,AddressType)
select 1,1,'INVOICE' union all    select 2,1,'DELIVERY' union all
select 3,2,'DELIVERY' union all    select 4,2,'CORRESPONDENCE' union all
select 5,4,'INVOICE' union all    select 6,5,'CORRESPONDENCE'

select
    c.CustomerID,
    c.Name,
    CASE MAX(CASE 
        WHEN a.AddressType = 'Invoice' THEN 2
        WHEN a.AddressType IS NOT NULL THEN 1
        END
    ) WHEN 2 THEN 'HASINVOICE'
    WHEN 1 THEN 'HASOTHER'
    ELSE 'NONE'
    END as AddressFlag
from
    @Customers c
        left join
    @Addresses a
        on
            c.CustomerID = a.CustomerID
group by
    c.CustomerID,
    c.Name
产生:

CustomerID  Name       AddressFlag
----------- ---------- -----------
5           Dana       HASOTHER
3           Egon       NONE
1           Peter      HASINVOICE
2           Ray        HASOTHER
4           Winston    HASINVOICE

我手头没有2000个实例(您真的应该升级,您落后于4-5个版本),但我认为这应该是可行的:

declare @Customers table (CustomerID int,Name varchar(10))
insert into @Customers (CustomerID,Name)
select 1,'Peter' union all    select 2,'Ray' union all
select 3,'Egon' union all    select 4,'Winston' union all
select 5,'Dana'

declare @Addresses table (AddressID int, CustomerID int,
                          AddressType varchar(30))
insert into @Addresses (AddressID,CustomerID,AddressType)
select 1,1,'INVOICE' union all    select 2,1,'DELIVERY' union all
select 3,2,'DELIVERY' union all    select 4,2,'CORRESPONDENCE' union all
select 5,4,'INVOICE' union all    select 6,5,'CORRESPONDENCE'

select
    c.CustomerID,
    c.Name,
    CASE MAX(CASE 
        WHEN a.AddressType = 'Invoice' THEN 2
        WHEN a.AddressType IS NOT NULL THEN 1
        END
    ) WHEN 2 THEN 'HASINVOICE'
    WHEN 1 THEN 'HASOTHER'
    ELSE 'NONE'
    END as AddressFlag
from
    @Customers c
        left join
    @Addresses a
        on
            c.CustomerID = a.CustomerID
group by
    c.CustomerID,
    c.Name
产生:

CustomerID  Name       AddressFlag
----------- ---------- -----------
5           Dana       HASOTHER
3           Egon       NONE
1           Peter      HASINVOICE
2           Ray        HASOTHER
4           Winston    HASINVOICE

我手头没有2000个实例(您真的应该升级,您落后于4-5个版本),但我认为这应该是可行的:

declare @Customers table (CustomerID int,Name varchar(10))
insert into @Customers (CustomerID,Name)
select 1,'Peter' union all    select 2,'Ray' union all
select 3,'Egon' union all    select 4,'Winston' union all
select 5,'Dana'

declare @Addresses table (AddressID int, CustomerID int,
                          AddressType varchar(30))
insert into @Addresses (AddressID,CustomerID,AddressType)
select 1,1,'INVOICE' union all    select 2,1,'DELIVERY' union all
select 3,2,'DELIVERY' union all    select 4,2,'CORRESPONDENCE' union all
select 5,4,'INVOICE' union all    select 6,5,'CORRESPONDENCE'

select
    c.CustomerID,
    c.Name,
    CASE MAX(CASE 
        WHEN a.AddressType = 'Invoice' THEN 2
        WHEN a.AddressType IS NOT NULL THEN 1
        END
    ) WHEN 2 THEN 'HASINVOICE'
    WHEN 1 THEN 'HASOTHER'
    ELSE 'NONE'
    END as AddressFlag
from
    @Customers c
        left join
    @Addresses a
        on
            c.CustomerID = a.CustomerID
group by
    c.CustomerID,
    c.Name
产生:

CustomerID  Name       AddressFlag
----------- ---------- -----------
5           Dana       HASOTHER
3           Egon       NONE
1           Peter      HASINVOICE
2           Ray        HASOTHER
4           Winston    HASINVOICE

我手头没有2000个实例(您真的应该升级,您落后于4-5个版本),但我认为这应该是可行的:

declare @Customers table (CustomerID int,Name varchar(10))
insert into @Customers (CustomerID,Name)
select 1,'Peter' union all    select 2,'Ray' union all
select 3,'Egon' union all    select 4,'Winston' union all
select 5,'Dana'

declare @Addresses table (AddressID int, CustomerID int,
                          AddressType varchar(30))
insert into @Addresses (AddressID,CustomerID,AddressType)
select 1,1,'INVOICE' union all    select 2,1,'DELIVERY' union all
select 3,2,'DELIVERY' union all    select 4,2,'CORRESPONDENCE' union all
select 5,4,'INVOICE' union all    select 6,5,'CORRESPONDENCE'

select
    c.CustomerID,
    c.Name,
    CASE MAX(CASE 
        WHEN a.AddressType = 'Invoice' THEN 2
        WHEN a.AddressType IS NOT NULL THEN 1
        END
    ) WHEN 2 THEN 'HASINVOICE'
    WHEN 1 THEN 'HASOTHER'
    ELSE 'NONE'
    END as AddressFlag
from
    @Customers c
        left join
    @Addresses a
        on
            c.CustomerID = a.CustomerID
group by
    c.CustomerID,
    c.Name
产生:

CustomerID  Name       AddressFlag
----------- ---------- -----------
5           Dana       HASOTHER
3           Egon       NONE
1           Peter      HASINVOICE
2           Ray        HASOTHER
4           Winston    HASINVOICE

@卢斯卡-是的。2000年的BOL仍然是。即使没有,这也只是为了构建一个自包含的脚本来演示这项技术。我想OP会把它写回他们原来的表格里。@Podyluska-是的。2000年的BOL仍然是。即使没有,这也只是为了构建一个自包含的脚本来演示这项技术。我想OP会把它写回他们原来的表格里。@Podyluska-是的。2000年的BOL仍然是。即使没有,这也只是为了构建一个自包含的脚本来演示这项技术。我想OP会把它写回他们原来的表格里。@Podyluska-是的。2000年的BOL仍然是。即使没有,这也只是为了构建一个自包含的脚本来演示这项技术。我想OP会把它写回他们原来的表格。