user2975180 user2975180 - 3 months ago 9
SQL Question

Select difference between two row using one table

I have

table-A
, I want to select date range from
2016-06-25
and
2016-06-26
to find the difference in name.

Based on the pic the result should be like that:

2016-06-26 joe 40


I guess the query some thing like that:

SELECT * FROM `2g`
WHERE 'Date' between '2016-06-25' AND 'Date' '2016-06-26'
and (`name` != `name`)


enter image description here

Answer

Please use My SQL query hope this work for you.

with x as   (select  *,k = COUNT(*)
            over(PARTITION BY Name,Age )
            from 2g)

select [Date]
      ,[Name]
      ,[Age] from x
where k <= 1 and Date between '2016-06-25' AND '2016-06-26' 

enter image description here

Comments