+ Reply to Thread
Results 1 to 2 of 2

Left function

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-11-2011
    Location
    London, England
    MS-Off Ver
    EXCEL 2013
    Posts
    324

    Left function

    HI guys

    i need some help linking two tables together to get an end result

    Please find below the code i have used

    The first part of the query provides me with the info i need


    PHP Code: 
    SELECT sub.*, 
    case 
    when rm_sales_band '2M to 4M' then 'Kirsty' else RM end as rm
    into 
    #rmtmp
    FROM 
    (SELECT[fdmsaccountno], 
    [
    ho], 
    [
    rm_sales_band], 
    [
    rm_code], 
    [
    post_code], 
    CASE 
    WHEN Isnumeric(RIGHT(LEFT(post_code2), 1)) = '0' THEN 
    LEFT
    (post_code2
    ELSE 
    LEFT(post_code1
    END AS 'sPostcode'
    [
    mcc_code
    FROM [FDMS].[dbo].[dim_outlet
    WHERE [rm_sales_band]IN '2M to 4m''4m +' 
    AND [
    ho] = 'Y' 
    AND rm_code 'na' 
    AND iso_account 'N' 
    AND fdmsaccountno NOT IN (SELECT [ta_mid
    FROM 
    fdms_partnerreporting
    .tmp.trade_assocations)) Sub 
    INNER JOIN 
    [geo_pca_sellers
    ON [pca] = spostcode 
    select 
    from #rmtmp 

    i have created a second query

    Which looks at all all mccs which “r” seller code have dealt with and gives highest seller code which deals with that particular mcc

    and the query is

    PHP Code: 
    with
    CTE_Group 
    as
    (
    select seller_codemcc_codecount(mcc_code) as mcc_count
    from 
    [FDMS].[dbo].[Dim_Outlet]
    where Rm_Sales_Band '2M to 4m' or Rm_Sales_Band '4m +' ) and
    (
    Seller_Code like 'r%'group by seller_codemcc_code
    ),



    CTE_RowNum as
    (
    select 
    c
    .*, 
    ROW_NUMBER() OVER(PARTITION BY mcc_code 
    ORDER BY mcc_count desc
    ) as RowNum
    from CTE_Group c


    select from CTE_RowNum
    where RowNum 
    1
    order by MCC_Code desc
    ,mcc_count desc 
    What i need from here is the seller code linked to that particular mcc code, brought over to the original query. If the following seller codes are not assigned to a particular mcc can you populate N.A
    R05
    R10
    R12
    R13

    Please find attached three images
    query1-
    query2
    ideal results

    From the ideal results, image i only need the seller code brought over
    Attached Images Attached Images
    Last edited by masond3; 08-03-2012 at 11:04 AM. Reason: Added code tags

  2. #2
    Forum Contributor
    Join Date
    11-11-2011
    Location
    London, England
    MS-Off Ver
    EXCEL 2013
    Posts
    324

    Re: Left function

    with
    PHP Code: 
    CTE_Group as
    (
    select seller_codemcc_codecount(mcc_code) as mcc_count
    from 
    [FDMS].[dbo].[Dim_Outlet]
    where Rm_Sales_Band '2M to 4m' or Rm_Sales_Band '4m +' ) and
    (
    Seller_Code like 'r%'group by seller_codemcc_code
    ),
    CTE_RowNum as
    (
    select 
    c
    .*, 
    ROW_NUMBER() OVER(PARTITION BY mcc_code 
    ORDER BY mcc_count desc
    ) as RowNum
    from CTE_Group c

    select from  #rmtmp q
    left join CTE_RowNum c
    on q
    .mcc_code c.mcc_code
    and c.RowNum 1
    order by q
    .MCC_Code desc,c.mcc_count desc 
    Last edited by Cutter; 08-03-2012 at 03:52 PM. Reason: Added code tags

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1