带有日期列表的联接表-包括对NULL的引用

时间:2020-01-20 10:11:36

标签: sql sql-server

更多来自我的大脑冻结时刻。我敢肯定这会很简单。

我有两个桌子。一个是按周列出零件使用情况的列表。这称为TransactionsPerWeek,如下所示:

public Function1(TelemetryClient telemetryClient)
        {
            _telemetryClient = telemetryClient;
        }

我还有一个DateList表,其中包含星期数和年数

    <?php
    // set user agent 
      ini_set('user_agent', 'NameOfAgent (http://www.example.net)');
    //initializing url to crawl
      $url = "http://www.example.net/somepage.html";
     //checking robots.txt and extracting more links, sublinks from url
 if(robots_allowed($url, "NameOfAgent")) {
        $input = @file_get_contents($url) or die("Could not access file: $url");
        $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
        if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
          foreach($matches as $match) {
            // $match[2] = link address it means that will show you link title
            // $match[3] = link text it means that will show you link title
          }
        }
      } else {
        die('Access denied by robots.txt');
      }
   
//function for extracting title, keywords, description from url
    function file_get_contents_curl($url)
    {
    $ch = curl_init();

        curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

                        $data = curl_exec($ch);
                            curl_close($ch);

                                return $data;
                                }

                                $html = file_get_contents_curl($url);

                                //parsing begins here:
                                $doc = new DOMDocument();
                                @$doc->loadHTML($html);
                                $nodes = $doc->getElementsByTagName('title');

                                //get and display what you need:
                                $title = $nodes->item(0)->nodeValue;


                       $metas = $doc->getElementsByTagName('meta');

                                for ($i = 0; $i < $metas->length; $i++)
                                {
                                    $meta = $metas->item($i);
                                        if($meta->getAttribute('name') == 'description')
                                                $description = $meta->getAttribute('content');
                                                    if($meta->getAttribute('name') == 'keywords')
                                                            $keywords = $meta->getAttribute('content');
                                } 

}

当我在一周和一年中将两者正确地结合在一起时,我会得到

ItemPK       xWeek     xYear      TotalQty
1234         2         2019        65
1234         4         2019        15
1234         5         2019        50

我需要的是在每行上都有ItemPK,即使TotalQty为0。所以实际上,我需要:

xWeek       xYear
 1          2019
 2          2019
 3          2019

这是我的代码...

ItemPK     xWeek    xYear    TotalQty
NULL        1       2019       0
1234        2       2019      65
NULL        3       2019       0
1234        4       2019      15
1234        5       2019      50

希望这很清楚。预先感谢。

1 个答案:

答案 0 :(得分:0)

好吧,所以我按照@larnu的建议进行了操作,并将该项目与日期进行交叉加入,然后将其加入每周交易表中,并且成功了。谢谢。

这是我的代码;

SELECT itempk, week, year
, ISNULL(transactionsPerWeek.TotalQty,0) as TotalQty
from item
CROSS JOIN
(
        select year, week from DatesList where date > 
        DateAdd(wk,-51,DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + @@DATEFIRST - 2) % 7,GETDATE()))
        AND date <
        DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + @@DATEFIRST - 2) % 7,GETDATE())
        group by year, week
) dates

LEFT JOIN 

(
        SELECT iit.ItemFK, year(iit.transactiondate) xYear, datepart(wk,iit.transactiondate) xWeek, abs(sum(iit.quantity)) TotalQty from iteminventorytransaction iit
        INNER JOIN ItemInventoryTransactionType iitt on ItemInventoryTransactionTypePK = iit.ItemInventoryTransactionTypeFK
        where iit.itemfk = 5311 and iit.ItemInventoryTransactionTypeFK in (10,8)
        and iit.TransactionDate BETWEEN
        -- 1 year up to the sunday of last week 
        DateAdd(wk,-51,DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + @@DATEFIRST - 2) % 7,GETDATE()))
        AND
        DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + @@DATEFIRST - 2) % 7,GETDATE())
        AND Quantity < 0
        group by iit.itemfk, year(iit.transactiondate), datepart(wk,iit.transactiondate)
    ) transactionsPerWeek
ON itempk = transactionsperweek.ItemFK and transactionsPerWeek.xYear = dates.year and transactionsPerWeek.xWeek = dates.week
where itempk = 5311