Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select count values occurrences (2 tables)

Author  Topic 

Van Helsing
Starting Member

11 Posts

Posted - 2009-04-10 : 14:17:00
Hi all

I am a newbie. Here’s my problem, a big problem in fact. I am working with a software on SQL Server
Let’s say that I have two tables.

Table 1(Box,primk,...)
Box
1
1
1
2
2
2
2
2
3
3
...........
..........


Table 2(Equipment,primk,...)
Equipment
Thimble
Thimble
Needle
Taffeta
Needle
Black spool
Scissors
Pattern
Pattern
Ruler
Thimble
Scissors
….
….

Box Equipment
1 Thimble
1 Thimble
1 Needle
1 Taffeta
2 Needle
2 Black spool
2 Scissors
2 Pattern
2 Pattern
2 Ruler
3 Thimble
3 Scissors


What i want to know is the number of tools in each box (with SQL Server and without Excel)
In other words, what I want is the following


Box Thimble Needle Taffeta
1 2 1 1
Box Needle Black spool Scissors Pattern Ruler
2 1 1 1 2 1
Box Thimble Scissors
3 1 1


Or


Box Thimble Needle Taffeta Black spool Scissors Pattern Ruler
1 2 1 1
2 1 1 1 2 1
3 1


In other words, i'd like to know the number of occurrences by box .

If I use this query:
select count (equipment)equipmentnumber,box numberbox
from Table 1 a ,Table 2 b
where a.primk=b.primk
and equipment='scissors'
group by equipment


I have this result
NumberBox NumberEquipment
2 1
3 1



If I use the following query

[code]select count (equipment)equipmentnumber,box numberbox
from Table 1 a ,Table 2 b
where a.primk=b.primk
and equipment='scissors'
group by equipment
union
select count (equipment)equipmentnumber,box numberbox
from Table 1 a ,Table 2 b
where a.primk=b.primk
and equipment='thimble'
group by equipment


I have:

NumberBox NumberEquipment
2 1
3 1

And not
NumberBox NumberEquipment
2 1
3 1
3 1



NumberBox NumberEquipment
2 1(thimble)
3 1(scissors)
3 1(thimble)

In this case, there is one scissors and one thimble in the third box. It’s the same number. That’s why, I have
NumberBox NumberEquipment
2 1
3 1

The second row is eliminated


Thanks in advance

