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.
| Author |
Topic |
|
calvinfoo
Posting Yak Master
129 Posts |
Posted - 2012-06-25 : 06:17:41
|
| Dear Gurus,I have a table storing Leave Application. Employees apply for leave, The table is simplified as below: Tbl_LAs 1. LA_ID2. LA_Date3. LA_EmpIDLA_ID | LA_Date | LA_EmpID1 | 2012/06/25 | 0012 | 2012/06/25 | 0023 | 2012/06/26 | 0024 | 2012/06/29 | 003etc...It simply stores the Date and Employee ID. Now, I want to display the data it on a calendar form, just a long horizontal:The data examples above will produce the output as below.EmpID | 25/06 | 26/06 | 27/06 | 28/06 | 29/06 001 | 1 | 0 | 0 | 0 | 0002 | 1 | 1 | 0 | 0 | 0003 | 0 | 0 | 0 | 0 | 1Would it be possible to do that with a stored procedure? Any examples or guide lines anyone done that?I need to display this table in ASP format, but with my limited knowledge on SQL, that I will loop at least two SQLs by:1. Use 1st sql to get all EmpID who is applying leave on the selected date range2. Loop 2nd sql to run each employee's leave within the selected date range, then display as YES if the record matches the date, 3. Move 2nd sql to the next record until the end of the date range4. Loop 1st sql to get send employee and loop 2nd sql again until all employees are displayed.Is there any better and easier way? |
|
|
sakthiananth
Starting Member
2 Posts |
Posted - 2012-06-25 : 06:32:46
|
| Sorry no idea ):Let's keep learning to explore towards future.): |
 |
|
|
xhostx
Constraint Violating Yak Guru
277 Posts |
Posted - 2012-06-25 : 08:18:02
|
| Hey,Look at this example, it is easy to understand.SELECT YEAR(ord.OrderDate) YEAR, SUM(CASE prod.CategoryID WHEN 1 THEN det.UnitPrice * det.Quantity ELSE 0 END) Beverages, SUM(CASE prod.CategoryID WHEN 2 THEN det.UnitPrice * det.Quantity ELSE 0 END) Condiments, SUM(CASE prod.CategoryID WHEN 3 THEN det.UnitPrice * det.Quantity ELSE 0 END) Confections, SUM(CASE prod.CategoryID WHEN 4 THEN det.UnitPrice * det.Quantity ELSE 0 END) [Dairy Products], SUM(CASE prod.CategoryID WHEN 5 THEN det.UnitPrice * det.Quantity ELSE 0 END) [Grains/Cereals], SUM(CASE prod.CategoryID WHEN 6 THEN det.UnitPrice * det.Quantity ELSE 0 END) [Meat/Poultry], SUM(CASE prod.CategoryID WHEN 7 THEN det.UnitPrice * det.Quantity ELSE 0 END) Produce, SUM(CASE prod.CategoryID WHEN 8 THEN det.UnitPrice * det.Quantity ELSE 0 END) Seafood FROM Orders ord INNER JOIN [Order Details] det ON det.OrderID = ord.OrderID INNER JOIN Products prod ON prod.ProductID = det.ProductID GROUP BY YEAR(ord.OrderDate) ORDER BY YEAR(ord.OrderDate)Good luck,kml |
 |
|
|
calvinfoo
Posting Yak Master
129 Posts |
Posted - 2012-06-25 : 20:29:48
|
quote: Originally posted by xhostx Hey,Look at this example, it is easy to understand.
Interesting... What if the column name is dynamic? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-06-25 : 21:08:05
|
then you will need to use Dynamic SQL KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|
|