Need Help

  • Hi Team,

    I need your help to find a solution on below query.

    Table Structure

    CREATE TABLE [dbo].[Sampletable1](
    [ServerName] [nvarchar](50) NULL,
    [ID] [nvarchar](50) NOT NULL,
    [Model] [nvarchar](50) NOT NULL,
    [Host] [nvarchar](50) NOT NULL,
    [BillDate] [date] NOT NULL
    )
    GO

    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SRMPV07PC01E02.NDC.COM', 'FCH19107Q89', 'SAN-B420-M3', 'SRMPV07PC01', '05-SEP-2017')
    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SRMPV07PC01E02.NDC.COM', 'FCH19107Q89', 'SAN-B420-M3', 'SRMPV07PC01', '19-SEP-2017')
    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SRMPV07PC01E01.NDC.COM', 'FCH19107M57', 'SAN-B420-M3', 'SRMPV07PC01', '12-SEP-2017')
    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SRMPV07PC01E01.NDC.COM', 'FCH19107M57', 'SAN-B420-M3', 'SRMPV07PC01', '19-SEP-2017')
    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SOMPV03HC01E02.NDC.COM', 'FCH1806706Q', 'SAN-B420-M3', 'SOMPV03HC01', '05-SEP-2017')
    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SOMPV03HC01E02.NDC.COM', 'FCH1806706Q', 'SAN-B420-M3', 'SOMPV03HC01', '12-SEP-2017')
    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SOMPV03HC01E02.NDC.COM', 'FCH1806706Q', 'SAN-B420-M3', 'SOMPV03HC01', '19-SEP-2017')

    Here my data's were loaded in 3 different dates

    select DISTINCT [BillDate] from [dbo].[Sampletable1]

    BillDate
    2017-09-05
    2017-09-12
    2017-09-19

    Now, I  need to find daily values for each server.  The server SRMPV07PC01E02.NDC.COM is loaded on Sep 5 then we can start the daily values as 1 from Sep 5 to next bill cycle Sep 12.(Sep 1 to Sep 4  value is 0 ). if server missed any billing cycle then put 0 on that period

    I Need the o/p like below

    ServerNameID10/1/201710/2/201710/3/201710/4/201710/5/201710/6/201710/7/201710/8/201710/9/201710/10/201710/11/201710/12/201710/13/201710/14/201710/15/201710/16/201710/17/201710/18/201710/19/201710/20/201710/21/201710/22/201710/23/201710/24/201710/25/201710/26/201710/27/201710/28/201710/29/201710/30/201710/31/2017
    SRMPV07PC01E02.NDC.COMFCH19107Q890000111111100000001111111111111
    SRMPV07PC01E01.NDC.COMFCH19107M570000000000011111111111111111111
    SOMPV03HC01E02.NDC.COMFCH1806706Q0000111111111111111111111111111

    Plese help me to solve this issue 

    Thanks
    Torrid.

  • torrid.inundate - Sunday, October 15, 2017 12:17 PM

    Hi Team,

    I need your help to find a solution on below query.

    Table Structure

    CREATE TABLE [dbo].[Sampletable1](
    [ServerName] [nvarchar](50) NULL,
    [ID] [nvarchar](50) NOT NULL,
    [Model] [nvarchar](50) NOT NULL,
    [Host] [nvarchar](50) NOT NULL,
    [BillDate] [date] NOT NULL
    )
    GO

    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SRMPV07PC01E02.NDC.COM', 'FCH19107Q89', 'SAN-B420-M3', 'SRMPV07PC01', '05-SEP-2017')
    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SRMPV07PC01E02.NDC.COM', 'FCH19107Q89', 'SAN-B420-M3', 'SRMPV07PC01', '19-SEP-2017')
    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SRMPV07PC01E01.NDC.COM', 'FCH19107M57', 'SAN-B420-M3', 'SRMPV07PC01', '12-SEP-2017')
    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SRMPV07PC01E01.NDC.COM', 'FCH19107M57', 'SAN-B420-M3', 'SRMPV07PC01', '19-SEP-2017')
    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SOMPV03HC01E02.NDC.COM', 'FCH1806706Q', 'SAN-B420-M3', 'SOMPV03HC01', '05-SEP-2017')
    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SOMPV03HC01E02.NDC.COM', 'FCH1806706Q', 'SAN-B420-M3', 'SOMPV03HC01', '12-SEP-2017')
    INSERT [dbo].[Sampletable1] ([ServerName1], [ID], [Model], [Host], [BillDate]) VALUES (N'SOMPV03HC01E02.NDC.COM', 'FCH1806706Q', 'SAN-B420-M3', 'SOMPV03HC01', '19-SEP-2017')

    Here my data's were loaded in 3 different dates

    select DISTINCT [BillDate] from [dbo].[Sampletable1]

    BillDate
    2017-09-05
    2017-09-12
    2017-09-19

    Now, I  need to find daily values for each server.  The server SRMPV07PC01E02.NDC.COM is loaded on Sep 5 then we can start the daily values as 1 from Sep 5 to next bill cycle Sep 12.(Sep 1 to Sep 4  value is 0 ). if server missed any billing cycle then put 0 on that period

    I Need the o/p like below

    ServerNameID10/1/201710/2/201710/3/201710/4/201710/5/201710/6/201710/7/201710/8/201710/9/201710/10/201710/11/201710/12/201710/13/201710/14/201710/15/201710/16/201710/17/201710/18/201710/19/201710/20/201710/21/201710/22/201710/23/201710/24/201710/25/201710/26/201710/27/201710/28/201710/29/201710/30/201710/31/2017
    SRMPV07PC01E02.NDC.COMFCH19107Q890000111111100000001111111111111
    SRMPV07PC01E01.NDC.COMFCH19107M570000000000011111111111111111111
    SOMPV03HC01E02.NDC.COMFCH1806706Q0000111111111111111111111111111

    Plese help me to solve this issue 

    Thanks
    Torrid.

    Please Note my Server billing circle Starts on Tuesday

  • Hi Torrid,

    Sorry, I think I'm probably missing something, as I'm not too sure what you're saying with regard to the billing cycles, but can't you just aggregate a bunch of case statements using DATEPART?

    It's not the most elegant of solutions, but I've done the 1st 10 below?

    Apologies if I've misunderstood.


    DECLARE @DateStart DATETIME
    DECLARE @DateEnd DATETIME

    SELECT @DateStart = '2017-09-01'
         ,@DateEnd = '2017-10-01'

    SELECT     ServerName
            ,SUM(CASE WHEN DATEPART(DAY, BillDate) = 1 THEN 1 ELSE 0 END) AS Day1
            ,SUM(CASE WHEN DATEPART(DAY, BillDate) = 2 THEN 1 ELSE 0 END) AS Day2
            ,SUM(CASE WHEN DATEPART(DAY, BillDate) = 3 THEN 1 ELSE 0 END) AS Day3
            ,SUM(CASE WHEN DATEPART(DAY, BillDate) = 4 THEN 1 ELSE 0 END) AS Day4
            ,SUM(CASE WHEN DATEPART(DAY, BillDate) = 5 THEN 1 ELSE 0 END) AS Day5
            ,SUM(CASE WHEN DATEPART(DAY, BillDate) = 6 THEN 1 ELSE 0 END) AS Day6
            ,SUM(CASE WHEN DATEPART(DAY, BillDate) = 7 THEN 1 ELSE 0 END) AS Day7
            ,SUM(CASE WHEN DATEPART(DAY, BillDate) = 8 THEN 1 ELSE 0 END) AS Day8
            ,SUM(CASE WHEN DATEPART(DAY, BillDate) = 9 THEN 1 ELSE 0 END) AS Day9
            ,SUM(CASE WHEN DATEPART(DAY, BillDate) = 10 THEN 1 ELSE 0 END) AS Day10
    FROM     [dbo].[Sampletable1]
    WHERE     BillDate >= @DateStart
    AND     BillDate < @DateEnd
    GROUP BY ServerName

  • Hi Farlzy,

         Thank you for your Reply. But the query o/p has slightly differed from my requirement. If the server is present on the weekly report ( It is nothing but BillDate) then I need mark that server is available for that entire week, not just on that day.

    below is my exact Prerequisite

    ServerNameID9/1/20179/2/20179/3/20179/4/20179/5/20179/6/20179/7/20179/8/20179/9/20179/10/20179/11/20179/12/20179/13/20179/14/20179/15/20179/16/20179/17/20179/18/20179/19/20179/20/20179/21/20179/22/20179/23/20179/24/20179/25/20179/26/20179/27/20179/28/20179/29/20179/30/2017
    SRMPV07PC01E02.NDC.COMFCH19107Q89000000000001111111111111100000
    SRMPV07PC01E01.NDC.COMFCH19107M57000000000001111111111111100000
    SOMPV03HC01E02.NDC.COMFCH1806706Q000011111111111111111111100000

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply