Looking for a smarter script to do this query

  • I am stuck on a very repetitive query now, I wonder if anyone can help me on figuring out a smarter script.

    I have a table:

    CREATE TABLE [dbo].[Staging](

    [ProjectWork Number] [nvarchar](255) NULL,

    [ProjectWork Name] [nvarchar](255) NULL,

    [Quarter] [nvarchar](7) NULL,

    [WeekNo] [nvarchar](12) NULL,

    [WeekValue] [float] NULL

    ) ON [PRIMARY]

    Data is like:

    ProjectWork NumberProjectWork NameQuarterWeekNoWeekValue

    3185-693185-69 - ABC CommercialQ1 2016Nov 01, 20150

    3185-693185-69 - ABC CommercialQ1 2016Nov 01, 201537.5

    3185-693185-69 - ABC CommercialQ1 2016Nov 01, 201525

    3185-693185-69 - ABC CommercialQ1 2016Nov 01, 201512

    3185-693185-69 - ABC CommercialQ1 2016Nov 01, 201525.5

    Another table:

    CREATE TABLE [dbo].[WeekTable](

    [ID] [int] IDENTITY(1,1) NOT NULL,

    [QuarterNo] [varchar](7) NOT NULL,

    [WeekNo] [varchar](12) NOT NULL,

    [MonthNo] [varchar](3) NULL

    ) ON [PRIMARY]

    There are 55 rows in the weektable with data like:

    IDQuarterNoWeekNoMonthNo

    1Q1 2016Nov 01, 2015Nov

    2Q1 2016Nov 08, 2015Nov

    3Q1 2016Nov 15, 2015Nov

    4Q1 2016Nov 22, 2015Nov

    5Q1 2016Nov 29, 2015Nov

    54Q4 2016Oct 23, 2016Oct

    55Q4 2016Oct 30, 2016Oct

    What I need to do is to get a result like this:

    ProjectWork NumberProjectWork Name[Nov 01, 2015] [Nov 08, 2015] [Nov 15, 2015]

    3185-693185-69 - ABC Commercial0 0 0

    3185-693185-69 - ABC Commercial37.5 37.5 37.5

    3185-693185-69 - ABC Commercial25 25 25

    3185-693185-69 - ABC Commercial12 12 12

    3185-693185-69 - ABC Commercial25.5 25.5 25.5

    My current query is:

    SELECT [ProjectWork Number], [ProjectWork Name], [Quarter], WeekNo

    ,SUM(CASE WHEN WeekNo='Jul 10, 2016' THEN weekvalue ELSE 0 END) As 'Jul 10, 2016'

    FROM staging

    GROUP BY [ProjectWork Number], [ProjectWork Name], [Quarter], Weekno

    The query is for only one week, as you can see there are 55 weekno there, I need a smarter query to combine the second table weektable in the query so it won't be like a stack of

    ,SUM(CASE WHEN WeekNo='Jul 10, 2016' THEN weekvalue ELSE 0 END) As 'Jul 10, 2016'

    Can anyone help me on this?

    Thank you very much.

  • You can do this using Dynamic SQL with a Crosstab. If you'd be willing to provide consumable data I could give you an example on how to do that.


    SELECT quote FROM brain WHERE original = 1
    0 rows returned

  • Y.B. (11/24/2016)


    You can do this using Dynamic SQL with a Crosstab. If you'd be willing to provide consumable data I could give you an example on how to do that.

    Thank you very much, could you test your query with the attached a small portion of the data?

  • While I do appreciate the effort that is not what I mean. You should post code that will allow me to query the data directly.

    If you need an example please read this article: http://www.sqlservercentral.com/articles/Best+Practices/61537/


    SELECT quote FROM brain WHERE original = 1
    0 rows returned

  • halifaxdal (11/24/2016)


    Y.B. (11/24/2016)


    You can do this using Dynamic SQL with a Crosstab. If you'd be willing to provide consumable data I could give you an example on how to do that.

    Thank you very much, could you test your query with the attached a small portion of the data?

    no doubt about it...if you post easy cut/paste scripts on this forum, you will get a(several) tried and tested answer(s)......

    please read this as https://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ and post back accordingly....not many volunteers have the time to convert an image into ready data to help you help us.

    no offence ....good luck

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • Thanks.

    I hope this will do now:

    CREATE TABLE [dbo].[Staging](

    [ProjectWork Number] [nvarchar](255) NULL,

    [WeekNo] [nvarchar](12) NULL,

    [WeekValue] [float] NULL

    ) ON [PRIMARY]

    GO

    INSERT INTO [dbo].[Staging] VALUES('12345', 'Dec 06, 2015', 12.3)

    INSERT INTO [dbo].[Staging] VALUES('12345', 'Dec 13, 2015', 12.3)

    INSERT INTO [dbo].[Staging] VALUES('12345', 'Dec 20, 2015', 12.3)

    INSERT INTO [dbo].[Staging] VALUES('12345', 'Dec 27, 2015', 12.3)

    INSERT INTO [dbo].[Staging] VALUES('12345', 'Jan 03, 2016', 12.3)

    INSERT INTO [dbo].[Staging] VALUES('RXAXVX', 'Jan 10, 2016', 45.6)

    INSERT INTO [dbo].[Staging] VALUES('3210', 'Jan 17, 2016', 45.6)

    INSERT INTO [dbo].[Staging] VALUES('3210', 'Jan 24, 2016', 45.6)

    INSERT INTO [dbo].[Staging] VALUES('3210', 'Jan 31, 2016', 45.6)

    INSERT INTO [dbo].[Staging] VALUES('3210', 'Nov 01, 2015', 45.6)

    INSERT INTO [dbo].[Staging] VALUES('3210', 'Nov 08, 2015', 45.6)

    INSERT INTO [dbo].[Staging] VALUES('3210', 'Nov 15, 2015', 78.9)

    INSERT INTO [dbo].[Staging] VALUES('3210', 'Nov 22, 2015', 78.9)

    INSERT INTO [dbo].[Staging] VALUES('34567', 'Nov 29, 2015', 78.9)

    INSERT INTO [dbo].[Staging] VALUES('34567', 'Apr 03, 2016', 78.9)

    INSERT INTO [dbo].[Staging] VALUES('34567', 'Apr 10, 2016', 78.9)

    INSERT INTO [dbo].[Staging] VALUES('34567', 'Apr 17, 2016', 78.9)

    INSERT INTO [dbo].[Staging] VALUES('34567', 'Apr 24, 2016', 0.12)

    INSERT INTO [dbo].[Staging] VALUES('34567', 'Feb 07, 2016', 0.12)

    INSERT INTO [dbo].[Staging] VALUES('34567', 'Feb 14, 2016', 0.12)

    INSERT INTO [dbo].[Staging] VALUES('ABCED', 'Feb 21, 2016', 0.12)

    INSERT INTO [dbo].[Staging] VALUES('ABCED', 'Feb 28, 2016', 0.12)

    INSERT INTO [dbo].[Staging] VALUES('ABCED', 'Jan 31, 2016', 0.12)

    INSERT INTO [dbo].[Staging] VALUES('ABCED', 'Mar 06, 2016', 3.8)

    INSERT INTO [dbo].[Staging] VALUES('ABCED', 'Mar 13, 2016', 3.8)

    INSERT INTO [dbo].[Staging] VALUES('ABCED', 'Mar 20, 2016', 3.8)

    INSERT INTO [dbo].[Staging] VALUES('RXAXVX', 'Mar 27, 2016', 3.8)

    INSERT INTO [dbo].[Staging] VALUES('RXAXVX', 'Jul 03, 2016', 0.135)

    INSERT INTO [dbo].[Staging] VALUES('RXAXVX', 'Jul 10, 2016', 0.135)

    INSERT INTO [dbo].[Staging] VALUES('RXAXVX', 'Jul 17, 2016', 0.135)

    INSERT INTO [dbo].[Staging] VALUES('RXAXVX', 'Jul 24, 2016', 0.135)

    INSERT INTO [dbo].[Staging] VALUES('RXAXVX', 'Jul 31, 2016', 0.135)

    INSERT INTO [dbo].[Staging] VALUES('RXAXVX', 'Jun 05, 2016', 333)

    INSERT INTO [dbo].[Staging] VALUES('EEEEE', 'Jun 12, 2016', 333)

    INSERT INTO [dbo].[Staging] VALUES('EEEEE', 'Jun 19, 2016', 333)

    INSERT INTO [dbo].[Staging] VALUES('EEEEE', 'Jun 26, 2016', 333)

    INSERT INTO [dbo].[Staging] VALUES('EEEEE', 'May 01, 2016', 333)

    INSERT INTO [dbo].[Staging] VALUES('EEEEE', 'May 08, 2016', 12.3)

    INSERT INTO [dbo].[Staging] VALUES('EEEEE', 'May 15, 2016', 12.3)

    INSERT INTO [dbo].[Staging] VALUES('BBB', 'May 22, 2016', 20.9)

    INSERT INTO [dbo].[Staging] VALUES('BBB', 'May 29, 2016', 20.9)

    INSERT INTO [dbo].[Staging] VALUES('BBB', 'Aug 07, 2016', 20.9)

    INSERT INTO [dbo].[Staging] VALUES('BBB', 'Aug 14, 2016', 20.9)

    INSERT INTO [dbo].[Staging] VALUES('BBB', 'Aug 21, 2016', 20.9)

    INSERT INTO [dbo].[Staging] VALUES('BBB', 'Aug 28, 2016', 20.9)

    INSERT INTO [dbo].[Staging] VALUES('BBB', 'Jul 31, 2016', 20.9)

    INSERT INTO [dbo].[Staging] VALUES('CACACA', 'Oct 02, 2016', 12.3)

    INSERT INTO [dbo].[Staging] VALUES('CACACA', 'Oct 09, 2016', 12.3)

    INSERT INTO [dbo].[Staging] VALUES('CACACA', 'Oct 16, 2016', 12.3)

    INSERT INTO [dbo].[Staging] VALUES('CACACA', 'Oct 23, 2016', 12.3)

    INSERT INTO [dbo].[Staging] VALUES('CACACA', 'Oct 30, 2016', 12.3)

    and second table:

    CREATE TABLE [dbo].[WeekTable](

    [ID] [int] IDENTITY(1,1) NOT NULL,

    [WeekNo] [varchar](12) NOT NULL

    ) ON [PRIMARY]

    GO

    INSERT INTO WeekTable Value ('')

    INSERT INTO WeekTable Value ('06-Dec-15')

    INSERT INTO WeekTable Value ('13-Dec-15')

    INSERT INTO WeekTable Value ('20-Dec-15')

    INSERT INTO WeekTable Value ('27-Dec-15')

    INSERT INTO WeekTable Value ('03-Jan-16')

    INSERT INTO WeekTable Value ('10-Jan-16')

    INSERT INTO WeekTable Value ('17-Jan-16')

    INSERT INTO WeekTable Value ('24-Jan-16')

    INSERT INTO WeekTable Value ('31-Jan-16')

    INSERT INTO WeekTable Value ('01-Nov-15')

    INSERT INTO WeekTable Value ('08-Nov-15')

    INSERT INTO WeekTable Value ('15-Nov-15')

    INSERT INTO WeekTable Value ('22-Nov-15')

    INSERT INTO WeekTable Value ('29-Nov-15')

    INSERT INTO WeekTable Value ('03-Apr-16')

    INSERT INTO WeekTable Value ('10-Apr-16')

    INSERT INTO WeekTable Value ('17-Apr-16')

    INSERT INTO WeekTable Value ('24-Apr-16')

    INSERT INTO WeekTable Value ('07-Feb-16')

    INSERT INTO WeekTable Value ('14-Feb-16')

    INSERT INTO WeekTable Value ('21-Feb-16')

    INSERT INTO WeekTable Value ('28-Feb-16')

    INSERT INTO WeekTable Value ('31-Jan-16')

    INSERT INTO WeekTable Value ('06-Mar-16')

    INSERT INTO WeekTable Value ('13-Mar-16')

    INSERT INTO WeekTable Value ('20-Mar-16')

    INSERT INTO WeekTable Value ('27-Mar-16')

    INSERT INTO WeekTable Value ('03-Jul-16')

    INSERT INTO WeekTable Value ('10-Jul-16')

    INSERT INTO WeekTable Value ('17-Jul-16')

    INSERT INTO WeekTable Value ('24-Jul-16')

    INSERT INTO WeekTable Value ('31-Jul-16')

    INSERT INTO WeekTable Value ('05-Jun-16')

    INSERT INTO WeekTable Value ('12-Jun-16')

    INSERT INTO WeekTable Value ('19-Jun-16')

    INSERT INTO WeekTable Value ('26-Jun-16')

    INSERT INTO WeekTable Value ('01-May-16')

    INSERT INTO WeekTable Value ('08-May-16')

    INSERT INTO WeekTable Value ('15-May-16')

    INSERT INTO WeekTable Value ('22-May-16')

    INSERT INTO WeekTable Value ('29-May-16')

    INSERT INTO WeekTable Value ('07-Aug-16')

    INSERT INTO WeekTable Value ('14-Aug-16')

    INSERT INTO WeekTable Value ('21-Aug-16')

    INSERT INTO WeekTable Value ('28-Aug-16')

    INSERT INTO WeekTable Value ('31-Jul-16')

    INSERT INTO WeekTable Value ('02-Oct-16')

    INSERT INTO WeekTable Value ('09-Oct-16')

    INSERT INTO WeekTable Value ('16-Oct-16')

    INSERT INTO WeekTable Value ('23-Oct-16')

    INSERT INTO WeekTable Value ('30-Oct-16')

  • I've created a dynamic sql for this, here is the generated dynamic query @sql

    WITH PivotData AS

    (

    select [ProjectWork Number], WeekNO, WeekValue From dbo.staging

    )

    select [ProjectWork Number], WeekValue, Apr-03-2016, Apr-10-2016, Apr-17-2016, Apr-24-2016, Aug-07-2016, Aug-14-2016, Aug-21-2016, Aug-28-2016, Dec-06-2015, Dec-13-2015, Dec-20-2015, Dec-27-2015, Feb-07-2016, Feb-14-2016, Feb-21-2016, Feb-28-2016, Jan-03-2016, Jan-10-2016, Jan-17-2016, Jan-24-2016, Jan-31-2016, Jan-31-2016, Jul-03-2016, Jul-10-2016, Jul-17-2016, Jul-24-2016, Jul-31-2016, Jul-31-2016, Jun-05-2016, Jun-12-2016, Jun-19-2016, Jun-26-2016, Mar-06-2016, Mar-13-2016, Mar-20-2016, Mar-27-2016, May-01-2016, May-08-2016, May-15-2016, May-22-2016, May-29-2016, Nov-01-2015, Nov-08-2015, Nov-15-2015, Nov-22-2015, Nov-29-2015, Oct-02-2016, Oct-09-2016, Oct-16-2016, Oct-23-2016, Oct-30-2016, Sep-04-2016, Sep-11-2016, Sep-18-2016, Sep-25-2016

    From PivotData

    PIVOT

    (

    Sum(WeekValue) For WeekNO in (Apr-03-2016, Apr-10-2016, Apr-17-2016, Apr-24-2016, Aug-07-2016, Aug-14-2016, Aug-21-2016, Aug-28-2016, Dec-06-2015, Dec-13-2015, Dec-20-2015, Dec-27-2015, Feb-07-2016, Feb-14-2016, Feb-21-2016, Feb-28-2016, Jan-03-2016, Jan-10-2016, Jan-17-2016, Jan-24-2016, Jan-31-2016, Jan-31-2016, Jul-03-2016, Jul-10-2016, Jul-17-2016, Jul-24-2016, Jul-31-2016, Jul-31-2016, Jun-05-2016, Jun-12-2016, Jun-19-2016, Jun-26-2016, Mar-06-2016, Mar-13-2016, Mar-20-2016, Mar-27-2016, May-01-2016, May-08-2016, May-15-2016, May-22-2016, May-29-2016, Nov-01-2015, Nov-08-2015, Nov-15-2015, Nov-22-2015, Nov-29-2015, Oct-02-2016, Oct-09-2016, Oct-16-2016, Oct-23-2016, Oct-30-2016, Sep-04-2016, Sep-11-2016, Sep-18-2016, Sep-25-2016)

    ) As PivotResult

    It generates an error saying:

    Msg 102, Level 15, State 1, Line 11

    Incorrect syntax near '-'.

    I've already updated my weektable so the value is like Apr-03-2016

    What's wrong with the query?

    Thanks.

  • Thank you, that is much better. Notice though your second script has a syntax error.

    It should 'VALUES' NOT 'VALUE'

    Unfortunately, because of the delay I won't be able to look at this until tomorrow. Maybe, someone will be able to jump in until then.

    Cheers,

    CREATE TABLE [dbo].[WeekTable](

    [ID] [int] IDENTITY(1,1) NOT NULL,

    [WeekNo] [varchar](12) NOT NULL

    ) ON [PRIMARY]

    GO

    INSERT INTO WeekTable Values ('')

    INSERT INTO WeekTable Values ('06-Dec-15')

    INSERT INTO WeekTable Values ('13-Dec-15')

    INSERT INTO WeekTable Values ('20-Dec-15')

    INSERT INTO WeekTable Values ('27-Dec-15')

    INSERT INTO WeekTable Values ('03-Jan-16')

    INSERT INTO WeekTable Values ('10-Jan-16')

    INSERT INTO WeekTable Values ('17-Jan-16')

    INSERT INTO WeekTable Values ('24-Jan-16')

    INSERT INTO WeekTable Values ('31-Jan-16')

    INSERT INTO WeekTable Values ('01-Nov-15')

    INSERT INTO WeekTable Values ('08-Nov-15')

    INSERT INTO WeekTable Values ('15-Nov-15')

    INSERT INTO WeekTable Values ('22-Nov-15')

    INSERT INTO WeekTable Values ('29-Nov-15')

    INSERT INTO WeekTable Values ('03-Apr-16')

    INSERT INTO WeekTable Values ('10-Apr-16')

    INSERT INTO WeekTable Values ('17-Apr-16')

    INSERT INTO WeekTable Values ('24-Apr-16')

    INSERT INTO WeekTable Values ('07-Feb-16')

    INSERT INTO WeekTable Values ('14-Feb-16')

    INSERT INTO WeekTable Values ('21-Feb-16')

    INSERT INTO WeekTable Values ('28-Feb-16')

    INSERT INTO WeekTable Values ('31-Jan-16')

    INSERT INTO WeekTable Values ('06-Mar-16')

    INSERT INTO WeekTable Values ('13-Mar-16')

    INSERT INTO WeekTable Values ('20-Mar-16')

    INSERT INTO WeekTable Values ('27-Mar-16')

    INSERT INTO WeekTable Values ('03-Jul-16')

    INSERT INTO WeekTable Values ('10-Jul-16')

    INSERT INTO WeekTable Values ('17-Jul-16')

    INSERT INTO WeekTable Values ('24-Jul-16')

    INSERT INTO WeekTable Values ('31-Jul-16')

    INSERT INTO WeekTable Values ('05-Jun-16')

    INSERT INTO WeekTable Values ('12-Jun-16')

    INSERT INTO WeekTable Values ('19-Jun-16')

    INSERT INTO WeekTable Values ('26-Jun-16')

    INSERT INTO WeekTable Values ('01-May-16')

    INSERT INTO WeekTable Values ('08-May-16')

    INSERT INTO WeekTable Values ('15-May-16')

    INSERT INTO WeekTable Values ('22-May-16')

    INSERT INTO WeekTable Values ('29-May-16')

    INSERT INTO WeekTable Values ('07-Aug-16')

    INSERT INTO WeekTable Values ('14-Aug-16')

    INSERT INTO WeekTable Values ('21-Aug-16')

    INSERT INTO WeekTable Values ('28-Aug-16')

    INSERT INTO WeekTable Values ('31-Jul-16')

    INSERT INTO WeekTable Values ('02-Oct-16')

    INSERT INTO WeekTable Values ('09-Oct-16')

    INSERT INTO WeekTable Values ('16-Oct-16')

    INSERT INTO WeekTable Values ('23-Oct-16')

    INSERT INTO WeekTable Values ('30-Oct-16')


    SELECT quote FROM brain WHERE original = 1
    0 rows returned

  • as an effort to troubleshoot this query, I modified it to:

    WITH PivotData AS

    (

    select [ProjectWork Number], WeekNO, WeekValue From dbo.staging

    )

    select [ProjectWork Number], WeekValue, 'Apr-03-2016', 'Apr-10-2016'

    From PivotData

    PIVOT

    (

    Sum(WeekValue) For WeekNO in ('Apr-03-2016', 'Apr-10-2016')

    ) As PivotResult

    And it is giving me error:

    Msg 102, Level 15, State 1, Line 10

    Incorrect syntax near 'Apr-03-2016'.

    also tried

    WITH PivotData AS

    (

    select [ProjectWork Number], WeekNO, WeekValue From dbo.staging

    )

    select [ProjectWork Number], WeekValue, 'Apr-03-2016', 'Apr-10-2016'

    From PivotData

    PIVOT

    (

    Sum(WeekValue) For WeekNO in (Apr-03-2016, Apr-10-2016)

    ) As PivotResult

    same error

    What am I missing here? Thank you very much.

  • I also tried:

    WITH PivotData AS (

    select [ProjectWork Number], WeekNO, WeekValue From dbo.staging

    )

    select [ProjectWork Number], WeekValue, [Apr-03-2016], [Apr-10-2016]

    From PivotData

    PIVOT (

    Sum(WeekValue) For WeekNO in ([Apr-03-2016], [Apr-10-2016])

    ) As PivotResult;

    Error: Invalid column name 'WeekValue'

  • I fixed the grammer error in my script, but it returns no sum value for each week:crying:

    Here is my script:

    Declare @sql as varchar(Max)

    Declare @Columns as varchar(Max)

    Select

    @Columns =

    COALESCE(@Columns +'], [', '[') + WeekNo

    From

    (

    Select DISTINCT weekno from weektable

    ) AS WT

    ORDER BY WT.WeekNo

    SET @Columns = @Columns + ']'

    PRINT @Columns

    SET @sql = '

    WITH PivotData AS

    (

    select [ProjectWork Number], WeekNO, WeekValue From dbo.staging

    )

    select [ProjectWork Number], ' + @Columns + '

    From PivotData

    PIVOT

    (

    Sum(WeekValue) For WeekNO in (' + @Columns + ')

    ) As PivotResult'

    PRINT @sql

    Exec (@sql)

    What's wrong with the Sum(WeekValue)? why it didn't come up with any value?

  • I finally fixed everything and have result now!!!

    The reason it didn't return value is because weekno in two tables does not match.

  • You could pivot this stuff in a Matrix in SSRS and be done already. Is that not an option? Wouldn't be if you had to output this result for something else, maybe... just thought I'd ask, though.

  • Unless you were using the WeekTable to filter weeks then you technically don't need it. Either way since the values didn't line up I didn't end up using it.

    Here is an example of doing a crosstab with dynamic SQL. Notice how surprisingly little code is needed.

    DECLARE @sql VARCHAR(MAX)

    ;WITH cte AS

    (

    SELECT DISTINCT WeekNo FROM Staging WHERE WeekNo <> ''

    )

    SELECT @sql =

    'SELECT [ProjectWork Number] ' +

    (SELECT ' , MAX(CASE WHEN WeekNo = ' + '''' + CAST( WeekNo AS VARCHAR(12)) + '''' + ' THEN WeekValue END) AS ' + '''' + CAST( WeekNo AS VARCHAR(12)) + ''''

    FROM cte FOR XML PATH(''),TYPE).value('./text()[1]', 'nvarchar(MAX)') +

    ' FROM Staging' +

    ' GROUP BY [ProjectWork Number]'

    EXECUTE (@sql)


    SELECT quote FROM brain WHERE original = 1
    0 rows returned

  • Thank you very much for the revised script which is amazing, the result returns lots of NULL for WeekNo fields, how can the script be modified to get that?

    I also need to modify the WeekNo so it is generated in the actual order, here is my script but not working:

    SELECT DISTINCT WeekNo FROM Staging WHERE WeekNo <> ''

    ORDER BY CASE

    WHEN SUBSTRING(WeekNo, 1,4) = 'Jan' THEN 1

    WHEN SUBSTRING(WeekNo, 1,4) = 'Feb' THEN 2

    WHEN SUBSTRING(WeekNo, 1,4) = 'Mar' THEN 3

    WHEN SUBSTRING(WeekNo, 1,4) = 'Apr' THEN 4

    WHEN SUBSTRING(WeekNo, 1,4) = 'May' THEN 5

    WHEN SUBSTRING(WeekNo, 1,4) = 'Jun' THEN 6

    WHEN SUBSTRING(WeekNo, 1,4) = 'Jul' THEN 7

    WHEN SUBSTRING(WeekNo, 1,4) = 'Aug' THEN 8

    WHEN SUBSTRING(WeekNo, 1,4) = 'Sep' THEN 9

    WHEN SUBSTRING(WeekNo, 1,4) = 'Oct' THEN 10

    WHEN SUBSTRING(WeekNo, 1,4) = 'Nov' THEN 11

    WHEN SUBSTRING(WeekNo, 1,4) = 'Dec' THEN 12

    END

    Error:

    Msg 145, Level 15, State 1, Line 1

    ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

    Do you have a fix?

    Thanks.

Viewing 15 posts - 1 through 15 (of 20 total)

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