Crosstab two joined tables multiplies the data in each column ....

below is code that crosstabs monthy sales numbers and totals for each month.  So what I need to do is add a column that totals last years sales (table will be called [2013]) and add a column called 2013 YTD. I also need to add a calculation column that
calculates growth from one year to another. So I join the 2013 table and add one last line that sums 2013 sales but i need only Jan-Mar at this point. in other words I just need to see the total sales for each dealer from 2013 Jan-Mar while still viewing Jan-Dec
for the current year. So looking at the end product I need to see the Dealer Info, Jan, Feb, Mar, Apr......and at the end 2013 Sales and Growth. Thus, the Where clause. Unfortunately, each months sales are multiplied x 3 with or without the 2013 sum line.
As soon as I join the tables the numbers get multiplied.
What am I missing??????
SELECT    
substring([2014].Dealer,18,50)AS
[Dealer Name],substring([2014].Dealer,9,1)AS
[District],substring([2014].Dealer,11,6)AS
[Dealer Code],
SUM(CASE
WHEN [2014].[Transaction Sales Month]
= 1 THEN
[2014].[Sales Count]
ELSE 0 END)
AS Jan,
SUM(CASE
WHEN [2014].[Transaction Sales Month]
= 2 THEN
[2014].[Sales Count]
ELSE 0 END)
AS Feb,
SUM(CASE
WHEN [2014].[Transaction Sales Month]
= 3 THEN
[2014].[Sales Count]
ELSE 0 END)
AS Mar,
SUM(CASE
WHEN [2014].[Transaction Sales Month]
= 4 THEN
[2014].[Sales Count]
ELSE 0 END)
AS Apr,
SUM(CASE
WHEN [2014].[Transaction Sales Month]
= 5 THEN
[2014].[Sales Count]
ELSE 0 END)
AS May,
SUM(CASE
WHEN [2014].[Transaction Sales Month]
= 6 THEN
[2014].[Sales Count]
ELSE 0 END)
AS Jun,
SUM(CASE
WHEN [2014].[Transaction Sales Month]
= 7 THEN
[2014].[Sales Count]
ELSE 0 END)
AS Jul,
SUM(CASE
WHEN [2014].[Transaction Sales Month]
= 8 THEN
[2014].[Sales Count]
ELSE 0 END)
AS Aug,
SUM(CASE
WHEN [2014].[Transaction Sales Month]
= 9 THEN
[2014].[Sales Count]
ELSE 0 END)
AS Sep,
SUM(CASE
WHEN [2014].[Transaction Sales Month]
= 10 THEN
[2014].[Sales Count]
ELSE 0 END)
AS Oct,
SUM(CASE
WHEN [2014].[Transaction Sales Month]
= 11 THEN
[2014].[Sales Count]
ELSE 0 END)
AS Nov,
SUM(CASE
WHEN [2014].[Transaction Sales Month]
= 12 THEN
[2014].[Sales Count]
ELSE 0 END
AS
Dec,
SUM([2014].[Sales Count])
AS [2014 Total],
FROM        
dbo.[2014]
GROUP
BY substring([2014].Dealer,18,50),substring([2014].Dealer,9,1),substring([2014].Dealer,11,6)

This needs to be moved to Transact-SQL forum. You may want to post DDL of your tables, some input data (as insert statements) and desired output.
In the meantime I think you'll find this blog post helpful:
Aggregates with multiple tables
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles

Similar Messages

  • How to name the data for each column I am acquiring in lvm file

    does anybody hint  How to name the data for each cloumn I am acquiring in lvm file.
    I want to tag or name ,eg temperature at top of a column which shows the temperature readings .I am writing into a labview measurement file.
    Thanks

    Use Set Waveform Attribute on each channel of your data.  Set an attribute with name "NI_ChannelName".  The value is a string containing the name you wish to call the channel.
    This account is no longer active. Contact ShadesOfGray for current posts and information.

  • How can I link specific cells in two different speadsheets so the data from a specific cell in spreadsheet A automatically updates in a specific cell in spreadsheet B. It works in Xcel- Any ideas?

    How can I link specific cells in two different tables so the data from a specific cell in table A automatically updates in a specific cell in table B? It works in Xcel- Any ideas?

    (1) your title ask the way to link different spreadsheets.
    In Numberland, a spreadsheet is an entire document.
    There is no way to link different documents.
    (2) in the message, you ask the way to link different tables.
    This feature is available and is described in iWork Formulas and Functions User Guide (which is available for free from the Help menu).
    Getting the info just requires a simple search in this available resource !
    Yvan KOENIG (VALLAURIS, France) 14 mai 2011 10:37:50

  • How to insert into two differents tables at the same time

    Hi
    I'm newer using JDev, (version 3.1.1.2 cause the OAS seems to support just the JSP 1.0)
    and I want to insert into two differents tables at the same time using one view.
    How can I do that ?
    TIA
    Edgar

    Oracle 8i supports 'INSTEAD OF' triggers on object views so you could use a process similar to the following:
    1. Create an object view that joins your two tables. 'CREATE OR REPLACE VIEW test AS SELECT d.deptno, d.deptname, e.empname FROM DEPT d, EMP E'.
    2. Create an INSTEAD OF trigger on the view.
    3. Put code in the trigger that looks at the :NEW values being processed and determines which columns should be used to INSERT or UPDATE for each table. Crude pseudo-code might be:
    IF :NEW.deptno NOT IN (SELECT deptno FROM DEPT) THEN
    INSERT INTO dept VALUES(:NEW.deptno, :NEW.deptname);
    INSERT INTO emp VALUES (:NEW.deptno, :NEW.empname);
    ELSE
    IF :NEW.deptname IS NOT NULL THEN
    UPDATE dept SET deptname = :NEW.deptname
    WHERE deptno = :NEW.deptno;
    END IF;
    IF :NEW.empname IS NOT NULL THEN
    UPDATE emp SET empname = :NEW.empname
    WHERE deptno = :NEW.deptno;
    Try something along those lines.
    null

  • Joining table reduce the significantly

    We have a module that create need to load record from 3 tables to construct the object, if we query for 20k object, it take 20s object, which look longer than expected.
    Thus we try to join that 3 tables and run single query to get the record. However that even more slow, it take 60s to get those objects.
    One problem is one of the tables use varchar as ID, we need to join that table with to_char(other_id) = id . However, even that create performance penality, this should no be that slow....
    Is this data look normal to you , will you think we have a lot of room to improve?

    Can you please provide:
    1. The ODBC settings for the datastore
    2. The full schemas for each table, including indices
    3. The exact SQL statements you are running (the single table query and the join)
    4. The rowcounts for each table
    5. Confirm if you updated optimiser statistics after populating the tables.
    Thanks,
    Chris

  • Which table stores the data of transaction FBL1N...?

    Hi,
    Please help me find out which table stores the data ,generated from transaction FBL1N..Actually i need to find out the g/l account against the vendor account that is being displayed in FBL3N's document overview.

    Hi,
    Data will be retrieved from following table :
    tables: lfa1, lfb1, bsik, bsid, kna1, t001,t005, SKAT, SKB1.
    In BSIK you have  both LIFNR and HKONT. You will get the data based on the LIFNR and HKONT. LIFNR is Vendor and HKONT is GL assigned to the vendor.
    Thanks,
    Sriram Ponna.
    Message was edited by:
            Sriram Ponna

  • I need to back up and sync multiple iPhones to a single iMac.  Each iPhone has a separate Apple ID and the data from each iPhone needs to be backed up separately from the others.  How can I do this without getting all of the contacts, etc. mixed? Tnx

    I need to back up and sync multiple iPhones to a single iMac.  Each iPhone has a separate Apple ID and the data from each iPhone needs to be backed up separately from the others.  How can I do this without getting all of the contacts, etc. mixed? Tnx

    This might help: http://support.apple.com/kb/HT1495.

  • Populate a table reading the data from a TXT file

    how can I populate a table reading the data from a TXT file?
    thanks

    Hey Kevin!
    Using FORMS.TEXT_IO to bulk load data from a file strikes me as re-inventing the wheel. It is just about justifiable in a self-service environment, but I regard the EXTERNAL TABLE is a better solution for that situation as well.
    The same applies to UTL_FILE. I think the ability to read text with UTL_FILE is primarily intended for read file-based configuration or file manipulation/processing rather than data loading.
    Re-writing a text file into SQL statements is too much like hard work (even with an editor that supports macro definition and regular expressions) for no real benefit. You lose all the bulk load peformance you would get from SQL*Loader. But for QAD I'd probably let you off with it.
    You missed out one obvious alternative: using Java to turn the contents of an XML file into a CLOB and inserting it into a table which is read by a PL/SQL procedure that parses the XML records and insert the retrieved data into a table.
    Stay lucky, APC

  • ML60 tcode !! which table stores the data which is shown in ML60 ..urgent !

    Hi Guys,
    Tcode ML60 shows the Relase Group, Relase strategy and all....
    Can anyone tell me which table is that which the stores the data which is shown in tcode ML60 ???
    The problem that i am facing is that for ML60 the amount that is fixed for every relase staretgy is in text fields eq 2.5k - 50 K USD but i want this value to be in numeric field and not as text.
    If i will find that table which stores all this information, may be i will get this field which stores this text as a numeric field.
    Please help me find this table.
    And also can any one tell me how to link PO document type to the relase group ??
    Points will be awarded.
    Thanks a lot
    Rahul

    Hi guys,
    Yes ML60 is a standard SAP transaction but may be ML60 is not configured to the system which you are using. Otherwise also if u will search google, you will find ML60.
    Hi Anji, i have already tried all these tables, but none of these tables store the limit for each release strategy.
    I can see the limit only in ML60 tcode
    Thanks
    Rahul

  • When target DB is down in which table is the data from source is stored, also where are the error messages stored in ODI

    When target DB is down in which table is the data from source is stored, also where are the error messages stored in ODI( I am not getting any error message in E$_TARGET_ANI_TEST).
    When i am running the interface i am getting the below error against the errored step
    "ORA-01045: user ABC lacks CREATE SESSION privilege; logon denied."
    Only E$_TARGET_ANI_TEST  is created with no data. No such tables like C$_0TARGET_ANI_TEST, I$_TARGET_ANI_TEST are created and also data is not inserted in the target table TARGET_ANI_TEST.

    Hi,
    I have checked that only E$ table is created. C$ and I$ table are not created ( I have selected my target schema as the part for the staging table).
    All the parameters for dropping the tables are selected as "<default>:false".
    I am importing the following KMs with the following parameters:
    1) CKM Oracle
    DROP_ERROR_TABLE
    :false
    DROP_CHECK_TABLE
    :false
    CREATE_ERROR_INDEX
    :true
    COMPATIBLE
    :9
    VALIDATE
    :false
    ENABLE_EDITION_SUPPORT
    :false
    UPGRADE_ERROR_TABLE
    :false
    2) LKM SQL to SQL
    DELETE_TEMPORARY_OBJECTS
    :true
    3) IKM SQL Incremental Update
    INSERT
    :true
    UPDATE
    :true
    COMMIT
    :true
    SYNC_JRN_DELETE
    :true
    FLOW_CONTROL
    :true
    RECYCLE_ERRORS
    :false
    STATIC_CONTROL
    :false
    TRUNCATE
    :false
    DELETE_ALL
    :false
    CREATE_TARG_TABLE
    :false
    DELETE_TEMPORARY_OBJECTS
    :true 

  • In which tables are the data for the aplication KSB1?

    I'm getting data from Qlik View with a BI tool called Qlik View.
    I need help to find in which tables are the data for KSB1 aplication.
    I've found something in COBK and COEP but there's more table involved since I didn't find all data.
    Somente can help me please?
    Regards.
    Robson

    I ran a trace and found some tables that may help you.
    COVP(View)
    CSKT
    CSLT
    CSKS
    CSKU
    AUFK
    Thanks
    Rathish

  • Data table to the data chart editor ?

    It is possible to link the values of a table to the data chart editor ? Not to copy and paste.

    sw_mero
    Welcome to the forum.
    Unfortunately not.
    Like many things in Pages, it is a bit of a hood ornament. It looks good in the demo but when you get to grips with it, it is not quite all there. Maybe in the next version of Pages.
    What you can do is copy and paste a chart created in Numbers, which will retain the links to the data in Numbers. Just make sure you save both the data and the Numbers file first.
    Peter

  • Why must I click the button two times to update the data?

    Dear All,
    I have written a simple vi about event case. I have found that I must to click the button two times to updata the data. I don't know why.
    And I can't use the stop button to stop the vi.
    Could you tell me the reason?
    Thank you very much.
    Attachments:
    event case.vi ‏42 KB

    Hi,
    1 : you have a "timeout case" but no timeout linked, so you lose the advantage of the event structure !
    2 : make a event for "stop" on value change
    3 : its normal, because le control terminals were out of the event struct, run it in high light mode you'll understand
    Remark : instead of having to clic a boolean to refresh the indicators, why don't you make a "value change event" directly on the control ?
    I modified your VI following step 1,2,3 above
    Hope this helps
    When my feet touch the ground each morning the devil thinks "bloody hell... He's up again!"
    Attachments:
    event case.vi ‏17 KB

  • Get frequent data for each column of a table

    What is the best way to get the most frequent data in each column of a table.
    we have around 25 tables and each table has around 20 columns , so rather than writing group by for each column of table , is there any easy way to find this?
    example we have table
    Column A       Column B         Column C
    Apple             Monday             Red
    orange            Tuesday            Green
    Apple              Monday            Red
    Lemon            Wednesday       Green
    Apple               Thursday         Red
    in this table, column A's frequnt data is Apple , column B's frequent data is Monday , Column C's frequnt data is Red
    Apple 3          Monday 2                  Red 3
    Orange 2        Tuesday 1                 Green 2
    Lemon 1          Wednesday 1         
                          Thursday 1
    Group by kind of query will give this result, but with 20 tables each having 20 - 30 columns if we need similar kind of result ..  is there any way to get this data.

    Hi,
    GROUP BY (using aggregate functions) is the best way to do what you described.
    PIVOT and UNPIVOT are probably what you'd want in this case.  The phrase "GROUP BY" may not actually appear in your code, but you'll essentially be doing a GROUP BY.
    Analytic functions (rather than aggregate functions) can do the job, too, but they'll be less efficient.  Analytic functions get you results about the groups, without losing each row's identify, but in this case, losing each row's identity is eactly what you want to do.  You only want 'Apple' to appear 1 time, not 3 times.
    The fact that you need to do the same query on 20 different tables suggests that there's something wrong with your table design.  Wouldn't it be better to have 1 big table, with a new column that has 20 unique values instead?
    I hope this answers your question.
    If not, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables involved, so that the people who want to help you can re-create the problem and test their ideas.
    Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
    Simplify the problem as much as possible.  For example, instead of posting a problem with 20 tables, each having 25 columns, post a similar problem involving, say, 2 tables, each with 3 columns.
    Always say which version of Oracle you're using (for example, 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • Which table stores the value of status column in leave request.

    Which table stores the value of status column in leave request.If i give pernr ,absence type and date how would i get the status
    Thanks
    Bala Duvvuri

    Hi,
    PTREQ_ACTOR      -              Request Participant
    PTREQ_ATTABSDATA    -           Request Data for Attendances/Absences
    PTREQ_BATCH              -      Time Stamp for Run of Background Jobs
    PTREQ_CKEY_MAPP      -        Cache Assignment Table
    PTREQ_HEADER             -      Request Header
    PTREQ_ITEM_TYPES       -        Request Item Types
    PTREQ_ITEMS                  -  Request Items
    PTREQ_NOTICE                -   Note for Request
    PTREQ_STATUS_CHK      -         Check Methods for Status Transitions
    PTREQ_STATUS_TRA      -         Status Transfers
    PTREQ_TYPE                    - Request Types
    PTREQ_XFEREVENTS       -        Permitted Status Transitions
    PTREQ_XFEREVNT_T        -       Texts for Status Transitions
    Value of staus column you can check it PTREQ_STATUS_CHK

Maybe you are looking for