SQL Server。如何将选择案例放在具有多个联接的另一个选择中

时间:2017-05-18 19:07:49

标签: sql-server sql-server-2008

提前抱歉,如果我的问题重复或类似于另一个问题,我已经检查过类似的其他帖子,但我无法做到正确。 所以,这是我的问题。 我有接下来的两个select语句,我在第一个中正确放置第二个查询时遇到了麻烦,我已经尝试在连接之后放入select中,如果你可以帮助我或给予建议它会很棒。

select distinct movements.date, movements.productcode, products.productname
,clasification.family, clients.clientname, clients.clientcode, documents.invoicereference
,movements.quantity, movements.price, movements.credit, movements.total, movements.exchange
,movements.state, movements.city
from invoices.movements

  inner join invoices.products
  on invoices.movements.idproduct=invoices.products.idproduct
  left outer join invoices.documents
  on invoices.movements.iddocument=invoices.documents.iddocument
  inner join invoices.coins
  on invoices.documents.idcoin=invoices.coins.idcoin
  inner join invoices.address
  on invoices.address.iddocument=invoices.documents.iddocument
  inner join invoices.clasifications
  on invoices.clasifications.family='aspen'
  inner join invoices.clients
  on invoices.documents.idclient=invoices.clients.idclient

这是我从上一个查询得到的。

+----+------------+------------+-----------+-----------+-----------------+--------+----------+------+-----+--------+-----+----+
|date|product_code|product_name|client_name|client_code|invoice_reference|quantity|unit_price|credit|total|exchange|state|city|
+----+------------+------------+-----------+-----------+-----------------+--------+----------+------+-----+--------+-----+----+

但我还想添加另一个引用客户端类型的列,它们有两个分类,如果它们的client_code就像这个'N00000'(或者更具体,如果它们以零结尾),它们是< strong>“休闲”客户如果不是(比如'N00013',他们是“固定”客户。这是查询。

select 
  case
   when invoices.clients.idclient='N00000' then 'Casual'
   else 'Fixed'
    end as client_type,
     clientcode, invoices.clients.clientname
from invoices.documents.
inner join invoices.clients
on invoices.documents.idclient=invoices.clients.idclient

我的问题是我似乎无法将查询正确地放在第一个内容中,如果有人可以帮助我,我真的很感激。

2 个答案:

答案 0 :(得分:1)

在我看来,您已经在第一个查询中完成了必要的连接,因此您只需要在第一个查询的列列表中添加CASE表达式,如下所示:

select distinct movements.date, movements.productcode, products.productname
,clasification.family, clients.clientname, clients.clientcode, documents.invoicereference
,movements.quantity, movements.price, movements.credit, movements.total, movements.exchange
,movements.state, movements.city, case
   when invoices.clients.idclient='N00000' then 'Casual'
   else 'Fixed'
    end as client_type
from invoices.movements

  inner join invoices.products
  on invoices.movements.idproduct=invoices.products.idproduct
  left outer join invoices.documents
  on invoices.movements.iddocument=invoices.documents.iddocument
  inner join invoices.coins
  on invoices.documents.idcoin=invoices.coins.idcoin
  inner join invoices.address
  on invoices.address.iddocument=invoices.documents.iddocument
  inner join invoices.clasifications
  on invoices.clasifications.family='aspen'
  inner join invoices.clients
  on invoices.documents.idclient=invoices.clients.idclient

答案 1 :(得分:1)

别名是你的朋友,代码格式也是如此。

  

(或者更具体地说,如果它们以零结尾),他们会&#34;休闲&#34;

select distinct 
    im.date
  , im.productcode
  , ip.productname
  , icla.family
  , icli.clientname
  , icli.clientcode
  , id.invoicereference
  , im.quantity
  , im.price
  , im.credit
  , im.total
  , im.exchange
  , im.state
  , im.city
  , case when id.idclient like '%0' then 'Casual'
         when id.idclient is not null then 'Fixed'
         end as client_type
from invoices.movements im
  inner join invoices.products ip
    on im.idproduct=ip.idproduct
  left outer join invoices.documents id
    on im.iddocument=id.iddocument
  inner join invoices.coins ico
    on id.idcoin=ico.idcoin
  inner join invoices.address
    on invoices.address.iddocument=id.iddocument
  inner join invoices.clasifications icla
    on icla.family='aspen'
  inner join invoices.clients icli
    on id.idclient=icli.idclient