简体   繁体   English

组由一对列sql

[英]group by a pair of a column sql

I have a table in SQL name: pgcPhieuThu 我在SQL名称中有一个表:pgcPhieuThu

在此处输入图片说明

I want to creat a stored procedure for SoPhieu with: + if parameter = 0 then SoPhieu= 'PT-1->n' else parameter = 1 then SoPhieu= 'BC-1->n' 我想使用以下方式为SoPhieu创建存储过程:+如果参数= 0,则SoPhieu ='PT-1-> n';否则,参数= 1,然后SoPhieu ='BC-1-> n'

ALTER proc pgcPhieuThu_TaoSoPhieu3 
 @HinhThuc int,@SoPhieu NVARCHAR(50) OUTPUT
as
declare @MaPT INT
SELECT  @MaPT= ISNULL(SUBSTRING(MAX(pgcPhieuThu.SoPhieu),4, LEN(pgcPhieuThu.SoPhieu)),0) + 1
FROM pgcphieuthu
    GROUP BY SUBSTRING(pgcPhieuThu.SoPhieu,1,3)
SET @SoPhieu = CASE WHEN @HinhThuc = 0 then 'PT-'+ CAST(@MaPT AS NVARCHAR(50))
                    ELSE 'BC-'+ CAST(@MaPT AS NVARCHAR(50))

but Column 'pgcphieuthu.SoPhieu' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. 但是列“ pgcphieuthu.SoPhieu”在选择列表中无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中。

SoPhieu is not an column in the table. SoPhieu不在表格中。 Its an parameter to the stored procedure. 它是存储过程的参数。 so replace the 'SoPhieu' to 'SoPhieu'with in the stored procedure. 因此,请在存储过程中将“ SoPhieu”替换为“ SoPhieu”。 It should work then on. 然后应该继续工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM