We help IT Professionals succeed at work.
Get Started

SQLite multiple count columns

Sam K
Sam K asked
on
750 Views
Last Modified: 2015-11-11
Using SQLite how can I get multiple count columns in an SQL query?
I have a table that has two columns in the same table that I am querying, "workpack group" and "pull status". the pull status is either "Completed", Started" or "" ("" not, NULL, I have inherited this table from a previous person who did not normalize the tables)
I would like to get a count of each pull status for each workpack group. I have achieved this by creating 3 separate views, querying the views and then deleting the views. This is a very wasteful method, that I would like to avoid if possible.
Below is my working code, as an example and i have attached a screenshot of the working output.
is there a better way to do this?
Thank you

CREATE VIEW wk_group_pulled AS SELECT
      cableList."Workpack Group",
      count(*)
FROM
      cableList
WHERE
      cableList."Workpack Group" != "A/G"
AND cableList."Pull Status" = "Completed"
GROUP BY
      cableList."Workpack Group";

CREATE VIEW wk_group_started AS SELECT
      cableList."Workpack Group",
      count(*)
FROM
      cableList
WHERE
      cableList."Workpack Group" != "A/G"
AND cableList."Pull Status" = "Started"
GROUP BY
      cableList."Workpack Group";

CREATE VIEW wk_group_blank AS SELECT
      cableList."Workpack Group",
      count(*)
FROM
      cableList
WHERE
      cableList."Workpack Group" != "A/G"
AND cableList."Pull Status" = ""
GROUP BY
      cableList."Workpack Group";

SELECT
      cableList."Workpack Group",
      wk_group_pulled."count(*)" AS completed,
      wk_group_started."count(*)" AS started,
      wk_group_blank."count(*)" AS pending
FROM
      cableList
LEFT JOIN wk_group_pulled ON cableList."Workpack Group" = wk_group_pulled."Workpack Group"
LEFT JOIN wk_group_started ON cableList."Workpack Group" = wk_group_started."Workpack Group"
LEFT JOIN wk_group_blank ON cableList."Workpack Group" = wk_group_blank."Workpack Group"
GROUP BY
      cableList."Workpack Group"
ORDER BY
      cableList."Workpack Group" ASC;

DROP VIEW
IF EXISTS wk_group_pulled;

DROP VIEW
IF EXISTS wk_group_started;

DROP VIEW
IF EXISTS wk_group_blank;
workpack_group_screen1.png
Comment
Watch Question
Chief Technology Officer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE