SQL - Assigning a query result to variable and then applying in a column

Dear Experts,

Being not sure whether this question is easy or hard to complete, basically having a simple query

SELECT
	Count(DISTINCT AP.LFB1.BUKRS) AS TotalCCs
FROM
	AP.LFB1

Open in new window


which results a single value 17 (so many Company codes exist in SAP)

I would like to use this value in the later queries, could you please advise about the best method regarding that?

Should be maybe assigned to a variable somehow and applying in the formulas?

Thanks in advance,
LVL 1
csehzIT consultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee SavidgeCommented:
This?

declare @iTotalCCs int

SELECT
	@iTotalCCs = Count(DISTINCT AP.LFB1.BUKRS) AS TotalCCs
FROM
	AP.LFB1

select iTotalCCs

Open in new window

0
csehzIT consultantAuthor Commented:
Thanks for the answer, basically running that code it brings an error message


Incorrect syntax near the keyword 'AS'.


Could you please advise where that is incorrect in the syntax?
0
Lee SavidgeCommented:
declare @iTotalCCs int

SELECT
	@iTotalCCs = Count(DISTINCT AP.LFB1.BUKRS)
FROM
	AP.LFB1

select iTotalCCs

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Dorababu MSenior Software EngineerCommented:
Are you looking something similar to this ?

DECLARE @Testtbl Table
(
	Id int,
	SName nvarchar(100)
)

INSERT INTO @Testtbl values(1,'Test'),(1,'Test'),(1,'Test'),
(1,'Test')

declare @iTotalCCs int

SELECT @iTotalCCs = x.count
FROM
( SELECT
	  COUNT ( DISTINCT(Id) )  count
FROM
	@Testtbl) as x

	SELECT @iTotalCCs

Open in new window

0
csehzIT consultantAuthor Commented:
That is brilliant, thank you for both of the solutions
0
Mark WillsTopic AdvisorCommented:
I know it has already been answered.... But...

You can also use CTE which is a common table expression. It really depends on how you plan to use that information.

Basically you Name a query, then you can use those results to do more. Bit like a subquery, except you do it first as a query.

;WITH TotalCC as
( SELECT Count(DISTINCT AP.LFB1.BUKRS) AS CountCCs
  FROM   AP.LFB1
) SELECT CountCCs from TotalCC    -- here I am selecting from the CTE named TotalCC, or could use the CTE like any other table

Open in new window


Those CTE's can be nested. But if it isnt a related / nested query, then declaring a variable means the variable can exists from query to query within a batch...
1
csehzIT consultantAuthor Commented:
Mark thank you for mentioning that option, basically I like in the variable kind of version that later it can be used in queries without the need of joins
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.