| Author |
Topic |
|
urmas80
Starting Member
20 Posts |
Posted - 2012-07-09 : 07:24:33
|
| HI all,It's should be basic for you, but not for me... I have table with a column of repeating values and i want to count the number of repeating values for each type of value:Values in columnAABBBCDDDEEEEExpected:Value CountA 2B 3C 1D 3E 4Thank you in advance |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-07-09 : 07:27:18
|
| [code]SELECT Col1, COUNT(*) as [Count]FROM YourTableGROUP BY Col1[/code] |
 |
|
|
urmas80
Starting Member
20 Posts |
Posted - 2012-07-10 : 15:35:26
|
| Great solution, thank you!Now additional question how i create table of value's that replicated 3 times.Expected: ValuesBDall that in one phrasethank you in advance |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-07-10 : 15:39:20
|
| [code]SELECT Col1, COUNT(*) as [Count]FROM YourTableGROUP BY Col1HAVING COUNT(*)=3[/code] |
 |
|
|
urmas80
Starting Member
20 Posts |
Posted - 2012-07-10 : 15:46:05
|
| so simple... I am Noob!! |
 |
|
|
urmas80
Starting Member
20 Posts |
Posted - 2012-07-11 : 18:29:42
|
| I got an Exercise that i am working on for a few days and i just can't figure it out, i hoped to find solution from your answers but it's keeps getting away from me...Here is the exercise:I got table: Product(maker, model, type)maker model typeA 1232 PCA 1233 PCA 1276 PrinterA 1298 LaptopA 1401 PrinterA 1408 PrinterA 1752 LaptopB 1121 PCB 1750 LaptopC 1321 LaptopD 1288 PrinterD 1433 PrinterE 1260 PCE 1434 PrinterE 2112 PCE 2113 PCFind out makers who produce only the models of the same type, and the number of those models exceeds 1.Deduce: maker, type |
 |
|
|
urmas80
Starting Member
20 Posts |
Posted - 2012-07-12 : 07:15:09
|
*bump |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-07-12 : 07:22:31
|
I am reluctant to post anything because you said "exercise"; Moderators of this forum do not look kindly upon people giving away answers to students.I would think about it in this terms:a) I need to get the Makers. So that is a "SELECT Maker FROM Product"b) I need to get makers who make only one product type. So that is a HAVING clause - "HAVING COUNT (Distinct type) = 1c) I need to get makers who make more than one model. That would again be another similar condition in the HAVING clause.d) Since you are using an aggregate function, you will need a GROUP by clause.If I say anything more, I may get banned from this site |
 |
|
|
urmas80
Starting Member
20 Posts |
Posted - 2012-07-12 : 09:59:05
|
| don't worry regarding my study, i can promise you i am not a student of any official academy, i am study on my own from online exercises, and that's why i has some huge holes in my understanding of the SQL.from what i understand, the dependency of the answer is between model and type, because the answer should be:maker typeD Printerso first of all i need to to build a condition that checks for which maker i have only one type of hardware. so that answer will be maker typeD PrinterC Laptoplest start with that, then i will try on my own... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-12 : 10:20:31
|
quote: Originally posted by urmas80 don't worry regarding my study, i can promise you i am not a student of any official academy, i am study on my own from online exercises, and that's why i has some huge holes in my understanding of the SQL.from what i understand, the dependency of the answer is between model and type, because the answer should be:maker typeD Printerso first of all i need to to build a condition that checks for which maker i have only one type of hardware. so that answer will be maker typeD PrinterC Laptoplest start with that, then i will try on my own...
look for GROUP BY,COUNT and HAVING clauses in books online------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|