Note that there are some explanatory texts on larger screens.

plurals
  1. POFetch the total from the result of a query
    primarykey
    data
    text
    <p>The query i am using is</p> <pre><code>select convert(varchar(10), sales.saledate, 103) [SaleDate], SUM(sales.Domestic) [Domestic], SUM(sales.Export) [Export], SUM(sales.Import) [Import], SUM(sales.Value) [Value], Sum(sales.Cancelled) [Cancelled], sum(sales.cancelledValue) [CancelledValue], SUM(sales.totalValue) [TotalValue] from ( select max(j.SaleDate) SaleDate, case when max(oc.Code) = 'AU' and max(dc.Code) = 'AU' then 1 else 0 end [Domestic], case when max(oc.Code) = 'AU' and max(dc.Code) &lt;&gt; 'AU' then 1 else 0 end [Export], case when max(oc.Code) &lt;&gt; 'AU' and max(dc.Code) = 'AU' then 1 else 0 end [Import], 1 [Total], max(ic.Total) [Value], case when max(c.CancelDate) is not null then 1 else 0 end [Cancelled], case when max(c.CancelDate) is not null then max(ic.Total) else 0 end [CancelledValue], case when max(c.CancelDate) is null then max(ic.Total) else 0 end [TotalValue] from invoices i left join Jobs j on i.JobKey = j.JobKey inner join tasks t on j.jobkey = t.jobkey inner join Consignments c on t.TaskKey = c.consignmentkey inner join places op on c.originplacekey = op.placekey inner join places dp on c.destinationplacekey = dp.placekey inner join places oC on dbo.ParentPlaceKey(c.originPlaceKey) = oc.placekey inner join places dC on dbo.ParentPlaceKey(c.destinationplacekey) = dc.placekey left join (select consignmentKey, sum(Value) [Value] from ConsignmentItems ci group by consignmentkey ) ci on ci.ConsignmentKey = c.ConsignmentKey left join (select invoicekey, sum(case when ci.ChargeItemKey = 'FRT_SLL' then oc.Value else 0 end) [Freight], sum(case when ci.ChargeItemKey = 'WTY_SLL' then oc.Value else 0 end) [Warranty], sum(case when ci.ChargeType = 4 then oc.Value else 0 end) [Total] from InvoiceCharges ic left join OptionCharges oc on ic.OptionChargeKey = oc.OptionChargeKey left join ChargeItems ci on oc.ChargeItemKey = ci.ChargeItemKey group by invoicekey ) ic on ic.InvoiceKey = i.InvoiceKey where j.SaleDate &gt;= '01-Apr-2013' and j.SaleDate &lt;= '10-May-2013' and j.operationalstorekey = dbo.StoreCode('AU-WEB') and j.saledate is not null and SelectedOptionKey is not null group by j.jobkey ) sales group by convert(varchar(10), sales.saledate, 103) order by max(sales.saledate) </code></pre> <p>The result of a sql query is</p> <pre><code>SaleDate Domestic Export Import Value Cancelled CancelledValue Totalvalue 11/04/2013 1 0 0 47.200 0 0.0000 47.2000 16/04/2013 6 0 0 249.750 0 0.0000 249.7500 22/04/2013 0 1 0 223.480 0 0.0000 223.4800 23/04/2013 0 3 0 670.440 0 0.0000 670.4400 </code></pre> <p>I want result like (want to add the TOTALS at the end)</p> <pre><code>SaleDate Domestic Export Import Value Cancelled CancelledValue Totalvalue 11/04/2013 1 0 0 47.200 0 0.0000 47.2000 16/04/2013 6 0 0 249.750 0 0.0000 249.7500 22/04/2013 0 1 0 223.480 0 0.0000 223.4800 23/04/2013 0 3 0 670.440 0 0.0000 670.4400 TOTALS 7 4 0 1190.432 0 0 1190.432 </code></pre> <p>Can anyone please tell me how to achieve this in above query, i am trying with temp tables which i dont want. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload