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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Coding Help

Author  Topic 

Unknownuser1
Starting Member

7 Posts

Posted - 2012-05-29 : 17:58:40
Hi I have the following code which works fine however I need to add in two more fields from 2 other tables
Im not sure where to join them in...

dbo.viewDis has custid, I need to get distance as X
dbo.viewAccum has custid, I need to retrieve the following 2 fields time, hours as Y
I have no clue where or how to get them to join I though of another select within the inner
Join but it keeps failing ANY help would be great.. Also how do I get the dat to save to a
location on my C:\temp1\data.txt as delimited...

Select Distinct
P.CustID,
P.Acct,
P.Title,
P.FirstName,
P.LastName,
A.Trips,
A.MoneySpent,
A.Bal
FROM dbo.Cust P
INNER JOIN
(SELECT
CustID,
Sum(Days) as Trips,
SUM(MoneySpent) as MoneySpent,
Sum(Balance) as Bal
FROM dbo.CAsh
Where Accumulator='DP'
GROUP BY CustID) A ON P.CustID = A.CustID

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-29 : 21:12:35
[code]
Select Distinct
P.CustID,
P.Acct,
P.Title,
P.FirstName,
P.LastName,
A.Trips,
A.MoneySpent,
A.Bal,
v1.distance AS [X],
v2.time,
v2.hours
FROM dbo.Cust P
INNER JOIN
(SELECT
CustID,
Sum(Days) as Trips,
SUM(MoneySpent) as MoneySpent,
Sum(Balance) as Bal
FROM dbo.CAsh
Where Accumulator='DP'
GROUP BY CustID) A ON P.CustID = A.CustID
INNER JOIN dbo.viewDis v1 ON v1.CustID = A.CustID
INNER JOIN dbo.viewAccum v2 ON v2.CustID = A.CustID
[/code]

if views wont have matching records always make them LEFT JOIN instead of INNER JOIN

for getting them into txt file use bcp with query out option

see

http://www.simple-talk.com/sql/database-administration/creating-csv-files-using-bcp-and-stored-procedures/

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Unknownuser1
Starting Member

7 Posts

Posted - 2012-05-30 : 05:54:37
Hi,

I wasnt quite clear on the request what I needed was to add in a 3rd and 4 table to the mix and get their sums and group by ...The top select works fine the lower one is what I was thinking is this.....

Select Distinct
P.CustID, P.Acct, P.Title, P.FirstName, P.LastName,
A.Trips, A.MoneySpent, A.Bal
FROM dbo.Cust P
INNER JOIN
(SELECT
CustID, Sum(Days) as Trips, SUM(MoneySpent) as MoneySpent,
Sum(Balance) as Bal
FROM dbo.CAsh
Where Accumulator='DP'
GROUP BY CustID) A ON P.CustID = A.CustID


The 2 new tables are ViewDis (X) - Sum (Distance)
and ViewACC (Y) Sum Fields required Hours, Min, (Group by) Building and Date.

Select Distinct
P.CustID, P.Acct, P.Title, P.FirstName, P.LastName,
A.Trips, A.MoneySpent, A.Bal, X.Distance, Y.Hours, Y.Min, Y.Building
FROM dbo.Cust P
INNER JOIN
(SELECT
CustID, Sum(Days) as Trips, SUM(MoneySpent) as MoneySpent,
Sum(Balance) as Bal, Sum(Distance) as Dist, Sum(Hours), Sum(Min)
FROM dbo.CAsh
Where Accumulator='DP'
GROUP BY CustID) A ON P.CustID = A.CustID
INNER JOIN dbo.viewDis X ON v1.CustID = A.CustID
INNER JOIN dbo.viewAcc Y ON v2.CustID = A.CustID
Go to Top of Page

Unknownuser1
Starting Member

7 Posts

Posted - 2012-05-30 : 10:11:39
Anyone know if the synthax is wrong as I keep getting errors...

Msg 207, Level 16, State 1, Line 4
Invalid column name 'Distance'
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-05-30 : 11:13:19
Well in your derived table "A" you are not specifying any column names for Sum(Hours) or Sum(Min), but that probably isn't a problem per se.

Can you confirm that the tables/views dbo.CAsh and dbo.viewDis both have a Distance column in them?
Go to Top of Page

Unknownuser1
Starting Member

7 Posts

Posted - 2012-05-30 : 11:21:28
Hi,

Thanks for taking a look.

Yes both of the tables have the named fields in them.
I wasnt sure if Ii was referencing them in the correct way or if the join was incorrect?
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-05-30 : 11:38:20
The only other thing that jumps out at me is that you are using reserved words (bad!!) for column names. You might try using quoted identifiers. IE: SELECT Y.[Min]
Go to Top of Page

Unknownuser1
Starting Member

7 Posts

