Finding Item and brand distribution base on customer master

Please help me to write query on my SQL 2008 database to how in one row the following data
1- Brand, item, Route(Salesman),Division (City), Total Available customer (Per Route),Total Available customer (Per Division), Total Customer purchased the Item (Per Route),Total Customer purchased the Item (Per Division),
I have two tables [CustomerMaster and SalesData] as shown below.
CustomerMaster
    | CODE |       NAME | ROUTE | DIVISION |
    |------|------------|-------|----------|
    | 1001 |  Customer1 |    R1 |     Div1 |
    | 1002 |  Customer2 |    R1 |     Div1 |
    | 1003 |  Customer3 |    R1 |     Div1 |
    | 1004 |  Customer4 |    R2 |     Div1 |
    | 1005 |  Customer5 |    R2 |     Div1 |
    | 1006 |  Customer6 |    R2 |     Div1 |
    | 1007 |  Customer7 |    R3 |     Div2 |
    | 1008 |  Customer8 |    R3 |     Div2 |
    | 1009 |  Customer9 |    R3 |     Div2 |
    | 1010 | Customer10 |    R4 |     Div2 |
SalesData
    | CUSTOMERCODE |               DATE | ITEM | BRAND | QUANTITY |
    |--------------|--------------------|------|-------|----------|
    |         1001 |  January, 01 2014  |  IT1 |   BR1 |        2 |
    |         1001 | February, 01 2014  |  IT2 |   BR2 |        5 |
    |         1003 |  January, 01 2014  |  IT1 |   BR1 |       20 |
    |         1003 | February, 01 2014  |  IT3 |   BR1 |        6 |
    |         1005 |  January, 01 2014  |  IT1 |   BR1 |        5 |
    |         1005 | February, 01 2014  |  IT2 |   BR2 |       11 |
    |         1007 |  January, 01 2014  |  IT1 |   BR1 |       15 |
    |         1007 | February, 01 2014  |  IT2 |   BR2 |        7 |
    |         1007 |    March, 01 2014  |  IT3 |   BR1 |        9 |
    |         1007 |    April, 01 2014  |  IT4 |   BR2 |       18 |
I need to answer below questions
To get the Brand distribution over Route and Division
1-Count of customers that bought a brand per Route and Per Division
*  ex:BR1 reached 2 customers out of 3 in R1
*  ex:BR1 reached 3 customers out of 6 in Div1
To get the Item distribution over Route and Division
1-Count of Customers that bought an item per Route and Per Brand
*  ex:IT2 reached 1 customers out of 3 in R1
*  ex:IT2 reached 2 customers out of 6 in Div1
It will be a great if someone can help me.

