How to include a column in Group results but avoid in calculation, please

  • Hello Good Morning,

    Could you please assist me here, with the below query i am getting results good, so i need to develop a pivottable in excel that it needs MerchState column as the filter, so fo this in the results i need to include Merchstate column values (state codes TX, AZ,CA etc) but it shouldn't effect the results what i am having now, so for this i try to add MerchState to the results (commented now), it gives me Avg(order_t1) as InitVal,Avg(order_t2) as TargVal column by considering merchstate column (which is obviously) so how to include this column in output but not in calclation,

    please assist me, i greatful to you ,

    Select

    CorpName + ' - ' + SrtName as Signer,

    BonerDesc,

    ---- MerchState,

    Count(CustID) as CustomersTotal,

    Avg(order_t1) as InitVal,

    Avg(order_t2) as TargVal,

    SUM(Pos_d1) as Src,

    SUM(Pos_d3) as Trgt,

    from TX_MedBrod.dbo.AcctMast

    where taxerPD='September-2013'

    group by

    CorpName + ' - ' + SrtName,

    BonerDesc,

    Order by

    1,2

    Thank you in Advance,

    Dhani

  • If the results change when you uncomment the [MerchState] field, that means the current GROUP BY overlaps multiple values in the [MerchState] field. The main question is: which of these [MerchState] values would you want in your final resultset? You could use the MAX or MIN statement on the [MerchState] field to prevent a change in the resultset. But this also implies you won't get all the values from the [MerchState] field.

    If possible provide us with some sample data (DDL and INSERT statements) so we can replicate your problem and provide a tested solution.

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **

Viewing 2 posts - 1 through 1 (of 1 total)

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