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
 multiple counts not working properly

Author  Topic 

mayhem121
Starting Member

3 Posts

Posted - 2012-03-19 : 12:36:42
Hi sql gurus,

I am stumped and really hope someone can help out, i've the following query:


select count(gb.ean13) as one, count (gb2.ean13) as two, gb.title,gb.category,gb2.category,gb.ean13
from table1 GB
inner join table2 GB2 on
GB.ean13 = GB2.ean13
where gb.category in ("tv")
and gb.title = gb2.title
and gb2.category in ("dvdplayers")
and gb.cc = "gb"
and gb2.cc = "gb"
group by gb.title,gb.category,gb2.category,gb.ean13



the results i'd want to see is:

one|two|title|category|category|ean13
4|2| |sonytv| tv| dvd players| 10021545445

meaning there are 4 "sonytv" in the TV category and there are 2 "sonytv" in the DVD player category...

I hope that makes sense?

I just cannot for the life of me get the count to work, hence signing up in hope that someone can help me

Thank you

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-19 : 12:56:45
It would make more sense if you Posted the DDL of the tables, some sample Data in DML form ..and you already have the expected results

We can't get there from here without a starting point

Read the hint link in my sig



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


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-19 : 14:06:22
so you've multiple categories listed against a title? are they stored as rows or columns?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mayhem121
Starting Member

3 Posts

Posted - 2012-03-20 : 04:24:50
Hello Visakh,

They are stored as rows, is that possible?
Go to Top of Page

mayhem121
Starting Member

3 Posts

Posted - 2012-03-20 : 04:25:27
i'll try and do create the data in DML form :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-20 : 14:55:28
this is my stab at it before post data


SELECT gb.title,gb.ean13
count(gb.ean13) as one, count (gb2.ean13) as two,
max(gb.category) as category1,
max(gb2.category) as category2
FROM table1 GB
inner join table2 GB2 on
GB.ean13 = GB2.ean13
and gb.title = gb2.title
and gb2.cc = gb.cc
where gb.category in ("tv")
and gb2.category in ("dvdplayers")
and gb.cc = "gb"
group by gb.title,gb.ean13


see if it fits your purpose else post data to have us more insight into your exact scenario

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -