I have the following code that will join two different tables together and provide the discrete data columns that I need. The issue is I also need a column that counts another instance from another table. See pointer reference undernearth the select statement.
SELECT
//count(of records returned from below query and duplicates of those found in outc table),
outage_duration.dgroup,
outage_duration.cause,
outage_duration.cdts,
outage_duration.cust_minutes,
outage_durati开发者_Python百科on.evntnum,
outage_duration.num_cust,
outage_duration.outage_type,
outage_duration.substation,
outage_duration.feeder,
outage_duration.out_minutes
FROM outage_duration,
aeven
WHERE outage_duration.evntnum = aeven.num_1
AND aeven.outage_type = 'T'
AND aeven.cdts >= '20110101060000UT'
AND aeven.curent = 'T'
AND aeven.open_and_curent = 'F'
AND aeven.ag_id NOT IN ('MEMT','MTRRDR','MTRTECH','GRDLT','FSRSR','ELECOPS','AMS','REVSEC')
AND outage_duration.out_minutes >= '240'
AND outage_duration.curent = 'T'
GROUP BY outage_duration.dgroup, outage_duration.cause, outage_duration.cdts, outage_duration.cust_minutes, outage_duration.evntnum, outage_duration.num_cust, outage_duration.outage_type, outage_duration.substation, outage_duration.feeder, outage_duration.out_minutes
ORDER BY outage_duration.evntnum
Sample AEVEN, Outage_Duration table
num_1
T344490
T344410
T344480
outage_duration
T344490
T344410
This would return T344490 and T44410 which is what the above search does. Now with these results I need to take a count from another table altogether and display the count as its own column beside the event number of how many occurences there were in that table.
outc (column 1 = unique id, column 2 = evntnum)
1. 1, T344490
2. 2, T344490
3. 3, T344410
4. 5, T344410
5. 6, T344410
6. 7, T344410
7. 8, T344410
The desired results would like to be the following:
1. T344410, 5, "other columns that were specified"
2. T344490, 2, "other columns that were specified"
How would I manage to do this?
SELECT
COUNT(*), --this
outage_duration.dgroup,
outage_duration.cause,
outage_duration.cdts,
outage_duration.cust_minutes,
outage_duration.evntnum,
outage_duration.num_cust,
outage_duration.outage_type,
outage_duration.substation,
outage_duration.feeder,
outage_duration.out_minutes
FROM
outage_duration
JOIN
aeven ON outage_duration.evntnum = aeven.num_1
JOIN
outc ON outage_duration.evntnum = outc. --insert column here
WHERE
AND aeven.outage_type = 'T'
AND aeven.cdts >= '20110101060000UT'
AND aeven.curent = 'T'
AND aeven.open_and_curent = 'F'
AND aeven.ag_id NOT IN ('MEMT','MTRRDR','MTRTECH','GRDLT','FSRSR','ELECOPS','AMS','REVSEC')
AND outage_duration.out_minutes >= '240'
AND outage_duration.curent = 'T'
GROUP BY outage_duration.dgroup, outage_duration.cause, outage_duration.cdts, outage_duration.cust_minutes, outage_duration.evntnum, outage_duration.num_cust, outage_duration.outage_type, outage_duration.substation, outage_duration.feeder, outage_duration.out_minutes
ORDER BY outage_duration.evntnum
Just add a COUNT(*) to the SELECT bit, and the table outc to the FROM
You already have a GROUP BY so should just slot itn
精彩评论