MYSQL Stored Procedure - Concat Row Value

拈花ヽ惹草 提交于 2021-01-29 05:19:50

问题


Update

Sorry for the delay in responding. I was traveling yesterday when I sent that in. I am on version 5.6. This is what I was thinking of and it does mark when there is change, but the issue is the sort causes the result set to fall out of order. I guess I will need to create a temporary table and just apply the conditional formatting once the order has been set.

SET @previousstate = '';

SELECT frm.fi_details.PortfolioCode AS 'Portfolio Code', id, if(@previousstate != frm.fi_details.PortfolioCode, concat('***',@previousstate:= frm.fi_details.PortfolioCode,'****'), frm.fi_details.PortfolioCode) AS test

FROM frm.fi_details

WHERE frm.fi_details.manager = 'Shartz' And frm.fi_details.PortfolioCode Like ('Brown%')

GROUP BY frm.fi_details.PortfolioCode, frm.fi_details.Symbol

ORDER BY frm.fi_details.PortfolioCode, frm.fi_details.WorstDate_Final;

brownfmi 143753 brownfmi

brownfmi 143748 brownfmi

brownfmi 143744 brownfmi

brownfmi 143739 brownfmi

brownfmi 143740 brownfmi

brownfmi 143738 brownfmi

brownfmi 143747 brownfmi

brownfmi 143749 brownfmi

brownfmi 143761 brownfmi

brownhcb 143769 brownhcb

brownhcb 143774 brownhcb

brownhcb 143780 brownhcb

brownhcb 143781 brownhcb

brownhcb 143773 brownhcb

brownhcb 143784 brownhcb

brownhcb 143783 brownhcb

brownhcb 143785 brownhcb

brownhcb 143786 brownhcb

brownhcb 143762 ****brownhcb****

brownhcb 143763 brownhcb brownhcb 143788 brownhcb brownhcb 143777 brownhcb brownhcb 143767 brownhcb brownhcb 143764 brownhcb brownhcb 143779 brownhcb brownhcb 143778 brownhcb brownhcb 143789 brownhcb brownhcb 143772 brownhcb brownhfi 143799 brownhfi brownhfi 143808 brownhfi brownhfi 143805 brownhfi brownhfi 143806 brownhfi brownhfi 143793 brownhfi brownhfi 143795 brownhfi brownhfi 143797 brownhfi brownhfi 143791 brownhfi brownhfi 143801 brownhfi brownhfi 143807 brownhfi brownhfi 143798 brownhfi brownhfi 143804 brownhfi brownhfi 143796 brownhfi brownhfi 143790 brownhfi brownhfi 143800 brownhfi brownhfi 143794 brownhfi brownhfi 143802 brownhfi brownhfi 143792 brownhfi brownhfi 143803 brownhfi brownira 143809 brownira browntrf 143838 browntrf browntrf 143860 browntrf browntrf 143905 browntrf browntrf 143891 browntrf browntrf 143846 browntrf browntrf 143877 browntrf browntrf 143857 browntrf browntrf 143872 browntrf browntrf 143834 browntrf browntrf 143898 browntrf browntrf 143841 browntrf browntrf 143817 browntrf browntrf 143826 browntrf browntrf 143822 browntrf browntrf 143868 browntrf browntrf 143883 browntrf browntrf 143899 browntrf browntrf 143840 browntrf browntrf 143900 browntrf browntrf 143850 browntrf browntrf 143869 browntrf browntrf 143865 browntrf browntrf 143837 browntrf browntrf 143844 browntrf browntrf 143851 browntrf browntrf 143901 browntrf browntrf 143833 browntrf browntrf 143856 browntrf browntrf 143870 browntrf browntrf 143875 browntrf browntrf 143887 browntrf browntrf 143903 browntrf browntrf 143828 browntrf browntrf 143847 browntrf browntrf 143820 browntrf browntrf 143890 browntrf browntrf 143889 browntrf browntrf 143867 browntrf browntrf 143866 browntrf browntrf 143830 browntrf browntrf 143896 browntrf browntrf 143836 browntrf browntrf 143852 browntrf browntrf 143876 browntrf browntrf 143904 browntrf browntrf 143874 browntrf browntrf 143813 browntrf browntrf 143854 browntrf browntrf 143815 browntrf browntrf 143878 browntrf browntrf 143842 browntrf browntrf 143897 browntrf browntrf 143863 browntrf browntrf 143879 browntrf browntrf 143880 browntrf browntrf 143849 browntrf browntrf 143816 browntrf browntrf 143862 browntrf browntrf 143858 browntrf browntrf 143831 browntrf browntrf 143832 browntrf browntrf 143835 browntrf browntrf 143873 browntrf browntrf 143848 browntrf browntrf 143839 browntrf browntrf 143827 browntrf browntrf 143864 browntrf browntrf 143823 browntrf browntrf 143895 browntrf browntrf 143884 browntrf browntrf 143871 browntrf browntrf 143810 browntrf browntrf 143824 browntrf browntrf 143821 browntrf browntrf 143812 browntrf browntrf 143892 browntrf browntrf 143881 browntrf browntrf 143861 browntrf browntrf 143829 browntrf browntrf 143819 browntrf browntrf 143811 browntrf browntrf 143894 browntrf browntrf 143818 browntrf browntrf 143882 browntrf browntrf 143843 browntrf browntrf 143859 browntrf browntrf 143855 browntrf browntrf 143885 browntrf browntrf 143902 browntrf browntrf 143893 browntrf browntrf 143886 browntrf browntrf 143845 browntrf browntrf 143825 browntrf browntrf 143888 browntrf browntrf 143853 browntrf browntrf 143814 browntrf


For some reason, I am having trouble thinking through how I would contact a row if it is different than the previous in a stored procedure. For example if I select states from my table, when the select gets to a new state have it append '**' to make it stand out. I know I have to set a variable and if else through it, but everything I tried has failed. :(

Thanks for your help.

For example

**AR**

AR

AR

AR

**CA**

CA

CA

CA


回答1:


You have not shown your existing code, so it is unclear how your question relates to stored procedure. If you are running MySQL 8.0, you can do this a select statement and window functions. To get a consistent result over consecutive executions, you would need a column that defines the ordering of the rows - I assumed id:

select
    id,
    case when row_number() over(partition by state order by id) = 1
        then concat('**', state, '**')
        else state
    end
from mytable
order by state, id


来源:https://stackoverflow.com/questions/62416667/mysql-stored-procedure-concat-row-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!