Share via

Populate Deviations between 2 rows of table in sql

Sai Roshna 1 Reputation point
2022-06-13T16:55:45.023+00:00

Help me to get the desired output as below for given input

210908-image.png

There are multiple/ Many fruits and staples available. few of them keep added to the table when necessary. i added few items for example (apple, kiwi, banana..... etc the list goes on)

i am looking for a query which displays only the items which were having different qty than previous day displaying both previous day qty and today qty.

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


3 answers

Sort by: Most helpful
  1. Jingyang Li 5,901 Reputation points Volunteer Moderator
    2022-06-13T19:32:03.58+00:00

    Had a hard time to post.
    Here is a link to the solution I posted in gitbub:
    8200076ab67788813ce773146e703a85

    Was this answer helpful?

    2 people found this answer helpful.

  2. Viorel 127K Reputation points
    2022-06-13T17:37:34.477+00:00

    For provided details, the next script seems acceptable:

    drop table if exists #t  
      
    select * into #t from Fruits  
      
    if (select count(distinct Apple) from #t) = 1 alter table #t drop column Apple  
    if (select count(distinct Kiwi) from #t) = 1 alter table #t drop column Kiwi  
    if (select count(distinct Grape) from #t) = 1 alter table #t drop column Grape  
    if (select count(distinct Orange) from #t) = 1 alter table #t drop column Orange  
    if (select count(distinct Banana) from #t) = 1 alter table #t drop column Banana  
      
    select * from #t  
    

    Was this answer helpful?

    1 person found this answer helpful.

  3. Naomi Nosonovsky 8,906 Reputation points
    2022-06-13T17:21:15.607+00:00

    Looks like your output is the same as input just without Kiwi and Banana columns? Am I right or can you explain the logic behind your output?

    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.