Connecting Tech Pros Worldwide Forums | Help | Site Map

Query with Counts Help...

Newbie
 
Join Date: May 2009
Posts: 2
#1: May 17 '09
Hello All,

I need help with a SQL Query.

I have a table with a StateId and PartNumber. I'll be joining some tables to get the state name from the StateId and Part Description from the Part Number.

Where I need the help is from the original table. What I have to do is list the part and quantity sold in each state.

I might have something like...

StateId PartNumber
19 123
18 456
19 123
18 789

I need a query to get the counts for each part in each state so I get something like...

19 123 2
18 456 1
18 789 1

So after all my joins and stuff I would get
Arkansas Misc Part Example Description 2
Alabama Another Part Example 1
Alabama My Test Part Description 1

After all is said and done, I'll sort by state then by description.....

Alabama Another Part Example 1
Alabama My Test Part Description 1
Arkansas Misc Part Example Description 2

I think I can join to get the state names and part descriptions, but I don't know how to get the counts appropriately.

Any help would be greatly appreciated!

Thanks!

ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#2: May 18 '09

re: Query with Counts Help...


follow this code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. select stateneme, partdescription, count(*)
  3. from YourMainTable M
  4. left join YourStateTable  st on st.stateid = m.stateid
  5. left join YourPartTable pt on pt.partnumber = m.partnumber
  6. group by statename, partdescription
  7. order by 2
  8.  
  9.  
I don't mean that you follow it literally. Just follow the logic.

Happy Coding!

--- CK
Newbie
 
Join Date: May 2009
Posts: 2
#3: May 21 '09

re: Query with Counts Help...


Thanks! That Helped!!
Reply