我对 SQL 很陌生,好吧,我已经 8 年左右没有使用它了。这是在使用 SQL Server 的餐厅销售点系统中完成的。无论如何,这是我当前的代码:
select cashier_id,grand_total, num_people_party, datetime
from invoice_totals
where datetime >= '2012-06-01'
最终,我想显示一份报告,其中列出了每个日期的收银员 ID ( )服务的总人数 ( num_people_served
) 和总计 ( )。grand_total
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
字段需要拆分才能按日期对总计进行分组。
在从下面的评论中添加除法字段时,添加后出现以下错误:
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)
有任何想法吗?