0

我对 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_totalcashier_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)

有任何想法吗?

4

2 回答 2

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)
于 2012-06-09T16:43:31.460 回答
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])
于 2012-06-09T16:46:40.860 回答