1

私が使用しているクエリは

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) <> 'AU' then 1 else 0 end [Export],
case when max(oc.Code) <> '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 >= '01-Apr-2013' and j.SaleDate <= '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)

SQLクエリの結果は

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を追加したい)

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

上記のクエリでこれを達成する方法を誰か教えてください。私は望んでいない一時テーブルを試しています。ありがとう。

4

1 に答える 1

2

UNIONこれは、結果の最後に を追加することで実現できます。何かのようなもの:

... order by max(sales.saledate)
UNION
(SELECT "TOTALS", SUM(sales.Domestic) AS Domestic, SUM(sales.Export) AS Export,
SUM(sales.Import) AS Import, SUM(sales.Value) [Value], Sum(sales.Cancelled) AS Cancelled, sum(sales.cancelledValue) AS CancelledValue, SUM(sales.totalValue) AS TotalValue
FROM "your_big_query"...
WHERE ...
)

そして今回は取り外しますgroup by convert(varchar(10), sales.saledate, 103)

編集

または、ステートメントGROUP BY Modifiersでこれを使用するために try を使用できます。GROUP BY

group by convert(varchar(10), sales.saledate, 103) WITH ROLLUP

こちらの公式ドキュメントをいくつかの例とともに読むこともできます。

これがうまくいくことを願っています!!

于 2013-05-07T03:23:09.143 に答える