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 |
krishna.kanigelpula
Starting Member
5 Posts |
Posted - 2011-06-13 : 22:51:42
|
Hi Team,I am very new to Databases concepts and I have an requirement in my hand which is far away to my technical knowledge. Could, some one please help with this.The requirements is I need an the following Columns for the customerNo that is i/p via the comma sepearated file:CustomerNoDateDetailsDate = The minimum date between the following:The first occurance of a BT incident and the first appointment date where the appontment is with any one of (BT Centre,Test House, any provider where there is the word BT anywhere in the name)Details = if the date above comes from incidents then the details column will provide the incident details. If the date comes from an appointment then the details will provide a concatenation of the provider the appointment is with and the status of the appointment (e.g. Foundation House - Attended).Could, you some one be able to shed some light on this request.There are Two tables this query is talking about Incident and Appoinemt table and the connection between these two should be on the customerno.Incident table has Customerno, Incident Date, Incident detailsAppointment Table has Customerno, Appoint with, Appoitn status, App DateThanks. |
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2011-06-14 : 01:20:24
|
krishna.kanigelpula - could you post the DDL of the tables. And any query you have tried on - even if it's throwing a error.Jack Vamvas--------------------http://www.sqlserver-dba.com |
 |
|
krishna.kanigelpula
Starting Member
5 Posts |
Posted - 2011-06-14 : 01:55:20
|
Hi Jack,Thanks for your time and effort on this requestI need the o/p to be CustomerNo, Date, Detail ans the specs are as follwsDate Column:A customer can have multiple BT Incidents and we can identify using the [iSuspectedBT] column in the Incident table and we have take the minimum of the BT incident for a given customers and then compare it with the Apptdate ( A customer has an appointment relation to BT, even there is an incident or not. So, the way we can find it is with keyword search in the APPT with column with the "%BT%','%Test House%' etc).In short the date feild should get populated with the Minimum of the ( Min(Appdate), Min(IncidentDate)) meeting the above criteria.Details Column:If the minimum date for that BT is from the Incident table, then the detail section should get populated with the Incident Details, if not it should get populated with the Appointment details.So, at the end of the day we need to have only one record for each patient ( irrespective, whether he has the multiple appointment or multiple incidents records), whichever occurs first.Appointment TableCREATE TABLE [dbo].[Appointment]([CustomerID] [nvarchar](10) PK,[iApptID] [int] NOT NULL, [dtApptDate] [datetime] NULL,[nvchApptWith] [nvarchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,[nvchApptDetails] [nvarchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,)Incident TableCREATE TABLE [dbo].[Incidents]([iIncidentID] [int] NOT NULL,[CustomerID] [nvarchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,[iIncidentNo] [int] NULL,[dtIncidentDate] [datetime] NULL,[nvchIncidentDetails] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,[iSuspectedBT] [int] NULL -- This is a flag 1 represents the BT incident) Appointment Table sample dateCustomerNo ApptDate App Detail Appt WithXYZ 09-Jun-10 Test1 Delhi Medical centreABC 14-May-08 Test2 BT medical centreDFG 04-Dec-07 Test3 Term Test HouseIncident Table Sample DataCustomerNo Incident Date Incident Detial SuspectBTXYZ 12-Jan-08 Detail1 1DGF 01-Jan-05 Detail2 1ABC 12-May-08 Detail3 1 sdfg 14-May-08 Detail4 0Say for instance, if we are looking for the customer ABC, he has entry in both Incident and Appointment table meeting the criteria, as the SUspectBT =1 in incident tale and the Apptwith in the appointment table has a keyword "BT%'So, the output would beCustomerNo Date DetailABC 12-May-08 Detail3.Thanks. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-06-14 : 03:21:48
|
[code]select *from Appointment a inner join Incident i ON a.CustomerID = i.CustomerIDwhere i.SuspectBT = 1and a.Detail like 'BT%'[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|