将值分组到标题 MSSQL 下
我想将我的数据记录分组在不同的“staff_id”下。
我的没有 GROUP BY 的查询通过返回普通表中的所有所需行来完美地工作。我将它输出到一个 asp.net ListView 中,它看起来像这样:
Staff_ID Field1 Field2 Field3 Field4 ...........
Staff_ID Field1 Field2 Field3 Field4 ...........
Staff_ID Field1 Field2 Field3 Field4 ...........
现在我试图让它们通过它们的 id 一起显示在一个 asp.net ListView 组中。 例如,像这样:
Staff_ID TOTALAMT
Field1 Field2 Field3 Field4 Field5
Field1 Field2 Field3 Field4 Field5
Staff_ID TOTALAMT
Field1 Field2 Field3 Field4 Field5
Field1 Field2 Field3 Field4 Field5
无论如何都要这样做吗?我尝试了如下所示的 GROUP BY 但出现错误:
Msg 8120, Level 16, State 1, Line 4
Column 'CLAIM_APPLICATION.CREATE_DATE' is invalid in the select list because
it is not contained in either an aggregate function or the GROUP BY clause.
这是我的查询:
SELECT
'' Seq,
'' Doc_Type,
left(CONVERT(varchar, A.CREATE_DATE, 112),8)Post_date,
left(CONVERT(varchar, A.CREATE_DATE, 112),8)Doc_date,
('S.C-'+left(CONVERT(varchar, A.CREATE_DATE, 112),6)+'-'+ A.user_id)Header,
('S.Claim-'+left(CONVERT(varchar, A.CREATE_DATE, 112),6))Header_Ref,
'' Trx_date,
'MYR' Curr,
'' Exh_rate,
'' Line_no,
'S' CR_DB,
'' Nega_post,
C.GL,
A.AMT,
'' Loc_curr,
'SC-'+ A.CLINIC_SHOP Text1,
B.staff_id,
''A,''B,''C,''D,
'I0'Tax_code,''WBS,''E,
A.cost_centre,
''F,
'0003' Pay_term,
left(CONVERT(varchar, getdate(), 112),8)Base_date
FROM CLAIM_APPLICATION AS A INNER JOIN
CLAIM_USER_DETAIL AS B ON A.user_id = B.user_id INNER JOIN
CLAIM_TYPE AS C ON A.CLAIM_ID = C.CLAIM_ID
WHERE (A.CLAIM_ID NOT IN (20, 21))
and (CONVERT(varchar, A.CREATE_DATE, 111) >= '2011/01/01')
and (CONVERT(varchar, A.CREATE_DATE, 111) <= '2011/04/30')
and (A.STATUS ='A')
-- and A.REF_NO <> 'MYSGCB'
-- and (A.user_id <>'bkkhoo')
GROUP BY B.staff_id
ORDER BY B.name
I want to group my record of data under different "staff_id".
My query without the GROUP BY works flawlessly by returning all desired rows in a normal table. I output it in an asp.net ListView and it looks like this like this:
Staff_ID Field1 Field2 Field3 Field4 ...........
Staff_ID Field1 Field2 Field3 Field4 ...........
Staff_ID Field1 Field2 Field3 Field4 ...........
Now I am trying to make them display in an asp.net ListView group together by their id.
For example, like this:
Staff_ID TOTALAMT
Field1 Field2 Field3 Field4 Field5
Field1 Field2 Field3 Field4 Field5
Staff_ID TOTALAMT
Field1 Field2 Field3 Field4 Field5
Field1 Field2 Field3 Field4 Field5
Anyway to do that? I tried the GROUP BY as shown below but i get an error:
Msg 8120, Level 16, State 1, Line 4
Column 'CLAIM_APPLICATION.CREATE_DATE' is invalid in the select list because
it is not contained in either an aggregate function or the GROUP BY clause.
Here's my query:
SELECT
'' Seq,
'' Doc_Type,
left(CONVERT(varchar, A.CREATE_DATE, 112),8)Post_date,
left(CONVERT(varchar, A.CREATE_DATE, 112),8)Doc_date,
('S.C-'+left(CONVERT(varchar, A.CREATE_DATE, 112),6)+'-'+ A.user_id)Header,
('S.Claim-'+left(CONVERT(varchar, A.CREATE_DATE, 112),6))Header_Ref,
'' Trx_date,
'MYR' Curr,
'' Exh_rate,
'' Line_no,
'S' CR_DB,
'' Nega_post,
C.GL,
A.AMT,
'' Loc_curr,
'SC-'+ A.CLINIC_SHOP Text1,
B.staff_id,
''A,''B,''C,''D,
'I0'Tax_code,''WBS,''E,
A.cost_centre,
''F,
'0003' Pay_term,
left(CONVERT(varchar, getdate(), 112),8)Base_date
FROM CLAIM_APPLICATION AS A INNER JOIN
CLAIM_USER_DETAIL AS B ON A.user_id = B.user_id INNER JOIN
CLAIM_TYPE AS C ON A.CLAIM_ID = C.CLAIM_ID
WHERE (A.CLAIM_ID NOT IN (20, 21))
and (CONVERT(varchar, A.CREATE_DATE, 111) >= '2011/01/01')
and (CONVERT(varchar, A.CREATE_DATE, 111) <= '2011/04/30')
and (A.STATUS ='A')
-- and A.REF_NO <> 'MYSGCB'
-- and (A.user_id <>'bkkhoo')
GROUP BY B.staff_id
ORDER BY B.name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 SQL Server 查询中使用 GROUP BY 并不能解决您的问题 - 您需要原始查询返回的所有数据。所需的分组不是在数据级别,而是在显示数据时进行。 Listview确实支持分组 - 请参阅此 多部分文章,说明如何根据您的需要进行分组(具体请参阅标题为 "按数据字段分组")。
Using
GROUP BY
in SQL Server query will not solve your problem - you need all data returned by original query. The desired grouping is not at data level but while displaying the data. Listview does support grouping - see this multi-part article that illustrates how to do the grouping as desired by you (specifically see part titled "Grouping By a Data Field").首先,您收到的错误是因为 group by 子句中的所有列都必须位于 select 子句中(如果它不是聚合列)。
然而,这不会给你你想要的结果。您应该做的是在数据网格中添加摘要行,这应该给出你想要什么。
另请参阅此处,了解不同选项的详细概述。
Firstly, the error you are getting is because all the columns in your group by clause must be in the select clause if it's not an aggregate column.
This however will not give you your desired result. What you should do is add summary rows in your datagrid, this should give you what you want.
Also look here for a good overview of your different options.