473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Help Needed For writting a query (SQl Server 2005)

Hi experts,

I am working on SQL Server 2005.
Now i have to write a query which will extract some information from a
table.
My main table is having few columns supose 3 columns.

EmpID Supervisor_ID Date_Of_Visit

Now records are entering in this tabl. Date of visit is date of
supervisor vist on which employee supervisor visited his work.
Generally its on monthly basis. A new row is entered in the table on
every visit. But in some cases employee may be visited more than once
may be two times or 10 times depending on the situation, and for each
visit one new row is inserted in the table.

Now i have to extract the information from last one yearppose I am
running this query today In August 2008 then it should show the
employee id and his date of visit from September 2007 to August 2008.
All 12 date of visits should appear in row format.
There will be 13 columns in the result.
One for EmpID and rest 12 for date of visits Starting from DOV1, Dov2
till DOV12. And if there will be more than onve Date_Of_Visit for any
employee then it should come in the same month column seprated by
comma. And if there is no visit in any month then that columns should
appear with null value

for ex

EmpID Supervisor_ID Date_Of_Visit
1 300 20/08/2007
1 300 25/08/2007
1 300 29/08/2007
2 250 21/08/2007
1 300 21/09/2007
2 250 21/09/2007
1 300 21/10/2007
2 250 21/10/2007
1 300 21/11/2007
2 250 21/11/2007
1 300 21/12/2007
2 250 21/12/2007
1 300 21/01/2008
2 250 21/01/2008
1 300 21/02/2008
2 250 21/02/2008
2 250 21/05/2008
1 300 02/08/2008
1 300 11/08/2008
2 250 08/08/2008
2 250 19/08/2008
Now i have to write a query which will give result like below

EmpID DOV1 DOV2 DOV3
DOV4 DOV5 DOV6 DOV7 DOV8 DOV9 DOV10
DOV11 DOV12

1 20/08/2007,25/08/2007,29/08/2007 21/09/2007 21/10/2007
21/11/2007 21/12/2007 21/01/2008 21/02/2008
02/08/2008,11/08/2008

2 21/08/2007 21/09/2007 21/10/2007
21/11/2007 21/12/2007 21/01/2008 21/02/2008
08/08/2008,19/08/2008

How i can get this result, Please help me.

Any help wil be appriciated.

Regards
Dinesh

Aug 23 '08 #1
2 1887
On Aug 23, 4:26*pm, Dinesh <dinesh...@gmail.comwrote:
Hi experts,

I am working on SQL Server 2005.
Now i have to write a query which will extract some information from a
table.
My main table is having few columns supose 3 columns.

EmpID *Supervisor_ID * *Date_Of_Visit

Now records are entering in this tabl. Date of visit is date of
supervisor vist on which employee supervisor visited his work.
Generally its on monthly basis. A new row is entered in the table on
every visit. But in some cases employee may be visited more than once
may be two times or 10 times depending on the situation, *and for each
visit one new row is inserted in the table.

Now i have to extract the information from last one yearppose I am
running this query today In August 2008 *then it should show the
employee id and his date of visit from September 2007 to August 2008.
All 12 date of visits should appear in row format.
There will be 13 columns in the result.
One for EmpID and rest 12 for date of visits Starting from DOV1, Dov2
till DOV12. And if there will be more than onve Date_Of_Visit for any
employee then it should come in the same month column seprated by
comma. And if there is no visit in any month then that columns should
appear with null value

for ex

EmpID *Supervisor_ID * *Date_Of_Visit
1 * * * 300 * * * * * * 20/08/2007
1 * * * 300 * * * * * * 25/08/2007
1 * * * 300 * * * * * * 29/08/2007
2 * * * 250 * * * * * * 21/08/2007
1 * * * 300 * * * * * * 21/09/2007
2 * * * 250 * * * * * * 21/09/2007
1 * * * 300 * * * * * * 21/10/2007
2 * * * 250 * * * * * * 21/10/2007
1 * * * 300 * * * * * * 21/11/2007
2 * * * 250 * * * * * * 21/11/2007
1 * * * 300 * * * * * * 21/12/2007
2 * * * 250 * * * * * * 21/12/2007
1 * * * 300 * * * * * * 21/01/2008
2 * * * 250 * * * * * * 21/01/2008
1 * * * 300 * * * * * * 21/02/2008
2 * * * 250 * * * * * * 21/02/2008
2 * * * 250 * * * * * * 21/05/2008
1 * * * 300 * * * * * * 02/08/2008
1 * * * 300 * * * * * * 11/08/2008
2 * * * 250 * * * * * * 08/08/2008
2 * * * 250 * * * * * * 19/08/2008

Now i have to write a query which will give result like below

EmpID * * * * * * * DOV1 * * * * * * * * DOV2 * * * DOV3
DOV4 * * *DOV5 * * * DOV6 * * * *DOV7 * * * *DOV8 *DOV9 DOV10
DOV11 * * * DOV12

1 * * *20/08/2007,25/08/2007,29/08/2007 21/09/2007 21/10/2007
21/11/2007 21/12/2007 *21/01/2008 *21/02/2008
02/08/2008,11/08/2008

2 * * * * * * * *21/08/2007 * * * * * * 21/09/2007 21/10/2007
21/11/2007 21/12/2007 *21/01/2008 *21/02/2008
08/08/2008,19/08/2008

How i can get this result, Please help me.

Any help wil be appriciated.

