做分組和總和SQL

[英]Doing Group By and Sum SQL


I am trying to sum one column while grouping another column. SQL consistently
gives me two separate rows.

我試圖在分組另一列時總結一列。 SQL一直給我兩個單獨的行。

Following is my script and my results:

以下是我的腳本和我的結果:

Select A.client, A.store, A.Type as 'Ticket Type', A.[bizdate] as 'DATE',     
sum(cast(round(c.[Amount2],2) as decimal (20,0))) 'QTY', c.SKU, c.[DESC], 
sum(cast(round(c.[Amount1],2)as decimal(20,2))) as 'Total'


From Mydatabase A inner join mydatabase.dbo.TicketHeader B on A.    
[ticket] = b.[ticket] and a.[yyyymmdd] = b.[yyyymmdd]
inner join mydatabase.dbo.ticketdetail C on C.[ticketheaderid]  =   
b.[id] and c.[class] = a.[class]

where  
A.Store = @store
and a.yyyymmdd = @yyyymmdd
and c.class <> 'balance'
and c.Subtype <> 'cancelled'
and a.type NOT IN    
('SKU','Total','CASH','CHANGE','NA','TAX1','TAX2','TAX3','TAX4','CHARGE',     
'Delivery Service', 'Tip')
and c.[DESC] <> 'NA'
and c.sku = '300129'

group by c.sku, c.[DESC],a.store, a.type, a.bizdate, a.client, c.[Amount2],    
c.[Amount1]  
order by [ticket type]

client, Store TicketType         date       qty   SKU    Description  Amount
1      2404  CURBSIDE / TO GO   20170214    1   300129  Beef TACO    2.00
1      2404  DINE IN            20170214    1   300129  Beef TACO    2.00
1      2404  DINE IN            20170214    2   300129  Beef TACO    4.00
1      2404  FOODSBY            20170214    2   300129  Beef TACO    4.00

I am trying to group the DINE IN Beef TACO to one line while summing the
amount.

我正在嘗試將DINE IN Beef TACO分組到一行,同時總結金額。

1 个解决方案

#1


1  

You have several design and code issues here. Things like Amount1 and Amount2 are an indication of possible poor normalization. And aliases A, B, C is a bad habit as it makes maintenance way more difficult than it should be. http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-table-aliases-like-a-b-c-or-t1-t2-t3.aspx

您在此處有幾個設計和代碼問題。 Amount1和Amount2之類的東西表明可能的標准化不佳。別名A,B,C是一個壞習慣,因為它使維護方式比應該更加困難。 http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-table-aliases-like-a-b-c-or-t1-t2-t3.aspx

That aside your code is a mess from a formatting perspective to the point it is painful to work with. Add some white space and get your grouping situated and this should work fine.

除了你的代碼從格式化的角度來看是一團糟,使用起來很痛苦。添加一些空白區域,讓你的分組位於,這應該工作正常。

Select A.client
    , A.store
    , A.Type as 'Ticket Type'
    , A.[bizdate] as 'DATE'
    , sum(cast(round(c.[Amount2],2) as decimal (20,0))) 'QTY'
    , c.SKU
    , c.[DESC]
    , sum(cast(round(c.[Amount1],2)as decimal(20,2))) as 'Total'
From Mydatabase A 
inner join mydatabase.dbo.TicketHeader B on A.[ticket] = b.[ticket] 
                                        and a.[yyyymmdd] = b.[yyyymmdd]
inner join mydatabase.dbo.ticketdetail C on C.[ticketheaderid] = b.[id] and c.[class] = a.[class]
where  
    A.Store = @store
    and a.yyyymmdd = @yyyymmdd
    and c.class <> 'balance'
    and c.Subtype <> 'cancelled'
    and a.type NOT IN    
    (
        'SKU','Total','CASH','CHANGE','NA','TAX1','TAX2','TAX3','TAX4','CHARGE','Delivery Service', 'Tip'
    )
    and c.[DESC] <> 'NA'
    and c.sku = '300129'
group by A.client
    , A.store
    , A.Type 
    , A.[bizdate] 
    , c.SKU
    , c.[DESC]
order by [ticket type]

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/02/15/391f919d15c30e2b7f94311add04ca41.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com