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 |
RoniR
Starting Member
16 Posts |
Posted - 2008-12-03 : 13:35:28
|
hi guys need help with somethingi have a table cli_cde y_cde n_cde pharamycid CLIENTID date_Cde (...)and another table for clients clientID DateOfBirth (...)in my cli_cde table i save an order for a client.I would need some help in quering the tables in order to get a result like this one,lets say19% of the Clients as 30 years old1% if the clients are 45 years old and so forthCan we do this?thanks for any helplet me know if anything is not clear |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-03 : 13:51:35
|
something likeSELECT Age,COUNT(ClientID)*100.0/(SELECT COUNT(ClientID) FROM clients)FROM(SELECT ClientID,CASE WHEN ((MONTH(DateOfBirth)<MONTH(GETDATE())) OR ((MONTH(DateOfBirth)=MONTH(GETDATE())) AND DAY(DateOfBirth)<DAY(GETDATE()))) THEN DATEDIFF(yy,DateOfBirth,GETDATE())-1ELSE DATEDIFF(yy,DateOfBirth,GETDATE())END AS AgeFROM clients )tGROUP BY Age |
 |
|
RoniR
Starting Member
16 Posts |
Posted - 2008-12-03 : 16:03:39
|
this is exactly what i need..one thinglets say my orders tables has 12 order for client 1,9 orders for client 2 ,10 orders for client 3 and 5 orders for client 4the thing is that i dont want to know the average age of clients from the clients table..i want to to know that from my orders table25% of the pople are 24 years oldi tried adding that SELECT Age,COUNT(ClientID)*100.0/tt.ClientOrders as AverageFROM(SELECT ClientID,CASE WHEN ((MONTH(DateOfBirth)<MONTH(GETDATE())) OR ((MONTH(DateOfBirth)=MONTH(GETDATE())) AND DAY(DateOfBirth)<DAY(GETDATE()))) THEN DATEDIFF(yy,DateOfBirth,GETDATE())-1ELSE DATEDIFF(yy,DateOfBirth,GETDATE())END AS AgeFROM clients )t,(SELECT COUNT(ClientID) as ClientOrders FROM sfc_Cdegroup by clientid) as ttGROUP BY Age,tt.ClientOrdersand so i gotAge average----------- ---------------------------------------22 33.33333333333323 33.33333333333324 33.33333333333325 33.33333333333322 20.00000000000023 20.00000000000024 20.00000000000025 20.00000000000022 11.11111111111123 11.11111111111124 11.11111111111125 11.11111111111122 8.33333333333323 8.33333333333324 8.33333333333325 8.333333333333instead SELECT COUNT(ClientID) as ClientOrders FROM sfc_Cdegroup by clientid returns ClientOrders------------93512which are the number of orders of each client...so i wanted to get100/9 =11.11%100/3 =33.33%100/5 =20%100/12=8.33%what did i miss and can u please explain to methanks a whole lot |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-03 : 23:00:54
|
may be thisSELECT Age,COUNT(DISTINCT ClientID)*100.0/(SELECT COUNT(DISTINCT ClientID) FROM clients c1INNER JOIN cli_cde cc1ON cc1.CLIENT_ID=c1.ClientID)FROM(SELECT ClientID,CASE WHEN ((MONTH(DateOfBirth)<MONTH(GETDATE())) OR ((MONTH(DateOfBirth)=MONTH(GETDATE())) AND DAY(DateOfBirth)<DAY(GETDATE()))) THEN DATEDIFF(yy,DateOfBirth,GETDATE())-1ELSE DATEDIFF(yy,DateOfBirth,GETDATE())END AS AgeFROM clients cINNER JOIN cli_cde ccON cc.CLIENT_ID=c.ClientID)tGROUP BY Age |
 |
|
|
|
|
|
|