Author |
Topic |
puush
Starting Member
14 Posts |
Posted - 2009-04-07 : 01:12:37
|
Hello,I have a script that pulls data from 4 tables and I want to pull data from one more table with this script. The fifth table I want to pull from is called "EFT_TRANS" and I want to query data from the "Amount" Field. The linking field in the "EFT_TRANS" table is "Player_ID", I think. This data needs to be in a fifth column named "PBT Amount" in the query results. I had some help writing this original script below and I know what it does but I don't totally understand all of the functions like the (sub select), Group By and Count-Sum functions. Any help would be greatly appreciated. The original script is below.____________________________________________________/* This script selects by given date range, a players Player_ID, Firstname, Lastname, address, city, state, zip, Total Number of trips and total coin in. */SELECT P.player_ID, p.FirstName, p.LastName, a.Address1A, a.Address1b, a.City1, a.State1, a.Zip1, x.stattype, x.NumTrips, X.cashinFROM CDS_PLAYER p, cds_account a,(select st.meta_id as player_ID, sd.stattype, sum(sd.CashIn)as cashin, count(st.stattrip_id) as NumTripsFROM CDS_STATTRIP st, CDS_StattripDetail sdWHERE st.idtype = 'p'AND st.StartGamingDate between '02/01/2009' and '02/28/2009' /* Enter Gaming Date range here */AND st.stattrip_id = sd.stattrip_idGROUP BY st.meta_id, sd.stattype)xWHERE x.player_id = p.player_idAND p.account_id = a.account_idORDER BY p.player_id, stattype----------------------------------------------------------Resultsplayer_ID,FirstName,LastName,Address1A,Address1b,City1,State1,Zip1,stattype,NumTrips,cashin,AvgPerTrip12,CINDY,MIL,P.O. BOX 8,,WILLITS,CA,95490,SLOT,7,$198.50 ,$28.36 13,EARNEST,POTT,50 SOUTH MAIN STREET #49,,WILLITS,CA,95490,SLOT,2,"$1,400.36 ",$700.18 14,THELMA,DD,18 LOCUST STREET #8,,WILLITS,CA,95490,SLOT,17,"$10,601.89 ",$623.64 15,RAELENE,DOERO,10 BAECHTEL RD,,WILLITS,CA,95490,SLOT,2,$382.89 ,$191.45 17,ROBERT,SHOAW,1 OSPREY COURT,,FORT______________________________Michael, IT Director DudeFrom the Great land of Lake Co, CA |
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-04-07 : 01:21:21
|
try like thisSELECT P.player_ID, p.FirstName, p.LastName, a.Address1A, a.Address1b, a.City1, a.State1, a.Zip1, x.stattype, x.NumTrips, X.cashin,e.amount AS avgpertripFROM CDS_PLAYER p, cds_account a,(select st.meta_id as player_ID, sd.stattype, sum(sd.CashIn)as cashin, count(st.stattrip_id) as NumTripsFROM CDS_STATTRIP st, CDS_StattripDetail sdWHERE st.idtype = 'p'AND st.StartGamingDate between '02/01/2009' and '02/28/2009' /* Enter Gaming Date range here */AND st.stattrip_id = sd.stattrip_idGROUP BY st.meta_id, sd.stattype)xINNER JOIN (select player_id,avg(amount) as amount from EFT_TRANS group by player_id) e ON e.player_id = p.playeridWHERE x.player_id = p.player_idAND p.account_id = a.account_idORDER BY p.player_id, stattype |
|
|
puush
Starting Member
14 Posts |
Posted - 2009-04-08 : 02:50:07
|
hello,Thanks for helping me man. I got the error below when i run this script in sql 200 query analyzer. i don't know what it means. Please assist. thanks.--------------------------------------------------------------------Server: Msg 107, Level 16, State 2, Line 1The column prefix 'p' does not match with a table name or alias name used in the query.---------------------------------------------------------------------SELECT P.player_ID, p.FirstName, p.LastName, a.Address1A, a.Address1b, a.City1, a.State1, a.Zip1, x.stattype, x.NumTrips, X.cashin,e.amount AS avgpertripFROM CDS_PLAYER p, cds_account a,(select st.meta_id as player_ID, sd.stattype, sum(sd.CashIn)as cashin, count(st.stattrip_id) as NumTripsFROM CDS_STATTRIP st, CDS_StattripDetail sdWHERE st.idtype = 'p'AND st.StartGamingDate between '02/01/2009' and '02/28/2009' /* Enter Gaming Date range here */AND st.stattrip_id = sd.stattrip_idGROUP BY st.meta_id, sd.stattype)xINNER JOIN (select player_id,avg(amount) as amount from EFT_TRANS group by player_id) e ON e.player_id = p.playeridWHERE x.player_id = p.player_idAND p.account_id = a.account_idORDER BY p.player_id, stattype______________________________Michael, IT Director DudeFrom the Great land of Lake Co, CA |
|
|
puush
Starting Member
14 Posts |
Posted - 2009-04-15 : 00:07:36
|
I figured it out with help from other fellows. The original query looked like it was built for MySQL. It lists the tables in the from clause seperated by comas and then joins in the where clause. That's not necessarily going to work with MS SQL. I instead used inner joins in the from clause for the first few tables rather than the lists.SELECTP.player_ID, p.FirstName, p.LastName, a.Address1A, a.Address1b,a.City1, a.State1, a.Zip1, x.stattype, x.NumTrips, X.cashin,e.amount AS avgpertripFROMCDS_PLAYER pINNER JOIN cds_account a on p.account_id = a.account_idINNER JOIN (select st.meta_id as player_ID, sd.stattype, sum(sd.CashIn)as cashin, count(st.stattrip_id) as NumTripsFROM CDS_STATTRIP st, CDS_StattripDetail sdWHERE st.idtype = 'p'AND st.StartGamingDate between '02/01/2009' and '02/28/2009' /* Enter Gaming Date range here */AND st.stattrip_id = sd.stattrip_idGROUP BY st.meta_id, sd.stattype) x ON x.player_id = p.player_idINNER JOIN (select player_id,avg(amount) as amount from EFT_TRANS group by player_id) e ON e.player_id = p.playeridORDER BY p.player_id, stattype______________________________Michael, IT Director DudeFrom the Great land of Lake Co, CA |
|
|
|
|
|