Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Count within a count and percentage

$
0
0

Hello,

I am trying to get a percentage of work completed.  The below UNION query gives me the information I need but by the format.  Thanks for any help or direction

/* counts total # of building that needs to be inspected */

SELECT occ_main.station,occ_user.preincidentbook,COUNT(occ_main.occup_id)As "Occupancy Count"
From occ_main
LEFT JOIN occ_user ON occ_user.occup_id = occ_main.occup_id
GROUP BY occ_user.preincidentbook,occ_main.station
UNION ALL

/* counts # of completed inspections with an inspection type of P * and the inspection was completed on or after 01/01/2014 */

SELECT occ_main.station,occ_user.preincidentbook,COUNT(Ins_main.occup_id)As "Inspection count"
From occ_main
LEFT JOIN ins_main ON INS_MAIN.occup_id = occ_main.occup_id
LEFT JOIN occ_user ON occ_user.occup_id = occ_main.occup_id
WHERE  ins_main.type='P' and ins_main.completed >='01/01/2014'
GROUP BY occ_user.preincidentbook,occ_main.station
ORDER by occ_main.station,occ_user.preincidentbook

part of the output

01     1    392
01     1    62
01     2    447
01     2    73
01     3    319
01     3    72

Desired out

01     1     62     392     17%

01     2     73     447     16%

01     3     72     319     22%


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>