Share via

Over Partition

Bone_12 361 Reputation points
2022-04-21T10:47:38.903+00:00

Hi,

Using the following logic,

select name,ref,a.date,  
	case when lap = '1' and syst = '1' then sys_boolean else 0 end as isnew,  
	case when lap = '2' and syst = '1' then sys_Text else '' end as category  
from [cf].[dbo].[cust] as a  
left join [cf].[dbo].[emp] as b on a.id = b.id  
GO  

my table output shows as follows:

195154-image.png

However, I need the output to show on 1 row as the above is duplicating records:

195162-image.png

Any idea how to do this, please?

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

3 answers

Sort by: Most helpful
  1. Bert Zhou-msft 3,521 Reputation points
    2022-04-22T02:34:43.107+00:00

    Hi,@Bone_12

    Welcome to Microsoft T-SQL Q&A Forum!

    First, when completing this code , you need to explain the data in sys_Text . Your code direction will explain that there are only two kinds of data in this field: sys and spaces .
    If this is the case , you can try to run the following code.

    ;with cte as  
    (  
    select name,ref,a.date,  
         max(case when lap = '1' and syst = '1' then sys_boolean else 0 end) as isnew,  
         case when (lap = '2' and syst = '1') then sys_Text else '' end as category  
     from [cf].[dbo].[cust] as a  
     left join [cf].[dbo].[emp] as b on a.id = b.id  
     group by name,max(case when lap = '1' and syst = '1' then sys_boolean else 0 end),a.date,category  
     )  
     select  name,ref,a.date,isnew, distinct(Replace(category,' ','sys')) from cte  
    

    Just a reminder: if this doesn't do what you want, please attach your DDL statement so we can test it.

    Best regards,
    Bert Zhou


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

  2. Jingyang Li 5,901 Reputation points Volunteer Moderator
    2022-04-21T18:16:23.343+00:00
     select name,ref,a.date,
         Max(case when lap = '1' and syst = '1' then sys_boolean else 0 end) as isnew,
          Max(case when lap = '2' and syst = '1' then sys_Text else '' end) as category
     from [cf].[dbo].[cust] as a
     left join [cf].[dbo].[emp] as b on a.id = b.id
     group by name,ref,a.date
    

    Was this answer helpful?

    0 comments No comments

  3. Ronen Ariely 15,221 Reputation points
    2022-04-21T11:05:43.573+00:00

    Hi,

    (1) Next time when you use code then please use the button for code so the text will be formatted well. I edited your message and did it for you this time :-)
    195125-image.png

    (2) In order to text a solution we need a table and data. Please provide queries to CREATE the table and INSERT some sample data. Next you need to present the expected result set according to the sample data, so we will be able to discuss it without the need to read minds or play "the guessing game"

    (3) In the meantime, In general, you can wrap your code inside a CTE and aggregate the data in the external query

    Read more here about using CTE.

    Finally, there is something not clear in your request. It is simple to aggregate the values by the columns name,ref,a.date and use SUM function to get the aggregated value for the final isnew column, but not cl,ear to me how you chose the value of the category if you have for example two or more different categories

    Please explain the logic to get the final result and don't forget to provide the missing information as mentioned above

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.