Try below code
select'1001' code ,'Customer1' name,'R1' route,'Div1' division into #CustomerMaster union all
select'1002','Customer2','R1','Div1'union all
select'1003','Customer3','R1','Div1'union all
select'1004','Customer4','R2','Div1'union all
select'1005','Customer5','R2','Div1'union all
select'1006','Customer6','R2','Div1'union all
select'1007','Customer7','R3','Div2'union all
select'1008','Customer8','R3','Div2'union all
select'1009','Customer9','R3','Div2'union all
select'1010','Customer10','R4','Div2'
select'1001' CUSTOMERCODE ,'January, 01 2014' DATE ,'IT1' ITEM ,'BR1' BRAND ,'2' QUANTITY into #SalesData union all
select'1001','February, 01 2014','IT2','BR2','5'union all
select'1003','January, 01 2014','IT1','BR1','20'union all
select'1003','February, 01 2014','IT3','BR1','6'union all
select'1005','January, 01 2014','IT1','BR1','5'union all
select'1005','February, 01 2014','IT2','BR2','11'union all
select'1007','January, 01 2014','IT1','BR1','15'union all
select'1007','February, 01 2014','IT2','BR2','7'union all
select'1007','March, 01 2014','IT3','BR1','9'union all
select'1007','April, 01 2014','IT4','BR2','18'a
--brand per Route
SELECT A1.*,B1.TCount from
(SELECT B.BRAND,A.ROUTE,COUNT(B.CUSTOMERCODE) Rcount
FROM #CustomerMaster A
INNER JOIN (select distinct BRAND,CUSTOMERCODE from #SalesData )B
ON B.CUSTOMERCODE=A.CODE
GROUP BY B.BRAND,A.ROUTE) A1
INNER JOIN
(SELECT A.ROUTE, COUNT(A.CODE) Tcount from #CustomerMaster A GROUP BY A.ROUTE)B1
ON A1.ROUTE=B1.ROUTE
--brand Per Division
SELECT A1.*,B1.TCount from
(SELECT B.BRAND,A.DIVISION,COUNT(B.CUSTOMERCODE) Dcount
FROM #CustomerMaster A
INNER JOIN (select distinct BRAND,CUSTOMERCODE from #SalesData )B
ON B.CUSTOMERCODE=A.CODE
GROUP BY B.BRAND,A.DIVISION) A1
INNER JOIN
(SELECT A.DIVISION, COUNT(A.CODE) Tcount from #CustomerMaster A GROUP BY A.DIVISION)B1
ON A1.DIVISION=B1.DIVISION
-- item per Route
SELECT A1.*,B1.TCount from
(SELECT B.ITEM,A.ROUTE,COUNT(B.CUSTOMERCODE) Rcount
FROM #CustomerMaster A
INNER JOIN (select distinct ITEM,CUSTOMERCODE from #SalesData )B
ON B.CUSTOMERCODE=A.CODE
GROUP BY B.ITEM,A.ROUTE) A1
INNER JOIN
(SELECT A.ROUTE, COUNT(A.CODE) Tcount from #CustomerMaster A GROUP BY A.ROUTE)B1
ON A1.ROUTE=B1.ROUTE
--item per Division
SELECT A1.*,B1.TCount from
(SELECT B.ITEM,A.DIVISION,COUNT(B.CUSTOMERCODE) Dcount
FROM #CustomerMaster A
INNER JOIN (select distinct ITEM,CUSTOMERCODE from #SalesData )B
ON B.CUSTOMERCODE=A.CODE
GROUP BY B.ITEM,A.DIVISION) A1
INNER JOIN
(SELECT A.DIVISION, COUNT(A.CODE) Tcount from #CustomerMaster A GROUP BY A.DIVISION)B1
ON A1.DIVISION=B1.DIVISION
Thanks
Saravana Kumar C

Similar Messages

  • How to find out the account group information in customer master record?

    how to find out the account group information in customer master record?
    in which tab? thanks in advance

    Hi
    Go to XD02 and select the Extras from the main menu , you will find Account group info -> click on the No.ranges.
    reward if it helps
    SR

  • Extend Distribution Channel in Customer Master

    Dear Gurus,
    Can any one explain me how to extend the Distribution channel in Customer master for the same Customer code. For example, my customer code is 100178 and the Distribution channel is PG, now i want to extend the customer to other channel PM, PR, PT.
    Regds
    Jeevagan.U

    Hi Friend,
    You can extend the CUSTOMER to different Distribution channel in XD01.
    Goto the transaction code XD01 and enter the Customer number whch you want to extend and the Sales organisation/Distribution channel/Division
    In the below , there is a Reference Tab in which you have to give the Customer/Sales organisation/Distribution channel/Division in which the customer was originally created and  Hit Enter.
    It will be extended to the said Distribtion channel
    In the same way you can extend this to Sales organisations and divisions also.
    regards,
    santosh

  • "Find Finder Items" and external volumes

    I have a particular file (an Entourage mail database) that I want to automate backup to a USB thumb drive. Once a day I already use the Backup utility to automatically copy it (currently about 350 MB) to the external thumb drive. No problem.
    Now I want to be able to delete old backups (say more than a week old). Before I can get there, I need to get the "Find Finder Items" action to work on external drives. Is this an action that only works on internal mounted volumes? I also have an external hard drive that doesn't seem to work with that action either. No matter what criteria I select, when I run the action I get "no results produced" which I know is incorrect.

    "Find finder items" works on all drives. But the drives have to be indexed by spotlight. can you do finder search on your thumb drive? also, how is it formatted? it's best if it's formatted mac os extended. thumb drives are usually formatted FAT by default.

  • Payment terms based on specific dist.channel, division and Plant but not from customer master.

    Dear All
    I have a requirement,
    Generally payments terms from customer master will flow to sales order. but here my requirement is to get a different payment terms based on specific distribution channel, division, customer  and plant those are there in sales order but not from customer master. is there any user exit or functional module to fulfill this requirement, or any other suggestion appreciated.
    Thanks,
    Reddy

    This Problem you can solve without user exit too.
    The Customer master data itself can solve the problem.
    You can maitain diffrent Payment Term as per the sales area (Dist.Ch+Div+S.org) of customer master.
    and each sales are wise u can change the plant also.
    You just need to set your logic with the combination of sales are and plant. And need to assign which payment term should determine to sales order.
    If you are rasing sales order with X sales area relavent to that payment term will trigger.
    Hope above logic will work, otherwise you can go with user exit.

  • Sales District and Partner function relationship in customer master

    Hello there
    I am working on a report and need to know one table relation for sales District and Partner function 
    i have a requirement , where i want to list all the partner number/Partner function assign to the customer with ref to the sales District
    we have one partner function Tab at sales Area tab in customer master , and another when we click on Sales district tab at header will get another window where we maintain partner function relate to the sales District , i want to have all those partner function in report , can you please help me where i can find that in table ? when i do F1 its says  KNVP but that table maintain at the sales area leval , not for the Sales District !! if you can help
    thanks
    Sonam

    Hi Sonam -
    and another when we click on Sales district tab at header will get another window where we maintain partner function relate to the sales District
    Do you have custom table/maintenance for sales district? Are you not maintaining Sales District in Sales Tab of customer master.
    You have to look for the field and table where you have maintained sales distict along with Customer and join the customer with KNVP customers and get all the partners.
    For Example - if you have mainationed sales district in KNVV table of BZIRK field then you have to join KNVV -KUNNR with KNVP-KUNNR and extract all records of customer distict and associated partner functions.
    Let me know,if the given solution is not applicable for your case.
    Thanks
    Amit Gupta

  • Automatic creation of Company Code and Sales Area Data for Customer Master

    Hi,
    We have a organisation structure setup with multiple company codes and sales areas.
    A customer will be created using VD01 via the Sales Order VA01. Because the customers are created using VD01 we need to automatically extend the customer for the various company codes and sales areas. The data on these views will be populated using reference customers.
    Any recommendations on the best approach (technically) to auto extend the customer master would be appreciated.
    I'm reluctant to use BDC's because of performance. We are in a Retail environment.
    Thanks in advance
    Craig

    The sample code has been taken from the SAP notes - in which have the affected as outlined in the example.
    The codes does not do what my requirements are, which is to identify the next available number in isolation of the BP type.

  • Validate Street and House Number in the Customer Master Data (xd01/xd02)

    All.
    I need to validate if the fields street and house number are in blank in the Customer Master Data transaction (xd01/xd02). I found an user-exit (EXIT_SAPMF02D_001), which I can validate the customer data (KNA1, KNB1, etc) before to save. The problem is in the KNA1 structure, there aren't the fields street (STREET) and house number (NOUSE_NUM1), there is only the field STRAS, which street and house number values are concatenated.I solved this problem using field-symbol to catch these values in the screen, but I know it isn't a good solution. Is there another solution to solve this ? I had problem using this user-exit. It shows a message error type 'E' when either field street or field house number are in blank, after show this message if the user click in the tabs navigator button before press enter button, the transaction shows the error message "System error on reading table  DYNTAB, screen no. 0370". Does Somebody know this problem?
    Best regards,
    Alexandre
    ps: I'm coding in the ECC 6.

    Amit.
    Thanks for your suggestion, but I need to validate these values typed by the user. When I create a customer, I can't get these values in the ADRC table.
    Regards, Alexandre

  • Maximum file/folder size for Copy Finder Items?

    As I mentioned in another recent post, I used Automator a while back to set up a workflow that would Get Finder Items and then Copy Finder Items. Set it up as a iCal plugin. Worked fine for a long time, but I just checked and saw that the largest of the three folders (1.8 GB) was not being completely copied. The other two folders, much smaller (20 MB or so) were fine, but several folders within that large 1.8 GB folder were being skipped. I opened the workflow, and ran it, and it came up with an error that just told me to check the workflow. Is there a maximum size on this type of workflow? Seemed to work fine before . . .

    The *Copy Finder Items* action will not make duplicate copies. If you want multiple copies of the same item, you will need to rename or duplicate the item before copying, since the copy action doesn't have an option to rename, and the duplicate action doesn't duplicate to a different location.

  • "Find Finder Items" returns same file twice

    Hey y'all,
    I'm trying to jerry-rig a photobooth for my wedding. I'm using automator to grab 3 stills from a webcam, and place them all in a folder. When I use "Find Finder Items" to send these images to an action that will rotate them, it finds one of the images twice. That is, the results for "Find Finder Items - where: path whose: name contains 'snap' " will return pictures/path/snap3.tiff, pictures/path/snap1.tiff, pictures/path/snap2.tiff, pictures/path/snap3.tiff. This means that when I send these results to my Rotate 90 Degrees action, Snap3 is rotated 180, and when I send them all into a PDF, I get snap3 twice.
    This also happens when I "Find Finder Items" and just look for all the .tiff files in /path, and when use "Get Folder Contents" on /path.
    Background: Running 10.5.8 Leopard on a PPC Mac mini. Fairly new to the world of Mac, so I may be missing something obvious.
    I've been searching for answers for a day or two now, and haven't come across this sort of issue; I apologize if this is old hat, but I really have made a goodfaith attempt to search these and other forums.
    Any help would be greatly appreciated.
    Edited to add: The folder only contains the 3 .tiff files, and the PDF I'm trying to dump them into. I thought maybe I was somehow creating a 4th identical file, but it's not the case.

    Hello & a warm welcome!
    no idea what is happening, other than Apple Software is usually the worst to accomplish thing the way YOU want to.
    GraphicConverter has a powerful Browse Folder function, & many tools for manipulation, & you can keep your Pics wherever you want...
    http://www.lemkesoft.com/

  • How do I get rid of box around selected finder items?

    How do I get rid of this black square box that has appeared on my finder items, and other things on my computer? Currently the black box is around the red x on top of the firefox window. It moves with the programs I have open.
    I would upload a screenshot if I could figure out how to do it on this computer.

    Open the Universal Access pane of System Preferences, click on Seeing, and turn VoiceOver off.
    (52092)

  • Regarding ALE-iDoc for Customer Master, J1ID and FD32

    Hello all
    Scenario:
    As per client's requirement we have to develop idoc so that followings requirements will meet:
    Say there are two servers a) MDM Server b) PRD Server
    1] As and when I'll create customer master data in MDM server then customer master should get created with that data in PRD server
        which I can achieve through BD12 by doing some idoc enhancements
    2] Simillarly when i'll create data using transaction J1ID and FD32 in MDM Server (Data relevant to customer master) then that data also should get created in PRD server using Idoc
    May I know which
    1)message type we can use (as we use DEBMAS in case of Customer Master) for J1ID and FD32
    2) Any transaction to send data from one server to another server for transaction J1ID and FD32 (as we use BD12 in case of Customer master for sending data created using  transaction XD01) 
    Thanks,
    Mahesh

    Hello Mahesh,
    In the case of customer master it would be better if you would use standard message types and t code to transfer ddata.
    But in the case J1ID and FD32 you can build a standalone idoc
    Follow these steps to build custom message types and futher transactions
    WE81 - Create message type
    WE31 - Create segment
    WE30 - Create Idoc type
    SALE - Define Logical system (not required if already exists)
    SM59 - Define RFC (not required if already exists)
    WE21 - Define ports (both in sender and receiver)
    WE20 - Define Partner profile (both in sender and receiver)
    BD46 - Distribue the model view.
    SE38 - Create a program to send the idoc in the sender sytem.
    SE37 - Build inbound function module to receive the idoc for receiver system
    WE57 - Assign FM to message type
    BD51 - to change the characteristics for inbound FM
    WE42 - Create Inbound process code.
    Hope this would help you.
    Thanks and Regards,
    Ranjith Nambiar

  • "Run Workflow" action in Automator does not "Ask for Finder Items"

    So I have a workflow called step_1 that does a bunch of stuff . . . in essence, it "Asks for a Finder Item" and then runs some Python shells and then outputs an Applescript format list {'item_1','item_2','item_3'}
    I'm trying to use this workflow in another workflow so I selected "Run Workflow" in Automator and located my original workflow step_1 but when I run it, the process goes straight through the workflow and doesn't "Ask for Finder Items" so i'm sure it doesn't do anything else in the workflow either.  It doesn't give me an error, it just pretty much bypasses everything within the workflow and it's pretty annoying.
    Any idea on how to get my workflow to work using "Run Workflow" ?

    Never tired Run Workflow before so as as a test I rn these two
    and the Ask for Finder items in the called workflow ran OK.
    Note this was run  from Automator. One thing I did see was that the Ask for finder items window did not come to the front and I had to look under my open windows to see it.

  • Vendor master from Customer master

    Hi Friends
    Is it possible to create a vendor master refering a customer master? If answer is yes, how it can be achieved?

    Hi,
    you can assign customer master to a vendor and same vendor number in customer master, so that to facilitate FI activity in clearing the open items by adjustment.
    Regards,
    gln

  • "Vendor" field in the customer master - XD03

    Hi,
    I would like to know how useful the "vendor" field can be in the "control data" tab of the customer master (and vice versa with the vendor master).
    Is this only for information only or can we use this information to consolidate open items for one particular vendor that is also a customer?
    And how to do this if consolidation is possible?
    Thanks,
    Ronan

    Hello,
    When a Customer is also a Vendor and decided to net of the transactions with each others account in order to offset the entries.
    We need to get USD 100 from Customer A
    We need to pay USD 50 to Vendor A
    Ultimately, we need to get 50 from Customer A (Customer A and Vendor A is the same entity)
    To do this, You need to create Customer A in the Vendor Master and Vendor A in the Customer Master.
    You also need to check "Clear Customer with Vendor" check box in the master data.
    This will result in customer items and vendor items cleared against each other.
    Regards,
    Ravi

Maybe you are looking for