Forgive me for the presentation

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-12 : 13:05:53
[code]SELECT Box,
SUM(CASE WHEN Equipment='Thimble' THEN 1 ELSE 0 END) AS Thimble,
SUM(CASE WHEN Equipment='Needle' THEN 1 ELSE 0 END) AS Needle,
SUM(CASE WHEN Equipment='Taffeta' THEN 1 ELSE 0 END) AS Taffeta,
SUM(CASE WHEN Equipment='Black spool' THEN 1 ELSE 0 END) AS [Black spool],
SUM(CASE WHEN Equipment='Scissors' THEN 1 ELSE 0 END) AS Scissors,
SUM(CASE WHEN Equipment='Pattern' THEN 1 ELSE 0 END) AS Pattern,
SUM(CASE WHEN Equipment='Ruler' THEN 1 ELSE 0 END) AS Ruler
FROM Table1 t1
JOIN Table2 t2
ON t2.primk=t1.primk
GROUP BY Box
[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-12 : 13:06:43
and if you want to dynamically determine the equipments to be returned use

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx
Go to Top of Page

Van Helsing
Starting Member

11 Posts

Posted - 2009-04-12 : 13:27:35
quote:
Originally posted by visakh16

SELECT Box,
SUM(CASE WHEN Equipment='Thimble' THEN 1 ELSE 0 END) AS Thimble,
SUM(CASE WHEN Equipment='Needle' THEN 1 ELSE 0 END) AS Needle,
SUM(CASE WHEN Equipment='Taffeta' THEN 1 ELSE 0 END) AS Taffeta,
SUM(CASE WHEN Equipment='Black spool' THEN 1 ELSE 0 END) AS [Black spool],
SUM(CASE WHEN Equipment='Scissors' THEN 1 ELSE 0 END) AS Scissors,
SUM(CASE WHEN Equipment='Pattern' THEN 1 ELSE 0 END) AS Pattern,
SUM(CASE WHEN Equipment='Ruler' THEN 1 ELSE 0 END) AS Ruler
FROM Table1 t1
JOIN Table2 t2
ON t2.primk=t1.primk
GROUP BY Box




quote:
Originally posted by visakh16

and if you want to dynamically determine the equipments to be returned use

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx




I’ll watch this Tuesday. I keep you informed. Thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-12 : 13:29:43
welcome

hoping to hear from you soon...
Go to Top of Page

kunal.mehta
Yak Posting Veteran

83 Posts

Posted - 2009-04-13 : 07:16:54
Hi,
try this one

declare @table1 table (Item varchar(25),Boxid int )
insert into @table1 values ('Thimble', 1)
insert into @table1 values ('Thimble', 1)
insert into @table1 values ('Needle', 1)
insert into @table1 values ('Taffeta', 1)
insert into @table1 values ('Needle', 2)
insert into @table1 values ('Black spool', 2)
insert into @table1 values ('Scissors', 2)
insert into @table1 values ('Pattern', 2)
insert into @table1 values ('Pattern', 2)
insert into @table1 values ('Ruler', 2)
insert into @table1 values ('Thimble', 3)
insert into @table1 values ('Scissorsl', 3)

select boxid,[Black spool],
[Needle],
[Pattern],
[Ruler],
[Scissors],
[Scissorsl],
[Taffeta],
[Thimble]
from
(
select boxid,item from @table1
) s
pivot
(count(item) for item in ([Black spool],[Needle],[Pattern],[Ruler],[Scissors],
[Scissorsl],[Taffeta],[Thimble])) p

Kunal
Go to Top of Page

Van Helsing
Starting Member

11 Posts

Posted - 2009-04-15 : 11:06:26
Hi
I tried your query visakh16 but it doesn'?t work. The system returned me the two following error messages:
Error 156: Incorrect syntax near the keyword 'CASE'
Error:16945 The cursor was not declared

If i use (select box), then i have:
Error 170: Incorrect syntax near the keyword 'SUM'

Maybe, it'?s my fault. I didn'?t specify the type of variable: varchar for box and equipment but I don't have a syntax error converting varchar value 'value' to a column of datatype int or something else.

kunal.mehta, thanks but i don't understand your query.I'm afraid of changing my database.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-15 : 13:09:52
quote:
Originally posted by Van Helsing

Hi
I tried your query visakh16 but it doesn'?t work. The system returned me the two following error messages:
Error 156: Incorrect syntax near the keyword 'CASE'
Error:16945 The cursor was not declared

If i use (select box), then i have:
Error 170: Incorrect syntax near the keyword 'SUM'

Maybe, it'?s my fault. I didn'?t specify the type of variable: varchar for box and equipment but I don't have a syntax error converting varchar value 'value' to a column of datatype int or something else.

kunal.mehta, thanks but i don't understand your query.I'm afraid of changing my database.


can you post your used query? the query i gave is working fine for me.
Go to Top of Page

Van Helsing
Starting Member

11 Posts

Posted - 2009-04-15 : 16:19:24
quote:
Originally posted by visakh16

can you post your used query? the query i gave is working fine for me.



I'll try to do it the next week.I have to evaluate the situation with my colleagues.My software isn't in English but the queries are in English.Unfortunately,error messages aren't in English.What do you want to see precisely? I used the code you gave me.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-16 : 10:43:41
i wanted to see your query if its different from mine. the query i used worked for me.
Go to Top of Page

Van Helsing
Starting Member

11 Posts

Posted - 2009-05-19 : 13:54:41
Hello,

Sorry to bring up this topic but i am always searching the solution.By the way,i add an information about primk.
Primk
1
2
...

Primk represents a an equipment category.For example 1 identifies Thimble,2 identifies Needle,3 identifies Taffeta



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-19 : 13:57:17
still you didnt post the actual query used
Go to Top of Page

cxmoore
Starting Member

9 Posts

Posted - 2009-05-19 : 13:58:56
Do you have a primary key that links the two tables?
Go to Top of Page

tosscrosby
Aged Yak Warrior

676 Posts

Posted - 2009-05-19 : 14:34:07
quote:
Originally posted by visakh16

still you didnt post the actual query used



From your post on 4/15 @ 11:06, your received an error about a cursor. Visakh's code does not have a cursor in it. If you cannot supply the actual code you are running you are not going to receive any accurate assistance. Visakh is really making a simple request in order to help YOU with a resolution.

Terry

-- Procrastinate now!
Go to Top of Page

Van Helsing
Starting Member

11 Posts

Posted - 2009-10-07 : 11:24:17
quote:
Originally posted by tosscrosby

quote:
Originally posted by visakh16

still you didnt post the actual query used



From your post on 4/15 @ 11:06, your received an error about a cursor. Visakh's code does not have a cursor in it. If you cannot supply the actual code you are running you are not going to receive any accurate assistance. Visakh is really making a simple request in order to help YOU with a resolution.

Terry

-- Procrastinate now!



Easy,please.
Sorry, guys

I didn’t want to be rude with you.
I had given up and started to turn crazy.But i have finally found the solution on a website.
That’s why I come back only today.

select a.box,
coalesce(sum(case when b.Equipment = 'Needle' then 1 end), 0) as numberofneedles,
coalesce(sum(case when b.Equipment = 'Thimble' then 1 end), 0) as numberofthimbles,
coalesce(sum(case when b.Equipment = 'Scissors' then 1 end), 0) as numberofScissors
.....
from table 1 a inner join table 2 b ON a.primk = b.primk
group by boxes a.box


Thanks for your understanding
Go to Top of Page

Van Helsing
Starting Member

11 Posts

Posted - 2009-10-07 : 11:43:05
Bye the way, the Error 156: Incorrect syntax near the keyword 'CASE' was due in the absence of comma after select distinct box.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-10-07 : 11:44:47
hey...don't need coalesce


SELECT a.box
, SUM(CASE WHEN b.Equipment = 'Needle' THEN 1 ELSE 0 END) AS numberofneedles
, SUM(CASE WHEN b.Equipment = 'Thimble' THEN 1 ELSE 0 END) AS numberofthimbles
, SUM(CASE WHEN b.Equipment = 'Scissors' THEN 1 ELSE 0 END) AS numberofScissors
.....
FROM table 1 a
INNER JOIN table 2 b
ON a.primk = b.primk
GROUP BY a.boxes -- a.caisse ????? Whats this ??????



and what is a.caisse



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Van Helsing
Starting Member

11 Posts

Posted - 2009-10-07 : 16:24:50
quote:
Originally posted by X002548

hey...don't need coalesce


SELECT a.box
, SUM(CASE WHEN b.Equipment = 'Needle' THEN 1 ELSE 0 END) AS numberofneedles
, SUM(CASE WHEN b.Equipment = 'Thimble' THEN 1 ELSE 0 END) AS numberofthimbles
, SUM(CASE WHEN b.Equipment = 'Scissors' THEN 1 ELSE 0 END) AS numberofScissors
.....
FROM table 1 a
INNER JOIN table 2 b
ON a.primk = b.primk
GROUP BY a.boxes -- a.caisse ????? Whats this ??????

Sorry for this mistake.I mean a.box



and what is a.caisse



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam





Go to Top of Page

Van Helsing
Starting Member

11 Posts

Posted - 2009-10-07 : 16:40:38
I have another question.I want to know boxes which have the two following equipments: a needle and a thimble.

I tried with

select box "Boxnumber",
case when Equipment='Thimble' and Equipment='Needle' then 1
end
from Table 1 a,Table 2 b
where a.primk=b.primk
group by Box,Equipment


and


select distinct Box
from Table 1 a,Table 2 b
where a.primk1=b.primk
and Equipment='Needle'
and exists
(select distinct Box
from Table 1 a,Table 2 b
where a.primk=b.primk
and Equipment='Thimble'


But it does not work.Thank you for your help.(I tried to edit my posts but without success)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-09 : 11:39:52
[code]select box "Boxnumber"
from Table 1 a
join Table 2 b
on a.primk=b.primk
and Equipment in ('Thimble','Needle')
group by Box
having count(distinct Equipment)=2
[/code]
Go to Top of Page

Van Helsing
Starting Member

11 Posts

Posted - 2009-10-09 : 13:53:07
quote:
Originally posted by visakh16

select box "Boxnumber"
from Table 1 a
join Table 2 b
on a.primk=b.primk
and Equipment in ('Thimble','Needle')
group by Box
having count(distinct Equipment)=2




Good evening.

I hope i will answer Monday.Thanks.

Bye.

Go to Top of Page
    Next Page

- Advertisement -