SQL Server。合并两个带内部联接的select语句

时间:2017-05-30 22:52:12

标签: sql sql-server-2008

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

select distinct 
    movements.date, movements.productcode, 
    products.productname,
    clasification.family, 
    clients.clientname, clients.clientcode,
    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|
+----+------------+------------+-----------+-----------+-----------------+--------+----------+------+-----+--------+-----+----+

但我还想添加两个列,一个用于客户端的分类,如果客户端最终用户安装程序,我可以获取, 制作人等,这是给我这个问题的查询。

select 
    invoices.classifications.classification_value, 
    client_name, client_code 
from 
    invoices.clients
inner join 
    invoices.classifications on invoices.classifications.cid_classification = invoices.clients.cid_clasifclient1
inner join 
    invoices.documents on invoices.documents.cid_client = invoices.clients.cid_client

,第二列是发票系列,我需要将列合并为一个,但是当我将其添加到第一个查询时,它不会返回任何值。

select 
    convert(varchar(20), documents_series) + '' +
         convert(varchar(6), document_number) as invoice_series 
from 
    invoices.documents

所以,如果你能帮助我,我真的很喜欢,如果我的广告SQL不是很好,我很抱歉。

1 个答案:

答案 0 :(得分:0)

由于连接链接似乎是客户端表,主要位于 idclient cid_client 字段之间,表示主/外键对(或一对多连接表),考虑将最后一个查询用作在这两个字段上连接的第一个查询中的派生表。另外,尝试使用table aliases如下所示来减少重复的长表名称:

this._nextScene = new sceneClass(); <-- wich is a type of ISceneConstructor
SceneManager.changeScene = function() {
    if (this.isSceneChanging() && !this.isCurrentSceneBusy()) {
        if (this._scene) {
            this._scene.terminate();
            this._scene.detachReservation();
            this._previousClass = this._scene.constructor;
        }
        this._scene = this._nextScene; // <-- where you see the assignation.
        if (this._scene) {
            this._scene.attachReservation();
            this._scene.create();
            this._nextScene = null;
            this._sceneStarted = false;
            this.onSceneCreate();
        }
        if (this._exiting) {
            this.terminate();
        }
    }
};

或者使用CTE而不是派生表:

select distinct m.date, m.productcode, p.productname
      , f.family, cl.clientname, cl.clientcode
      , m.quantity, m.price, m.credit, m.total 
      , m.exchange,m.state, m.city
      , sub.clasification_value, sub.invoice_series

from invoices.movements m    
inner join invoices.products p
   on m.idproduct = p.idproduct
left outer join invoices.documents d
   on m.iddocument = d.iddocument
inner join invoices.coins c
   on d.idcoin = c.idcoin
inner join invoices.address a
   on a.iddocument = d.iddocument
inner join invoices.clasifications f
   on f.family='aspen'
inner join invoices.clients cl
  on d.idclient = cl.idclient

inner join 
   (select subcl.idclient, subf.clasification_value
           , subcl.client_name, subcl.client_code 
           , convert(varchar(20), subd.documents_series)+''+
             convert(varchar(6), subd.document_number) as invoice_series
    from invoices.clients subcl
    inner join invoices.clasifications subf
      on subf.cid_clasification = cl.cid_clasifclient1
    inner join invoices.documents subd
      on subd.cid_client = cl.cid_client) As sub

  on sub.idclient = cl.idclient