Posted - 2012-05-30 : 12:02:04
Hi There,

Sorry not sure what you mean, "You might try using quoted
identifiers. IE: SELECT Y.[Min]"

So far I had it working but then I got this
Invalid column name 'Min' - when referenciong the SUM...
I have tried to rename this but keep getting the name I change it to as an error...
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-05-30 : 12:09:41
I guess MS is calaling them Delimited Identifiers. Basically, you wrap the column name in double quotation marks (") or brackets ([ ]) so SQL can handle it correctly. You use these when your column names do not comply with the rules for identifiers (reserved words, have spaces, etc).

Here is a link withmore info:
http://msdn.microsoft.com/en-us/library/ms175874.aspx
Go to Top of Page

Unknownuser1
Starting Member

7 Posts

Posted - 2012-05-30 : 12:36:42
Hi there,

I got the go ahead from powers that be to post the script with a few changes so here it is.....Along with the error message!

Msg 207, Level 16, State 1, Line 12 Invalid column name 'ClubADW'.

Select distinct P.PlayerID, P.Acct, P.Title, P.FirstName, P.LastName, P.HomeAddr1, P.HomeAddr2, P.HomeCity, P.HomePostalCode, P.HomeCountryCode, P.HomeTel1Type, P.HomeTel1, P.HomeTel2Type, P.HomeTel2, P.IsSMSSend, P.HomeEmail, P.IsEmailSend, P.BirthDt, P.TypeID, P.IsVIP, P.IsBanned, P.IsProblemGambler, P.IsNoMail,P.IsReturnMail, P.IsMailToAlt, P.Sex, P.SetupCasinoID, P.SetupDtm, P.MaxTrpCasinoId, P.PtsBal, P.LastTripDt, P.LTDPts, P.HostEmpID, P.SetupEmpID, P.LastEditDtm, P.LastEditEmpID, P.PlayerID, P.GroupID, A.ActionD, A.TheorWin, A.CashbuyIn, A.ChipBuyIn, A.BuyIn, A.CasinoWin, A.Bet, A.EarnedPts, A.BonusPts, A.RedeemPts, A.AdjPtsDr, A.AdjPtsCr, A.NetPts, A.EarnedComp, A.AdjCompDr, A.AdjCompCr, A.CompSpent, A.Unusedcomp, A.RatingPeriodMinutes, A.PlayTimeHours, A.AvgBetPerPlay, A.Trips, A.AuthAward, A.AwardUsed, A.UnsettledAward, A.Food, http://A.Travel, A.Merchandise, A.OtherAward, A.CompUsed, A.CasinoID, X.ClubADW FROM dbo.ViewCustomers P INNER JOIN (SELECT PlayerID, Sum(ActionDays) as ActionD, SUM(TheorWin) as TheorWin, Sum(CashBuyIn) as CashbuyIn, Sum(ChipBuyIn) as ChipBuyIn, Sum(BuyIn) as BuyIn, Sum(CasinoWin) as CasinoWin,Sum(Bet) as Bet, Sum(EarnedPts) as EarnedPts, Sum(BonusPts) as BonusPts,Sum(RedeemPts) as RedeemPts, Sum(AdjPtsDr) as AdjPtsDr, Sum(AdjPtsCr) as AdjPtsCr, Sum(NetPts) as NetPts, Sum(EarnedComp) as EarnedComp, Sum(AdjCompDr) as AdjCompDr, Sum(AdjCompCr) as AdjCompCr, Sum (CompSpent) as CompSpent, Sum(UnusedComp) as Unusedcomp, Sum(RatingPeriodMinutes) as RatingPeriodMinutes, Sum(PlayTimeHours) as PlayTimeHours, Sum(AvgBetPerPlay) as AvgBetPerPlay, Sum(Trips) as Trips, Sum(AuthAward) as AuthAward, Sum(AwardUsed) as AwardUsed, Sum(UnsettledAward) as UnsettledAward, Sum(Food) as Food, Sum(Travel) as Travel, Sum(Merchandise) as Merchandise, Sum(OtherAward) as OtherAward, Sum(CompUsed) as CompUsed, Sum (TheorWin) as ClubADW, CasinoID FROM dbo.viewAccum1 Where Accumulator='DP' GROUP BY PlayerID, CasinoID) A ON P.PlayerID = A.PlayerID left JOIN dbo.ViewAccum1Day X ON A.PlayerID = X.PlayerID
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-05-30 : 12:58:08
I assume the "http://A.Travel," is s typo..

Is 'ClubADW' supposed to be coming from dbo.ViewAccum1Day or from the derived table A? Currently, it is coming from dbo.ViewAccum1Day, so verify that that view has that column in it.

Here is your code formatted for fun:
SELECT DISTINCT 
P.PlayerID,
P.Acct,
P.Title,
P.FirstName,
P.LastName,
P.HomeAddr1,
P.HomeAddr2,
P.HomeCity,
P.HomePostalCode,
P.HomeCountryCode,
P.HomeTel1Type,
P.HomeTel1,
P.HomeTel2Type,
P.HomeTel2,
P.IsSMSSend,
P.HomeEmail,
P.IsEmailSend,
P.BirthDt,
P.TypeID,
P.IsVIP,
P.IsBanned,
P.IsProblemGambler,
P.IsNoMail,
P.IsReturnMail,
P.IsMailToAlt,
P.Sex,
P.SetupCasinoID,
P.SetupDtm,
P.MaxTrpCasinoId,
P.PtsBal,
P.LastTripDt,
P.LTDPts,
P.HostEmpID,
P.SetupEmpID,
P.LastEditDtm,
P.LastEditEmpID,
P.PlayerID,
P.GroupID,
A.ActionD,
A.TheorWin,
A.CashbuyIn,
A.ChipBuyIn,
A.BuyIn,
A.CasinoWin,
A.Bet,
A.EarnedPts,
A.BonusPts,
A.RedeemPts,
A.AdjPtsDr,
A.AdjPtsCr,
A.NetPts,
A.EarnedComp,
A.AdjCompDr,
A.AdjCompCr,
A.CompSpent,
A.Unusedcomp,
A.RatingPeriodMinutes,
A.PlayTimeHours,
A.AvgBetPerPlay,
A.Trips,
A.AuthAward,
A.AwardUsed,
A.UnsettledAward,
A.Food,
A.Travel,
A.Merchandise,
A.OtherAward,
A.CompUsed,
A.CasinoID,
X.ClubADW
FROM
dbo.ViewCustomers P
INNER JOIN
(
SELECT
PlayerID,
Sum(ActionDays) AS ActionD,
Sum(TheorWin) AS TheorWin,
Sum(CashBuyIn) AS CashbuyIn,
Sum(ChipBuyIn) AS ChipBuyIn,
Sum(BuyIn) AS BuyIn,
Sum(CasinoWin) AS CasinoWin,
Sum(Bet) AS Bet,
Sum(EarnedPts) AS EarnedPts,
Sum(BonusPts) AS BonusPts,
Sum(RedeemPts) AS RedeemPts,
Sum(AdjPtsDr) AS AdjPtsDr,
Sum(AdjPtsCr) AS AdjPtsCr,
Sum(NetPts) AS NetPts,
Sum(EarnedComp) AS EarnedComp,
Sum(AdjCompDr) AS AdjCompDr,
Sum(AdjCompCr) AS AdjCompCr,
Sum (CompSpent) AS CompSpent,
Sum(UnusedComp) AS Unusedcomp,
Sum(RatingPeriodMinutes) AS RatingPeriodMinutes,
Sum(PlayTimeHours) AS PlayTimeHours,
Sum(AvgBetPerPlay) AS AvgBetPerPlay,
Sum(Trips) AS Trips,
Sum(AuthAward) AS AuthAward,
Sum(AwardUsed) AS AwardUsed,
Sum(UnsettledAward) AS UnsettledAward,
Sum(Food) AS Food,
Sum(Travel) AS Travel,
Sum(Merchandise) AS Merchandise,
Sum(OtherAward) AS OtherAward,
Sum(CompUsed) AS CompUsed,
Sum (TheorWin) AS ClubADW,
CasinoID
FROM
dbo.viewAccum1
WHERE
Accumulator = 'DP'
GROUP BY
PlayerID,
CasinoID
) A
ON P.PlayerID = A.PlayerID
LEFT JOIN
dbo.ViewAccum1Day X
ON A.PlayerID = X.PlayerID
Go to Top of Page

Unknownuser1
Starting Member

7 Posts

Posted - 2012-05-31 : 04:48:02
Hi There,

Yes "http://A.Travel," was a type when pasting....

Thanks Re-formatted version looks much clearer.
However still getting the same error:

Msg 207, Level 16, State 1, Line 71
Invalid column name 'ClubADW'.


I have checked and the table (dbo.ViewAccum1Day)
does have the field as -- TheorWin

So a little lost on this one...

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-05-31 : 12:08:44
Well that is not the column that is being referenced. Let me pair down the query a bit.
SELECT DISTINCT 
A.CasinoID,
X.ClubADW
FROM
dbo.ViewCustomers P
INNER JOIN
(
SELECT
Sum (TheorWin) AS ClubADW,
CasinoID
FROM
dbo.viewAccum1
) A
ON P.PlayerID = A.PlayerID
LEFT JOIN
dbo.ViewAccum1Day X
ON A.PlayerID = X.PlayerID
Go to Top of Page
   

- Advertisement -