我的目标是在每日每月的表格中显示销售数据,总计和其他平均值计算在底部,
我有一个数据结果集,其中一列名为'Day',对应于该月的日期,自动数据类型为int。
select datepart(day, a.date ) as 'Day'
在我的第二个结果集上,是在底部加载和,发生单词'Sum'与Day的列对齐,我使用Union All将结果集合并在一起,预期结果集就像这样
day sales
1 10
2 20
3 30
4 10
5 20
6 30
.
.
.
31 10
Sum 130
我所做的是将最初在int中的day值转换为varchar数据类型。这是为了成功加入列而它确实如此,新的冲突是数字的排序
select * from #SalesDetailed
UNION ALL
select * from #SalesSum
order by location, day
答案 0 :(得分:1)
试试这个
select Day, Sum(Col) as Sales
from #SalesDetailed
Group by Day With Rollup
修改(工作样本):
select
CASE WHEN Day IS NULL THEN 'SUM' ELSE STR(Day) END as Days,
Sum(Sales) from
(
Select 1 as Day , 10 as Sales UNION ALL
Select 2 as Day , 20 as Sales
) A
Group by Day With Rollup
编辑2:
select CASE WHEN Day IS NULL THEN 'SUM' ELSE STR(Day) END as Days,
Sum(Sales) as Sales
from #SalesDetailed
Group by Day With Rollup
答案 1 :(得分:1)
假设您的联合查询返回正确的结果,只是弄乱了订单,您可以在order by子句中使用case
和isnumeric
来操纵您的排序:
SELECT *
FROM
(
SELECT *
FROM #SalesDetailed
UNION ALL
SELECT *
FROM #SalesSum
) u
ORDER BY location,
ISNUMERIC(day) DESC,
CASE WHEN ISNUMERIC(day) = 1 THEN cast(day as int) end
如果day是数字,isnumeric
将返回1,而当数字不是时,Private Function GetPetTypes() As String
Const SUBFORM_NAME As String = "Pets_Infomation"
Const PET_TYPEFIELD As String = "PetType"
Dim strPetList As String
With Me(SUBFORM_NAME).Form.RecordsetClone
If .RecordCount > 0 Then
' Start with first record
.MoveFirst
Do While Not .EOF
If strPetList <> "" Then
strPetList = strPetList & ","
End If
strPetList = strPetList & .Fields(PET_TYPEFIELD)
.MoveNext
Loop
' Go Back to first record in case it needs to be reused
.MoveFirst
End If
End With
GetPetTypes = strPetList
End Function
将返回0。