MYSQL 存储过程 - 连接行值(续)

时间:2020-12-28 21:32:30

标签: mysql

更新/回答 为了解决这个问题,我在比较运算符的每一侧添加了关键字 BINARY。这产生了预期的结果。另请注意,我将变量设置为空字符串。将此设置为 NULL 并没有解决问题。

SET @previousstate = '';
SELECT  
if(**BINARY** @previousstate != **BINARY** frm.fi_details_temp.PortfolioCode,

结束更新/回答

这与MYSQL Stored Procedure - Concat Row Value有些相关。该线程基于 Mysql 5.7。

以下语句在 MySQL 5.7 下有效。我正在尝试让它在 MySql 8 中工作。我不得不将 SET 语句更新为“SET @previousstate = NULL;”否则我会得到“操作'<>'的排序规则(utf8mb4_0900_ai_ci,IMPLICIT)和(utf8mb4_unicode_ci,IMPLICIT)的非法混合”。这个错误远远超出了我的理解。我只是在一家小公司工作的营销人员。所以这就是我将它设置为 NULL 的原因。

话虽如此,在更新 SET 语句后,结果集返回,但 if() 语句不像在 5.7 下那样工作。返回的第一行应该在第一列数据的开始和结束处用'*'标记,并且当列中的数据发生变化时也应该标记它。

所以它应该是这样的:

*brownfdn*
brownfdn
*brownfmi*
brownfmi

但我得到了这个:

brownfdn
brownfdn
brownfmi
brownfmi

你对我做错了什么有任何想法吗?我感谢您的帮助!!使用 MySql 8 一直很困难。

5.7

CREATE table fi_details_temp AS (
SELECT
    frm.fi_details.PortfolioCode,
    frm.fi_details.MaturityDate_Final
From
    frm.fi_details
Where
    frm.fi_details.manager = 'Bartz' And
    frm.fi_details.PortfolioCode Like ('Crown%')
Group By
frm.fi_details.PortfolioCode,
frm.fi_details.MaturityDate_Final

Order By
    frm.fi_details.PortfolioCode;
 
SET @previousstate = '';

SELECT  
if(@previousstate != frm.fi_details_temp.PortfolioCode, concat('*',@previousstate:= frm.fi_details_temp.PortfolioCode,'*'), frm.fi_details_temp.PortfolioCode) as 'Portfolio Code',
    frm.fi_details_temp.MaturityDate_Final as 'Maturity Date'
From    frm.fi_details_temp;
DROP TABLE IF EXISTS fi_details_temp;
END

*** 创建语句 - 以上是精简版 ***

CREATE TABLE `fi_details` (
  `id` int NOT NULL AUTO_INCREMENT,
  `RecordDate` datetime DEFAULT CURRENT_TIMESTAMP,
  `PortfolioCode` varchar(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `manager` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `RunDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `RunDate_Final` date DEFAULT NULL,
  `AsOfDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `AsOfDate_Final` date DEFAULT NULL,
  `Symbol` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `Coupon` double DEFAULT NULL,
  `MaturityDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `MaturityDate_Final` date DEFAULT NULL,
  `ParValue` double DEFAULT NULL,
  `MarketValue` double DEFAULT NULL,
  `AdjustedUnitCost` double DEFAULT NULL,
  `TotalAdjustedCost` double DEFAULT NULL,
  `SPRating` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `MoodyRating` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `YieldCost` double DEFAULT NULL,
  `YieldMarket` double DEFAULT NULL,
  `YieldCall` double DEFAULT NULL,
  `YieldPut` double DEFAULT NULL,
  `YieldWorst` double DEFAULT NULL,
  `WorstDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `WorstDate_Final` date DEFAULT NULL,
  `CallDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `CallDate_Final` date DEFAULT NULL,
  `CallPrice` double DEFAULT NULL,
  `PutDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `PutDate_Final` date DEFAULT NULL,
  `PutPrice` double DEFAULT NULL,
  `FirstCouponDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `FirstCouponDate_Final` date DEFAULT NULL,
  `LastCouponDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `LastCouponDate_Final` date DEFAULT NULL,
  `Freq` int DEFAULT NULL,
  `BondCalendarCode` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `SecTypeCode` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `Security` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_fi_details_MaturityDate_Final` (`MaturityDate_Final`),
  KEY `idx_fi_details_PortfolioCode` (`PortfolioCode`),
  KEY `idx_fi_details_PortfolioCode_MaturityDate_Final` (`PortfolioCode`,`MaturityDate_Final`),
  KEY `idx_fi_details_manager_MaturityDate_Final` (`manager`,`MaturityDate_Final`),
  KEY `idx_fi_details_MaturityDate_Final_manager` (`MaturityDate_Final`,`manager`)
) ENGINE=InnoDB AUTO_INCREMENT=234850 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE DEFINER=`root`@`localhost` PROCEDURE `fi_details_Worst_Date_Hartz_Brown`()
BEGIN
DROP TABLE IF EXISTS fi_details_temp;
CREATE table fi_details_temp AS (
SELECT
    frm.fi_details.PortfolioCode,
    frm.fi_details.MaturityDate_Final,
    frm.fi_details.YieldWorst,
    frm.fi_details.WorstDate_Final,
    frm.fi_details.YieldMarket,
    frm.fi_details.Symbol,
    frm.fi_details.Security,
    frm.fi_details.Coupon,
    frm.fi_details.ParValue,
    frm.fi_details.MarketValue,
    frm.fi_details.TotalAdjustedCost,
    frm.fi_details.AdjustedUnitCost,
    frm.fi_details.YieldCost,
    frm.fi_details.YieldCall,
    frm.fi_details.YieldPut,
    frm.fi_details.CallDate_Final
From
    frm.fi_details
Where
    frm.fi_details.manager = 'Hartz' And
    frm.fi_details.PortfolioCode Like ('Brown%')
Group By
frm.fi_details.PortfolioCode,
    frm.fi_details.MaturityDate_Final,
    frm.fi_details.YieldWorst,
    frm.fi_details.WorstDate_Final,
    frm.fi_details.YieldMarket,
    frm.fi_details.Symbol,
    frm.fi_details.Security,
    frm.fi_details.Coupon,
    frm.fi_details.ParValue,
    frm.fi_details.MarketValue,
    frm.fi_details.TotalAdjustedCost,
    frm.fi_details.AdjustedUnitCost,
    frm.fi_details.YieldCost,
    frm.fi_details.YieldCall,
    frm.fi_details.YieldPut,
    frm.fi_details.CallDate_Final
Order By
    frm.fi_details.PortfolioCode,
    frm.fi_details.WorstDate_Final );
 
SET @previousstate = NULL;
SELECT  
if(@previousstate != frm.fi_details_temp.PortfolioCode, concat('*',@previousstate:= frm.fi_details_temp.PortfolioCode,'*'), frm.fi_details_temp.PortfolioCode) as 'Portfolio Code',
    frm.fi_details_temp.MaturityDate_Final as 'Maturity Date',
    frm.fi_details_temp.YieldWorst as 'Worst Yield',
    frm.fi_details_temp.WorstDate_Final as 'Worst Date',
    frm.fi_details_temp.YieldMarket as 'Yield to Market',
    frm.fi_details_temp.Symbol,
    frm.fi_details_temp.Security as 'Description',
    frm.fi_details_temp.Coupon,
    frm.fi_details_temp.ParValue as 'Par Value',
    frm.fi_details_temp.MarketValue as 'Market Value xAI',
    frm.fi_details_temp.TotalAdjustedCost as 'Adjusted Cost',
    frm.fi_details_temp.AdjustedUnitCost as 'Unit Adjusted Cost',
    frm.fi_details_temp.YieldCost as 'Yield to Cost',
    frm.fi_details_temp.YieldCall as 'Yield to Call',
    frm.fi_details_temp.YieldPut as 'Yield to Put',
    frm.fi_details_temp.CallDate_Final as 'Next Call Date'
From    frm.fi_details_temp;
DROP TABLE IF EXISTS fi_details_temp;
END

0 个答案:

没有答案