sql1
sql1

Reputation: 11

How to join two queries?

I have 2 separate queries and want to join them and don't know how to. If I run the below I get the error:

The used SELECT statements have a different number of columns.

How do I join them successfully?

first query

SELECT

hardware_info.id as ComputerID,
software_info.OSPlatform as OperatingSystemName,
software_info.OSVersion as OSVersion,
agent_info.AgentName as DeviceName,
hardware_info.last_modified as LastUpdate,
software_info.AD_MachineOUPath as NTDomainName,
software_info.AD_UserOUPath as AD_Site,
hardware_info.ComputerManufacturer as Manufacturer,
hardware_info.MachineModel as Model,
hardware_info.CPUCount as NoOfCPUs,
software_info.OSPlatform as OperatingSystemName,
hardware_info.MachineSerialno as SerialNumber,
hardware_info.CPUIDString as CPUDescription,
hardware_info.CPUSpeed as MaxClockSpeed,
hardware_info.PrimaryMACAddress as PhysicalAddress,
hardware_info.PhysicalMemorySize as Memory    

FROM
agent_info,
hardware_info,
software_info,
machine_tracking_info,
installed_software_titles     
WHERE
agent_info.heartbeat_record_id = hardware_info.agent_info_record_id limit 10

SAMPLE RESULT

ComputerID 1    
OperatingSystemName 116
OSVersion 1584  
DeviceName USALVWAM1    
LastUpdate 2016-07-08 04:27:01  
NTDomainName Servers\from New Computers
AD_Site Global-Users\US\CS  
Manufacturer    VMware, Inc.    
Model   GenericPC
NoOfCPUs    1   
OperatingSystemName SerialNumber VMware-42 04 90 30 1d a2 bc dc-69 a6 eb    47  68 76 b1 3b    
CPUDescription  Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz
MaxClockSpeed   2500000000  
PhysicalAddress 00:50:56:84:40:50
Memory 4294967296

**second query**
SELECT 
enum_value as osplatform 
FROM
software_info
INNER JOIN
    enum_osplatform ON software_info.OSPlatform = enum_osplatform.enum_key

**SAMPLE RESULT**

osplatform windows 2012 r2

Upvotes: 0

Views: 132

Answers (2)

Matt
Matt

Reputation: 15061

As above you can UNION and use NULL's in the second part.

I have approached it differently. Just need to fill in the XXX when you provide the information.

SELECT
h.id AS ComputerID,
a.AgentName AS DeviceName,
h.last_modified AS LastUpdate,
s.AD_MachineOUPath AS NTDomainName,
s.AD_UserOUPath AS AD_Site,
h.ComputerManufacturer AS Manufacturer,
h.MachineModel AS Model,
h.CPUCount AS NoOfCPUs,
s.OSPlatform AS OperatingSystemName,
h.MachineSerialno AS SerialNumber,
h.CPUIDString AS CPUDescription,
h.CPUSpeed AS MaxClockSpeed,
h.PrimaryMACAddress AS PhysicalAddress,
h.PhysicalMemorySize AS Memory, 
s.OSVersion,
enum_value as OSPlatform
FROM agent_info a
INNER JOIN hardware_info h ON a.heartbeat_record_id = h.agent_info_record_id 
INNER JOIN software_info s ON a.heartbeat_record_id = s.agent_info_record_id
INNER JOIN enum_osplatform e ON s.OSPlatform = e.enum_key

Upvotes: 0

ashkufaraz
ashkufaraz

Reputation: 5297

Number field in tow query must be equal.

try like this

 SELECT 
    hardware_info.id as ComputerID,
    agent_info.AgentName as DeviceName,
    hardware_info.last_modified as LastUpdate,
    software_info.AD_MachineOUPath as NTDomainName,
    software_info.AD_UserOUPath as AD_Site,
    hardware_info.ComputerManufacturer as Manufacturer,
    hardware_info.MachineModel as Model,
    hardware_info.CPUCount as NoOfCPUs,         
    hardware_info.MachineSerialno as SerialNumber,
    hardware_info.CPUIDString as CPUDescription,
    hardware_info.CPUSpeed as MaxClockSpeed,
    hardware_info.PrimaryMACAddress as PhysicalAddress,
    hardware_info.PhysicalMemorySize as Memory, 
    software_info.OSVersion ,
    software_info.OSPlatform as OperatingSystemName,
    null      
FROM
    agent_info,
    hardware_info,
    software_info,
    machine_tracking_info,
    installed_software_titles     
WHERE
    agent_info.heartbeat_record_id = hardware_info.agent_info_record_id

union

SELECT 
    null as ComputerID,
    null as DeviceName,
    null as LastUpdate,
    null as NTDomainName,
    null as AD_Site,
    null as Manufacturer,
    null as Model,
    null as NoOfCPUs,           
    null as SerialNumber,
    null as CPUDescription,
    null as MaxClockSpeed,
    null as PhysicalAddress,
    null as Memory,
    null as OSVersion,
     osplatform ,enum_value
FROM
    software_info
INNER JOIN
    enum_osplatform ON software_info.OSPlatform = enum_osplatform.enum_key

Upvotes: 2

Related Questions