|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Scheduled VS MadeI am looking for the best method the extract "Supposed to make" - "Did
Make", where "Supposed to make" and "Did Make" come from different queries. I need to show when parts are scheduled and not made, and when parts are made and not scheduled. I have no trouble pulling the data, the way I do it is to use a left join on one table, then another left join on the other and union them together to eliminate dupes. It works fine but I just wonder if it's the most efficient? Any thoughts ? I appreciate your help. Paul It would help us better assist you if you could include table DDL, query
strategy used so far, sample data in the form of INSERT statements, and an illustration of the desired results. (For help with that refer to: http://www.aspfaq.com/5006 ) The less 'set up' work we have to do, the more likely you are going to have folks tackle your problem and help you. Without this effort from you, we are just playing guessing games. -- Show quoteArnie Rowland, Ph.D. Westwood Consulting, Inc Most good judgment comes from experience. Most experience comes from bad judgment. - Anonymous "Paul Ilacqua" <pilac***@twcny.rr.com> wrote in message news:ecKyQ9szGHA.1268@TK2MSFTNGP02.phx.gbl... >I am looking for the best method the extract "Supposed to make" - "Did >Make", where "Supposed to make" and "Did Make" come from different queries. > I need to show when parts are scheduled and not made, and when parts > are made and not scheduled. > I have no trouble pulling the data, the way I do it is to use a left join > on one table, then another left join on the other and union them together > to eliminate dupes. It works fine but I just wonder if it's the most > efficient? Any thoughts ? > I appreciate your help. > > Paul > > > > I have seperate views) that pull parts needed and parts produced along with
dates due, then use the left join method to give me parts needed and not produced and the parts produced and not needed. Views contain a date part number & qty. Output I get now... it's correct but I'm wondering as this scales will it be efficient. (I may add a 3rd column to the output) query code below.... output D_DATE PART NEEDED PRODUCED ------------------------------------------------------ ------------------------- ----------- ----------- 2006-08-28 00:00:00.000 18514 0 870 2006-08-28 00:00:00.000 19014 0 204 2006-08-28 00:00:00.000 19041 280 0 2006-08-28 00:00:00.000 19099 220 0 2006-08-28 00:00:00.000 19134 220 0 2006-08-28 00:00:00.000 19139 220 0 2006-08-28 00:00:00.000 19140 220 0 2006-08-28 00:00:00.000 19141 2952 0 2006-08-28 00:00:00.000 21063 220 0 2006-08-28 00:00:00.000 21064 220 0 2006-08-28 00:00:00.000 21065 700 0 2006-08-28 00:00:00.000 21066 220 0 2006-08-28 00:00:00.000 21068 220 0 2006-08-28 00:00:00.000 21400 1160 884 2006-08-28 00:00:00.000 21727 220 0 2006-08-28 00:00:00.000 21909 220 0 2006-08-28 00:00:00.000 21943 290 0 SELECT PN.PLAN_DATE AS D_DATE, PN.PART, PN.NEEDED, ISNULL(PP.PRODUCED,0) AS PRODUCED FROM LABEL_AUDIT..V_PARTS_NEEDED PN LEFT JOIN ( SELECT PROD_DATE, PART, PRODUCED FROM LABEL_AUDIT..V_PRODUCED_II ) PP ON PP.PROD_DATE = PN.PLAN_DATE AND PN.PART = PP.PART UNION SELECT PROD_DATE, PP.PART, ISNULL(PN.NEEDED,0) AS NEEDED, PP.PRODUCED FROM LABEL_AUDIT..V_PRODUCED_II PP LEFT JOIN ( SELECT PLAN_DATE, PART, NEEDED FROM LABEL_AUDIT..V_PARTS_NEEDED )PN ON PP.PROD_DATE = PN.PLAN_DATE AND PP.PART = PN.PART Show quote "Arnie Rowland" <ar***@1568.com> wrote in message news:eq1rYptzGHA.5048@TK2MSFTNGP05.phx.gbl... > It would help us better assist you if you could include table DDL, query > strategy used so far, sample data in the form of INSERT statements, and an > illustration of the desired results. (For help with that refer to: > http://www.aspfaq.com/5006 ) > > > > The less 'set up' work we have to do, the more likely you are going to > have folks tackle your problem and help you. Without this effort from you, > we are just playing guessing games. > > > -- > Arnie Rowland, Ph.D. > Westwood Consulting, Inc > > Most good judgment comes from experience. > Most experience comes from bad judgment. > - Anonymous > > > "Paul Ilacqua" <pilac***@twcny.rr.com> wrote in message > news:ecKyQ9szGHA.1268@TK2MSFTNGP02.phx.gbl... >>I am looking for the best method the extract "Supposed to make" - "Did >>Make", where "Supposed to make" and "Did Make" come from different >>queries. >> I need to show when parts are scheduled and not made, and when >> parts are made and not scheduled. >> I have no trouble pulling the data, the way I do it is to use a left join >> on one table, then another left join on the other and union them together >> to eliminate dupes. It works fine but I just wonder if it's the most >> efficient? Any thoughts ? >> I appreciate your help. >> >> Paul >> >> >> >> > > On Sat, 2 Sep 2006 18:27:44 -0400, Paul Ilacqua wrote:
(snip) Show quote >SELECT PN.PLAN_DATE AS D_DATE, PN.PART, PN.NEEDED, Hi Paul,> ISNULL(PP.PRODUCED,0) AS PRODUCED > FROM LABEL_AUDIT..V_PARTS_NEEDED PN > LEFT JOIN > ( > SELECT PROD_DATE, PART, PRODUCED > FROM LABEL_AUDIT..V_PRODUCED_II > ) PP > ON PP.PROD_DATE = PN.PLAN_DATE > AND PN.PART = PP.PART >UNION > SELECT PROD_DATE, PP.PART, > ISNULL(PN.NEEDED,0) AS NEEDED, PP.PRODUCED > FROM LABEL_AUDIT..V_PRODUCED_II PP > LEFT JOIN > ( > SELECT PLAN_DATE, PART, NEEDED > FROM LABEL_AUDIT..V_PARTS_NEEDED > )PN > ON PP.PROD_DATE = PN.PLAN_DATE > AND PP.PART = PN.PART I can't test to confirm or deny my suspicions, since you didn't post CREATE TABLE statements for the tables used in the query and INSERT statements with some sample data. But I have the suspicion that the SQL below is equivalent to your query, yet faster. SELECT COALESCE (PN.PLAN_DATE, PP.PROD_DATE) AS D_DATE, COALESCE (PN.PART, PP.PART) AS PART, COALESCE (PN.NEEDED, 0) AS NEEDED, COALESCE (PP.PRODUCED, 0) AS PRODUCED FROM LABEL_AUDIT..V_PARTS_NEEDED AS PN FULL OUTER JOIN LABEL_AUDIT..V_PRODUCED_II AS PP ON PP.PROD_DATE = PN.PLAN_DATE AND PP.PART = PN.PART; -- Hugo Kornelis, SQL Server MVP HUGO,
Thanks for the reply... I believe I have my fix.... thank you very much. I added the date criteria line and I'll convert it to an SP. Thanks again. I have never used COALESCE in a where clause ! I'll never stop learning. Paul SELECT COALESCE (PN.PLAN_DATE, PP.PROD_DATE) AS D_DATE, COALESCE (PN.PART, PP.PART) AS PART, COALESCE (PN.NEEDED, 0) AS NEEDED, COALESCE (PP.PRODUCED, 0) AS PRODUCED FROM LABEL_AUDIT..V_PARTS_NEEDED AS PN FULL OUTER JOIN LABEL_AUDIT..V_PRODUCED_II AS PP ON PP.PROD_DATE = PN.PLAN_DATE AND PP.PART = PN.PART WHERE COALESCE (PN.PLAN_DATE, PP.PROD_DATE) BETWEEN '20060828' and '20060903' ORDER BY 1 Show quote "Hugo Kornelis" <h***@perFact.REMOVETHIS.info.INVALID> wrote in message news:48gmf21icfgnrktvq0qep7ea6ri3qrbs4v@4ax.com... > On Sat, 2 Sep 2006 18:27:44 -0400, Paul Ilacqua wrote: > > (snip) >>SELECT PN.PLAN_DATE AS D_DATE, PN.PART, PN.NEEDED, >> ISNULL(PP.PRODUCED,0) AS PRODUCED >> FROM LABEL_AUDIT..V_PARTS_NEEDED PN >> LEFT JOIN >> ( >> SELECT PROD_DATE, PART, PRODUCED >> FROM LABEL_AUDIT..V_PRODUCED_II >> ) PP >> ON PP.PROD_DATE = PN.PLAN_DATE >> AND PN.PART = PP.PART >>UNION >> SELECT PROD_DATE, PP.PART, >> ISNULL(PN.NEEDED,0) AS NEEDED, PP.PRODUCED >> FROM LABEL_AUDIT..V_PRODUCED_II PP >> LEFT JOIN >> ( >> SELECT PLAN_DATE, PART, NEEDED >> FROM LABEL_AUDIT..V_PARTS_NEEDED >> )PN >> ON PP.PROD_DATE = PN.PLAN_DATE >> AND PP.PART = PN.PART > > Hi Paul, > > I can't test to confirm or deny my suspicions, since you didn't post > CREATE TABLE statements for the tables used in the query and INSERT > statements with some sample data. But I have the suspicion that the SQL > below is equivalent to your query, yet faster. > > SELECT COALESCE (PN.PLAN_DATE, PP.PROD_DATE) AS D_DATE, > COALESCE (PN.PART, PP.PART) AS PART, > COALESCE (PN.NEEDED, 0) AS NEEDED, > COALESCE (PP.PRODUCED, 0) AS PRODUCED > FROM LABEL_AUDIT..V_PARTS_NEEDED AS PN > FULL OUTER JOIN LABEL_AUDIT..V_PRODUCED_II AS PP > ON PP.PROD_DATE = PN.PLAN_DATE > AND PP.PART = PN.PART; > > -- > Hugo Kornelis, SQL Server MVP On Sun, 3 Sep 2006 20:24:28 -0400, Paul Ilacqua wrote:
>HUGO, Hi Paul,> Thanks for the reply... I believe I have my fix.... thank you very much. I >added the date criteria line and I'll convert it to an SP. Thanks again. I'm sorry - I must have missed the date requirement in your original post. > I COALESCE is basically the same as ISNULL, but with these differences:>have never used COALESCE in a where clause ! I'll never stop learning. >Paul - ISNULL is proprietary T-SQL syntax; COALESCE is more portable since it's defined in the ANSI standard for SQL. - COALESCE accepts more than two arguments: the first non-NULL argument is returned. - ISNULL uses a method for determining the result datatype that isn't used anywhere else in SQL Server - all data is converted to the datatype of the first argument. COALESCE uses the standard method of converting everything to the datatype with highest precedence. -- Hugo Kornelis, SQL Server MVP Hugo ,
Am I correct to assume then COALESCE is almost required when using a FULL OUTER JOIN especially with several fields? Paul Show quote "Hugo Kornelis" <h***@perFact.REMOVETHIS.info.INVALID> wrote in message news:ugbpf2lm64nabm67oevncu4t3ibjte2psh@4ax.com... > On Sun, 3 Sep 2006 20:24:28 -0400, Paul Ilacqua wrote: > >>HUGO, >> Thanks for the reply... I believe I have my fix.... thank you very much. >> I >>added the date criteria line and I'll convert it to an SP. Thanks again. > > Hi Paul, > > I'm sorry - I must have missed the date requirement in your original > post. > >> I >>have never used COALESCE in a where clause ! I'll never stop learning. >>Paul > > COALESCE is basically the same as ISNULL, but with these differences: > > - ISNULL is proprietary T-SQL syntax; COALESCE is more portable since > it's defined in the ANSI standard for SQL. > - COALESCE accepts more than two arguments: the first non-NULL argument > is returned. > - ISNULL uses a method for determining the result datatype that isn't > used anywhere else in SQL Server - all data is converted to the datatype > of the first argument. COALESCE uses the standard method of converting > everything to the datatype with highest precedence. > > -- > Hugo Kornelis, SQL Server MVP On Mon, 4 Sep 2006 19:49:38 -0400, Paul Ilacqua wrote:
>Hugo , Hi Paul,> Am I correct to assume then COALESCE is almost required when using a FULL >OUTER JOIN especially with several fields? >Paul I don't recall ever using FULL OUTER JOIN without also using COALESCE in the same query. I've been tying to think up a case where it would be warranted, but no luck yet. So yes, when using FULL OUTER JOIN, COALESCE is definitely "almost required" - and probably even "required". -- Hugo Kornelis, SQL Server MVP |
|||||||||||||||||||||||