从DateTime标记单独的日期,然后按日期总计显示

时间:2012-06-09 16:38:04

标签: sql-server

我对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字段需要拆分才能按日期对总计进行分组。


在添加下面评论中的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)

有什么想法吗?

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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])