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 |
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-03-08 : 10:10:48
|
When I run this query the below sample data comes out as concurr.Select p.Reg, p.FO, p.CLMS, Age = Datediff(day,max(p.filedate), getdate()), FileDate = max(FileDate),Min(ddsrcpt) AS DDSReceipt, DDSAge = Datediff(day,min(p.ddsrcpt), getdate()), Title = case when min(p.Title) <> max(p.Title) then 'Concurr' else min(p.Title) endFrom testdata pwhere p.doc ='s09'AND ( Datediff(DAY, filedate, Getdate()) > 300 ) Group By p.fo, p.Reg, p.DOC, p.CLMSorder by clms This one comes out concurr.clms file date DDS DDSCode DDSReceipt Title215689 03/24/2011 DE S09 03/24/2011 T2215689 03/24/2011 DE S09 03/25/2011 T16 This one doesn't come out as concurr. How can I change the code to make it come out concurr since they are duplicates but the titles are different. Only thing on this one is the DDSReceipt dates are the same.clms file date DDS DDSCode DDSReceipt Title254698 05/05/2011 DE S09 06/10/2011 T2254698 05/06/2011 DE S09 06/10/2011 T16 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-08 : 10:13:32
|
| order by clms,[file date]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-03-08 : 10:30:29
|
| Thanks but that still didn't work. They both are still coming up. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-08 : 10:32:39
|
| can you explain what should be your output?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-03-08 : 10:45:17
|
I have duplicate clms in the database but the title could be T2 or T16. If there are duplicate clms but their titles are different then the query checks it at this part.Title = case when min(p.Title) <> max(p.Title) then 'Concurr' else min(p.Title) endso for this record it comes out concurr based of the above case statement:clms file date DDS DDSCode DDSReceipt Title211568 04/15/2011 DE S09 03/02/2012 T2254698 04/15/2011 DE S09 08/22/2011 T16 Now this record doesn't come out concurr but it should:clms file date DDS DDSCode DDSReceipt Title215689 03/24/2011 DE S09 03/24/2011 T2215689 03/24/2011 DE S09 03/25/2011 T16 So I need another case statement if clms, filedate and ddsreceipt date are the same then make them concurr (only one record to come out and call it concurr)This one doesn't come out as concurr either:clms file date DDS DDSCode DDSReceipt Title254698 05/05/2011 DE S09 06/10/2011 T2254698 05/06/2011 DE S09 06/10/2011 T16 So I would need another case statement to check if clms, and DDSReceipt date is the same and filedate isn't then make that record concurr. I hope this make sense. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-08 : 10:53:47
|
| how have you applied GROUP by? can you show your group by part also?As per your current case statement i would have expected it to work fine unless you've included dates also in group by------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-03-08 : 10:57:41
|
Here's group by in bold:Select p.Reg, p.FO, p.CLMS, Age = Datediff(day,max(p.filedate), getdate()), FileDate = max(FileDate),Min(ddsrcpt) AS DDSReceipt, DDSAge = Datediff(day,min(p.ddsrcpt), getdate()), Title = case when min(p.Title) <> max(p.Title) then 'Concurr' else min(p.Title) endFrom testdata pwhere p.doc ='s09'--@docAND ( Datediff(DAY, filedate, Getdate()) > 300 ) Group By p.fo, p.Reg, p.DOC, p.CLMSorder by clms, filedate |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-03-08 : 11:00:15
|
| When I added p.filedate and p.ddsrcpt none of them came out as concurr |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-08 : 11:01:14
|
| hmm..i see lots of other fields here (p.Reg, p.FO etc). unless you show there values also we wont be able to make out why its behaving sowhy not post all relevant info in single post itself so that we can avoid some of these iterationssee how to post a question clearly with required datahttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-08 : 11:02:35
|
quote: Originally posted by JJ297 When I added p.filedate and p.ddsrcpt none of them came out as concurr
thats obvious as value of filedate and ddsrcpt were different for the ones which didnt work. it will get split up to two groups and the condition will not hold good any more in case expression------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-03-08 : 11:08:55
|
Sorry I got rid of the joins. Here's the entire query:Select p.DOC, p.Reg, n.RegionAcronym, p.FO, p.CLMS, Age = Datediff(day,max(p.filedate), getdate()), FileDate = max(FileDate),o.mailingaddressstate as DDS,o.ddsofficecode as DDSCode,Min(ddsrcpt) AS DDSReceipt, DDSAge = Datediff(day,min(p.ddsrcpt), getdate()), Title = case when min(p.Title) <> max(p.Title) then 'Concurr' else min(p.Title) endFrom pendingdds p join Offices.dbo.OfficeCodes d on d.officecode = p.docjoin natdocfile non n.doc = p.fojoin offices.dbo.doorsinfo oon o.officecode = p.fowhere p.doc ='s09' AND ( Datediff(DAY, filedate, Getdate()) > 300 ) Group By p.fo, p.Reg, n.regionacronym,p.DOC, p.CLMS, o.ddsofficecode,o.mailingaddressstateorder by clms, age desc |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-08 : 11:44:08
|
| so...how do you expect us to guess the values for other fields?See link posted and give data in required format if you really want someone to help you out!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-03-08 : 12:01:59
|
| Okay I have to make all the tables that join together and put data into them. Stay tuned I will do that. That would help huh. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-08 : 12:33:53
|
| ok..that will make it clear for us------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-03-08 : 14:03:52
|
Here's the join tables information:CREATE TABLE [dbo].[DoorsInfo]( [DDSOfficeCode] [varchar](255) NULL, ) ON [PRIMARY]GOSET ANSI_PADDING OFFinsert into DoorsInfoselect 'S09' union allselect 'S08'CREATE TABLE [dbo].[natdocfileb]( [RegionAcronym] [char](3) NULL, ) ON [PRIMARY]GOSET ANSI_PADDING OFFinsert into natdocfilebselect 'PHI' union allselect 'BOS'CREATE TABLE [dbo].[OfficeCodes]( [OfficeCode] [char](3) NOT NULL, ) ON [PRIMARY]GOSET ANSI_PADDING OFFinsert into officecodesselect '029' union allselect '268' union allselect 'B68' union allselect 'C80' union allselect '001' union allselect '002' union allselect '003' union allselect '009' CREATE TABLE [dbo].[TestData]( [fo] [varchar](3) NOT NULL, [reg] [varchar](3) NULL, [doc] [varchar](4) NOT NULL, [clms] [char](6) NOT NULL, [cos] [char](6) NOT NULL, [FileDate] [datetime] NULL, [DDSRcpt] [datetime] NULL, [Title] [varchar](3) NOT NULL, CONSTRAINT [PK_testdata] PRIMARY KEY CLUSTERED ( [fo] ASC, [clms] ASC, [cos] ASC, [Title] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFinsert into testdataselect '029', 'c', 's09', '129698', '129698', '03/24/2011', '03/24/2011', 'T2' union allselect 'C80', 'c', 's09', '129698', '129698', '03/24/2011', '03/25/2011', 'T16' union allselect '268', 'c', 's09', '005747', '005747', '06/20/2011', '06/20/2011', 'T16' union allselect '268', 'c', 's09', '005747', '005747', '06/20/2011', '06/20/2011', 'T2' union allselect 'C80', 'c', 's09', '698745', '698745', '05/11/2011', '05/12/2011', 'T16' union allselect 'B68', 'c', 's09', '978456', '978456', '06/03/2011', '01/18/2012', 'T16' union allselect '029', 'c', 's09', '125987', '125987', '05/05/2011', '06/10/2011', 'T2' union allselect '268', 'c', 's09', '125987', '125987', '05/06/2011', '06/10/2011', 'T16' Here's the query:Select p.DOC, p.Reg, n.RegionAcronym, p.FO, p.CLMS, --getting age of case when it was filed Age = Datediff(day,max(p.filedate), getdate()), FileDate = max(FileDate),o.mailingaddressstate as DDS,o.ddsofficecode as DDSCode,Min(ddsrcpt) AS DDSReceipt,--getting ddsage of case when it was receipted into the dds DDSAge = Datediff(day,min(p.ddsrcpt), getdate()),--if Title = case when min(p.Title) <> max(p.Title) then 'Concurr' else min(p.Title) end From testdata p join Offices.dbo.OfficeCodes d on d.officecode = p.docjoin natdocfile non n.doc = p.fojoin offices.dbo.doorsinfo oon o.officecode = p.fowhere p.doc ='s09'--@doc AND ( Datediff(DAY, filedate, Getdate()) > 300 ) Group By p.fo, p.Reg, n.regionacronym,p.DOC, p.CLMS, o.ddsofficecode,o.mailingaddressstateorder by clms, age desc |
 |
|
|
|
|
|
|
|