sql - How to select multi record depending on some column's condition? -
say there sql server table contain 2 columns: id, value
the sample data looks this:
id value ------------------ 1 30 1 30 2 50 2 50 3 50
when run query:
select id, newid(), value table1 order id
the result looks this:
1 30 e152ad19-9920-4567-87ff-c4822fd9e485 1 30 54f28c58-aba9-4dfb-9a80-ce9c4c390cbb 2 50 ........ 2 50 ........ 3 50 4e5a9e26-feec-4cc7-9ac5-96747053b6b2
but want : how many record of id depending on (sum of value /30 )'s result, example of id 2, it's value's sum 50+50=100, , 100/30=3, id 2 display in query result 3 times
the final result want this:
1 e152ad19-9920-4567-87ff-c4822fd9e485 1 54f28c58-aba9-4dfb-9a80-ce9c4c390cbb 2 4e5a9e26-feec-4cc7-9ac5-96747053b6b2 2 .... 2 .... 3 d861563e-e01a-4198-9e92-7beb4678e5d1
please note id of 2 display 3 times, wait helps, thanks.
how like
create table table1 ([id] int, [value] int) ; insert table1 ([id], [value]) values (1, 30), (1, 30), (2, 50), (2, 50), (3, 50) ; ;with summedvals ( select id, sum(value) / 30 cnt table1 group id ) , vals ( select id, cnt - 1 cnt summedvals union select id, cnt - 1 cnt vals cnt > 0 ) select id, newid() vals order 1
Comments
Post a Comment