| 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 ServerLet’s say that I have two tables.Table 1(Box,primk,...)Box1112222233..................... Table 2(Equipment,primk,...)EquipmentThimbleThimbleNeedleTaffetaNeedleBlack spoolScissorsPatternPatternRulerThimbleScissors….….Box Equipment1 Thimble1 Thimble1 Needle1 Taffeta2 Needle2 Black spool2 Scissors 2 Pattern2 Pattern2 Ruler3 Thimble3 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 followingBox Thimble Needle Taffeta 1 2 1 1 Box Needle Black spool Scissors Pattern Ruler2 1 1 1 2 1Box Thimble Scissors 3 1 1 OrBox Thimble Needle Taffeta Black spool Scissors Pattern Ruler1 2 1 1 2 1 1 1 2 13 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 numberboxfrom Table 1 a ,Table 2 bwhere a.primk=b.primkand equipment='scissors'group by equipment I have this resultNumberBox NumberEquipment2 13 1If I use the following query[code]select count (equipment)equipmentnumber,box numberboxfrom Table 1 a ,Table 2 bwhere a.primk=b.primkand equipment='scissors'group by equipmentunionselect count (equipment)equipmentnumber,box numberboxfrom Table 1 a ,Table 2 bwhere a.primk=b.primkand equipment='thimble'group by equipment I have:NumberBox NumberEquipment2 13 1And notNumberBox NumberEquipment2 13 13 1NumberBox NumberEquipment2 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 NumberEquipment2 13 1The second row is eliminatedThanks in advanceForgive 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 RulerFROM Table1 t1JOIN Table2 t2ON t2.primk=t1.primkGROUP BY Box[/code] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
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 RulerFROM Table1 t1JOIN Table2 t2ON t2.primk=t1.primkGROUP BY Box
quote: Originally posted by visakh16 and if you want to dynamically determine the equipments to be returned usehttp://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. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-12 : 13:29:43
|
welcome hoping to hear from you soon... |
 |
|
|
kunal.mehta
Yak Posting Veteran
83 Posts |
Posted - 2009-04-13 : 07:16:54
|
Hi,try this onedeclare @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 ) spivot(count(item) for item in ([Black spool],[Needle],[Pattern],[Ruler],[Scissors],[Scissorsl],[Taffeta],[Thimble])) pKunal |
 |
|
|
Van Helsing
Starting Member
11 Posts |
Posted - 2009-04-15 : 11:06:26
|
| HiI 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. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-15 : 13:09:52
|
quote: Originally posted by Van Helsing HiI 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. |
 |
|
|
Van Helsing
Starting Member
11 Posts |
Posted - 2009-04-15 : 16:19:24
|
quote: Originally posted by visakh16can 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. |
 |
|
|
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. |
 |
|
|
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.Primk12...Primk represents a an equipment category.For example 1 identifies Thimble,2 identifies Needle,3 identifies Taffeta |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-19 : 13:57:17
|
| still you didnt post the actual query used |
 |
|
|
cxmoore
Starting Member
9 Posts |
Posted - 2009-05-19 : 13:58:56
|
| Do you have a primary key that links the two tables? |
 |
|
|
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! |
 |
|
|
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, guysI 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.primkgroup by boxes a.box Thanks for your understanding |
 |
|
|
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. |
 |
|
|
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.caisseBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
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.caisseBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam
|
 |
|
|
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 withselect box "Boxnumber",case when Equipment='Thimble' and Equipment='Needle' then 1endfrom Table 1 a,Table 2 bwhere a.primk=b.primkgroup by Box,Equipment andselect distinct Boxfrom Table 1 a,Table 2 bwhere a.primk1=b.primkand Equipment='Needle'and exists(select distinct Boxfrom Table 1 a,Table 2 bwhere a.primk=b.primkand Equipment='Thimble' But it does not work.Thank you for your help.(I tried to edit my posts but without success) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-09 : 11:39:52
|
| [code]select box "Boxnumber"from Table 1 ajoin Table 2 bon a.primk=b.primkand Equipment in ('Thimble','Needle')group by Boxhaving count(distinct Equipment)=2[/code] |
 |
|
|
Van Helsing
Starting Member
11 Posts |
Posted - 2009-10-09 : 13:53:07
|
quote: Originally posted by visakh16
select box "Boxnumber"from Table 1 ajoin Table 2 bon a.primk=b.primkand Equipment in ('Thimble','Needle')group by Boxhaving count(distinct Equipment)=2
Good evening.I hope i will answer Monday.Thanks.Bye. |
 |
|
|
Next Page
|