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

Union two rows

$
0
0

I have a query with two tables, union'd together to produce a single result - but cannot achieve this. I make both tables look the same; (same fields, order & datatypes) yet the results are two rows.

In all my years working with SQL I have never encountered such as this, and need another set of eyes for help.

The results of each query have potentially different values, which should not matter, but it does.

What am I doing wrong?

SELECT
                      A.ActivityDate, A.EID EID, 
                      ISNULL(FelonyArrests, 0) FelonyArrests, ISNULL(MisdArrests, 0) MisdArrests, ISNULL(NTA, 0) NTA , 
                      ISNULL(FSAO, 0) FSAO, ISNULL(Narcotics, 0) Narcotics, ISNULL(ArrestWarrantsIssued, 0) ArrestWarrantsIssued, 
                      ISNULL(Backups, 0) Backups, 
                      ISNULL(CannabisSeizedQty, 0) CannabisSeizedQty, 
                      ISNULL(CocaineSeizedQty, 0) CocaineSeizedQty,  ISNULL(PillsSeizedQty, 0) PillsSeizedQty, 
                      ISNULL(CourtesyNotices, 0) CourtesyNotices, ISNULL(DICalls, 0) DICalls, ISNULL(DUICitations, 0) DUICitations,
                      ISNULL(DUIArrests, 0) DUIArrests, ISNULL(FIR, 0) FIR, ISNULL(Firearms, 0) Firearms, 
                      ISNULL(HeroinSeizedQty, 0) HeroinSeizedQty, ISNULL(ParkingCitations, 0) ParkingCitations, 
                      ISNULL(PropertyRecoveredVal, 0) PropertyRecoveredVal, ISNULL(SICalls, 0) SICalls, ISNULL(TotalReports, 0) TotalReports, 
                      ISNULL(TrafficCitations, 0) TrafficCitations, ISNULL(VehicleRecovered, 0) VehicleRecovered, 
                      ISNULL(VehicleRecoveredValue, 0) VehicleRecoveredValue, ISNULL(VehicleStops, 0) VehicleStops, 
                      ISNULL(DrugMoneySeized,0) DrugMoneySeized, ISNULL(OtherSeizedQty,0) OtherSeizedQty, 
                      0 WarrantArrests, 0 CommunityEventHours, 0 SpecialDetailHours, 0 SurveillanceHours, 
                      0 SearchWarrantHours, 
                      0 SearchWarrantsExec, 0 TrainingHours, 0 BikePatrolHours, 
                      C.Squad, lastname + ', ' + firstname Deputy, 
                      0 HoursWorked
FROM				  STATSPOOL.dbo.t_AgencyWideStats A LEFT JOIN
                      STATS.dbo.StatUsers C ON A.EID = C.EID AND A.ActivityDate BETWEEN C.StartDate AND ISNULL(C.EndDate, GETDATE())
WHERE       C.UNITID = 4 AND C.UNITID != 1 AND C.SQUAD IS NOT NULL
AND			A.EID = 4805
AND			A.ActivityDate = '7/12/13'
UNION
SELECT
                      A.StatDate ActivityDate, A.EID EID, 
                      0 FelonyArrests, 0 MisdArrests, 0 NTA,
                      0 FSAO, 0 Narcotics, 0 ArrestWarrantsIssued, 
                      0 Backups, 
                      0 CannabisSeizedQty, 0 CocaineSeizedQty, 0 PillsSeizedQty, 0 CourtesyNotices, 0 DICalls, 
                      0 DUICitations, 0 DUIArrests, 0 FIR, 0 Firearms, 0 HeroinSeizedQty, 0 ParkingCitations, 0 PropertyRecoveredVal, 
                      0 SICalls, 0 TotalReports, 0 TrafficCitations, 0 VehicleRecovered, 0 VehicleRecoveredValue, 0 VehicleStops, 
                      0 DrugMoneySeized, 0 OtherSeizedQty, 
                      ISNULL(WarrantArrests, 0) WarrantArrests, ISNULL(CommunityEventHours, 0) CommunityEventHours, 
                      ISNULL(SpecialDetailHours, 0) SpecialDetailHours, ISNULL(SurveillanceHours, 0) SurveillanceHours, 
                      ISNULL(SearchWarrantHours, 0) SearchWarrantHours, ISNULL(SearchWarrantExec, 0) SearchWarrantExec, 
                      ISNULL(TrainingHours, 0) TrainingHours, ISNULL(BikePatrolHours, 0) BikePatrolHours, 
                      c.Squad, lastname + ', ' + firstname Deputy, 
                      0 HoursWorked
FROM				  POPS_STATS A LEFT JOIN
                      STATS.dbo.StatUsers C ON A.EID = C.EID AND A.StatDate BETWEEN C.StartDate AND ISNULL(C.EndDate, GETDATE())
WHERE				C.Squad IS NOT NULL
AND			A.EID = 4805
AND			A.STATDate = '7/12/13'


Viewing all articles
Browse latest Browse all 23857

Trending Articles



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