Simple SQL Query question
Author
Discussion

LarryUSA

Original Poster:

4,378 posts

287 months

Thursday 22nd March 2018
quotequote all
I have a database (DB_Service) with multiple tables in it. The data I want 'software version installed' is in the table SOFTWARE_INVENTORY. So, the query is:

select * from SOFTWARE_INVENTORY;

However, this table only contains a device ID to which the data belongs. I need to match the device ID to a username. That exists in another table 'DEVICE'. So, the query is:

select * from DEVICE;

But this gives me two separate sets of data, really I want both sets of data merged so I can export to Excel and filter/sort.

What query could I use to extract the data from both tables and merge them into one CSV file? Then I could sort on software version and it would show the associated device ID and username?

anxious_ant

2,626 posts

110 months

Thursday 22nd March 2018
quotequote all
LarryUSA said:
I have a database (DB_Service) with multiple tables in it. The data I want 'software version installed' is in the table SOFTWARE_INVENTORY. So, the query is:

select * from SOFTWARE_INVENTORY;

However, this table only contains a device ID to which the data belongs. I need to match the device ID to a username. That exists in another table 'DEVICE'. So, the query is:

select * from DEVICE;

But this gives me two separate sets of data, really I want both sets of data merged so I can export to Excel and filter/sort.

What query could I use to extract the data from both tables and merge them into one CSV file? Then I could sort on software version and it would show the associated device ID and username?
Select *,B.* from SOFTWARE_INVENTORY A
inner join DEVICE B on A.DEVICE_ID = B.DEVICE_ID


LarryUSA

Original Poster:

4,378 posts

287 months

Thursday 22nd March 2018
quotequote all
Wow! Thanks anxious_ant, that worked just with a copy and paste command. I knew PH would come up with the answer!

The icing on the cake would be when I select all the data, right click and 'Save Results As...' if it would export the column headings! But it's no big deal...

LarryUSA

Original Poster:

4,378 posts

287 months

Thursday 22nd March 2018
quotequote all
fbc said:
anxious_ant said:
Select *,B.* from SOFTWARE_INVENTORY A
inner join DEVICE B on A.DEVICE_ID = B.DEVICE_ID
Just to expand on that slightly, in the inner join statement, field A.DEVICE_ID is the field in the SOFTWARE_INVENTORY table containing the Device ID, and likewise B.DEVICE_ID is the field in the DEVICE table containing the Device ID. These fields may have differing names in the respective tables (they shouldn't, but they might).
Both tables use DEVICE_ID for the name so we're all good.

anxious_ant

2,626 posts

110 months

Thursday 22nd March 2018
quotequote all
LarryUSA said:
Wow! Thanks anxious_ant, that worked just with a copy and paste command. I knew PH would come up with the answer!

The icing on the cake would be when I select all the data, right click and 'Save Results As...' if it would export the column headings! But it's no big deal...
If you are using SQL Management Studio right click on the grid output and select "Copy with headers" and paste it into Excel then save as CSV.

sas62

5,929 posts

109 months

Thursday 22nd March 2018
quotequote all
If this is SQL server you can get heading included.

Query - Query Options - Results - Grid - Include Column Headings

LarryUSA

Original Poster:

4,378 posts

287 months

Thursday 22nd March 2018
quotequote all
anxious_ant said:
LarryUSA said:
Wow! Thanks anxious_ant, that worked just with a copy and paste command. I knew PH would come up with the answer!

The icing on the cake would be when I select all the data, right click and 'Save Results As...' if it would export the column headings! But it's no big deal...
If you are using SQL Management Studio right click on the grid output and select "Copy with headers" and paste it into Excel then save as CSV.
That's it! Worked perfectly, thanks so much. If I had asked here about 5 days ago, would have saved me a lot of grief, lol.

anxious_ant

2,626 posts

110 months

Thursday 22nd March 2018
quotequote all
LarryUSA said:
That's it! Worked perfectly, thanks so much. If I had asked here about 5 days ago, would have saved me a lot of grief, lol.
No problems, glad I could help smile
Have a rather complex SQL issue to sort out tomorrow so wish my luck!

arnie12

165 posts

223 months

Thursday 22nd March 2018
quotequote all
I really want to know the complex sql query biggrin

LarryUSA

Original Poster:

4,378 posts

287 months

Thursday 22nd March 2018
quotequote all
arnie12 said:
I really want to know the complex sql query biggrin
It's in my first post - to me that was complex, lol biggrin

anxious_ant

2,626 posts

110 months

Friday 23rd March 2018
quotequote all
arnie12 said:
I really want to know the complex sql query biggrin
2 words, Dynamic SQL beer:

arnie12

165 posts

223 months

Friday 23rd March 2018
quotequote all
Data warehouse developer here, could help if mega stuck smile