SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to make daily report
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

boboalex
Starting Member

Indonesia
2 Posts

Posted - 06/04/2012 :  03:00:13  Show Profile  Reply with Quote
I have a table with 3 column (date, company, actual) like this.
20120101 A 100.00
20120101 B 100.00
20120101 C 1000.00
20120102 B 200.00
20120102 A 200.00

I want to make report with all company eventhough there's no transaction in that day with result like this
20120101 A 100.00
20120101 B 100.00
20120101 C 1000.00
20120102 A 200.00
20120102 B 200.00
20120102 C 0
How to make this report?

regards

Alex

webfred
Flowing Fount of Yak Knowledge

Germany
8529 Posts

Posted - 06/04/2012 :  06:46:50  Show Profile  Visit webfred's Homepage  Reply with Quote
I think you will also have a "company-table".
Then you need a calendar table with all needed dates without gaps (search for calendar table here or in google).
Then you can join the company-table, the calendar-table and the above given table.



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

boboalex
Starting Member

Indonesia
2 Posts

Posted - 06/04/2012 :  06:53:13  Show Profile  Reply with Quote
hi webfred,
i only have that table
is that possible with 1 table?
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8529 Posts

Posted - 06/04/2012 :  06:57:52  Show Profile  Visit webfred's Homepage  Reply with Quote
You can generate a derived table with distinct company from your existing table as a company-table.
As I told you, you can search for calendar table to get examples on how to generate this.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48012 Posts

Posted - 06/04/2012 :  10:10:49  Show Profile  Reply with Quote
see

http://visakhm.blogspot.com/2010/02/generating-calendar-table.html

use it like

SELECT m.[Date],m.company,COALESCE(n.actual,0.00) AS actual
FROM
(
SELECT f.[Date],t.company
FROM dbo.CalendarTable(@startdate,@enddate,0,0) f
cross join (select distinct company from yourtable)t
)m
LEFT JOIN YourTable n
ON n.company = m.company
AND n.[Date] = m.[Date]


@startdate and @enddate represent date range between which you want transactions to be listed. it can be parameterised to take values as input from user
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Edited by - visakh16 on 06/04/2012 10:11:58
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000