Removing Duplicate rows in 1 transaction ID while retaining other transaction item
I have records like this :
Trans_ID ITEM_NAME TOTAL_TRANS 101 a 5 101 a 5 101 &nb开发者_StackOverflow中文版sp; b 5 101 c 5 101 b 5 102 a 3 102 c 3 102 c 3etc (aprox 270k record)
what query should i use to make it like this :
Trans_ID ITEM_NAME VALUE TOTAL_TRANS
101 a 2/5 5 101 b 2/5 5 101 c 1/5 5 102 a 1/3 3 102 c 2/3 3thank you very much
Try this
SELECT
`Trans_ID`,
`ITEM_NAME`,
CONCAT(count(*), '/', `TOTAL_TRANS`) AS VALUE,
`TOTAL_TRANS`
FROM mytable
GROUP BY `ITEM_NAME`, `Trans_ID`, `TOTAL_TRANS`
You can try this
SELECT Trans_ID, ITEM_NAME,
CAST(COUNT(*) AS varchar) + '/' + CAST(TOTAL_TRANS AS VARCHAR) AS VALUE,
TOTAL_TRANS
FROM mytable
GROUP BY Trans_ID, ITEM_NAME, TOTAL_TRANS
I'm making some assumptions about your column data types, but this works for the sample data you provided.
SELECT Trans_ID, ITEM_NAME,
convert(varchar(5), count(*)) + '/' + convert(varchar(5), TOTAL_TRANS) as [VALUE],
TOTAL_TRANS
FROM Trans
GROUP BY Trans_ID, ITEM_NAME, TOTAL_TRANS
精彩评论