Left join

  • Hi All,

    I have below tables.

    I am using below query

    SELECT * FROM @MONTHS M

    LEFT JOIN DBO.PERIOD P ON M.PERIOD=P.PERIOD

    its display all 36 months data whenever data is not match its display as null. i want data in following manner for eg. i have data for FS item only for sep 13 but i want that data for sep 12 as well with value column data as null other data remain as it is.

    itemtypecodeitemmonthnbr yearnbrperiod valueitemtypename

    111 FS9 20122012-09-01nullCOS

    CREATE TABLE [dbo].[period](

    [itemtypecode] [varchar](50) NULL,

    [item] [varchar](50) NULL,

    [monthnbr] [int] NULL,

    [yearnbr] [int] NULL,

    [period] [date] NULL,

    [value] [int] NULL,

    [itemtypename] [varchar](50) NULL

    ) ON [PRIMARY]

    SET NOCOUNT ON

    @DocumentDt date

    set @DocumentDt = '1-Jan-2014'

    DECLARE @Months table (

    Period DATE

    )

    -- Date Calculation

    DECLARE @CutOffDatedate

    DECLARE @StartDtdate

    DECLARE @EndDtdate

    DECLARE @CurrentDate datetime

    SET @CurrentDate = @DocumentDt

    SET @CutOffDate = DATEADD(MONTH,((YEAR(@DocumentDt)-1900)*12)+ 8, 0)

    IF @CurrentDate >= @CutOffDate

    BEGIN

    SET @StartDt = DATEADD(YEAR, -1, @CutOffDate)

    SET @EndDt = DATEADD(DAY, -1, DATEADD(Year, 2, @CutOffDate))

    END

    ELSE

    BEGIN

    SET @StartDt = DATEADD(YEAR, -2, @CutOffDate)

    SET @EndDt = DATEADD(DAY, -1, DATEADD(YEAR, 1, @CutOffDate))

    END

    DECLARE @MonthDiff INT;

    DECLARE @counter INT;

    SET @counter = 0;

    SELECT @MonthDiff = DATEDIFF(mm, @StartDt, @EndDt);

    WHILE @counter <= @MonthDiff

    BEGIN

    INSERT @Months

    SELECT (DATEADD(mm, @counter, @StartDt));

    SET @counter = @counter + 1;

    END

    SELECT * FROM @MONTHS M

    LEFT JOIN DBO.PERIOD P ON M.PERIOD=P.PERIOD

  • You likely have not gotten any responses to your question because you didn't post enough information. In order to help we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    Also, you should look at using a tally table instead of that much slower while loop. http://www.sqlservercentral.com/articles/62867/[/url]

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 2 posts - 1 through 1 (of 1 total)

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