Home > Blockchain >  Want to select all rows except first row of every customer_id
Want to select all rows except first row of every customer_id

Time:12-16

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

CodePudding user response:

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

CodePudding user response:

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
)
  • Related