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.
| Author |
Topic |
|
pwcphoto
Yak Posting Veteran
69 Posts |
Posted - 2005-06-22 : 05:07:55
|
| I have a table used for documenting images that contains 3 Fields, Cat1_ID, Cat2_ID, Cat3_ID which are type INTEach of these fields can contain a category number of which right now there are approximately 30 categories. Any of the 30 category IDs can be entered in any or all of these fields or they can be set NULL. The WHERE clause would be WHERE User_ID = xxx as each image belongs to a user. The idea being to display how many images a user has in each of the categories.What I need is to count how many of each category (1-30) there are in all 3 of the fields, don't care which field they are in, just want a grand total for each of the 30 categories. I would like it to only count as one though if the same category is duplicated in 2 or more fields.I have looked through the forums and BOL and I see things that are close to what I want but I am still not sure how it is done. The table contains around 5,000,000 records so i need to have a very efficient count. Once the count is derived I will need to figure some better way to save it as a derrived table so that I don't have to go through this count procedure each time the page is displayed. Only when an image is added, deleted or modified.Any Help would be appreciated.Phil-----------------------I used to be a rocket scientist. Now I'm just a space cadet... |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-06-22 : 05:15:04
|
| Post table structure with sample data and expected outcomeMadhivananFailing to plan is Planning to fail |
 |
|
|
pwcphoto
Yak Posting Veteran
69 Posts |
Posted - 2005-06-22 : 05:34:58
|
| OK, Here is the table structureImage_ID, User_ID, Category1_ID, Category2_ID, Category3_ID1 82 12 10 12 82 12 15 253 82 15 1 184 82 12 12 12 ' only counts as 1 entry for category 12 even though it is there 3 timesResults for user 82----------------------Category 1 = 2Category 10 = 1Category 12 = 3Category 15 = 2Category 18 = 1Category 25 = 1-----------------------I used to be a rocket scientist. Now I'm just a space cadet... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-06-22 : 06:04:08
|
Something like this but not sure whether this works for your actual datadeclare @t table(i int, cat1 int, cat2 int , cat3 int)insert into @t values(82,12, 10 ,1)insert into @t values(82,12, 15 ,25)insert into @t values(82,15 ,1 ,18)insert into @t values(82,12,12 ,12)select i,category,count(counting) counts from(Select distinct i,cat1 as Category,(select count(cat1) from @t where i=T.i and cat1=T.cat1) as counting from @t TUnion allSelect distinct i,cat2,(select count(cat2) from @t where i=T.i and cat2=T.cat2) from @t TUnion allSelect distinct i,cat3,(select distinct count(cat3) from @t where i=T.i and cat3=T.cat3) from @t T) T group by i,category MadhivananFailing to plan is Planning to fail |
 |
|
|
pwcphoto
Yak Posting Veteran
69 Posts |
Posted - 2005-06-23 : 15:10:31
|
| Madhivanan,Thank you for the query but I am still having a bit of a problem with it. I converted what you showed to the following representing the actual table fields and table name. It almost works.-----------------------------------------------------Translation of original values from your sample:T = UF_rt_Imagesi = UF_rt_Images.user_idcat1 = UF_rt_Images.dt_category1_IDcat2 = UF_rt_Images.dt_category2_IDcat3 = UF_rt_Images.dt_category3_ID-------------------------------------------select UF_rt_Images.user_ID,category,count(counting) counts from(Select distinct UF_rt_Images.User_ID,dt_category1_ID as Category,(select count(dt_category1_ID) from UF_rt_Images where UF_rt_Images.user_id=82 and UF_rt_Images.dt_category1_ID=dt_category1_ID) as counting from UF_rt_ImagesUnion allSelect distinct UF_rt_Images.User_ID,dt_category2_ID,(select count(dt_category2_ID) from UF_rt_Images where UF_rt_Images.User_ID=82 and UF_rt_Images.dt_category2_ID=dt_category2_ID) from UF_rt_ImagesUnion allSelect distinct UF_rt_Images.user_id,dt_category3_ID,(select distinct count(dt_category3_ID) from UF_rt_Images where UF_rt_Images.user_ID=82 and UF_rt_Images.dt_category3_ID =dt_category3_ID ) from UF_rt_Images) UF_rt_Images where UF_rt_Images.user_ID=82 group by User_ID,categoryI had to add "where UF_rt_Images.user_ID=82" to the very end of the query so it would return only info for user 82 instead of all users.Here are the results which are not correct, but it is getting close. Have I missed something? A little too overzealous in changing out field names instead of leaving them as variables?82 NULL 382 0 382 1 182 2 182 3 182 4 182 6 182 7 182 8 182 11 182 12 182 15 182 16 182 17 282 20 282 24 382 28 182 30 182 31 182 33 3Thanks,Phil-----------------------I used to be a rocket scientist. Now I'm just a space cadet... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-06-24 : 01:16:05
|
| Can you post some actual data you used?MadhivananFailing to plan is Planning to fail |
 |
|
|
pwcphoto
Yak Posting Veteran
69 Posts |
Posted - 2005-06-24 : 02:51:08
|
| OK, Here is the query of all of the images for user 82. I deleted the entries that had nothing in the category fields, those are all set to either a 0 or null of which there were a few hundred.SELECT Image_ID, User_ID, dt_category1_ID, dt_Category2_ID, dt_category3_ID FROM UF_rt_Images WHERE user_ID = 82 ORDER BY Image_ID ASCResultsimage_ID, user_id, dt_category1_ID, dt_category2_ID, dt_category3_ID11231 82 20 0 011648 82 20 0 029661 82 15 0 0100126 82 8 0 0100129 82 8 0 0100210 82 8 0 0101322 82 8 0 0106573 82 17 0 0118468 82 11 0 0118469 82 11 0 0118479 82 6 0 0118485 82 24 0 0121319 82 12 0 0121350 82 12 0 0121373 82 11 0 0122555 82 1 0 0122561 82 12 0 0123023 82 16 0 0125800 82 6 0 0128576 82 16 0 0158049 82 15 0 0159393 82 6 0 0166443 82 15 0 0181622 82 8 0 0181633 82 8 0 0181647 82 8 0 0188402 82 24 0 0215084 82 24 0 0228316 82 15 0 0228320 82 15 0 0228804 82 15 0 0228811 82 15 0 0402255 82 1 0 0513676 82 7 17 30515839 82 7 20 31515843 82 7 20 31515845 82 7 20 311000006 82 1 24 331000008 82 1 24 331000010 82 28 24 331000011 82 28 24 331000017 82 2 33 241000020 82 2 33 241000021 82 2 NULL NULL1000022 82 2 NULL NULL1000026 82 33 NULL NULL1000027 82 33 4 31000033 82 6 NULL NULLThis is the actual data in the table.Phil-----------------------I used to be a rocket scientist. Now I'm just a space cadet... |
 |
|
|
pwcphoto
Yak Posting Veteran
69 Posts |
Posted - 2005-06-26 : 15:05:34
|
| Did everyone give up on this one? Still could use the help.Phil-----------------------I used to be a rocket scientist. Now I'm just a space cadet... |
 |
|
|
|
|
|
|
|