Click here to Skip to main content
15,435,848 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Team,
I want a query, to find the first and last value in a given table but I want the result by some condition..
here is my table data :


No.         Calweek  volume 
----------  -------  --------
0500000001  202201.  100
0500000001  202202.  100
0500000001  202203   100
0500000144  202204   200
0500000144  202205   200
0500000144  202206   200
0500000144  202207   200
0500000002  202208   300
0500000002  202209   300


here I want My result as like this

No.           First week   last week   volume 
----------    ----------   ---------   ------
0500000001    202201       202203.     100
0500000144    202204       202207      200


Please let me know how we can achieve this output in SQL

What I have tried:

I tried First value and last value but last value getting worng
Posted
Updated 17-Jul-22 10:05am
v2
Comments
Richard Deeming 13-Jul-22 5:52am     CRLF
"I want the result by some condition" And you didn't think it was worth trying to tell us what that condition is? If you can't explain precisely what your requirements are, then you don't understand the problem you are trying to solve. Nobody else can help you until you understand the problem and explain it clearly.

1 solution

You can use ranking functions. See:
About ROW_NUMBER(), RANK(), and DENSE_RANK() Functions in SAP HANA | SAP Blogs[^]
How to pivot/unpivot in SAP HANA | SAP Blogs[^]

Assuming that Calweek is numeric field, something like that should do the job.
SQL
SELECT DISTINCT [No.], 
    MAX([volume]) OVER(PARTITION BY [No.], [volume] ORDER BY [Calweek]) AS [VOL]
    MIN(Calweek) OVER(PARTITION BY [No.], [volume] ORDER BY Calweek,) AS FWEEK,
    MAX(Calweek) OVER(PARTITION BY [No.], [volume] ORDER BY [Calweek] DESC) AS LWEEK,
FROM <YourSchema>.<YourTable>;


Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900