理解SQL查询的指导

时间:2016-12-07 16:43:49

标签: sql

我一直在尝试使用以下查询作为参考创建过去一周左右的查询,并需要一些帮助将其分解,所以我知道每个部分的作用。

Select Distinct
        tblCustomers.fldEmail
      , tblCustomers.fldCustomerID
      , tblCustomers.fldName
      , tblCustomers.fldContactName
From    tblBigClubs
Inner Join tblBigClubMatch
        On tblBigClubs.fldBigClubID = tblBigClubMatch.fldBigClubID
Inner Join tblCustomers
        On tblBigClubMatch.fldCustomerID = tblCustomers.fldCustomerID
Where   (tblBigClubs.fldBigClubID In (Select Distinct
                                                dbo.tblBigClubMatch.fldBigClubID
                                      From      dbo.tblOrders
                                      Inner Join dbo.tblOrderItems
                                                On dbo.tblOrders.fldOrderID = dbo.tblOrderItems.fldOrderID
                                      Inner Join dbo.tblBigClubMatch
                                                On dbo.tblOrders.fldCustomerID = dbo.tblBigClubMatch.fldCustomerID
                                      Where     (dbo.tblOrders.fldOrderDate > DateAdd(Year, -2, GetDate()))
                                                And (dbo.tblOrderItems.fldStockCode Like 'TO%')
                                      Group By  dbo.tblBigClubMatch.fldBigClubID
                                      Having    (Sum(dbo.tblOrderItems.fldQtyOrder) >= 30))
         And tblCustomers.fldEmail Is Not Null
         And dbo.tblCustomers.fldEmail <> ''
         And Len(dbo.tblCustomers.fldEmail) > 8
        );

有人可以解释每个部分在做什么吗?

2 个答案:

答案 0 :(得分:1)

首先,简化和格式化:

Select Distinct c.fldEmail, c.fldCustomerID, c.fldName, c.fldContactName
From tblBigClubs b
   Join tblBigClubMatch m On m.fldBigClubID = b.fldBigClubID 
   Join tblCustomers c On c.fldCustomerID = m.fldCustomerID
Where Len(c.fldEmail) > 8
   and b.fldBigClubID In 
      (Select Distinct im.fldBigClubID
       From dbo.tblOrders o
            Join dbo.tblOrderItems i On i.fldOrderID = o.fldOrderID
            Join dbo.tblBigClubMatch im On im.fldCustomerID = o.fldCustomerID
       Where o.fldOrderDate > DateAdd(Year, -2, GetDate())
           And i.fldStockCode Like 'TO%'
       Group By  im.fldBigClubID
       Having Sum(i.fldQtyOrder) >= 30);

以上功能相同。 (使用别名消除了很多混乱,使SQL更易于阅读和理解) 然后,检查谓词。只有两个:

Len(c.fldEmail) > 8 - 如果满足,那么很明显,fldEmail不能为空,

b.fldBigClubID In subquery条款。这似乎是找到fldBigClubID fldStockCode的所有值,其中TOfldOrderDate开头而TO是过去两年,其中有30个或更多订单周期。

总结一下,对于那些在过去两年中以Select fldEmail, fldCustomerID, fldName, fldContactName From tblCustomers Where fldCustomerID In (Select distinct o.fldCustomerID From dbo.tblOrders o Join dbo.tblOrderItems i On i.fldOrderID = o.fldOrderID Join dbo.tblBigClubMatch m On m.fldCustomerID = o.fldCustomerID Where o.fldOrderDate > DateAdd(Year, -2, GetDate()) And i.fldStockCode Like 'TO%' Group By m.fldBigClubID Having Sum(i.fldQtyOrder) >= 30); 开头的股票代码为30个或更多订单的客户,似乎从客户表中获取了四个字段。

实际上,这也应该等同于:

require(['jquery', 'nouislider'], function ($, noUiSlider) {

答案 1 :(得分:0)

不可能完成整个查询,但如果您有特定部分给您带来麻烦,我们可以提供帮助。回应您的评论

DateAdd(Year, -2, GetDate())

这将返回2年减去今天的日期。你可以运行

SELECT DateAdd(Year, -2, GetDate())

返回

2014-12-07 10:56:07.290

因此看起来他们正在拉动过去两年内订购了30件或更多件商品的客户。