help with SQL query / view / count problem....

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 1
Reputation: jasperjerome is an unknown quantity at this point 
Solved Threads: 0
jasperjerome jasperjerome is offline Offline
Newbie Poster

help with SQL query / view / count problem....

 
0
  #1
Oct 12th, 2009
Greetings! I am hoping someone can help me with a MS SQL query/view problem.

I am creating a "name my new baby" website where parents can upload a picture of their child and have

users cast votes for a good name. Users will see the child's photo and they will be able to "agree"

with a current suggested name or post their own suggested name. I know most parents would rather name

their own kid..this is just for fun to see what I can accomplish.

So far I have two tables like this:

Kids:
kidID (int unique primary key)
parentId (int..cross referenced to parents account)
photoFile (varchar 25 characters referencing file photo)

Votes:
nameId (int unique primary key)
suggestedName (varchar 15 characters)
votescast (int...this counts how many votes each name has)

What I would like is to have a "masterlist" page that shows a list of all current photos with the
currently top voted name. So if newborn.jpg has 25 votes for "Sammy" and littlecutie.jpg has 3 votes

for "Jessica" my query would return the fields "KidId, photoFile, suggestedName, VotesCast" with the

date being:

5, redheadnewborn.jpg, "Sammy", 25
12, littlecutie.jpg, "Jessica", 3

I am only interested in seeing the top voted name for each photo. I have tried a number of queries

involving views...none of which seem to return what I want.

I know this will probably involve the sql count function but I can't find an example that is close to what I want. Any help would be very appreciated. If it helps I am using MS SQL 2008.

Thanks,
J
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,410
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 614
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #2
Oct 13th, 2009
Odd database design but you could do something like:
  1. IF OBJECT_ID('Kids_Test', 'U') IS NOT NULL DROP TABLE Kids_Test
  2. IF OBJECT_ID('Votes_Test', 'U') IS NOT NULL DROP TABLE Votes_Test
  3. GO
  4. CREATE TABLE Kids_Test
  5. (
  6. KidID int identity(1000, 1) PRIMARY KEY,
  7. ParentId int,
  8. PhotoFile varchar(25)
  9. )
  10.  
  11. CREATE TABLE Votes_Test
  12. (
  13. NameId int identity(1000, 1) PRIMARY KEY, --is this the voters id?
  14. KidId int, --kid this relates to
  15. SuggestedName varchar(15),
  16. --VotesCast int --You shouldn't store a bucket total in a table like that. You will probably want
  17. --to store individual votes so you can log IP addr of who voted, etc etc
  18. )
  19. GO
  20. Declare @ID int
  21. INSERT INTO Kids_Test (ParentId, PhotoFile) VALUES (1, 'redheadnewborn.jpg')
  22. SET @ID = Cast(SCOPE_IDENTITY() AS int)
  23. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  24. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  25. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  26. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  27. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  28. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  29. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  30. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  31. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  32. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  33. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  34. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  35. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  36. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  37. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  38. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  39. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  40. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  41. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  42. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  43. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  44. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  45. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  46. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  47. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Sammy')
  48. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'SomeoneElse')
  49. INSERT INTO Kids_Test (ParentId, PhotoFile) VALUES (1, 'littlecutie.jpg')
  50. SET @ID = Cast(SCOPE_IDENTITY() AS int)
  51. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Jessica')
  52. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Jessica')
  53. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Jessica')
  54. INSERT INTO Votes_Test (KidId, SuggestedName) VALUES (@ID, 'Bob')
  55.  
  56. SELECT *,
  57. (
  58. SELECT Top 1 SuggestedName
  59. FROM Votes_Test
  60. WHERE Votes_Test.KidId = Kids_Test.KidId
  61. GROUP BY KidId, SuggestedName
  62. ORDER BY Count(*) DESC
  63. ) AS SuggestedName,
  64. (
  65. SELECT Top 1 Count(*)
  66. FROM Votes_Test
  67. WHERE Votes_Test.KidId = Kids_Test.KidId
  68. GROUP BY KidId, SuggestedName
  69. ) AS Cnt
  70. FROM Kids_Test
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 27
Reputation: kool.net is an unknown quantity at this point 
Solved Threads: 0
kool.net kool.net is offline Offline
Light Poster
 
0
  #3
Oct 16th, 2009
hey simply add a new field in votes table as a foreign key of kids
like kid_id.
then in your query u sort you results with votescast.
Just think about dat........ its not a big issue
ok
have a gr8 day with sql
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 27
Reputation: kool.net is an unknown quantity at this point 
Solved Threads: 0
kool.net kool.net is offline Offline
Light Poster
 
0
  #4
Oct 16th, 2009
your query shud b like
  1. SELECT kids.kidID , kids.PhotoFile , votes.kid_id, votes.SuggestedName , votes.votescast FROM kids, votes WHERE kids.KidID = votes.kid_id ORDER BY votes.votescast DESC";
  2.  
Last edited by peter_budo; Oct 17th, 2009 at 1:00 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks).
Reply With Quote Quick reply to this message  
Reply

Tags
count, query, sql, view

Message:


Thread Tools Search this Thread



Tag cloud for count, query, sql, view
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC