码迷,mamicode.com
首页 > 数据库 > 详细

Sql语法高级应用之四:使用视图实现多表联合数据明细

时间:2018-07-11 15:03:21      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:lse   wot   组成   rop   enc   war   合数   案例   path   

 之前章节我们讲到:如果某个表的数据是多个表的联合,并且存在列与列的合并组成新列,用视图是最好的方案。

下面我分享两个个真实的SQL语句案例

技术分享图片
USE Wot_Inventory
GO
IF EXISTS (SELECT 1 FROM sys.views WHERE Name = InvoiceSearchListView)
    DROP VIEW InvoiceSearchListView;
GO
CREATE VIEW InvoiceSearchListView 
AS 
SELECT ROW_NUMBER()OVER(ORDER BY i.CreateDatetime DESC) AS Id, 
i.InvoiceId InvoiceId,i.InvoiceCode,i.[Status],
CASE WHEN ls.[Status] IS NULL THEN 99 ELSE ls.[Status] END AS TraceState_Auto,i.TraceState TraceState_Own,
i.SalesChannelId,i.SalesChannelName,i.WarehouseId,i.WarehouseName,sh.ShipperId,sh.ShipperName,sh.ShipperNo,
sh.MonthlyAccount,i.LogisticCode,i.Receivable,i.AgencyFund,
Sketch = (
  STUFF(
   (SELECT CASE WHEN id.Number > 0 THEN ,  + pro.ProductName + pro.Spec + ( + CONVERT(NVARCHAR(10),id.Number) + pro.Unit + ) ELSE ‘‘ END 
      FROM dbo.Products pro INNER JOIN dbo.InvoiceDetail id ON id.ProductId = pro.ProductId
      WHERE id.InvoiceId = i.InvoiceId
      FOR XML PATH(‘‘)
    ),1,1,‘‘)
),
i.SalesGroupId,i.SalesGroupName,i.SalesUserId,i.SalesUserName,ls.SyncDate,
ls.LastTraceDesc,i.LastDescUserName,i.LastDescDate,i.LastDesc,
i.CreateDatetime,i.AgreedDate,i.PrintCheckDate,i.OutWarehouseDate,i.TraceStateDate,i.ContrastDate,i.CompleteDate,
i.OrderId,i.OrderNo,i.PrintType,i.PrintNumber,i.CustomerId,i.CustomerName,i.CustomerPhone,
i.Country,i.Province,i.City,i.InsurreValue,i.FragileInsurreValue,i.Freight,
i.IsReceived,i.IsComplete 
FROM Wot_Inventory.dbo.Invoice i 
LEFT JOIN Wot_Inventory.dbo.Logistics ls ON ls.InvoiceId = i.InvoiceId 
LEFT JOIN Wot_Sales.dbo.Shipper sh ON i.ShipperId = sh.ShipperId 
WHERE i.[State] <> -1 

GO
带多列合并

 

技术分享图片
USE Wot_Inventory
GO
IF EXISTS (SELECT 1 FROM sys.views WHERE Name = LogisticsFollowView)
    DROP VIEW LogisticsFollowView;
GO
CREATE VIEW LogisticsFollowView 
AS 
SELECT ROW_NUMBER()OVER(ORDER BY i.OutWarehouseDate DESC) AS Id, 
       i.InvoiceId,
       i.IsOutWarehouse,
       i.OutWarehouseDate,
       sh.ShipperId,
       sh.ShipperNo,
       sh.ShipperName,
       i.LogisticCode,
       ls.LastTraceDate,
       ls.LastTraceDesc,
       ls.[Status],
       i.TraceState,
       ls.SyncDate,
       i.LastFollowDate,
       i.LastFollowDesc,
       i.LastFollowUserName,
       i.SalesChannelName,
       sh.MonthlyAccount,
       i.Receiver,
       i.Country,
       i.Province,
       i.City,
       i.OrderNo,
       i.InvoiceCode,
       i.TraceStateDate,
       i.TraceStateUser,
       i.SalesGroupId,
       i.SalesUserId,
       i.SalesUserName,
       i.OrderId,
       i.CustomerName,
       i.CustomerPhone,
       i.PrintCheckDate 
FROM Wot_Inventory.dbo.Invoice i 
LEFT JOIN Wot_Inventory.dbo.Logistics ls ON ls.InvoiceId = i.InvoiceId 
LEFT JOIN Wot_Sales.dbo.Shipper sh ON i.ShipperId = sh.ShipperId 
WHERE i.[State] <> -1 AND i.LogisticCode IS NOT NULL AND i.[Status] >= 4 AND i.IsAddressCheck = 1 AND i.IsPrintCheck = 1 AND i.TraceState NOT IN(6,3,4)
不带多列合并 ,在视图中添加条件

 

 

PS:欢迎扫描下方二维码或点击链接,加入QQ群

技术分享图片

技术分享图片

 

Sql语法高级应用之四:使用视图实现多表联合数据明细

标签:lse   wot   组成   rop   enc   war   合数   案例   path   

原文地址:https://www.cnblogs.com/ydcnblog/p/9293540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!