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
 Static information in a query result

Author  Topic 

rc1138
Starting Member

35 Posts

Posted - 2010-08-18 : 19:29:54
Hi,

I was hoping someone can help me regarding a query that will populate a certain column with static information while having the rest of the columns ordered based on that static column

The table structures are

Table 1 (Where Budget1 Resides)

Id int
SiteNbr int
acctNum int
amt numeric(9, 2)

Id SiteNbr acctNum amt
1 34 1903 4.00

Table 2 (Where Budget2 Resides)

Id int
SiteNbr int
acctNum int
amt numeric(9, 2)

Id SiteNbr acctNum amt
1 34 1904 12.00
2 34 1903 5.00

Table 3 (Where the account names reside)

Id int
acctNum int
acctDesc varchar(75)

Id acctNum acctDesc
1 1903 Maintenance
2 1904 Meals
3 1905 Wages

acctDesc contains a static number of items it will always contain (Maintenance, Meals, Wages)


SELECT A.acctDesc AS Account, G.amt AS [Budget1], S.amt AS [Budget2]
FROM Table3 AS A INNER JOIN
Table2 AS G ON A.Id = G.Id INNER JOIN
Table1 AS S ON A.Id = S.Id
Where SiteNbr = '34'

I tried the above query but am only getting empty headers - I guess I require more subqueries per column ?

The output I am looking for should be like this

acctDesc Budget1 Budget2
Maintenance 0.00 5.00
Meals 4.00 12.00
Wages 0.00 0.00

Any help would be greatly appreciated

nathans
Aged Yak Warrior

938 Posts

Posted - 2010-08-18 : 20:16:10
Check this sample out, Im not sure if you had a mistake in your desired resultset or if I was confused by your question.

declare @Table1 table (Id int, SiteNbr int, acctNum int, amt numeric(9, 2))
insert into @Table1
select 1, 34, 1903, 4.00

declare @Table2 table (Id int, SiteNbr int, acctNum int, amt numeric(9, 2) )
insert into @Table2
select 1, 34, 1904, 12.00 union all
select 2, 34, 1903, 5.00

declare @Table3 table (Id int, acctNum int, acctDesc varchar(75))
insert into @Table3
select 1, 1903, 'Maintenance' union all
select 2, 1904, 'Meals' union all
select 3, 1905, 'Wages'

select ttt.acctDesc,
isnull(tt.amt, 0.00) as [Budget1],
isnull(t.amt, 0.00) as [Budget2]
from @Table3 ttt
left
join @Table2 tt on
ttt.Id = tt.Id
left
join @Table1 t on
ttt.Id = t.Id
order
by 1


Nathan Skerl
Go to Top of Page

rc1138
Starting Member

35 Posts

Posted - 2010-08-18 : 21:15:37
Hi Nathan,

Thanks for the reply

The tables are already created and populated - What my question pretty much was how to format the query to make my output look like this

acctDesc Budget1 Budget2
Maintenance 0.00 5.00
Meals 4.00 12.00
Wages 0.00 0.00

Based on the 3 tables mentioned in the question

Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2010-08-18 : 22:39:49
I just wrote it out for you into temporary table variables so we could work with the same dataset.

Did you run what I posted? Look at the output and let me know if thats what you are looking for.

Nathan Skerl
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-19 : 13:39:13
quote:
Originally posted by rc1138

Hi Nathan,

Thanks for the reply

The tables are already created and populated - What my question pretty much was how to format the query to make my output look like this

acctDesc Budget1 Budget2
Maintenance 0.00 5.00
Meals 4.00 12.00
Wages 0.00 0.00

Based on the 3 tables mentioned in the question





SELECT t3.acctDesc,
SUM(CASE WHEN Category='Budget1' THEN amt ELSE 0.00 END) AS Budget1,
SUM(CASE WHEN Category='Budget2' THEN amt ELSE 0.00 END) AS Budget2
FROM Table3 t3
LEFT JOIN (SELECT siteNbr,acctNum,amt,'Budget1' AS Category
FROM Table1
UNION ALL
SELECT siteNbr,acctNum,amt,'Budget2'
FROM table2) t
ON t.acctNum=t3.acctNum
GROUP BY t3.acctDesc


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

Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2010-08-19 : 14:52:36
I was confused because example query joined on Id across all tables.
Go to Top of Page
   

- Advertisement -