Using coalesce and joins to get data in SQL Server

时间:2016-02-12 21:03:43

标签: sql-server join

I have 3 tables. The first table 'Status_Mapping' has following columns

self.view.frame

'Lead_transaction' has the columns:

Status_original  Status_Site
accepted         Call Verified
duplicate        Duplicate Leads
dq               DQ

'Lead_Instance' table:

Lead_transaction_id   Rate   Status
  11                  0.01   accepted
  12                  0.02   accepted
  13                  0.01   duplicate

What I want to do is loop through the status_mapping table and get the count(lead_instance_id) and sum(rate) for each status and then site status from the previous table with product_id = 6 and affiliate_id = 10 My End result should be like

Lead_Instance_id   Lead_transaction_id   product_id  affiliate_id
 1                    11                   6            10
 2                    12                   7            11
 3                    13                   6            10

SQL Attempt

Total              Sum   Status
 1                 0.01   Call Verified
 1                 0.01   Duplicate Leads
 0                 0.00   DQ

2 个答案:

答案 0 :(得分:0)

[string][int]$_.name.substring(0,4)

答案 1 :(得分:0)

    sp = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);

    final ArrayList<CharSequence> lista_string = new ArrayList<CharSequence>();

    for (int i = 0; i < lista.size(); i++) {

        Hue e = null;
        e = lista.get(i);
        e.setValor(sp.load(getActivity(), getResId(e.getNome_hue(), ListaHues.class), 1));
        lista_string.add(lista.get(i).getDescricao());
    }

http://sqlfiddle.com/#!3/09c8b/1

The important part here is to do the inner join first so you can limit the results to the product and affiliate that you want and then afterward use an outer join to keep the full list of statuses. This is a case where you'll also occasionally see a right join instead of the nesting.