Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
typo, add link, trim noise
Source Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620

I suggest a radiallyradically new approach with crosstab() from the additional module tablefunctablefunc. You need to install it once per database. Detailed instructions:

  • We need a single unique column per group for crosstab(). Since you don't have that per (keyword_id int, project_id) I am adding a surrogate key with row_number() to project_keyword.

  • JOIN that to a set of dates you are interested in (today, yesterday, .. etc.). One row per date.

  • LEFT JOIN to project_reports to attach the position for each date if available. Default to 0 with COALESCE.

  • Cross-tabulate to get a single row with one position column per given date.

  • Finally JOIN to keyword just to add the name. Add the current date and remove rn from the result while being at it.
    Aside: "name" is not a good name. I'd use something descriptive instead.

Can't provide a fiddle, installing additional modulesAside: "name" is not allowed therea good name. I'd use something descriptive instead.

I suggest a radially new approach with crosstab() from the additional module tablefunc. You need to install it once per database. Detailed instructions:

  • We need a single unique column per group for crosstab(). Since you don't have that per (keyword_id int, project_id) I am adding a surrogate key with row_number() to project_keyword.

  • JOIN that to a set of dates you are interested in (today, yesterday, ...). One row per date.

  • LEFT JOIN to project_reports to attach the position for each date if available. Default to 0 with COALESCE.

  • Cross-tabulate to get a single row with one position column per given date.

  • Finally JOIN to keyword just to add the name. Add the current date and remove rn from the result while being at it.
    Aside: "name" is not a good name. I'd use something descriptive instead.

Can't provide a fiddle, installing additional modules is not allowed there.

I suggest a radically new approach with crosstab() from the additional module tablefunc. You need to install it once per database. Detailed instructions:

  • We need a single unique column per group for crosstab(). Since you don't have that per (keyword_id int, project_id) I am adding a surrogate key with row_number() to project_keyword.

  • JOIN that to a set of dates you are interested in (today, yesterday etc.). One row per date.

  • LEFT JOIN to project_reports to attach the position for each date if available. Default to 0 with COALESCE.

  • Cross-tabulate to get a single row with one position column per given date.

  • Finally JOIN to keyword just to add the name. Add the current date and remove rn from the result while being at it.

Aside: "name" is not a good name. I'd use something descriptive instead.

Explanation wasn't clear enough
Source Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620
  • We need a single unique column per group for crosstab(). Since you don't have that per (keyword_id int, project_id) I am adding a surrogate key with row_number() to project_keyword.

  • JOINJOIN that to a set of dates you are interested in (today, yesterday, ...). One row per date.

  • LEFT JOINLEFT JOIN to project_reports to attach the position for said dayseach date if available. Default to 0 with COALESCE.

  • Cross-tabulate to get one columna single row with theone position column per given date.

  • Finally JOINJOIN to keyword just to add the namename. Add the current date and remove rn from the result while being at it.
    Aside: "name" is not a good name. I'd use something descriptive instead.

  • We need a single unique column per group for crosstab(). Since you don't have that per (keyword_id int, project_id) I am adding a surrogate key with row_number() to project_keyword.

  • JOIN that to a set of dates you are interested in (today, yesterday, ...). One row per date.

  • LEFT JOIN to project_reports for said days.

  • Cross-tabulate to get one column with the position per date.

  • Finally JOIN to keyword just to add the name. Add the current date and remove rn from the result while being at it.

  • We need a single unique column per group for crosstab(). Since you don't have that per (keyword_id int, project_id) I am adding a surrogate key with row_number() to project_keyword.

  • JOIN that to a set of dates you are interested in (today, yesterday, ...). One row per date.

  • LEFT JOIN to project_reports to attach the position for each date if available. Default to 0 with COALESCE.

  • Cross-tabulate to get a single row with one position column per given date.

  • Finally JOIN to keyword just to add the name. Add the current date and remove rn from the result while being at it.
    Aside: "name" is not a good name. I'd use something descriptive instead.

clarify
Source Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620
Loading
format
Source Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620
Loading
added 79 characters in body
Source Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620
Loading
explain
Source Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620
Loading
Source Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620
Loading