Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Buda56
Starting Member
14 Posts |
Posted - 2009-08-05 : 01:19:50
|
Hi, I would like to develop an SQL query that would produce the above type results. I need to create a query/report that returns items in a N, N-1 type senario also an aditional report that would give me a count. If the item is in N then it would not show in N-1, if it is not in N then it would be in a subsequent N-x dependant upon the date.The data source is a list of machine names with dates when patched, a bit value that confirms the patching was successful, and I would like to produce a report that shows those that were patched and those that were not based on the month of the patch date ie. current month would be N, previous month would be N-1, 2 months previous would be N-2 etc.Hope this makes sence..Regards..Peter  |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
|
|
Buda56
Starting Member
14 Posts |
Posted - 2009-08-05 : 19:42:56
|
| Hi, Ok sorry this is my first post here so here goes.DDL:[Code]CREATE TABLE [dbo].[Master_SWD_PatchAll]( [ID] [int] IDENTITY(1,1) NOT NULL, [Account] [nvarchar](6) NULL, [ServerName] [nvarchar](50) NULL, [PDate] [datetime] NULL, [PrStatus] [bit] NULL, [PrSvcName] [nvarchar](max) NULL, [PrSysStatus] [bit] NULL, [PrSysSource] [nvarchar](max) NULL, [PatchReq] [bit] NULL, [PatchInst] [nvarchar](max) NULL, [PatchInstStatus] [bit] NULL, [PoSvcStatus] [bit] NULL, [PoSvcName] [nvarchar](max) NULL, [PoSysStatus] [bit] NULL, [PoSysSource] [nvarchar](max) NULL, [Comments] [varchar](max) NULL, [Exempt] [bit] NULL, CONSTRAINT [PK_Master_SWD_PatchAll] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY][/Code]Not sure about the DML[Code]INSERT INTO [WPS_OPS].[dbo].[Master_SWD_PatchAll] ([Account] ,[ServerName] ,[PDate] ,[PrStatus] ,[PrSvcName] ,[PrSysStatus] ,[PrSysSource] ,[PatchReq] ,[PatchInst] ,[PatchInstStatus] ,[PoSvcStatus] ,[PoSvcName] ,[PoSysStatus] ,[PoSysSource] ,[Comments] ,[Exempt])[/Code]Haven't attempted a solution as not exactly sure as to how to go about this, all I can gather from the end result I require would be that I can get a count of Servers that meet criteria "N, N-1, N-2 etc" and perhaps have naother query that would return some sort of Drill down on the N, N-1 results so I can see the actual servers that meet the criteria.The summary output would look something like:N - 650N-1 - 45N-2 - 8EtcDetailed data would be something like:Servername, PatchdateIn both instances above a Server that appears in N would not be in N-1, N-2 etc even though there would be an entry that would match that criteria.I have thought of using datediff then grouping on the result but the issue of only appearing in one group is something I'm not sure how to deal with.Hope this makes it clearer..Regards..Peter |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2009-08-05 : 20:52:05
|
I'm not following, post some sample data and then your desired output. Try to simplify the data to just illustrate what you are looking to do, we do not need the entire table layout, if you are not using all the columns. Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
Buda56
Starting Member
14 Posts |
Posted - 2009-08-05 : 21:53:51
|
| Hi, Here is some sample data:BBP FLNAPF007 16/03/2009BBP FLNAPF007 16/04/2009BBP FLNAPM004 16/03/2009BBP FLNLCF006 16/03/2009BBP FLNLCM006 16/04/2009BBP FLNLKA015 16/03/2009BBP FLNLKA016 16/02/2009BBP FLNLKA017 16/03/2009BBP FLNLKA018 16/04/2009BBP FLNLKA019 16/03/2009BBP FLNPKA005 16/03/2009BBP FLNPKA006 16/03/2009BBP FLNPKA008 16/03/2009BBP FLNPKA014 16/04/2009BBP FLNPKF002 16/04/2009BBP FLNPKM003 16/03/2009BBP FLNPKM023 16/03/2009Sample summary out put would be:N 5N-1 11N-2 1Sample drill down output would be:N BBP FLNPKA014 16/04/2009N BBP FLNPKF002 16/04/2009N BBP FLNLCM006 16/04/2009N BBP FLNAPF007 16/04/2009N BBP FLNLKA018 16/04/2009N-1 BBP FLNAPF007 16/03/2009N-1 BBP FLNAPM004 16/03/2009N-1 BBP FLNLCF006 16/03/2009N-1 BBP FLNLKA015 16/03/2009N-1 BBP FLNLKA017 16/03/2009N-1 BBP FLNLKA019 16/03/2009N-1 BBP FLNPKA005 16/03/2009N-1 BBP FLNPKA006 16/03/2009N-1 BBP FLNPKA008 16/03/2009 N-1 BBP FLNPKM003 16/03/2009N-1 BBP FLNPKM023 16/03/2009N-2 BBP FLNLKA016 16/02/2009Regards..Peter |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2009-08-05 : 22:53:22
|
Still don't know if I'm following the sum lables, but this will give you the output you asked for.declare @Mytable table(col1 varchar(3),col2 varchar(10),dt datetime)Insert into @Mytable(Col1,col2,dt)select 'BBP', 'FLNAPF007', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNAPF007', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNAPM004', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNLCF006', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNLCM006', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNLKA015', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNLKA016', convert(datetime,'16/02/2009',104) union Allselect 'BBP', 'FLNLKA017', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNLKA018', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNLKA019', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKA005', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKA006', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKA008', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKA014', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNPKF002', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNPKM003', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKM023', convert(datetime,'16/03/2009',104)declare @MYDATE datetimeset @MyDate = convert(datetime,'16/04/2009',104)select 'N' + case when datediff(Month,@MyDate,dt) = 0 then '' else convert(varchar(3),datediff(Month,@MyDate,dt)) end as SUMFIELD,count(*) as mycountfrom @Mytablegroup by dtorder by SumField Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
Buda56
Starting Member
14 Posts |
Posted - 2009-08-05 : 23:41:49
|
| Hi, thanks for that, the end result gives me the N, N-1 summary that I required but it also gives me duplicates in N and N-1. I need to have no duplicates in N or any subsequent N-x.My next question I guess is how to apply the "Select" to an existing table of data??[Code]declare @Mytable table(col1 varchar(10),col2 varchar(20),dt datetime)Insert into @Mytable(Col1,col2,dt)select account, Servername, convert(datetime,[PDate], 104) FROM Master_SWD_PatchAll UNION ALLdeclare @MYDATE datetimeset @MyDate = convert(datetime, GETDATE(),104)select 'N' + case when datediff(Month,@MyDate,dt) = 0 then '' else convert(varchar(3),datediff(Month,@MyDate,dt)) end as SUMFIELD,count(*) as mycountfrom @Mytablegroup by dtorder by SumField[/Code]Unfortunately I get the following error "Msg 156, Level 15, State 1, Line 7Incorrect syntax near the keyword 'declare'"Regards..Peter |
 |
