Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting Data

Status
Not open for further replies.

stathread

Programmer
Jan 17, 2006
38
US
Does anyone have a way of splitting data in half?
Like placing a 1 & 2 in an empty field.

for instance.

Before:
item count
apples 1000
bannanas 2000


After
item count
apples 500
apples 500
bannanas 1000
bannanas 1000
 
OK, two Easter quickies:
Code:
select item, [count]/2
from myTable
union all
select item, [count]/2
from myTable
order by item

select item, [count]/2
from myTable
cross join
(	select 1 as no union all
	select 2
) blah

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Thank you but what if i dont have a field with the count of how many apples or banannas there are?
 
Create "numbers" table holding integers from 1 to say, 1000. The rest is easy...

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
So I have a field with items and add a column to my table named count. How would i go about placing the count of each item in the count field.

for example

select distinct item,count(*) from my table

item count
cars 200
trucks 350
planes 500

^^ how can i add a field with these values?
 
Now I am confused...

Can you post sample input data and exact expected output - as in original post?

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Sorry,

Here is the input data

Item

Car
Truck
Car
Bus
Bus
Truck
Car
Car
Truck
Truck

Id like to add a field called Count and input how many there are in the list this way I can split it with your above code.

Item Count

Truck 6
Car 4
Truck 6
Car 4
Bus 2
Bus 2
Truck 6
Car 4
Car 4
Truck 6
Truck 6
Truck 6
 
so the finished output would look like this..

Item Count Split

Truck 6 1
Car 4 2
Truck 6 1
Car 4 2
Bus 2 3
Bus 2 3
Truck 6 1
Car 4 2
Car 4 2
Truck 6 1
Truck 6 1
Truck 6 1
 
I see only 4 trucks :p

This should do it:
Code:
select A.item, B.[count]
from myTable A
inner join
(	select item, count(*) as [count]
	from myTable
	group by item
) B
in A.item = B.item


------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
I'm not following it again... why Truck gets 1, Car 2 and Bus 3?

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Ok back on track its still a lil confusing but i used letters this time.

Item Count Split

Truck 6 a
Car 4 c
Truck 6 a
Car 4 c
Bus 2 f
Bus 2 e
Truck 6 a
Car 4 d
Car 4 d
Truck 6 b
Truck 6 b
Truck 6 b
 
If there are no other values to uniquely identify row (primary key or whatever) this is mission impossible...

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top