简体   繁体   中英

Counting partial values or conditional values

I hope someone can lend an assist and some advise here. I'm trying to get a fairly complex result and not sure if I can do it as one query with subqueries, a union, or simply separate queries to be merged into excel after the fact.

I'm working with a legacy database from my predecessor with the following tables: Business (columns working with: id, sector, state) Forms (columns working with: Submitted (Y/N), id, business_id) Inventory (Columns working with: In_stock (Y/N), id, form_id)

I'm trying to get a final result that looks like this:

| SubmittedForms | Unsubmitted Forms | Sector | State |
|-----------------------------------------------------|
|       10       |         5         |  Agr   |  UT   |
|       0        |         7         |  Chem  |  MT   |
|       2        |         1         |  Bio   |  OK   |
|       13       |         0         |  Chem  |  NM   |

The main problem I'm getting is that while submitted forms doesn't need any further arguments and is a simple count, the unsubmitted forms are dependent on the Inventory.in_stock='Y'. Here's my query for the submitted forms:

SELECT COUNT(Forms.id) AS Submitted, Business.sector, Business.state
FROM Forms
JOIN Business ON Forms.business_id=Business.id
WHERE Forms.submitted='Y'
GROUP BY Business.state, Business.sector

Unfortunately, I can't seem to get the unsubmitted forms number to calculate correctly. It just returns the total count of rows where in_stock is Y for that sector.

If it's easier to run a separate query for Submitted and Unsubmitted that's fine for the end result but I need some help getting the correct count of Unsubmitted forms with the in_stock flagged as Y. Also, I attempted to use a COUNT DISTINCT but takes way too long, was still running after 10 minutes. Another complication I can envision in a single query option is the possibility of 0/null values in either Submitted or Unsubmitted forms

Any help is greatly appreciated!

One option:

SELECT COUNT(CASE WHEN Forms.submitted = 'Y' THEN 1 END) SubmittedForms,
       COUNT
        ( CASE WHEN Forms.submitted = 'N'
                AND EXISTS ( SELECT 1
                               FROM Inventory
                              WHERE form_id = Forms.id
                                AND in_stock = 'Y'
                           )
               THEN 1
           END
        ) UnsubmittedForms,
       Business.sector Sector,
       Business.state State
  FROM Forms
 RIGHT
 OUTER
  JOIN Business 
    ON Forms.business_id = Business.id
 GROUP
    BY Business.sector,
       Business.state
;

Another option, which might perform better:

SELECT COUNT(CASE WHEN Forms.submitted = 'Y' THEN 1 END) SubmittedForms,
       COUNT(CASE WHEN Forms.submitted = 'N' THEN 1 END) UnsubmittedForms,
       Business.sector Sector,
       Business.state State
  FROM ( SELECT *
           FROM Forms
          WHERE submitted = 'Y'
             OR id IN ( SELECT DISTINCT form_id
                          FROM Inventory
                           AND in_stock = 'Y'
                      )
       ) Forms
 RIGHT
 OUTER
  JOIN Business 
    ON Forms.business_id = Business.id
 GROUP
    BY Business.sector,
       Business.state
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM