将 DISTINCT 与 ORDER BY 子句一起使用
Use DISTINCT with ORDER BY clause
我想改变我的以下存储过程
ALTER Procedure [dbo].[spEGCRedemptionReportForMHR]
@DateTo datetime,
@DateFrom datetime,
@MerchantID varchar(11),
@Pan varchar(16)
As
Set @DateTo = @DateTo +1
Select Distinct
Convert(varchar(50),pt.TransactionDate,103) 'TransactionDate',
m.MerchantName1 MerchantName,
m.MerchantAddress Location,
m.MerchantID,
pt.TerminalID,
pt.batchnumber 'Batch #',
pt.SequenceNumber 'Receipt #',
pt.PAN 'Card Number',
c.EmbossName 'Card Holder Name',
Convert(Decimal(10,2),Case when pt.TransactionTypeID=2 then (pt.TotalAmount) end) As 'Points Redeemed',
Convert(Decimal(10,2),Case when pt.TransactionTypeID=2 then (((pt.TotalAmount)/(cc.usdconversionrate))/2) end) as 'Total Payment Amount (AED)', --/cc.USDConversionRate end) As 'Total Amount in AED',
Convert(Decimal(10,2),Case when pt.TransactionTypeID=2 then (((pt.TotalAmount)/(cc.usdconversionrate))/2) -15 end) as 'Total loaded Amount (AED)',
3.00 as 'Procco Share',
Convert(Decimal(10,2),Case when pt.TransactionTypeID=2 then (((pt.TotalAmount)/(cc.usdconversionrate))/2) - 3 end) as 'Settlement Amount'
from POS_Transactions pt
inner join Terminal t on t.TerminalID=pt.TerminalID
inner join Merchant m on m.MerchantID=t.MerchantID
inner join Card c on c.EmbossLine=pt.PAN
inner join Share s on s.MerchantID=m.MerchantID,Currency cc
where IsEmaar =1 and
cc.CurrencyCode='AED'
--and m.isemaarmerchant = 1
and (m.MerchantID=@MerchantID or @MerchantID='-999')
and (pt.TransactionDate>=@datefrom and pt.TransactionDate<=@dateto)
and (pt.PAN=@Pan or @Pan ='-999')
order by pt.TransactionDate
但是每次我尝试执行它时它都会抛出一个错误
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
我已经在我的 select 中使用了 pt.TransactionDate
但它仍然要求我包括它,因为它在我的 order by 条款中。我的查询可能有什么问题?
是的,pt.TransactionDate 列出现在 SELECT 中,但是在 CONVERT 函数内部,如下所示:
CONVERT(VARCHAR(50), pt.TransactionDate, 103) 'TransactionDate',
单独添加 pt.TransactionDate 作为额外的列,如下所示:
...
SELECT DISTINCT
CONVERT(VARCHAR(50), pt.TransactionDate, 103) 'TransactionDate',
pt.TransactionDate, -- <-- here
m.MerchantName1 MerchantName,
m.MerchantAddress Location,
m.MerchantID,
pt.TerminalID,
....
尝试:
ORDER BY 'TransactionDate'
这个错误没有多大帮助,但是 ughai 的评论是正确的。引擎对SELECT语句中列的修改方式有讲究,"ORDER BY."
中必须包含准确的形式
出于输出原因连接列时,我已经 运行 加入了这个,但它很快就变得丑陋了。
你必须
ORDER BY Convert(varchar(50),pt.TransactionDate,103)
如果您使用 GROUP BY 而不是 DISTINCT 来消除重复项,那么您可以包括分组字段,无论它们是否在 select 列表中。缺点是您必须列出 所有 使用的列,但这样做就可以了。
GROUP BY
pt.TransactionDate,
m.MerchantName1,
m.MerchantAddress ,
m.MerchantID,
pt.TerminalID,
pt.batchnumber,
pt.SequenceNumber,
pt.PAN ,
c.EmbossName,
pt.TransactionTypeID,
pt.TotalAmount,
cc.usdconversionrate
ORDER BY pt.TransactionDate
我想改变我的以下存储过程
ALTER Procedure [dbo].[spEGCRedemptionReportForMHR]
@DateTo datetime,
@DateFrom datetime,
@MerchantID varchar(11),
@Pan varchar(16)
As
Set @DateTo = @DateTo +1
Select Distinct
Convert(varchar(50),pt.TransactionDate,103) 'TransactionDate',
m.MerchantName1 MerchantName,
m.MerchantAddress Location,
m.MerchantID,
pt.TerminalID,
pt.batchnumber 'Batch #',
pt.SequenceNumber 'Receipt #',
pt.PAN 'Card Number',
c.EmbossName 'Card Holder Name',
Convert(Decimal(10,2),Case when pt.TransactionTypeID=2 then (pt.TotalAmount) end) As 'Points Redeemed',
Convert(Decimal(10,2),Case when pt.TransactionTypeID=2 then (((pt.TotalAmount)/(cc.usdconversionrate))/2) end) as 'Total Payment Amount (AED)', --/cc.USDConversionRate end) As 'Total Amount in AED',
Convert(Decimal(10,2),Case when pt.TransactionTypeID=2 then (((pt.TotalAmount)/(cc.usdconversionrate))/2) -15 end) as 'Total loaded Amount (AED)',
3.00 as 'Procco Share',
Convert(Decimal(10,2),Case when pt.TransactionTypeID=2 then (((pt.TotalAmount)/(cc.usdconversionrate))/2) - 3 end) as 'Settlement Amount'
from POS_Transactions pt
inner join Terminal t on t.TerminalID=pt.TerminalID
inner join Merchant m on m.MerchantID=t.MerchantID
inner join Card c on c.EmbossLine=pt.PAN
inner join Share s on s.MerchantID=m.MerchantID,Currency cc
where IsEmaar =1 and
cc.CurrencyCode='AED'
--and m.isemaarmerchant = 1
and (m.MerchantID=@MerchantID or @MerchantID='-999')
and (pt.TransactionDate>=@datefrom and pt.TransactionDate<=@dateto)
and (pt.PAN=@Pan or @Pan ='-999')
order by pt.TransactionDate
但是每次我尝试执行它时它都会抛出一个错误
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
我已经在我的 select 中使用了 pt.TransactionDate
但它仍然要求我包括它,因为它在我的 order by 条款中。我的查询可能有什么问题?
是的,pt.TransactionDate 列出现在 SELECT 中,但是在 CONVERT 函数内部,如下所示:
CONVERT(VARCHAR(50), pt.TransactionDate, 103) 'TransactionDate',
单独添加 pt.TransactionDate 作为额外的列,如下所示:
...
SELECT DISTINCT
CONVERT(VARCHAR(50), pt.TransactionDate, 103) 'TransactionDate',
pt.TransactionDate, -- <-- here
m.MerchantName1 MerchantName,
m.MerchantAddress Location,
m.MerchantID,
pt.TerminalID,
....
尝试:
ORDER BY 'TransactionDate'
这个错误没有多大帮助,但是 ughai 的评论是正确的。引擎对SELECT语句中列的修改方式有讲究,"ORDER BY."
中必须包含准确的形式出于输出原因连接列时,我已经 运行 加入了这个,但它很快就变得丑陋了。
你必须
ORDER BY Convert(varchar(50),pt.TransactionDate,103)
如果您使用 GROUP BY 而不是 DISTINCT 来消除重复项,那么您可以包括分组字段,无论它们是否在 select 列表中。缺点是您必须列出 所有 使用的列,但这样做就可以了。
GROUP BY
pt.TransactionDate,
m.MerchantName1,
m.MerchantAddress ,
m.MerchantID,
pt.TerminalID,
pt.batchnumber,
pt.SequenceNumber,
pt.PAN ,
c.EmbossName,
pt.TransactionTypeID,
pt.TotalAmount,
cc.usdconversionrate
ORDER BY pt.TransactionDate