• daniness (4/16/2014)


    Hi Sean,

    This is the query:

    SELECT ISNULL(ath_stlmnt_instr_id, ae.stlmnt_line_instr_id) AS ath_instr_id

    ,ath_instr_id AS orig_instr_id

    ,ath_gl_num

    ,ath_cost_cntr

    ,CASE

    WHEN ath_postype = 'GLD'

    THEN sum(ath_usdamt)

    WHEN ath_postype = 'GLC'

    THEN sum(ath_usdamt) * - 1

    END AS ath_usdamt

    ,count(*)

    FROM dbo.ACCTING_TRANSACTION_HISTORY

    left join Accting_body ae on ae.header_id = ath_header_id AND ae.body_id = ath_body_id

    inner join MTB_CHART_ACCTS mca on mca.acct = ath_gl_num

    WHERE mca.acct_category = 'AST'

    AND ath_postype IN ('GLD', 'GLC')

    group by ath_stlmnt_instr_id, ath_instr_id, ath_gl_num, ath_cost_cntr

    Union

    SELECT stlmnt_instr_id,instr_id,gl_num, cost_cntr, usdamt

    FROM dbo.ACCTING_ADJUST

    group by stlmnt_instr_id, instr_id, gl_num, cost_cntr

    Why do you have a group by in the second query? The second query can't possibly work because you are grouping by columns not in the select list.

    I think you need to do some reading on grouping and aggregate data. You seem to be very confused about how it works. You need to first run each query independent of the other (get rid of the union for now). Once you have each query returning the results you want, then you can use your UNION. You might also be able to use UNION ALL instead of just union but that depends on the data and the requirements.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/