-2

I have the table customer which conatins data of customer in multiple months.

My task is to show the repeated customer with there details in which i need the fetch the rows from second occurrences but the my query is fetching the first occurrence also Customer table

and i want a query to get this result. Result

  • 1
    Please don’t link to images, add all relevant information directly to your question preferably as editable text – NickW Dec 15 '22 at 11:11

2 Answers2

0

SELECT * FROM ( SELECT ROW_NUMBER()OVER(PARTITION BY customer_id ORDER BY Id ASC) Sno,* FROM @tbl ) a WHERE a.Sno > 1

Ravi Sharma
  • 362
  • 1
  • 5
0

You can try something like this

SELECT *
FROM table_name
WHERE customer_id IN (
  SELECT customer_id
  FROM table_name
  WHERE customer_id IS NOT NULL
  GROUP BY customer_id
  HAVING COUNT(*) > 1
)
AND (customer_id, transaction_date) NOT IN (
  SELECT customer_id, MIN(transaction_date)
  FROM table_name
  WHERE customer_id IS NOT NULL
  GROUP BY customer_id
)