1

我有以下查询:

select distinct(ab.id) 
from WidgetClicks ab
join  ManufacturerWidgets aa 
    on ab.ManufacturerWidgetId = aa.Id
join ManufacturerRetailers ad 
    on aa.ManufacturerId = ad.ManufacturerId
join Products ac 
    on ab.ProductId = ac.Id  
where ad.Enabled = 0 
    and ad.RetailerId = 189 
    and  aa.ManufacturerId = 46  
    and aa.CountryId = 72 
    and ac.Id = 6914 
    and ab.CreatedAt >= '2011-10-31 00:00:00.000' 
    and ab.CreatedAt <= '2012-03-31 00:00:00.000'   

启用= 1 和retailerId 我得到完全相同的结果,即使我不应该 - 结果应该少得多。

我敢肯定它是直截了当的,但它让我发疯!任何帮助表示赞赏

编辑:

我实际上需要做的基本上是写 3 条语句来获得结果 - 再次不确定如何执行此操作,但这是语句需要做的(用简单的英语):

  1. 从affiliateretailer 表中选择 * where manufacturerid = 46 and enabled = true

  2. 从制造商零售商中选择 *,其中零售商是上述选择中剩余的零售商,其中制造商 ID = 46

  3. Select * from widgetClicks join on retialer d where productid = abc and wc.CreatedAt >= '2011-10-31 00:00:00.000' and wc.CreatedAt <= '2012-03-31 00:00:00.000'

编辑 **

对,我有我想要的 Sql 查询......

    SELECT COUNT(*) 
    FROM WidgetClicks 
    WHERE CreatedAt >= '2011-10-31 00:00:00.000' 
    and CreatedAt <= '2012-03-31 00:00:00.000' 
    and ProductId = 6914 
    AND RetailerId in (
                        SELECT RetailerId 
                        FROM AffiliateUpdateFiles auf
                        WHERE auf.Enabled = 1 AND auf.ManufacturerId = 46 and RetailerId <> 189
                        )
    AND ManufacturerWidgetId in (
                                select id 
                                from ManufacturerWidgets 
                                where  ManufacturerId = 46 and CountryId = 72
                                )

这就是你的东西-我实际上在Linq中需要它-如果有人可以为我转换它,我将不胜感激......我会试一试!

4

3 回答 3

1

尝试改用 LEFT OUTER JOIN。即使您的联接表中没有相应的行,这也会从 WidgetClicks 返回结果。

如果您发现在将联接更改为 LEFT OUTER JOIN 后获得了预期的行,请检查联接表中的数据 - 它们不包含您期望的行。

于 2012-04-16T17:35:48.450 回答
0

这是将上述内容转换为 Linq 的解决方案:

public int GetWidgetClicksForManufacturerCategoryModel(int manufacturerId, int countryId, int categoryId, int productId, DateTime myStartDate, DateTime myEndDate)
        {
            var widgetInCountry = (from mw in _er.ManufacturerWidgets
                                                where mw.ManufacturerId == manufacturerId && mw.CountryId == countryId
                                                select mw.Id).ToList();

            var enabledWidgets = (from auf in _er.AffiliateUpdateFiles
                        where auf.Enabled && auf.ManufacturerId == manufacturerId 
                        select auf.RetailerId).ToList();



            var widgetClicks = (from wc in _er.WidgetClicks
                          where wc.CreatedAt.CompareTo(myStartDate) >= 0 && wc.CreatedAt.CompareTo(myEndDate) <= 0 
                          && wc.ProductId == productId 
                          && enabledWidgets.Contains(wc.RetailerId) 
                          && widgetInCountry.Contains(wc.ManufacturerWidgetId)
                                               select wc.Id);

            return widgetClicks.Count();
        }
于 2012-04-20T13:31:07.427 回答
0

鉴于您的连接结构,如果从 WHERE 子句中删除“ad”表过滤器没有效果,则表明 ab-to-aa 连接或 ab-to-ac 连接正在过滤掉由于那些 WHERE 的东西条款。

ab -- aa -- ad
|
ac

因此,请尝试将广告过滤器保留在 WHERE 子句中,但删除其余部分,然后将它们一一添加,以查看哪个 WHERE 过滤器导致的结果比您预期的要少。一旦你看到事情被过滤掉了,你就可以确定你需要改变什么——并可能进行重组。您可能希望在此时使用一些 OUTER 连接或创建别名表的连接,例如: X JOIN (Y OUTER JOIN Z WHERE ...) AS Q ON X.id = Q.id WHERE ...

于 2012-04-16T17:42:02.373 回答