Regardstabse
Dinesh
Dear Sir,
Thanks for the early reply.
I am sneding you the my create table and insert statements for your
convenience. I can not see the Northwind data base so some difficulty
to understand the logic, becuase i am not aware of that databse. given
below is the Create table and insert statment
CREATE TABLE [dbo].[Superviser_Visit](
[EmpID] [int] NOT NULL,
[Supervisor_ID] [int] NOT NULL,
[Date_Of_Visit] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS
NULL
)

INSERT INTO Superviser_Visit VALUES (1, 300, 20/08/2007)
INSERT INTO Superviser_Visit VALUES (1, 300, 25/08/2007)
INSERT INTO Superviser_Visit VALUES (1, 300, 29/08/2007)
INSERT INTO Superviser_Visit VALUES (2, 250, 21/08/2007)
INSERT INTO Superviser_Visit VALUES (1, 300, 21/09/2007)
INSERT INTO Superviser_Visit VALUES (2, 250, 21/09/2007)
INSERT INTO Superviser_Visit VALUES (1, 300, 21/10/2007)
INSERT INTO Superviser_Visit VALUES (2, 250, 21/10/2007)
INSERT INTO Superviser_Visit VALUES (1, 300, 21/11/2007)
INSERT INTO Superviser_Visit VALUES (2, 250, 21/11/2007)
INSERT INTO Superviser_Visit VALUES (1, 300, 21/12/2007)
INSERT INTO Superviser_Visit VALUES (2, 250, 21/12/2007)
INSERT INTO Superviser_Visit VALUES (1, 300, 21/01/2008)
INSERT INTO Superviser_Visit VALUES (2, 250, 21/01/2008)
INSERT INTO Superviser_Visit VALUES (1, 300, 21/02/2008)
INSERT INTO Superviser_Visit VALUES (2, 250, 21/02/2008)
INSERT INTO Superviser_Visit VALUES (2, 250, 21/05/2008)
INSERT INTO Superviser_Visit VALUES (1, 300, 02/08/2008)
INSERT INTO Superviser_Visit VALUES (1, 300, 11/08/2008)
INSERT INTO Superviser_Visit VALUES (2, 250, 08/08/2008)
INSERT INTO Superviser_Visit VALUES (2, 250, 19/08/2008)
So if possible please give some idea to me now.
Thanks for the help.

Regards
Dinesh
Aug 24 '08 #2
Dinesh (di*******@gmail.com) writes:
Hi Erland,
Why i am getting the error if i am trying to use this query with a
table which is having empID as primary key.
Suppose i am having one employee table and that contains two columns
EmpID and EmpName.
Now if i am writting the query

Select E.EmpName,XYZ.DOV1,XYZ.DOV2,XYZ.DOV3,XYZ.DOV4,XYZ. DOV5,
XYZ.DOV6,XYZ.DOV7,XYZ.DOV8,XYZ.DOV9,XYZ.DOV10,XYZ. DOV11,XYZ.DOV12
From Employee As E Left outer join
(
DECLARE @today datetime
SELECT @today = convert(char(8), getdate(), 112)
What are the variable declarations doing in the middle of the
query?

I'm sorry, but I feel obliged to rant a bit here. My intention with
posting answers to questions in SQL forums is not only to give
direct answers, but also help people to help themselves.

The query I posted is fairly advanced, and employs several tricks
in the book. But the problem you had also called for those tricks.

If you want to play the role of the parrot, and just take the query
as it is, with no intention of understanding what is doing, I have
failed.

To start with, why would you join this query with Employees in the first
place? The Employees table is already in the query, but as you did
not provide it the script, I used a dummy, the derived table E. And
I did point this out in my previous post.

So I'm afraid that you will have to spend some time to understand how
this query works and why it works. After all, you are the one who will
be responsible for it and for maintaining it, not me.

As for the variable, I initially added a variable for my Northwind
example, as Northwind only has data in the 1990s. When I posted my
second query, using your tables, I maintained the variable as it
still seemed handy. But you could easily replace it in the query
if you prefer.
FROM (SELECT EmpID = 1 UNION ALL SELECT 2) AS E
So here is the place where you should put in your Employees table.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Aug 26 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
5
by: - | last post by:
Hello to all I need help on some sql statements and wondering is anyone out there is able to help. I've extracted some data from the database into a new table consisting of 4 fields CustomerID...
13
by: Mike | last post by:
Normally scheduling a job is a very elementary operation but for some hidden reason I've been unable to schedule a job which runs on a 28 day cycle, even though I have at least 16 other jobs...
0
by: jaggee | last post by:
Hello, This is regarding log analysis of a web system, I am finding my backend SQL programming has taken so much of time to process the application due to following quires for a log table having...
4
by: T. Wintershoven | last post by:
Hi Can someone please tell me whats wrong with the last line of the query below. The first three lines work fine but when i add the fourth line i get an error message (see text at ERROR...
4
by: Pumkin | last post by:
Hello guys, I need help in something as I don't know if it is possible what I want. I have a select like this... SELECT Cod1 as SQL, Cod2 as Oracle FROM table and I need to sort by alias SQL...
0
by: mandrax7 | last post by:
I used Visual Studio 6 and I had made a simple program to access a SQL 2000 database, now I'm trying to do the same thing with Visual studio 20005, but.....nothing works. I used to connect with...
2
by: Dinesh | last post by:
Hi experts, I am working on SQL server 2005 reporting services and i am getting a problem in writting a query. Situation is given below. There is one table in database Named Child Now i...
0
by: Dinesh | last post by:
Hi experts, I am working on SQL Server 2005. Now i have to write a query which will extract some data from 3 to 4 tables. My main table is having few columns supose 4 columns. EmpNo EmpName...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.