我對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)
任何想法?
可能重複http://stackoverflow.com/questions/133081/most-efficient- sql-server-to-date-date-date-datetime) –