简体   繁体   中英

Want to select all rows except first row of every customer_id

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

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

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM