2012-06-09 113 views
0

我對SQL很新,很好,我還沒有使用它在8年左右。這是在使用SQL Server的餐廳銷售點系統中完成的。總之,這裏是我當前的代碼:從日期時間戳分開的日期,然後按日期顯示總計

select cashier_id,grand_total, num_people_party, datetime 
from invoice_totals 
where datetime >= '2012-06-01' 

最後,我想顯示一個報告,其中列出的總人數服務(num_people_served)和總計(grand_total)收銀員ID(cashier_id)對於每一日期。

所以:

Cashier ID 223 
2012-06-01 10 200.00 
2012-06-02 23 245.00 

Cashier ID 224 
2012-06-01 15 123.00 
2012-06-02 24 254.00 

etc... 

這應該不是太難,但我不記得我的搜索還沒有證明是有用的。此外,應該注意DateTime字段將需要拆分,以便按日期對總計進行分組。


在從下面的評論添加dividion場,我碰到下面的錯誤,一旦它被添加:

Message: Could not retrieve datatable. 
SELECT CASHIER_ID AS SERVER, CONVERT(VARCHAR(10),[DATETIME],111) AS DATE, SUM(GRAND_TOTAL) AS TOTAL_SALES, SUM(NUM_PEOPLE_PARTY) AS NUMBER_SERVED, SUM(GRAND_TOTAL)/SUM(NUM_PEOPLE_PARTY) AS CASHPERCUSTOMER 
FROM INVOICE_TOTALS 
WHERE [DATETIME] >= '2012-06-01' 
GROUP BY CASHIER_ID, CONVERT(VARCHAR(10),[DATETIME],111) 
Stack Trace: at PCA_DataMod.PCA_DB.OpenDT(String SQLLoadInfo, Boolean NoSchema) 
    at PCA_DataMod.PCA_DB.OpenDT(String SQLLoadInfo) 
    at pcAmerica.Desktop.POS.Forms.frmReportCustomWiz.GetRawSQLFields() 
    at pcAmerica.Desktop.POS.Forms.frmReportCustomWiz.Display_Step(WizardStep Index) 

Message: Divide by zero error encountered. 
Stack Trace: at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) 
    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) 
    at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 
    at System.Data.SqlClient.SqlDataReader.HasMoreRows() 
    at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout) 
    at System.Data.SqlClient.SqlDataReader.Read() 
    at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping) 
    at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue) 
    at System.Data.Common.DataAdapter.Fill(DataSet dataSet, String srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords) 
    at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) 
    at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) 
    at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) 
    at PCA_DataMod.PCA_DB.OpenDT(String SQLLoadInfo, Boolean NoSchema) 

任何想法?

+0

可能重複http://stackoverflow.com/questions/133081/most-efficient- sql-server-to-date-date-date-datetime) –

回答

2

你可以group by收銀員和天:

select  cashier_id 
,   dateadd(d, 0, datediff(d, 0, [datetime])) 
,   sum(grand_total) 
,   sum(num_people_party) 
from  invoice_totals 
where  [datetime] >= '2012-06-01' 
group by 
      cashier_id 
,   dateadd(d, 0, datediff(d, 0, [datetime])) 

在SQL Server 2008及更高版本,可以替換

dateadd(d, 0, datediff(d, 0, [datetime])) 

cast([datetime] as date) 
+0

這似乎很好,有幾個後續步驟: - 如何更改報告中列的名稱,它當前顯示作爲CASHIER_ID第1列第2列第3列? - 是否有一種方法可以省略顯示時間,它目前顯示每個記錄的日期,然後是每天12:00:00 AM的時間? - 如何添加另外1列,將總數除以每條記錄的服務人數? – tmparisi

+0

1,在結尾添加'as alias',比如'sum(grand_total)'作爲total'。 2.用convert(varchar(10),[datetime],111)替換'dateadd(d,0,datediff(d,0,[datetime]))''3. sum(grand_total)/ sum(num_people_party)作爲CashPerCustomer' – Andomar

+0

其中的3個完美的工作!但是,分界線似乎會引發錯誤。我更新了原始問題以顯示拋出的錯誤消息。我非常感謝你的幫助! – tmparisi

0

您可以將通過日期部分:

select cashier_id,SUM(grand_total), SUM(num_people_party), DATEPART(dd,[datetime]) AS Day, DATEPART(mm,[datetime]) AS Month, DATEPART(yyyy,[datetime]) AS Year 
from invoice_totals 
where datetime >= '2012-06-01' 
group by cashier_id, DATEPART(dd,[datetime]), DATEPART(mm,[datetime]), DATEPART(yyyy,[datetime]) 

或者更簡單地說

select cashier_id, SUM(grand_total), SUM(num_people_party), DAY([datetime]) AS Day, MONTH([datetime]) AS Month, YEAR([datetime]) AS Year 
from invoice_totals 
where datetime >= '2012-06-01' 
group by cashier_id, DAY([datetime]), MONTH([datetime]), YEAR([datetime]) 
的[SQL Server中最有效的方式來獲得日期+時間日期?(