|
|
matty
Posting Yak Master
161 Posts |
Posted - 2009-08-06 : 01:36:00
|
quote: Originally posted by Buda56 Hi, thanks for that, the end result gives me the N, N-1 summary that I required but it also gives me duplicates in N and N-1. I need to have no duplicates in N or any subsequent N-x.My next question I guess is how to apply the "Select" to an existing table of data??[Code]declare @Mytable table(col1 varchar(10),col2 varchar(20),dt datetime)Insert into @Mytable(Col1,col2,dt)select account, Servername, convert(datetime,[PDate], 104) FROM Master_SWD_PatchAll UNION ALLdeclare @MYDATE datetimeset @MyDate = convert(datetime, GETDATE(),104)select 'N' + case when datediff(Month,@MyDate,dt) = 0 then '' else convert(varchar(3),datediff(Month,@MyDate,dt)) end as SUMFIELD,count(*) as mycountfrom @Mytablegroup by dtorder by SumField[/Code]Unfortunately I get the following error "Msg 156, Level 15, State 1, Line 7Incorrect syntax near the keyword 'declare'"Regards..Peter
|
 |
|
|
Buda56
Starting Member
14 Posts |
Posted - 2009-08-06 : 06:56:00
|
| Matty, Unfortunately by removing the "Union All" I end up with multiple N's, N-1's, N-2's etc..<Results>NULL 585N* 1N-1 97N-1 244N-1 366N-1 2N-10 53N-10 83N-10 34N-10 8N-10 31N-10 27N-10 91N-10 36N-10 54N-10 28N-10 50N-10 65N-10 103N-10 68N-10 116N-10 35N-11 35N-11 69N-11 53N-11 70N-11 32N-11 2N-11 24N-11 83N-11 30N-11 49N-11 26N-11 50N-11 70N-11 47N-11 62N-11 108N-11 44N-11 4N-11 16N-11 41N-11 64N-12 50N-12 53N-12 1N-12 1N-12 8N-12 36N-12 1N-12 24N-12 85N-12 36N-12 44N-12 24N-12 42N-12 60N-12 46N-12 53N-12 111N-12 27N-12 4N-12 22N-13 85N-13 14N-13 30N-13 42N-13 28N-13 61N-13 68N-13 51N-13 114N-13 51N-13 30N-13 61N-13 48N-13 45N-13 66N-14 23N-14 48N-14 60N-14 55N-14 68N-14 34N-14 1N-14 63N-14 1N-14 11N-14 4N-14 6N-14 14N-14 2N-14 12N-14 7N-15 59N-15 124N-15 41N-15 17N-2 87N-2 106N-2 73N-2 112N-2 40N-2 176N-2 154N-2 98N-2 129N-2 124N-2 70N-2 75N-2 129N-2 67N-2 91N-2 157N-2 60N-2 4N-2 24N-2 87N-2 108N-3 71N-3 5N-3 24N-3 83N-3 109N-3 80N-3 116N-3 40N-3 215N-3 138N-3 78N-3 98N-3 64N-3 62N-3 130N-3 62N-3 90N-3 153N-3 59N-3 4N-3 24N-4 71N-4 107N-4 40N-4 1N-4 19N-4 41N-4 3N-4 2N-4 4N-4 9N-4 3N-4 55N-4 4N-4 185N-4 138N-4 109N-4 126N-4 66N-4 62N-4 121N-4 64N-4 91N-4 151N-5 16N-5 81N-5 1N-5 184N-5 117N-5 35N-5 101N-5 41N-5 2N-5 93N-5 74N-5 83N-5 150N-5 61N-5 60N-5 24N-5 82N-5 96N-6 22N-6 88N-6 96N-6 55N-6 102N-6 39N-6 69N-6 11N-6 6N-6 11N-6 6N-6 69N-7 288N-7 211N-7 1N-7 3N-7 215N-7 90N-7 40N-7 62N-7 43N-7 2N-7 68N-7 74N-7 156N-7 61N-7 57N-8 38N-8 80N-8 56N-8 107N-8 40N-8 196N-8 12N-8 21N-8 28N-8 2N-8 117N-8 41N-8 29N-9 4N-9 21N-9 54N-9 72N-9 54N-9 88N-9 34N-9 1N-9 2N-9 13N-9 87N-9 32N-9 51N-9 28N-9 57N-9 59N-9 44N-9 60N-9 121N-9 36N-9 3N-9 24</Results>Regards..Peter |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2009-08-06 : 13:58:18
|
Since I can't actually see the data you are working with, my assumption is the duplicates are going to be due to the time of the record (i.e 1/1/2009 is differant than 1/1/2009 11:30 AM) if that is the case then just change the grouping toselect 'N' + case when datediff(Month,@MyDate,dt) = 0 then '' else convert(varchar(3),datediff(Month,@MyDate,dt)) end as SUMFIELD,count(*) as mycountfrom @Mytablegroup by datediff(Month,@MyDate,dt)order by SumField Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
Buda56
Starting Member
14 Posts |
Posted - 2009-08-06 : 18:14:35
|
| Vinnie, Thanks that now gives me single N's, N-1's etc. I have one single question left with this and that is: "Is there anyway to return no duplicates in the N, N-1 etc i.e. a server in N should not appear in N-1 or any other variant on N-x and the same for any server that doesn't appear in N but appears in a varaint of N-x".I was doing some work last night on a query that will show me the servername and the last patch month for each year which I will use to show a drill down.<code>SELECT vswp.ServerName, MAX(vswp.[Month]) AS 'Month',vswp.[Year]FROM vwSWD_Win_PMonth vswpWHERE vswp.[Year] = YEAR(GETDATE()) OR vswp.[Year] = (YEAR(GETDATE())-1)GROUP BY vswp.ServerName,vswp.[Year]ORDER BY vswp.ServerName</code>Gives results likeADLDESA01 6 2009ADLDESA02 6 2009ADLDFEA01 12 2008ADLDFEA01 7 2009ADLDFEA02 11 2008ADLDFEA02 6 2009ADLDFEA03 12 2008ADLDFEA03 7 2009ADLDFEA04 11 2008ADLDFEA04 6 2009ADLDFEA09 12 2008ADLDFEA09 7 2009ADLDFEA10 7 2008ADLDFEA11 12 2008ADLDFEA11 7 2009ADLDFEA13 12 2008ADLDFEA13 7 2009ADLDFEA14 12 2008ADLDFEA14 7 2009ADLDFEA15 12 2008ADLDFEA15 7 2009ADLDFEA16 12 2008ADLDFEA16 7 2009ADLDFEA17 12 2008ADLDFEA17 7 2009ADLDFEA18 11 2008ADLDFEA18 7 2009ADLDFEA19 10 2008ADLDFEA19 7 2009ADLDFEA20 12 2008ADLDFEA20 7 2009ADLDFEA21 12 2008ADLDFEA21 7 2009ADLDFEB01 12 2008ADLDFEB01 7 2009Regards..Peter |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2009-08-06 : 23:40:47
|
I'm not following you. It will be a lot easier to answer your question if you post what you want your results to be using the above data. Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
Buda56
Starting Member
14 Posts |
Posted - 2009-08-07 : 00:13:07
|
| Vinnie, Sorry for the confusion, what you provided is excellent and is what I require. The only extra thing I would like is there anyway to return no duplicates in the N, N-1 etc i.e. a server in N should not appear in N-1 or any other variant on N-x and the same for any server that doesn't appear in N but appears in a varaint of N-x.My last post was only showing the work I am doing on getting a server list to match your summary view.Regards..Peter. |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2009-08-07 : 14:31:59
|
Without you showing me sample data and exaclty what you want your results to be, all I can do is assume. But is this what you are looking for?Basically for each Server if N exists it will show it and not show any prior n-x, and if doesn't exist then it will show all prior n-x.declare @Mytable table(col1 varchar(3),col2 varchar(10),dt datetime)Insert into @Mytable(Col1,col2,dt)select 'BBP', 'FLNAPF007', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNAPF007', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNAPF007', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNAPF007', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNAPF007', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNAPF007', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNAPF007', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNAPF007', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNAPM004', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNLCF006', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNLCM006', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNLKA015', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNLKA016', convert(datetime,'16/02/2009',104) union Allselect 'BBP', 'FLNLKA017', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNLKA017', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNLKA017', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNLKA017', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNLKA017', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNLKA018', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNLKA019', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKA005', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKA006', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKA006', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKA006', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKA006', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKA006', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKA006', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKA008', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKA014', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNPKF002', convert(datetime,'16/04/2009',104) union Allselect 'BBP', 'FLNPKM003', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKM003', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKM003', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKM003', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKM003', convert(datetime,'16/03/2009',104) union Allselect 'BBP', 'FLNPKM023', convert(datetime,'16/03/2009',104)declare @MYDATE datetimeset @MyDate = convert(datetime,'16/04/2009',104)select Col2,'N' + case when datediff(Month,@MyDate,dt) = 0 then '' else convert(varchar(3),datediff(Month,@MyDate,dt)) end as SUMFIELD,count(*) as mycountfrom @Mytable awhere datediff(Month,@MyDate,dt) = 0group by Col2,datediff(Month,@MyDate,dt)Union allselect Col2,'N' + case when datediff(Month,@MyDate,dt) = 0 then '' else convert(varchar(3),datediff(Month,@MyDate,dt)) end as SUMFIELD,count(*) as mycountfrom @Mytable awhere not exists (Select * from @Mytable aa where aa.Col2 = a.Col2 and datediff(Month,@MyDate,aa.dt) = 0)group by Col2,datediff(Month,@MyDate,dt) Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
Buda56
Starting Member
14 Posts |
Posted - 2009-08-12 : 08:03:31
|
| Vinnie, Thanks that was exactly what I need, thank you for your time and patience.Regards..Peter |
 |
|
|
|
|
|
|
|