WQL Query and displaying custom fields - sql

I'm running a WQL Query from SCCM to grab a list of computers with one of two applications installed. I'd like to display the name of the offending application in a column alongside the computer name. Do you know of an easy way to do this?
Another way to accomplish this would be multiple Select Statements, of course.
My query code:
select SMS_G_System_SYSTEM.ResourcelD From SMS_R_System inner
join SMS_G_System_SYSTEM on SMS_G_System_5VSTEM.ResourcelD =
SMS_R_System.ResourcelD inner join
SMS_G_System_ADD_REMOVE_PROGRAMS on
SMS_G_System_ADD_REMOVE_PROGRAM5. ResourcelD =
SMS_R_System. Resourceld where
(SMS_G_System_ADD_REMOVE_PROGRAMS. DisplayName like "%Chrome%") or
(SMS_G_System_ADD_REMOVE_PROGRAMS,DisplayName like "%Firefox%”)

To add a column you add it to your select.
SELECT
SMS_R_System.Netbios_Name0 as 'Name',
SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName as 'Software'
FROM
....
etc
Let me know if this helps...

Related

Getting value from a table and add value from table 2 if excist

I'm new to SQL and im wondering if something like this is possible.
It's probably a super simple solution but i cant seem to solve it. I'm using an Oracle database.
Employee_Table:
employeeNr
username
address
epost
Computer_Table:
employeeNr
ComputerNumber
ComputerUpdated
When I'm using
SELECT *
From Employee_Table,
Computer_Table
WHERE Employee_Table.EmployeeNr = Computer_Table.EmployeeNr
AND Employee_Table.username LIKE ('%SomeUsername%')
When I'm using this sqlstring I only get users with computers. I would like to get all users and the computers of those who has one.
Now the million dollar question. What modifications do I need to make?
Never use commas in the FROM clause. Always use explicit JOIN.
In this case, you want a LEFT JOIN:
SELECT *
FROM Employee_Table e LEFT JOIN
Computer_Table c
ON c.EmployeeNr = c.EmployeeNr
WHERE e.username LIKE '%SomeUsername%';
When you use LEFT JOIN, conditions on the first table go on the WHERE clause. Conditions on the second table go in the ON clause.

Adding multiple rows of data into one - TSQL

I'm currently working on a project of my own that uses an SQL DB to store character information for an RPG game. I'm trying to write a select statement to retrieve the information related to the equipment the user is using, at the moment I have:
SELECT
CE.CharacterID, CE.EquipmentSlotID, CE.ItemID, I.ItemTypeID, I.Name, I.Image, IT.Name, IT.Description, concat(IA.AttributeID, ' ', IA.value )
FROM
CharacterEquipment CE INNER JOIN Item I
ON CE.ItemID = I.ItemID
INNER JOIN ItemType IT
ON I.ItemTypeID=IT.ItemTypeID
INNER JOIN ItemAttribute IA
ON I.ItemID=IA.ItemID
WHERE CE.CharacterID = 1
ORDER BY CE.ItemID ASC;
This returns this:
My problem is that each item (ItemID) can have multiple attributes (AttributeID) and I want all these attributes to appear on a single line but I'm not sure how I'd go about combining the results
EDIT:
Sorry guys, should have said, I've been experimenting with the group concat function but SQL isnt my strong suit so im having bother writing it and figuring out how it works, any chance any one could explain it abit more indepth? Thanks
Possible solution to this would be improving your database structure and try denormalizing it.
2.But if you wish to continue with same structure try using Concatenation using COALESCE function
For your reference :Concatenate many rows to one

How to Select Specific data on query with RIGHT JOIN statement?

I am joining 2 tables using RIGHT JOIN statement. I used below query and it works good. However it still display all data whenever I tried to select specific user
SELECT TBLNOTIFICATIONS.NOTIFICATION_ID, TBLNOTIFICATIONS.NOTIFICATION_TYPE, FILENAMES_LIST.LOCATION_FILENAME, TBLNOTIFICATIONS.NOTIFICATION_DATE
FROM TBLNOTIFICATIONS
RIGHT JOIN FILENAMES_LIST
ON TBLNOTIFICATIONS.NOTIFICATION_ID=FILENAMES_LIST.NOTIFICATION_ID
WHERE TBLNOTIFICATIONS.USER_ID='JCON'
What should I do to select data from specific user?
Thanks in advance.
You are filtering on the left table, so all the data of the right table will still be shown.
It is probably enough to change the query to a LEFT JOIN to get the results you want.
Besides that, you can use aliases to make your query more readable, like so:
SELECT tn.NOTIFICATION_ID, tn.NOTIFICATION_TYPE, fl.LOCATION_FILENAME, tn.NOTIFICATION_DATE
FROM TBLNOTIFICATIONS AS tn
LEFT JOIN FILENAMES_LIST AS fl
ON tn.NOTIFICATION_ID = fl.NOTIFICATION_ID
WHERE tn.USER_ID='JCON'

Compare two tables and find matches in ms-access 2007

I am trying to compare two tables in ms access 2007 to find matches from the tables which I know to have one field in common. When I try to run the following query
SELECT
FROM FirstTrial INNER JOIN ConsolidatedDatabase
ON FirstTrial.ModelNumbr = ConsolidatedDatabase.ModelNumbr;
I get an error 'Query must have at least one destination field'
What do I need to change in order to make the query work?
You need some fields:
SELECT *
FROM FirstTrial INNER JOIN ConsolidatedDatabase
ON FirstTrial.ModelNumbr = ConsolidatedDatabase.ModelNumbr;
It would be better to put some names instead of *
In MS Access, you can use the query design window.

'dynamic' SQL join possible?

I have a table Action with a (part of the) structure like this:
Action:
ActionTypeID,
ActionConnectionID
...
ActionTypeID refers to types 1-5 which correspond to different tables (1=Pro, 2=Project etc.)
ActionConnectionID is the primaryKey in the corresponding table;
ie. ActionTypeID=1,
ActionConnectionID=43 -> would point
to Pro.ProID=43 and ActionTypeID=2,
ActionConnectionID=233 -> would point
to Project.ProjectID=233
Is there a way to 'dynamically join the different tables depending on the value in the ActionTypeID column?
ie. for records with the ActionTypeID=1 this would be:
Select Action.*
From Action Left Join Pro On Action.ActionConnectionID=Pro.ProID
for records with the ActionTypeID=2 this would be:
Select Action.*
From Action Left Join Project On Action.ActionConnectionID=Project.ProjectID
etc.
If this is not possible to accomplish in one query I will have to loop over all the possible ActionTypes and perform the query and then afterwards join the data in one query again - that would be possible, but doesnt sound like the most efficient way :-)
Something like this should do:
Select Action.*
From Action
Left Join Pro
ON Action.ActionConnectionID=Pro.ProID and ActionTypeID=1
Left Join Project
ON Action.ActionConnectionID=Project.ProjectID and ActionTypeID=2
If that doesn't work for either try using dynamic sql which is a bad solution or properly normalize your data.
Are you just trying to select everything without any filters at all? I always hate when people give answers that are basically "don't do t like that, do it like this instead" but now I'm going to go ahead and do that myself. Have you considered a different schema where you don't have to write this kind of query? I assume that the Pro, Project, etc, tables all have the same schema - can they be combined into one table? Perhaps you don't have control over that and are working with a DB you can't change (been there myself). You should explore using UNION to join up the pieces that you need.
(Select Action.*
From Action Left Join Pro On Action.ActionConnectionID=Pro.ProID)
UNION
(Select Action.*
From Action Left Join Project On Action.ActionConnectionID=Project.ProjectID)