How to use CTE for Budget Detail

  • I have a Budget detail table with expenses and salaries projections. Some employee split the salary in two different account, I need a query that bring the expenses and the salaries which some of them are split into 2 accounts and the percentages of each split. I need to bring all budget informationΒ  that is listed as "E" and employee salary information listed as "P".

    CREATE TABLE Budget (
    budget_type varchar (1),
    a_gl1 varchar (6),
    a_gl2 varchar(6),
    employee_number varchar(10),
    p_empl_proj_num varchar (6),
    budget_req_amt1 int,
    budget_req_qty1 int,
    Description (50),

    ******************

    Insert into Budget (

    budget_type, account_id,
    a_gl1, a_gl2,employee_number,
    budget_req_amt1, percent,
    Effective_date, emp_proj_num,
    a_projection_no, Description)

    values (P, 6252,
    010470, 513120,
    100015, 150000,
    50, 10/1/2019,
    2020, 0,

    P, 6252, 010470, 514120,
    100015, 150000,
    50, 10/1/2019,
    2020, 0,

    P, 6252, 010470, 513120,
    100016, 135000,
    100, 10/1/2019,
    2020, 0,

    P, 6252, 010470, 513120,
    100015, 140085,
    100, 9/30/2019,
    0,
    P, 2568, 410900, 533120,
    100050, 123270,
    85, 10/1/2019,
    2020, 0,

    P, 2568, 450910, 535120,
    100050, 123270,
    15, 10/1/2019,
    2020,
    E, 2568, 012050, 502630,
    15000, 10/1/2019,
    0, Office supply
    E, 2568, 013151, 492863,
    80000, 10/1/2019,
    0, Car maintenance
    E, 6252, 032565, 568261,
    85000, 9/30/2019,
    0, Utilities)
  • Good start, but your query does not parse and you have not provided your desired results.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Capture

  • Note to OP: please try running your own query. And then, please fix it so that it works πŸ™‚

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • hI pHIL

    Insert into Budget (

    budget_type, account_id,

    a_gl1, a_gl2,employee_number,

    budget_req_amt1, percent,

    Effective_date, emp_proj_num,

    a_projection_no, Description)

    values (β€˜P’, β€˜6252,’ β€˜010470’, β€˜513120’, β€˜100015’, β€˜150000’, β€˜50’, β€˜10/1/2019’, β€˜2020,’’ 0,’ β€˜NULL’

    β€˜P’, β€˜6252’, Β β€˜010470’, β€˜514120’, β€˜100015’, β€˜150000’, β€˜50’, β€˜10/1/2019’, β€˜2020’, β€˜0’,’NULL’

    β€˜P’, β€˜6252’, Β β€˜010470’, β€˜513120’, β€˜100016’, β€˜135000’, β€˜100’, β€˜10/1/2019’,’2020’,’ 0’, β€˜NULL’

    β€˜P’, β€˜6252’, Β β€˜010470’, β€˜513120’,’100015’, β€˜140085’,’100’, β€˜9/30/2019’,’0’,’ NULL’

    β€˜P’, β€˜2568’, Β β€˜410900’, β€˜533120’, β€˜100050’, β€˜123270’, β€˜85’, β€˜10/1/2019’, β€˜2020’, β€˜0’,’ NULL’

    β€˜P’, β€˜2568’, Β β€˜450910’, β€˜535120’,’100050’, β€˜123270’,’15’, β€˜10/1/2019’,’2020’,’ NULL’

    β€˜E’, β€˜2568’, Β β€˜012050’, β€˜502630’,’NULL’, β€˜15000’, β€˜NULL’, β€˜10/1/2019’,’NULL’, β€˜0’, Β β€˜Office supply’

    β€˜E’, β€˜2568’, Β β€˜013151’, β€˜492863’, β€˜NULL’, β€˜80000’, β€˜NULL’,β€˜10/1/2019’,’2020’, β€˜0’, β€˜Car maintenance’

    β€˜E’, β€˜6252’, Β β€˜032565’, β€˜568261’,’NULL’, β€˜85000’,’NULL’, β€˜9/30/2019’, β€˜NULL’, β€˜0’, β€˜Utilities’)

  • To Create table:

    CREATE TABLE Budget (

    budget_type varchar (1),

    Account_id (4) varchar

    a_gl1 varchar (6),

    a_gl2 varchar(6),

    employee_number varchar(10),

    p_empl_proj_num varchar (6),

    budget_req_amt1 int,

    percent int,

    Effective_date (datetime),

    Description (50),

     

  • Sorry everybody I was trying to edit my post but I cant

    CREATE TABLE Budget (Β Β  Β Β budget_type varchar (1), Β  Β account_id (4) varcharΒ Β  Β Β a_gl1 varchar (6),Β  Β  Β a_gl2 varchar(6),Β Β  Β employee_number varchar(10), emp_proj_num varchar (6), budget_req_amt1 int,Β Β  Β  percent int, Effective_date (datetime),Β Β emp_proj_num varchar (6), Β a_projection_no (4) varchar,Β Description (50),

    Insert into Budget (Β budget_type,Β account_id, a_gl1,Β a_gl2,employee_number,budget_req_amt1,Β percent,Effective_date,Β emp_proj_num,a_projection_no,Β Description)

    values (β€˜P’,Β β€˜6252,’ β€˜010470’,Β β€˜513120’, β€˜100015’,Β β€˜150000’, β€˜50’,Β β€˜10/1/2019’, β€˜2020,’’ 0,’ β€˜NULL’

    β€˜P’,Β β€˜6252’,Β Β β€˜010470’,Β β€˜514120’, β€˜100015’,Β β€˜150000’, β€˜50’,Β β€˜10/1/2019’, β€˜2020’,Β β€˜0’,’NULL’

    β€˜P’,Β β€˜6252’,Β Β β€˜010470’,Β β€˜513120’, β€˜100016’,Β β€˜135000’, β€˜100’,Β β€˜10/1/2019’,’2020’,’ 0’, β€˜NULL’

    β€˜P’,Β β€˜6252’,Β Β β€˜010470’,Β β€˜513120’,’100015’,Β β€˜140085’,’100’,Β β€˜9/30/2019’,’0’,’ NULL’

    β€˜P’,Β β€˜2568’,Β Β β€˜410900’,Β β€˜533120’, β€˜100050’,Β β€˜123270’, β€˜85’,Β β€˜10/1/2019’, β€˜2020’,Β β€˜0’,’ NULL’

    β€˜P’,Β β€˜2568’,Β Β β€˜450910’,Β β€˜535120’,’100050’,Β β€˜123270’,’15’,Β β€˜10/1/2019’,’2020’,’ NULL’

    β€˜E’,Β β€˜2568’,Β Β β€˜012050’,Β β€˜502630’,’NULL’, β€˜15000’, β€˜NULL’,Β β€˜10/1/2019’,’NULL’, β€˜0’,Β Β β€˜Office supply’

    β€˜E’,Β β€˜2568’,Β Β β€˜013151’,Β β€˜492863’, β€˜NULL’, β€˜80000’,Β β€˜NULL’,β€˜10/1/2019’,’2020’, β€˜0’,Β β€˜Car maintenance’

    β€˜E’,Β β€˜6252’,Β Β β€˜032565’,Β β€˜568261’,’NULL’, β€˜85000’,’NULL’,Β β€˜9/30/2019’, β€˜NULL’, β€˜0’,Β β€˜Utilities’)

  • Do you even have SQL Server installed somewhere?

    Because your code does not run. Even your modified code is riddled with errors. Here's a sample:

    1. CREATE TABLE statements do not end with a comma
    2. VARCHAR needs a length.
    3. PERCENT is a reserved word and cannot be used, without square brackets, as a column name
    4. Description (50) is not a valid column definition
    5. NULL is not literal text
    6. INT values don't need single quotes
    7. VALUES statements aren't written like that
    8. There is an important difference between single quotes and what you have used, and that's that single quotes are valid.

    Really, you need to make sure that what you post actually runs in SSMS before you post it here and waste people's time.

    Also, please format it in future to make it easier to read. Use temp tables, not permanent ones:

    DROP TABLE IF EXISTS #Budget;

    CREATE TABLE #Budget
    (
    budget_type CHAR(1) NOT NULL
    ,account_id VARCHAR(4) NOT NULL
    --and so on
    );

    --INSERT statements here

     

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

Viewing 9 posts - 1 through 8 (of 8 total)

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