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
 Determining number of occurrances of values

Author  Topic 

lvirden
Starting Member

4 Posts

Posted - 2011-09-09 : 14:00:58
Table 1 has a column which is a list of names of items.
Table 2 is a list of problems reported, with columns for time the user reported the problem , name of item identified as part of the problem, etc.

For example:
Table 1

Table
Chair
Lamp
Rug

Table 2
1,Table,table wobbles,1/1/11
2,Table,table is dirty,2/2/11
3,Run,rug is out of place,2/13/11
4,Lamp,bulb is burned out,4/1/11

Now, what i am wondering is how to get 2 reports out of this setup
1) a list of each item in table 1 and the number of times that value appears in table 2, sorted by the number of appearances. Note that in the above examples, the resulting report should show chair as having 0 appearances.

2) a list of each table 1 item, and the most recent time it appeared in a report
If an item has never appeared, its should be treated as a date of 0.

Do these two report descriptions seem as if they could be done via straight SQL, or am I going to have to figure out how to write some code around the SQL?

Thank you!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-09 : 14:07:56
they both are straight forward. try it out and we will help you if you face issues. i will give you some hints

1, use left join and count after doing group by
2, use left join and max after doing group by

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

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-09-09 : 14:12:45
Is this a homework assignment?



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

lvirden
Starting Member

4 Posts

Posted - 2011-09-09 : 14:44:25
quote:
Originally posted by X002548

Is this a homework assignment?



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/







No, homework passed me by about 40 years ago.

I have done relatively simple sql statements. I can see getting counts for the items that are present in the table - it is representing the non-presents that is the biggest boggle of my mind. I will keep trying to read up on things.
Go to Top of Page
   

- Advertisement -