SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query from 2 tables from 2 databases


Query from 2 tables from 2 databases

Author
Message
ahsal
ahsal
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 10
hello,

below is my query, the query result is2 columns from2 different databases
the columns "OriginalPremium" & "Share"

Share is always staticin EACH query run but in some queries are 50 some are 60..etc
the question: how to get the SUM of the "OriginalPremium"in my Query result ??


SELECT OriginalPremium, Users.Share
FROM [URSYSTEM].[PolicyTable] a inner join [LoginDB].[ahsal-UR].Users on Producer = LoginDB.[ahsal-UR].Users.Username


Now my output is:
-----------------------------------
|OriginalPremium | Share |
-----------------------------------
| 10 | 50 |
-----------------------------------
| 15 | 50 |
-----------------------------------
| 15 | 50 |
-----------------------------------


what i want is:
-----------------------------------
|OriginalPremium | Share |
-----------------------------------
| 30 | 50 |
-----------------------------------



by the way i used Sum(OriginalPremium) as Total

it shows me this error:

Msg 8120, Level 16, State 1, Line 3
Column 'LoginDB.ahsal-UR.Users.Share'is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Thom A
Thom A
SSC Guru
SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)

Group: General Forum Members
Points: 78217 Visits: 19840
Welcome to SSC.

For any of the users here to answer your question(s) we'll need some things from you. In this case, we need DDL and Samples Data. There's a link in my signature on how you can do that.

On the matter of the error, the error message is giving you the answer. When using an aggregate, you will often need to include a GROUP BY clause. Have a Google, and see what you can learn there. If you have any question on it, then please do ask.

Once you've posted back with some DDL and sample data, someone will be able to help you further.


Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
ahsal
ahsal
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 10
Thom A - Saturday, December 9, 2017 8:57 AM
Welcome to SSC.

For any of the users here to answer your question(s) we'll need some things from you. In this case, we need DDL and Samples Data. There's a link in my signature on how you can do that.

On the matter of the error, the error message is giving you the answer. When using an aggregate, you will often need to include a GROUP BY clause. Have a Google, and see what you can learn there. If you have any question on it, then please do ask.

Once you've posted back with some DDL and sample data, someone will be able to help you further.


people are helping each other. for sure i did my web search then i posted...
here is the answer in case you wanted one day
https://forums.asp.net/t/2133098.aspx?SQL+Query+to+be+one+record
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (847K reputation)SSC Guru (847K reputation)SSC Guru (847K reputation)SSC Guru (847K reputation)SSC Guru (847K reputation)SSC Guru (847K reputation)SSC Guru (847K reputation)SSC Guru (847K reputation)

Group: General Forum Members
Points: 847162 Visits: 46672
ahsal - Saturday, December 9, 2017 2:31 PM
Thom A - Saturday, December 9, 2017 8:57 AM
Welcome to SSC.

For any of the users here to answer your question(s) we'll need some things from you. In this case, we need DDL and Samples Data. There's a link in my signature on how you can do that.

On the matter of the error, the error message is giving you the answer. When using an aggregate, you will often need to include a GROUP BY clause. Have a Google, and see what you can learn there. If you have any question on it, then please do ask.

Once you've posted back with some DDL and sample data, someone will be able to help you further.


people are helping each other. for sure i did my web search then i posted...
here is the answer in case you wanted one day
https://forums.asp.net/t/2133098.aspx?SQL+Query+to+be+one+record


You were lucky to find someone that overlooked the fact that you didn't even post the query that gave you the error. They must be hungry for points. Wink

Glad you got someone to answer and that it worked out for you.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search