Re: Fairly Difficult SQL problem
Ian Skinner wrote: > > > SELECT > MDL.R_MDL_INSTID, > MDL.R_MDL_DRAWDTE, > MIN(MDL.R_MDL_DRAWDTE - B.R_MDL_DRAWDTE) AS > R_MDL_DRAWDTEDIFF, > MDL.R_MDL_RID, > MDL.R_MDL_MOBLID, > MDL.R_MDL_BEDCNT, > MDL.R_MDL_LOCTYP > > FROM > EBIS.REC_MDL_DB_REC MDL INNER JOIN EBIS.REC_MDL_DB_REC B ON > (MDL.R_MDL_INSTID = B.R_MDL_INSTID AND >MDL.R_MDL_MOBLID = B.R_MDL_MOBLID AND >MDL.R_MDL_DRAWDTE > B.R_MDL_DRAWDTE) > > WHERE > MDL.R_MDL_INSTID = ' ' AND > > (MDL.R_MDL_DRAWDTE BETWEEN value="#DateFormat(firstDay,'mmdd')#" cfsqltype="cf_sql_char" > maxlength="8"> AND cfsqltype="cf_sql_char" maxlength="8">) > > ORDER BY > MDL.R_MDL_DRAWDTE, > MDL.R_MDL_MOBLID > SELECT MDL.R_MDL_INSTID, MDL.R_MDL_DRAWDTE, MIN(MDL.R_MDL_DRAWDTE - B.R_MDL_DRAWDTE) AS R_MDL_DRAWDTEDIFF, MDL.R_MDL_RID, MDL.R_MDL_MOBLID, MDL.R_MDL_BEDCNT, MDL.R_MDL_LOCTYP FROM EBIS.REC_MDL_DB_REC MDL LEFT JOIN EBIS.REC_MDL_DB_REC B ON (MDL.R_MDL_INSTID = B.R_MDL_INSTID AND MDL.R_MDL_MOBLID = B.R_MDL_MOBLID AND MDL.R_MDL_DRAWDTE > B.R_MDL_DRAWDTE) WHERE MDL.R_MDL_INSTID = ' ' AND MDL.R_MDL_DRAWDTE LIKE ) GROUP BY MDL.R_MDL_INSTID, MDL.R_MDL_DRAWDTE, MDL.R_MDL_RID, MDL.R_MDL_MOBLID, MDL.R_MDL_BEDCNT, MDL.R_MDL_LOCTYP ORDER BY MDL.R_MDL_DRAWDTE, MDL.R_MDL_MOBLID > When I test this I get the following error: "[MERANT][SequeLink JDBC > Driver][ODBC Socket][Oracle][ODBC][Ora]ORA-00933: SQL command not properly > ended" Any suggestions on why this SQL command is not properly ended? Using the JDBC/ODBC bridge? What does it do in SQL+? Jochem ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Your ad could be here. Monies from ads go to support these lists and provide more resources for the community. http://www.fusionauthority.com/ads.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Re: Fairly Difficult SQL problem
Dina Hess wrote: > Just in case your database doesn't support some of that cool stuff Jochem > uses, try this. It should all work in Oracle, as long as it is 9i. > SELECTs.id, s.sponsor, d.date, MIN(datediff(day, dp.date, d.date)) as > daysSince > FROM drive d left outer join sponsor s on d.sponsor_id = s.id left > outer JOIN drive dp ON d.sponsor_id = dp.sponsor_id AND d.date > dp.date > WHERE MONTH(d.date) = 6 > GROUP BY s.id, s.sponsor, d.date > > Sponsor 4 reports a NULL value for daysSince because there is no previous > drive date. Using a LEFT JOIN so you also get the sponsors without previous drives back is indeed better. But you should be able to do an inner join between d and s. Jochem ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Get the mailserver that powers this list at http://www.coolfusion.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Re: Fairly Difficult SQL problem
Ian Skinner wrote: > > Is there any problem mixing FROM ... JOIN clauses with WHERE joins? No. > A question about part of the WHERE clause. Does this piece "EXTRACT (MONTH > FROM d.date) = #month#" have anything to do with getting the days since last > drive piece? Or, is it as I assume the code that selects the month of data > that I want. Getting the month of data. > Which is unfortunately more difficult for me, since the > designers of this database in the mists of the long forgotten past for some > reason of inexplicable wisdom chose to store all dates as numeric strings > mmdd. Not hard at all: WHERE d.date LIKE 'mm%' Jochem ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Get the mailserver that powers this list at http://www.coolfusion.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Fairly Difficult SQL problem
OK, I'm Trying the suggestions provide by Jochem and Dina. Having a strange error I can't get a bead on. My real SQL SELECT MDL.R_MDL_INSTID, MDL.R_MDL_DRAWDTE, MIN(MDL.R_MDL_DRAWDTE - B.R_MDL_DRAWDTE) AS R_MDL_DRAWDTEDIFF, MDL.R_MDL_RID, MDL.R_MDL_MOBLID, MDL.R_MDL_BEDCNT, MDL.R_MDL_LOCTYP FROM EBIS.REC_MDL_DB_REC MDL INNER JOIN EBIS.REC_MDL_DB_REC B ON (MDL.R_MDL_INSTID = B.R_MDL_INSTID AND MDL.R_MDL_MOBLID = B.R_MDL_MOBLID AND MDL.R_MDL_DRAWDTE > B.R_MDL_DRAWDTE) WHERE MDL.R_MDL_INSTID = ' ' AND (MDL.R_MDL_DRAWDTE BETWEEN AND ) ORDER BY MDL.R_MDL_DRAWDTE, MDL.R_MDL_MOBLID When I test this I get the following error: "[MERANT][SequeLink JDBC Driver][ODBC Socket][Oracle][ODBC][Ora]ORA-00933: SQL command not properly ended" Any suggestions on why this SQL command is not properly ended? Thank You -- Ian Skinner Web Programmer BloodSource Sacramento, CA -Original Message- From: Dina Hess [mailto:[EMAIL PROTECTED] Sent: Thursday, July 03, 2003 10:22 AM To: CF-Talk Subject: Re: Fairly Difficult SQL problem Just in case your database doesn't support some of that cool stuff Jochem uses, try this. It works in SQL 2000: SELECTs.id, s.sponsor, d.date, MIN(datediff(day, dp.date, d.date)) as daysSince FROM drive d left outer join sponsor s on d.sponsor_id = s.id left outer JOIN drive dp ON d.sponsor_id = dp.sponsor_id AND d.date > dp.date WHERE MONTH(d.date) = 6 GROUP BY s.id, s.sponsor, d.date Sponsor 4 reports a NULL value for daysSince because there is no previous drive date. ~Dina - Original Message - From: "Jochem van Dieten" <[EMAIL PROTECTED]> To: "CF-Talk" <[EMAIL PROTECTED]> Sent: Thursday, July 03, 2003 10:53 AM Subject: Re: Fairly Difficult SQL problem > Ian Skinner wrote: > > > >>I have tables something like the following data structures. > >> > >>Sponsor > >>ID Sponsor ... > >>1 Foo > >>2 Bar > >>3 Clyde > >>4 New Sponsor > >> > >>Drives > >>ID SponsorID Date ... > > Date is a reserved word. > > > >>1 1 4/15/03 > >>2 2 4/18/03 > >>3 3 4/24/03 > >>4 2 5/9/03 > >>5 1 5/16/03 > >>6 3 6/3/03 > >>7 2 6/11/03 > >>8 1 6/15/03 > >>9 4 6/18/03 > >> > >>What I need to retreive is all the drives for a given month (June for > >>example) plus the number of days it has been since any previous drive for > >>a given Sponsor. > > SELECTs.id, d.sponsor, d.date, MIN(d.date - dp.date) as daysSince > FROM sponsor s, drives d INNER JOIN drives pd ON (d.sponsorid = > pd.sponsorid AND d.date > dp.date) > WHERE EXTRACT (MONTH FROM d.date) = #month# >AND s.id = d.sponsorid > GROUP BY s.id, d.sponsor, d.date > > Jochem > > > > ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Your ad could be here. Monies from ads go to support these lists and provide more resources for the community. http://www.fusionauthority.com/ads.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Re: Fairly Difficult SQL problem
Just in case your database doesn't support some of that cool stuff Jochem uses, try this. It works in SQL 2000: SELECTs.id, s.sponsor, d.date, MIN(datediff(day, dp.date, d.date)) as daysSince FROM drive d left outer join sponsor s on d.sponsor_id = s.id left outer JOIN drive dp ON d.sponsor_id = dp.sponsor_id AND d.date > dp.date WHERE MONTH(d.date) = 6 GROUP BY s.id, s.sponsor, d.date Sponsor 4 reports a NULL value for daysSince because there is no previous drive date. ~Dina - Original Message - From: "Jochem van Dieten" <[EMAIL PROTECTED]> To: "CF-Talk" <[EMAIL PROTECTED]> Sent: Thursday, July 03, 2003 10:53 AM Subject: Re: Fairly Difficult SQL problem > Ian Skinner wrote: > > > >>I have tables something like the following data structures. > >> > >>Sponsor > >>ID Sponsor ... > >>1 Foo > >>2 Bar > >>3 Clyde > >>4 New Sponsor > >> > >>Drives > >>ID SponsorID Date ... > > Date is a reserved word. > > > >>1 1 4/15/03 > >>2 2 4/18/03 > >>3 3 4/24/03 > >>4 2 5/9/03 > >>5 1 5/16/03 > >>6 3 6/3/03 > >>7 2 6/11/03 > >>8 1 6/15/03 > >>9 4 6/18/03 > >> > >>What I need to retreive is all the drives for a given month (June for > >>example) plus the number of days it has been since any previous drive for > >>a given Sponsor. > > SELECTs.id, d.sponsor, d.date, MIN(d.date - dp.date) as daysSince > FROM sponsor s, drives d INNER JOIN drives pd ON (d.sponsorid = > pd.sponsorid AND d.date > dp.date) > WHERE EXTRACT (MONTH FROM d.date) = #month# >AND s.id = d.sponsorid > GROUP BY s.id, d.sponsor, d.date > > Jochem > > > > ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Signup for the Fusion Authority news alert and keep up with the latest news in ColdFusion and related topics. http://www.fusionauthority.com/signup.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Fairly Difficult SQL problem
Thanks, of course my real table uses a different field name for date. It's going to take a bit of trial and error to understand every thing you are doing in your example and translate it to my real world code where I also have 4 other tables to join in for other information. Is there any problem mixing FROM ... JOIN clauses with WHERE joins? (WHERE tableA.id = tableB.id). The existing query I'm attempting to modify uses all WHERE joins, and I'm not sure I really want to convert them all to FROM ... JOINS as all these tables have compound keys. A question about part of the WHERE clause. Does this piece "EXTRACT (MONTH FROM d.date) = #month#" have anything to do with getting the days since last drive piece? Or, is it as I assume the code that selects the month of data that I want. Which is unfortunately more difficult for me, since the designers of this database in the mists of the long forgotten past for some reason of inexplicable wisdom chose to store all dates as numeric strings mmdd. Fun has since ensued in retrieving information for specific date ranges. Sorry about that last bit, just got to remind myself it almost a Three day weekend, where I can spend all my time on my moonlighting project. Yippee. -- Ian Skinner Web Programmer BloodSource Sacramento, CA -Original Message- From: Jochem van Dieten [mailto:[EMAIL PROTECTED] Sent: Thursday, July 03, 2003 8:54 AM To: CF-Talk Subject: Re: Fairly Difficult SQL problem Ian Skinner wrote: > >>I have tables something like the following data structures. >> >>Sponsor >>IDSponsor ... >>1 Foo >>2 Bar >>3 Clyde >>4 New Sponsor >> >>Drives >>IDSponsorID Date ... Date is a reserved word. >>1 1 4/15/03 >>2 2 4/18/03 >>3 3 4/24/03 >>4 2 5/9/03 >>5 1 5/16/03 >>6 3 6/3/03 >>7 2 6/11/03 >>8 1 6/15/03 >>9 4 6/18/03 >> >>What I need to retreive is all the drives for a given month (June for >>example) plus the number of days it has been since any previous drive for >>a given Sponsor. SELECTs.id, d.sponsor, d.date, MIN(d.date - dp.date) as daysSince FROM sponsor s, drives d INNER JOIN drives pd ON (d.sponsorid = pd.sponsorid AND d.date > dp.date) WHERE EXTRACT (MONTH FROM d.date) = #month# AND s.id = d.sponsorid GROUP BY s.id, d.sponsor, d.date Jochem ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Get the mailserver that powers this list at http://www.coolfusion.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Re: Fairly Difficult SQL problem
Ian Skinner wrote: > >>I have tables something like the following data structures. >> >>Sponsor >>IDSponsor ... >>1 Foo >>2 Bar >>3 Clyde >>4 New Sponsor >> >>Drives >>IDSponsorID Date ... Date is a reserved word. >>1 1 4/15/03 >>2 2 4/18/03 >>3 3 4/24/03 >>4 2 5/9/03 >>5 1 5/16/03 >>6 3 6/3/03 >>7 2 6/11/03 >>8 1 6/15/03 >>9 4 6/18/03 >> >>What I need to retreive is all the drives for a given month (June for >>example) plus the number of days it has been since any previous drive for >>a given Sponsor. SELECTs.id, d.sponsor, d.date, MIN(d.date - dp.date) as daysSince FROM sponsor s, drives d INNER JOIN drives pd ON (d.sponsorid = pd.sponsorid AND d.date > dp.date) WHERE EXTRACT (MONTH FROM d.date) = #month# AND s.id = d.sponsorid GROUP BY s.id, d.sponsor, d.date Jochem ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq This list and all House of Fusion resources hosted by CFHosting.com. The place for dependable ColdFusion Hosting. http://www.cfhosting.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Fairly Difficult SQL problem
Thanks, but I am not sure that quite meets my needs. Correct me if I'm wrong. This is for a calendar that displays all the drives for a month. One piece of information needed for the display is how many days has it been since a previous drive (if it exists) for each sponsor. So, I can't just select from before today, since the calendar can both go into the future and past as well as current month. The date difference needs to be for each instance of a drive by a sponsor. So if a sponsor had a drive on June 16th and will have a drive on August 22nd. I need to know how many days have passed since the previous drive for each of these instances. I could do this, by running a query to find the previous drive as I output each record from the query of all the drives for the month being displayed. But I would rather not, if I can avoid it. The data isn't gigantic, but pretty respectable. There are about 300-400 drives a month and the table holds data going back to 1988. There are also 4 supporting tables joined to this one to provide other information about the drive. So, I would like to make this as efficient as possible. Thanks for any help. -- Ian Skinner Web Programmer BloodSource Sacramento, CA -Original Message- From: Cantrell, Adam [mailto:[EMAIL PROTECTED] Sent: Thursday, July 03, 2003 8:16 AM To: CF-Talk Subject: RE: Fairly Difficult SQL problem > > What I need to retreive is all the drives for a given month > (June for > > example) plus the number of days it has been since any > previous drive for > > a given Sponsor. Does it need to be in the same query - I might treat that as two separate queries - one to retrieve all drives for the given month. - one to retrieve the most recent drive (drives less than today's date) for each individual sponsor. Convert the second query into something you can reference by sponsorID - like an array whose keys are sponsorID's. Then as you're looping through your first query you can just reference the array to output the number of days since the last drive for that given sponsor. Something like - datediff("d",now(),arrayRecentDrive[qryDrives.sponsorID]) Adam. > -Original Message- > From: Ian Skinner [mailto:[EMAIL PROTECTED] > Sent: Thursday, July 03, 2003 9:53 AM > To: CF-Talk > Subject: Fairly Difficult SQL problem > > > Trying this on the more active CF-Talk list. > > ... > > > I have tables something like the following data structures. > > > > Sponsor > > ID Sponsor ... > > 1 Foo > > 2 Bar > > 3 Clyde > > 4 New Sponsor > > > > Drives > > ID SponsorID Date ... > > 1 1 4/15/03 > > 2 2 4/18/03 > > 3 3 4/24/03 > > 4 2 5/9/03 > > 5 1 5/16/03 > > 6 3 6/3/03 > > 7 2 6/11/03 > > 8 1 6/15/03 > > 9 4 6/18/03 > > > > What I need to retreive is all the drives for a given month > (June for > > example) plus the number of days it has been since any > previous drive for > > a given Sponsor. > > > > RecordSet > > Sponsor.ID Sponsor Drives.ID SponsorID Date > > DaysSinceLastDrive ... > > 3 Clyde 6 3 6/3/03 > > 40 > > 2 Bar 7 2 6/11/03 > > 33 > > 1 Foo 8 1 6/15/03 > > 30 > > 4 New Sponsor 9 4 6/18/03 > > N/A > > > > Any suggestions on how I could do this in SQL so I don't > have to something > > unpleasant like looping over one query result, running > another query each > > iteration. > > > > If it matters this is ColdFusion MX, no updates querying a Oracle 9i > > database. > > > > Thank You > > -- > > Ian Skinner > > Web Programmer > > BloodSource > > Sacramento, CA > > > ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq This list and all House of Fusion resources hosted by CFHosting.com. The place for dependable ColdFusion Hosting. http://www.cfhosting.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Fairly Difficult SQL problem
> > What I need to retreive is all the drives for a given month > (June for > > example) plus the number of days it has been since any > previous drive for > > a given Sponsor. Does it need to be in the same query - I might treat that as two separate queries - one to retrieve all drives for the given month. - one to retrieve the most recent drive (drives less than today's date) for each individual sponsor. Convert the second query into something you can reference by sponsorID - like an array whose keys are sponsorID's. Then as you're looping through your first query you can just reference the array to output the number of days since the last drive for that given sponsor. Something like - datediff("d",now(),arrayRecentDrive[qryDrives.sponsorID]) Adam. > -Original Message- > From: Ian Skinner [mailto:[EMAIL PROTECTED] > Sent: Thursday, July 03, 2003 9:53 AM > To: CF-Talk > Subject: Fairly Difficult SQL problem > > > Trying this on the more active CF-Talk list. > > ... > > > I have tables something like the following data structures. > > > > Sponsor > > ID Sponsor ... > > 1 Foo > > 2 Bar > > 3 Clyde > > 4 New Sponsor > > > > Drives > > ID SponsorID Date ... > > 1 1 4/15/03 > > 2 2 4/18/03 > > 3 3 4/24/03 > > 4 2 5/9/03 > > 5 1 5/16/03 > > 6 3 6/3/03 > > 7 2 6/11/03 > > 8 1 6/15/03 > > 9 4 6/18/03 > > > > What I need to retreive is all the drives for a given month > (June for > > example) plus the number of days it has been since any > previous drive for > > a given Sponsor. > > > > RecordSet > > Sponsor.ID Sponsor Drives.ID SponsorID Date > > DaysSinceLastDrive ... > > 3 Clyde 6 3 6/3/03 > > 40 > > 2 Bar 7 2 6/11/03 > > 33 > > 1 Foo 8 1 6/15/03 > > 30 > > 4 New Sponsor 9 4 6/18/03 > > N/A > > > > Any suggestions on how I could do this in SQL so I don't > have to something > > unpleasant like looping over one query result, running > another query each > > iteration. > > > > If it matters this is ColdFusion MX, no updates querying a Oracle 9i > > database. > > > > Thank You > > -- > > Ian Skinner > > Web Programmer > > BloodSource > > Sacramento, CA > > > ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq This list and all House of Fusion resources hosted by CFHosting.com. The place for dependable ColdFusion Hosting. http://www.cfhosting.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Fairly Difficult SQL problem
Trying this on the more active CF-Talk list. ... > I have tables something like the following data structures. > > Sponsor > IDSponsor ... > 1 Foo > 2 Bar > 3 Clyde > 4 New Sponsor > > Drives > IDSponsorID Date ... > 1 1 4/15/03 > 2 2 4/18/03 > 3 3 4/24/03 > 4 2 5/9/03 > 5 1 5/16/03 > 6 3 6/3/03 > 7 2 6/11/03 > 8 1 6/15/03 > 9 4 6/18/03 > > What I need to retreive is all the drives for a given month (June for > example) plus the number of days it has been since any previous drive for > a given Sponsor. > > RecordSet > Sponsor.IDSponsor Drives.ID SponsorID Date > DaysSinceLastDrive... > 3 Clyde 6 3 6/3/03 > 40 > 2 Bar 7 2 6/11/03 > 33 > 1 Foo 8 1 6/15/03 > 30 > 4 New Sponsor 9 4 6/18/03 > N/A > > Any suggestions on how I could do this in SQL so I don't have to something > unpleasant like looping over one query result, running another query each > iteration. > > If it matters this is ColdFusion MX, no updates querying a Oracle 9i > database. > > Thank You > -- > Ian Skinner > Web Programmer > BloodSource > Sacramento, CA > ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq This list and all House of Fusion resources hosted by CFHosting.com. The place for dependable ColdFusion Hosting. http://www.cfhosting.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4