Here's the scenario:
I have 3 tables in a SQL Server 2008 database - SERVERS, InstalledPatches and Patchlist.
The SERVERS table has list of servers. InstalledPatches has list of servers and patches installed on them. Patchlist has list of all patches that SHOULD be installed on each server. All patches in PATCHLIST should be ideally installed on all servers in SERVERS table. I am trying to find the patches that are missing.
Sample data:
SERVERS
SERVERID SERVERNAME
-----------------------
1 ABC
.. ..
1500 XYZ
INSTALLEDPATCHES:
SERVERID PATCHID
-----------------
1 1
1 2
2 1
.. ..
1500 1
1500 2
PATCHLIST:
PATCHID PATCHNUMBER
---------------------
1 aaa
2 bbb
3 ccc
4 ddd
.. ..
15 ZZZ
Final report should indicate missing patches:
SERVERID MissingPATCHID
-------------------------
1 3
1 4
1 1500
2 3
2 4
2 1500
..
I have tried to use below query, but cant find all missing patches for each server.
SELECT
A.*
FROM
INSTALLEDPATCHES A
RIGHT OUTER JOIN
PATCHLIST B ON A.PATCHID = B.PATCHID
WHERE
A.PATCHID IS NULL
Any help would be really appreciated.
Thanks.
What about something like?
select s.SERVERID,
pl.PATCHID MissingPATCHID
from SERVERS s
cross join PATCHLIST pl
where not exists (select SERVERID,
PATCHID
from INSTALLEDPATCHES ip
where ip.SERVERID = s.SERVERID
and ip.PATCHID = pl.PATCHID)
I just created this SQLFiddle demo.
Try my query. It works now.
select s.serverid, p1.patchid as MissingPatchID
from [servers] as s
left join patchlist as p1
on 1=1
left join installedpatches as p2
on s.serverid = p2.serverid
and p1.patchid = p2.patchid
where p2.patchid is null
Related
I have 2 database's and I need to transfer data from database 1 to database 2.
Only the data that in database 1 but not in database 2.
DB 1
----
MyTBL 1
-------
111
222
333
444
555
666
DB 2
----
MyTBL 2
-------
111
222
666
I need to transfer from 1 to 2.
It will looks like this:
DB 2
----
MyTBL 2
--------
111
222
333
444
555
666
I need SQL query, I work in SQL Server 2012
I tried a few things - but without success.
Guessing at your names:
INSERT DB2.dbo.MyTBL2
(DataColumn)
SELECT DataColumn
from DB1.dbo.MyTBL1
EXCEPT SELECT DataColumn
from DB2.dbo.MyTBL2
SELECT EXCEPT can be very powerful. More info at https://learn.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-except-and-intersect-transact-sql?view=sql-server-ver15
If both databases are on the same instance, merge is a reasonable solution for this. Assuming the schema for all objects is dbo, run the merge statement inside db2:
merge dbo.MyTBL2 t2
using db1.dbo.MyTbl1 t1 on t1.yourcolumn = t2.yourcolumn
when not matched then
insert (yourcolumn) values (t1.yourcolumn);
I have a table like
Id RefNumber LotNum
---------------------------
1 Ref-1 10
2 Ref-1 11
Lotnumber:
Lot-Id Lot-Name
-------------------
10 Apple
11 Banana
I need my output to look like this:
Ref-1 Apple,Banana
Please help me - how can I achieve this?
On SQL Server 2017 and later, we can use STRING_AGG here:
SELECT
r.RefNumber,
STRING_AGG(l.[Lot-Name]) WITHIN GROUP (ORDER BY l.[Lot-Id]) AS LotNames
FROM Refs r
LEFT JOIN Lotnumber l
ON r.LotNum = l.[Lot-Id]
GROUP BY
r.RefNumber;
I have the following tables:
Manufacturer: Model range:
IDManufacturer Manufacturer IDModelRange IDManufacturer ModelRange
1 Mercedes 1 1 Benz
2 Audi 2 1 E-Klasse
3 2 TT
4 2 A4
I would like to query the data from both tables and the result to be like:
IDManufacturer+ModelRange
1 Benz
1 E-Klasse
I tried all joins but I couldn't find the right one. Need some help! Thanks
I'm assuming you are trying to find the Models for a specific Manufacturer.
If you are filtering by the ID you don't need the JOIN as stated in the comments.
If you are filtering by the name you can do it with a INNER JOIN. Here is an example:
Declare
#myManufacturerChoice VARCHAR(100) = 'Mercedes';
SELECT
M.IDManufacturer,
mo.ModelRange Manufacturer
from Manufacturer M
inner join [Model range] mo on
M.IDManufacturer = mo.IDManufacturer
where
M.Manufacturer = #myManufacturerChoice
Also a reference link about joins.
In SQL Server 2008, I want to join two tables depending on date sequence. More specifically, I need to left join Payments table to Profiles table by the following rules:
UserId has to be matched.
Every record in Payments matches the record in Profiles with the closest Profiles.CreationDate before Payments.PayDate.
For a simplified example,
Table Payments:
UserId PayDate Amount
1 2012 400
1 2010 500
2 2014 600
Table Profiles:
UserId CreationDate Address
1 2009 NY
1 2015 MD
2 2007 NJ
2 2013 MA
3 2008 TX
Desired Result:
UserId CreationDate PayDate Amount Address
1 2009 2010 500 NY
1 2009 2012 400 NY
2 2013 2014 600 MA
It's guaranteed that a user have at least 1 Profiles record before he pays. Another restriction is that I not authorized to write anything into the database.
I idea is first left join Payments with Profiles, then within the record group matching each (UserId, PayDate) tuple, sort it by CreationDate, then select the last record. But I don't know how to implement it in SQL language, or are there any better ways to do this merge?
Use Outer Apply to do this.
SELECT py.UserId,
CreationDate,
PayDate,
Amount,
Address
FROM Payments py
OUTER APPLY (SELECT TOP 1 *
FROM Profiles pr
WHERE py.UserId = pr.UserId
and PayDate> CreationDate
ORDER BY CreationDate desc) cs
SQLFIDDLE DEMO
I have two tables,Blog table has a FK BlogTagID column point to BlogTag table:
Blog table:
BlogID BlogTagID BlogTitle
1 2 test1
2 1 test2
3 2 test3
BlogTag table:
BlogTagID BlogTagName
1 JAVA
2 .NET
3 PHP
I would like to get the result:
BlogTagName count
JAVA 1
.NET 2
PHP 0
How to get this?Thank you very much!
try this code
select BlogTagName, count(blogid)
from BlogTag bt
left join Blog b on b.blogtagid = bt.BlogTagID
group by BlogTagName
SQL FIDDLE : http://sqlfiddle.com/#!3/356c5/8/0
You can try this also
SELECT BlogTagName,COUNT(BlogTagID) FROM Blog b JOIN BlogTagID bt WHERE b.BlogTagID=bt.BlogTagID GROUP BY BlogTagID;