| Author |
Topic |
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-10-18 : 14:13:27
|
Could someone tell me why I'm getting incorrect syntax near it's referring to all of these lines:SELECT fo, (count(cossn), 0) AS [TotPenFODDS], 0 AS [Pend250], 0 AS [Pend300], 0 AS [Pend351],0 AS [Pend400]Here'd the entire query:SELECT fo, (count(cossn), 0) AS [TotPenFODDS], 0 AS [Pend250], 0 AS [Pend300], 0 AS [Pend351],0 AS [Pend400]FROM T16pendallWHERE MFT_POSN1_CD='b' OR MFT_POSN1_CD='d' OR (MFT_POSN1_CD= 'a' ANDAGED_ALIEN_RSW = 'y')GROUP BY foUNIONSELECT fo, (count(cossn), 0) AS [TotPenFODDS], 0 AS [Pend250], 0 AS [Pend300], 0 AS [Pend351], 0 AS [Pend400]FROM T16pendallWHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 250) AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 299) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 250) AND (AGED_ALIEN_RSW = 'y') AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 299GROUP BY foUNIONSELECT fo, (count(cossn), 0) AS [TotPenFODDS], 0 AS [Pend250], 0 AS [Pend300], 0 AS [Pend351],0 AS [Pend400]FROM T16pendallWHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 300) AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 350) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 300) AND (AGED_ALIEN_RSW = 'y') AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 350 GROUP BY foUNIONSELECT fo, (count(cossn), 0) AS [TotPenFODDS], 0 AS [Pend250], 0 AS [Pend300], 0 AS [Pend351],0 AS [Pend400]FROM T16pendallWHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 351) AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 400) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 351) AND (AGED_ALIEN_RSW = 'y') AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 400) GROUP BY foUNIONSELECT fo, (count(cossn), 0) AS [TotPenFODDS], 0 AS [Pend250], 0 AS [Pend300], 0 AS [Pend351],0 AS [Pend400]FROM T16pendallWHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 400) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 400) AND (AGED_ALIEN_RSW = 'y')GROUP BY fo |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-10-18 : 14:51:35
|
Thanks I got rid of that and it worked but not the way I wanted it too.I'm trying to get the counts for the following fields:TotPenFODDS, Pend250 (over 250 - 299 Days)Pend300 (over 300 - 350 Days)Pend351 (over 351 - 400 Days)Pend400 (over 400 Days)This works fine since I got rid of the (count(cossn), 0)SELECT fo, count(cossn) AS [TotPenFODDS], 0 AS [Pend250], 0 AS [Pend300], 0 AS [Pend351],0 AS [Pend400]FROM T16pendallWHERE MFT_POSN1_CD='b' OR MFT_POSN1_CD='d' OR (MFT_POSN1_CD= 'a' ANDAGED_ALIEN_RSW = 'y')GROUP BY foUNIONSELECT fo, count(cossn) AS [TotPenFODDS], 0 AS [Pend250], 0 AS [Pend300], 0 AS [Pend351], 0 AS [Pend400]FROM T16pendallWHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 250) AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 299) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 250) AND (AGED_ALIEN_RSW = 'y') AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 299)GROUP BY foUNIONSELECT fo, count(cossn) AS [TotPenFODDS], 0 AS [Pend250], 0 AS [Pend300], 0 AS [Pend351],0 AS [Pend400]FROM T16pendallWHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 300) AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 350) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 300) AND (AGED_ALIEN_RSW = 'y') AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 350) GROUP BY foUNIONSELECT fo, count(cossn) AS [TotPenFODDS], 0 AS [Pend250], 0 AS [Pend300], 0 AS [Pend351],0 AS [Pend400]FROM T16pendallWHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 351) AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 400) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 351) AND (AGED_ALIEN_RSW = 'y') AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 400) GROUP BY foUNIONSELECT fo, count(cossn) AS TotPenFODDS, 0 AS Pend250, 0 AS Pend300, 0 AS Pend351,0 AS Pend400FROM T16pendallWHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 400) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 400) AND (AGED_ALIEN_RSW = 'y')GROUP BY foBut it's giving me this, it's not giving me any numbers in the other fields. Is my query wrong?FO TotPenFODDS Pend250 Pend300 Pend351 Pend400B19 397 0 0 0 0704 341 0 0 0 0317 210 0 0 0 0K82 4 0 0 0 0399 643 0 0 0 0521 140 0 0 0 0856 1713 0 0 0 0918 587 0 0 0 0293 643 0 0 0 0887 1380 0 0 0 0 Here's the T16Pendall Table and some data to go into it:CREATE TABLE [dbo].[T16pendall]( [COSSN] [char](6) NOT NULL, [AGED_ALIEN_RSW] [char](1) NOT NULL, [MFT_POSN1_CD] [char](1) NOT NULL, [FO] [varchar](3) NOT NULL, [FLG_CDT] [datetime] NULL) ON [PRIMARY]select '458962', 'Y', 'D', '200', '3/15/1993' union allselect '454442', 'Y', 'D', 'A38', '9/13/1996' union allselect '569962', 'Y', 'D', '250', '12/21/1999' union allselect '895962', 'Y', 'D', 'C07', '7/10/2002' union allselect '999962', 'Y', 'D', '199', '2/3/2003' union allselect '452262', 'Y', 'D', '265', '1/22/2004' union allselect '458962', 'Y', 'D', '200', '5/5/2004' union allselect '458962', 'Y', 'D', '858', '6/8/2004' union allselect '458962', 'Y', 'D', '334', '7/19/2004' union allselect '458962', 'Y', 'D', '200', '4/8/2010'Thanks! |
 |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2010-10-18 : 15:25:54
