显示零计数的行

时间:2016-02-10 15:44:48

标签: sql count aggregate

我试图显示行,即使它们返回零计数。但是没有运气。 我尝试过使用左连接。

select
    a.Month,
    count(b.InsuranceFromJob) [Number of Participants without Insurance]
from
    hsAdmin.ReportPeriodLkup a
left join hsAdmin.ClientReport b on
    b.ReportPeriod = a.ReportPeriodId
where
    b.insurancefromjob = 2 and
    a.reportperiodid between (@lastReportId - 11) and @lastReportId
group by
    a.Month

1 个答案:

答案 0 :(得分:1)

由于clientreport位于where,因此只有clientreport中存在的行才会出现在结果集中。

将支票移至联接,您将获得所需的结果:

// New condition_variable, to nofity main thread when child is done with level
std::condition_variable cv2;

// When a child is done, it will update this counter
int counter = 0; // This is already protected by cvMtx, otherwise it could be atomic.

// This is to sync cout
std::mutex cout_mutex;

void tfunc()
{
    for (int i = 0; i < 5; i++)
    {
        {
            std::lock_guard<std::mutex> l(cout_mutex);
            std::cout << "Level " << i + 1 << " " << std::this_thread::get_id() << std::endl;
        }

        {
            std::lock_guard<std::mutex> l(cvMtx);
            counter++; // update counter &
        }

        cv2.notify_all(); // notify main thread we are done.

        {
            //wait for notify
            unique_lock<mutex> lck(mtx);
            cv.wait(lck);
            // Note that I've removed the "ready" flag here
            // That's because u would need multiple ready flags to make that work
        }
    }
}

int main()
{
    std::thread t1(tfunc);
    std::thread t2(tfunc);

    for (int i = 0; i < 5; i++)
    {
        {
            unique_lock<mutex> lck(cvMtx);

            // Wait takes a predicate which u can take advantage of
            cv2.wait(lck, [] { return (counter == 2); });
            counter = 0;

            // This thread will get notified multiple times
            // But it only will wake up when counter matches 2
            // Which equals to how many threads we've created.              
        }

        // Sleeping a bit to know the code is working
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));

        // Wake up all threds and continue to next level.
        unique_lock<mutex> lck(mtx);
        cv.notify_all();
    }

    t1.join();
    t2.join();
    return 0;
}