|
| I think you will find this skeleton will run MUCH faster and be easier to read and maintain: SELECT fo, COUNT(cossn) AS pend_fodds_tot, SUM (CASE WHEN (DATEDIFF(DAY, flg_cdt, CURRENT_TIMESTAMP) BETWEEN 251 AND 298 THEN 1 ELSE 0 END) AS pend_250, SUM (CASE WHEN (DATEDIFF(DAY, flg_cdt, CURRENT_TIMESTAMP) BETWEEN 301 AND 349 THEN 1 ELSE 0 END) AS pend_300, SUM (CASE WHEN (DATEDIFF(DAY, flg_cdt, CURRENT_TIMESTAMP) BETWEEN 350 AND 399 THEN 1 ELSE 0 END) AS pend_351, SUM (CASE WHEN (DATEDIFF(DAY, flg_cdt, CURRENT_TIMESTAMP) > 400 THEN 1 ELSE 0 END) AS pend_400 FROM T16_Pend_All WHERE (mft_posn1_cd IN ('b' , 'd') OR (mft_posn1_cd = 'a' AND aged_alien_rsw = 'y') GROUP BY fo;--CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-10-18 : 15:28:27
|
Got a bit further...I got the first two queries to work:Select a.fo, Totpenfodds, Pend250From(SELECT fo, count(cossn) AS [TotPenFODDS]FROM T16pendallWHERE MFT_POSN1_CD='b' OR MFT_POSN1_CD='d' OR (MFT_POSN1_CD= 'a' ANDAGED_ALIEN_RSW = 'y')GROUP BY fo)ainner join(SELECT fo, count(cossn) AS [Pend250]FROM T16pendallWHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 250) AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 299) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 250) AND (AGED_ALIEN_RSW = 'y') AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 299)GROUP BY fo)bon a.fo = b.foIt gave me this which is what I want. FO Totpenfodds Pend250B19 397 3704 341 1317 210 1399 643 10521 140 2856 1713 2918 587 1293 643 9887 1380 53913 2153 6A33 402 13743 382 3 When I tried to add the other fields (Pend300, Pend351, and Pend400 I am getting incorrect syntax near 'FO' referring to the last line.Select a.fo, Totpenfodds, Pend250, Pend300, Pend351, Pend400From(SELECT fo, count(cossn) AS [TotPenFODDS]FROM T16pendallWHERE MFT_POSN1_CD='b' OR MFT_POSN1_CD='d' OR (MFT_POSN1_CD= 'a' ANDAGED_ALIEN_RSW = 'y')GROUP BY fo)ainner join(SELECT fo, count(cossn) AS [Pend250]FROM T16pendallWHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 250) AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 299) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 250) AND (AGED_ALIEN_RSW = 'y') AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 299)GROUP BY fo)bon a.fo = b.foinner join(SELECT fo, count(cossn) AS [Pend300]FROM T16pendallWHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 300) AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 350) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 300) AND (AGED_ALIEN_RSW = 'y') AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 350) GROUP BY fo)cinner join(SELECT fo, count(cossn) AS [Pend351]FROM T16pendallWHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 351) AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 400) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 351) AND (AGED_ALIEN_RSW = 'y') AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 400) GROUP BY fo)don c.FO = d.FOinner join(SELECT fo, count(cossn) Pend400FROM T16pendallWHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 400) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 400) AND (AGED_ALIEN_RSW = 'y')GROUP BY fo)eon c.FO = d.FOWhat am I doing wrong? |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-10-18 : 15:31:21
|
| Thanks jcelko. I tried that but I got incorrect syntax near keyword 'Then'I do like your format as it is cleaner.Please help |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-10-18 : 15:40:14
|
THANK YOU TARA that worked!!! Is this way okay to indent?Select a.fo, Totpenfodds, Pend250, Pend300, Pend351, Pend400From (SELECT fo, count(cossn) AS [TotPenFODDS] FROM T16pendall WHERE MFT_POSN1_CD='b' OR MFT_POSN1_CD='d' OR (MFT_POSN1_CD= 'a' AND AGED_ALIEN_RSW = 'y') GROUP BY fo )ainner join (SELECT fo, count(cossn) AS [Pend250] FROM T16pendall WHERE (MFT_POSN1_CD = 'b' OR MFT_POSN1_CD = 'd') AND (DATEDIFF (day, FLG_CDT, GETDATE()) > 250) AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 299) OR (MFT_POSN1_CD = 'a') AND (DATEDIFF(day, FLG_CDT, GETDATE()) > 250) AND (AGED_ALIEN_RSW = 'y') AND (DATEDIFF(day, FLG_CDT, GETDATE()) < 299) GROUP BY fo )bon a.fo = b.fo |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-10-18 : 18:21:17
|
| okay thanks |
 |
|
|
|
|
|