Llacy80
asked on
SQL Query help
Hello. I need assistance. I only know the very basics of SQL syntax so I am kind of at a loss on how to write out the query so I get the results I need.
Please see attachment.
Basically what I am trying to accomplish is to get the query to show results as they are in the screen shot but without duplications in the item_id. The results from the above query have multiple dups in the item_id column. My goal is to be able to see how many parts (item_id) in the last year have sold that also have a null value in the class_id4 column. I have tried adding a group by statement but it does not return the results like I had hoped. If someone could help by telling me exactly how the syntax should look, I would greatly appreciate it. I have been struggling with this one for a day now.
SQL-Query.docx
Please see attachment.
Basically what I am trying to accomplish is to get the query to show results as they are in the screen shot but without duplications in the item_id. The results from the above query have multiple dups in the item_id column. My goal is to be able to see how many parts (item_id) in the last year have sold that also have a null value in the class_id4 column. I have tried adding a group by statement but it does not return the results like I had hoped. If someone could help by telling me exactly how the syntax should look, I would greatly appreciate it. I have been struggling with this one for a day now.
SQL-Query.docx
ok, I "think" your issue is the date_last_modified
it's not possible to be precise as you don't display the 'duplications' as such... anyway here goes, I think it may be something like this:
it's not possible to be precise as you don't display the 'duplications' as such... anyway here goes, I think it may be something like this:
select
item_id
, class_id4
, dateadd(dd, datediff(dd,0, date_last_modified), 0)
from <<whatever your tables/joins need to be>>
where class_id4 is null
and (date_last_modified >= dateadd(month, -12 , dateadd(dd, datediff(day,0, getDate()), 0) )
group by
item_id
, class_id4
, dateadd(dd, datediff(dd,0, date_last_modified), 0)
But I notice you are using a CROSS JOIN - this looks to be very bad!(Sorry to be so blunt). PLEASE don't use CROSS JOIN, there simply has to be a better way.
pleas have a look at this: http://sqlfiddle.com/#!3/b588c/1
there are 2 tables
each table has just 10 rows
if I CROSS JOIN those 2 tables, do I get 10 rows?
there are 2 tables
each table has just 10 rows
if I CROSS JOIN those 2 tables, do I get 10 rows?
CREATE TABLE TableX
([ColX] varchar(1))
;
INSERT INTO TableX
([ColX])
VALUES
('X'), ('X'), ('X'), ('X'), ('X'), ('X'), ('X'), ('X'), ('X'), ('X')
;
CREATE TABLE TableY
([ColY] varchar(1))
;
INSERT INTO TableY
([ColY])
VALUES
('Y'), ('Y'), ('Y'), ('Y'), ('Y'), ('Y'), ('Y'), ('Y'), ('Y'), ('Y')
;
SELECT
colx
, coly
FROM tablex
CROSS JOIN tabley
;
ASKER
Thank you for your post. I do apologize for not posting the code...thought it only was about 2-3 lines so surely it was not that big of a deal.
However, the statement you posted above on your second comment does not work, it is giving me a group by error.
Incorrect syntax near the keyword Group.
However, the statement you posted above on your second comment does not work, it is giving me a group by error.
Incorrect syntax near the keyword Group.
please post exactly what you are executing (as a code block please) - seriously this is the fastest way to solution
ASKER
selectitem_id
, class_id4
, dateadd(dd, datediff(dd,0, date_last_modified), 0)
from invoice_hdr, inv_mast
where class_id4 is null
and (date_last_modified >= dateadd(month, -12 , dateadd(dd, datediff(day,0, getDate()), 0) )
GROUP BY item_id, class_id4, dateadd(dd, datediff(dd,0, date_last_modified), 0) ;
ASKER
Please go easy on me seriously...I am a network admin, I don't normally deal with sql queries!!
one to many opening brackets ;)
SELECT item_id, class_id4, dateadd(dd, datediff(dd, 0, date_last_modified), 0)
FROM invoice_hdr, inv_mast
WHERE class_id4 IS NULL
AND date_last_modified >= dateadd(month, - 12, dateadd(dd, datediff(day, 0, getDate()), 0))
GROUP BY item_id, class_id4, dateadd(dd, datediff(dd, 0, date_last_modified), 0);
ASKER
Ok. thanks :) I ran it but I am still getting duplicate data in the item_id column. Mmmhhh..Any other ideas would be appreciated.
please do not take this the wrong way, I did not create this domain name
This site is useful for formatting and some basic syntax checking of sql:
http://poorsql.com/
if we use the query you posted it looks like thisknowing how to solve it may be harder - but at least you know where the syntax error is detected.
This site is useful for formatting and some basic syntax checking of sql:
http://poorsql.com/
if we use the query you posted it looks like thisknowing how to solve it may be harder - but at least you know where the syntax error is detected.
Now the bigger issue, and it really is a BIGGER issue!
SELECT ...
FROM invoice_hdr, inv_mast
when you list tables like this, it is exactly the same as this:
SELECT ...
FROM invoice_hdr
CROSS JOIN inv_mast
Now, let's assume there are 100,000 invoice_hdr records and 100,000 invoice_mast records
when a cross join is performed, what this does it to MULTIPLY the records,
so you will get 100,000 * 100,000 = 10,000,000,000 records!!
There will be a field in those tables that should be used for joining, it will be something like "invoiceID" or "invoiceNo"
Those 2 tables should be joined like this:
SELECT ...
FROM invoice_hdr
INNER JOIN inv_mast ON invoice_hdr.invoiceID = inv_mast.invoiceID
and with this type of join you now only have 100,000 records to scan.
please don't use CROSS JOIN
SELECT ...
FROM invoice_hdr, inv_mast
when you list tables like this, it is exactly the same as this:
SELECT ...
FROM invoice_hdr
CROSS JOIN inv_mast
Now, let's assume there are 100,000 invoice_hdr records and 100,000 invoice_mast records
when a cross join is performed, what this does it to MULTIPLY the records,
so you will get 100,000 * 100,000 = 10,000,000,000 records!!
There will be a field in those tables that should be used for joining, it will be something like "invoiceID" or "invoiceNo"
Those 2 tables should be joined like this:
SELECT ...
FROM invoice_hdr
INNER JOIN inv_mast ON invoice_hdr.invoiceID = inv_mast.invoiceID
and with this type of join you now only have 100,000 records to scan.
please don't use CROSS JOIN
ASKER
Thank you for the info...I should have said something before but I did not add the cross join in to the statement, it was automatically put there by the view I put together....
I guess I will just keep messing around with it to see if I can somehow get it to work. Thanks.
I guess I will just keep messing around with it to see if I can somehow get it to work. Thanks.
Hi Llacy80
Could clarify; you appear to have two tables in your query
dbo.invoice_hdr and dbo.inv_mast
but your where clause and select fields do not reference the dbo.invoice_hdr if it is not needed remove it that will remove the cross join and also MAY fix your problem.
If not please copy and paste what is left of your query and I will run through it for you
Regards
Simon
Could clarify; you appear to have two tables in your query
dbo.invoice_hdr and dbo.inv_mast
but your where clause and select fields do not reference the dbo.invoice_hdr if it is not needed remove it that will remove the cross join and also MAY fix your problem.
If not please copy and paste what is left of your query and I will run through it for you
Regards
Simon
ASKER
Hi Simon,
Thanks for the response. I am still struggling with this and banging my head against the wall! So any help is appreciated. Here is what I have.
SELECT DISTINCT dbo.inv_mast.item_id, dbo.inv_mast.class_id4, dbo.invoice_hdr.order_date
FROM dbo.invoice_hdr CROSS JOIN
dbo.inv_mast
WHERE (dbo.inv_mast.class_id4 IS NULL) AND (dbo.invoice_hdr.order_dat e >= DATEADD(m, - 12, GETDATE()))
GROUP BY dbo.inv_mast.item_id, dbo.inv_mast.class_id4, dbo.invoice_hdr.order_date
Thanks for the response. I am still struggling with this and banging my head against the wall! So any help is appreciated. Here is what I have.
SELECT DISTINCT dbo.inv_mast.item_id, dbo.inv_mast.class_id4, dbo.invoice_hdr.order_date
FROM dbo.invoice_hdr CROSS JOIN
dbo.inv_mast
WHERE (dbo.inv_mast.class_id4 IS NULL) AND (dbo.invoice_hdr.order_dat
GROUP BY dbo.inv_mast.item_id, dbo.inv_mast.class_id4, dbo.invoice_hdr.order_date
ASKER
Also, I have tried using the following syntax that the other poster gave me but it too is returning hundreds/thousands dups in the item_id field.
SELECT DISTINCT dbo.inv_mast.item_id, dbo.inv_mast.class_id4, dbo.invoice_hdr.order_date
FROM dbo.invoice_hdr CROSS JOIN
dbo.inv_mast
WHERE (dbo.inv_mast.class_id4 IS NULL) AND (dbo.invoice_hdr.order_dat e >= DATEADD(m, - 12, GETDATE()))
GROUP BY dbo.inv_mast.item_id, dbo.inv_mast.class_id4, dbo.invoice_hdr.order_date
SELECT DISTINCT dbo.inv_mast.item_id, dbo.inv_mast.class_id4, dbo.invoice_hdr.order_date
FROM dbo.invoice_hdr CROSS JOIN
dbo.inv_mast
WHERE (dbo.inv_mast.class_id4 IS NULL) AND (dbo.invoice_hdr.order_dat
GROUP BY dbo.inv_mast.item_id, dbo.inv_mast.class_id4, dbo.invoice_hdr.order_date
HI;
Try this and let us know who you get on
SELECT DISTINCT
dbo.inv_mast.item_id
,dbo.inv_mast.class_id4
,dbo.invoice_hdr.order_dat e
FROM inv_mast
WHERE (dbo.inv_mast.class_id4 IS NULL)
AND (dbo.invoice_hdr.order_dat e >= DATEADD(m, -12, GETDATE()))
GROUP BY dbo.inv_mast.item_id
,dbo.inv_mast.class_id4
,dbo.invoice_hdr.order_dat e
Regards
Simon
Try this and let us know who you get on
SELECT DISTINCT
dbo.inv_mast.item_id
,dbo.inv_mast.class_id4
,dbo.invoice_hdr.order_dat
FROM inv_mast
WHERE (dbo.inv_mast.class_id4 IS NULL)
AND (dbo.invoice_hdr.order_dat
GROUP BY dbo.inv_mast.item_id
,dbo.inv_mast.class_id4
,dbo.invoice_hdr.order_dat
Regards
Simon
ASKER
Simon,
Hi. After I added invoice_hdr to the from statement, it ran but unforutnately it is still returning about 1k duplicate items for each item_id. Any other ideas? Thanks so much.
Lacy
Hi. After I added invoice_hdr to the from statement, it ran but unforutnately it is still returning about 1k duplicate items for each item_id. Any other ideas? Thanks so much.
Lacy
but: how will dbo.invoice_hdr.order_date be available if dbo.invoice_hdr is not joined?
@Llacy80, is there anyway you can get us a list of all fields in both tables?
You are having issues here because the 2 tables are not joined properly - please believe me on this.
@Llacy80, is there anyway you can get us a list of all fields in both tables?
You are having issues here because the 2 tables are not joined properly - please believe me on this.
>>1k duplicate items for each item_id
this is because of the cross join!
this is because of the cross join!
please believe me on this.
Hi;
That's fine; which field is used in both tables. I am guessing some sort of header id which is in both tables
Regards
Simon
That's fine; which field is used in both tables. I am guessing some sort of header id which is in both tables
Regards
Simon
ASKER
I do believe you, :) However even when I don't do the cross join I get 1-2k dup items for each item_id. THis is just driving me insane..I just need to be able to show my boss which parts have sold in the last year that have null value in the class_id4 field...Something I thought would be so simple has turned in to big ole headache.
Well here goes.... The list of all fields in both tables...
dbo.inv_mast.inv_mast_uid, dbo.inv_mast.item_id, dbo.inv_mast.item_desc, dbo.inv_mast.delete_flag, dbo.inv_mast.weight, dbo.inv_mast.net_weight,
dbo.inv_mast.date_created, dbo.inv_mast.date_last_mod ified, dbo.inv_mast.last_maintain ed_by, dbo.inv_mast.inactive, dbo.inv_mast.cube,
dbo.inv_mast.date_inactive , dbo.inv_mast.date_reactive , dbo.inv_mast.catch_weight_ indicator, dbo.inv_mast.purchasing_we ight, dbo.inv_mast.class_id1,
dbo.inv_mast.class_id2, dbo.inv_mast.class_id3, dbo.inv_mast.class_id4, dbo.inv_mast.class_id5, dbo.inv_mast.upc_or_ean, dbo.inv_mast.upc_or_ean_id ,
dbo.inv_mast.serialized, dbo.inv_mast.product_type, dbo.inv_mast.hose, dbo.inv_mast.fitting, dbo.inv_mast.d_length, dbo.inv_mast.catalog_item,
dbo.inv_mast.short_code, dbo.inv_mast.track_lots, dbo.inv_mast.requisition, dbo.inv_mast.price1, dbo.inv_mast.price2, dbo.inv_mast.price3, dbo.inv_mast.price4,
dbo.inv_mast.price5, dbo.inv_mast.price6, dbo.inv_mast.price7, dbo.inv_mast.price8, dbo.inv_mast.price9, dbo.inv_mast.price10,
dbo.inv_mast.default_produ ct_group, dbo.inv_mast.default_sales _discount_ group, dbo.inv_mast.default_purch ase_disc_g roup, dbo.inv_mast.sales_pricing _unit,
dbo.inv_mast.sales_pricing _unit_size , dbo.inv_mast.purchase_pric ing_unit, dbo.inv_mast.purchase_pric ing_unit_s ize, dbo.inv_mast.extended_desc ,
dbo.inv_mast.commission_cl ass_id, dbo.inv_mast.other_charge_ item, dbo.inv_mast.invoice_type, dbo.inv_mast.pick_ticket_t ype, dbo.inv_mast.default_selli ng_unit,
dbo.inv_mast.update_via_pr icing_serv ice, dbo.inv_mast.default_purch asing_unit , dbo.inv_mast.hi, dbo.inv_mast.ti, dbo.inv_mast.convert_quant ities,
dbo.inv_mast.haz_mat_flag, dbo.inv_mast.class_code, dbo.inv_mast.item_terms_di scount_pct , dbo.inv_mast.tpcx_status, dbo.inv_mast.default_trans fer_unit,
dbo.inv_mast.keywords, dbo.inv_mast.fulltext_time stamp, dbo.inv_mast.vendor_consig ned, dbo.inv_mast.created_by, dbo.inv_mast.disposition,
dbo.inv_mast.source_type_c d, dbo.inv_mast.use_tags_flag , dbo.inv_mast.base_unit, dbo.inv_mast.tag_hold_clas s_uid, dbo.inv_mast.shippable_uni t_flag,
dbo.inv_mast.auto_allocati on_flag, dbo.inv_mast.parker_produc t_cd, dbo.inv_mast.parker_divisi on_cd, dbo.inv_mast.iva_taxable_f lag, dbo.inv_mast.restricted_fl ag,
dbo.inv_mast.currency_id, dbo.inv_mast.service_terms _discount_ pct, dbo.inv_mast.service_commi ssion_clas s_id, dbo.inv_mast.override_spec ific_costi ng,
dbo.inv_mast.sold_outside_ us_flag, dbo.inv_mast.configurable_ flag, dbo.inv_mast.last_pricing_ service_da te, dbo.inv_mast.commodity_cod e,
dbo.inv_mast.default_price _family_ui d, dbo.inv_mast.spa_item_flag , dbo.inv_mast.avail_for_sch _delivery_ flag, dbo.inv_mast.item_type_cd,
dbo.inv_mast.cust_parent_i nv_mast_ui d, dbo.inv_mast.unspsc_code, dbo.inv_mast.dci_code, dbo.inv_mast.epa_cert_req_ flag, dbo.inv_mast.aia_enabled_f lag,
dbo.inv_mast.aia_remnant_q ty, dbo.inv_mast.manufacturer_ program_ty pe_uid, dbo.inv_mast.manufacturer_ program_ty pe_pct, dbo.inv_mast.length,
dbo.inv_mast.width, dbo.inv_mast.height, dbo.inv_mast.use_oc_tax_ru les_flag, dbo.inv_mast.tally_flag, dbo.inv_mast.oc_print_on_p ick_ticket _flag,
dbo.inv_mast.oc_print_on_i nvoice_fla g, dbo.inv_mast.use_revisions _flag, dbo.inv_mast.single_use_or _reusable, dbo.inv_mast.unitconv_over ride_oe_fl ag,
dbo.inv_mast.unitconv_over ride_purc_ flag, dbo.inv_mast.item_sales_ta x_class, dbo.inv_mast.lifo_pool_ite m_class, dbo.inv_mast.country_of_or igin_req_f lag,
dbo.inv_mast.nmfc_hdr_uid, dbo.inv_mast.catch_lot_wei ght_flag, dbo.inv_mast.ucc128_pack_t ype_cd, dbo.inv_mast.ucc128_standa rd_pack_si ze,
dbo.inv_mast.redemption_it em_flag, dbo.inv_mast.redemption_va lue, dbo.inv_mast.bo_fill_canad ian_purcha se_flag, dbo.inv_mast.country_of_or igin_code,
dbo.inv_mast.apply_state_f uel_surcha rge_flag, dbo.inv_mast.type_of_sale, dbo.invoice_hdr.invoice_no , dbo.invoice_hdr.order_no, dbo.invoice_hdr.order_date ,
dbo.invoice_hdr.invoice_da te, dbo.invoice_hdr.customer_i d, dbo.invoice_hdr.bill2_name , dbo.invoice_hdr.bill2_cont act, dbo.invoice_hdr.bill2_addr ess1,
dbo.invoice_hdr.bill2_addr ess2, dbo.invoice_hdr.bill2_city , dbo.invoice_hdr.bill2_stat e, dbo.invoice_hdr.bill2_post al_code, dbo.invoice_hdr.ship2_name ,
dbo.invoice_hdr.ship2_cont act, dbo.invoice_hdr.ship2_addr ess1, dbo.invoice_hdr.ship2_addr ess2, dbo.invoice_hdr.ship2_city , dbo.invoice_hdr.ship2_stat e,
dbo.invoice_hdr.ship2_post al_code, dbo.invoice_hdr.carrier_na me, dbo.invoice_hdr.fob, dbo.invoice_hdr.terms_desc , dbo.invoice_hdr.po_no,
dbo.invoice_hdr.salesrep_i d, dbo.invoice_hdr.salesrep_n ame, dbo.invoice_hdr.brokerage, dbo.invoice_hdr.freight, dbo.invoice_hdr.ar_account _no,
dbo.invoice_hdr.gl_freight _account_n o, dbo.invoice_hdr.gl_brokera ge_account _no, dbo.invoice_hdr.brokerage_ amt, dbo.invoice_hdr.period,
dbo.invoice_hdr.year_for_p eriod, dbo.invoice_hdr.store_no, dbo.invoice_hdr.invoice_ty pe AS Expr1, dbo.invoice_hdr.ship_to_id , dbo.invoice_hdr.ship_date,
dbo.invoice_hdr.total_amou nt, dbo.invoice_hdr.amount_pai d, dbo.invoice_hdr.terms_take n, dbo.invoice_hdr.allowed, dbo.invoice_hdr.paid_in_fu ll_flag,
dbo.invoice_hdr.paid_by_ch eck_no, dbo.invoice_hdr.date_paid, dbo.invoice_hdr.print_flag , dbo.invoice_hdr.print_date , dbo.invoice_hdr.company_no ,
dbo.invoice_hdr.customer_i d_number, dbo.invoice_hdr.date_creat ed AS Expr2, dbo.invoice_hdr.date_last_ modified AS Expr3,
dbo.invoice_hdr.last_maint ained_by AS Expr4, dbo.invoice_hdr.printed, dbo.invoice_hdr.printed_da te, dbo.invoice_hdr.corp_addre ss_id,
dbo.invoice_hdr.shipping_c ost, dbo.invoice_hdr.bill2_coun try, dbo.invoice_hdr.ship2_coun try, dbo.invoice_hdr.invoice_re ference_no ,
dbo.invoice_hdr.invoice_ad justment_t ype, dbo.invoice_hdr.invoice_de sc, dbo.invoice_hdr.memo_amoun t, dbo.invoice_hdr.bad_debt_a mount,
dbo.invoice_hdr.invoice_cl ass, dbo.invoice_hdr.period_ful ly_paid, dbo.invoice_hdr.year_fully _paid, dbo.invoice_hdr.approved, dbo.invoice_hdr.fc_thru_da te,
dbo.invoice_hdr.cumulative _fc, dbo.invoice_hdr.net_due_da te, dbo.invoice_hdr.terms_due_ date, dbo.invoice_hdr.terms_id, dbo.invoice_hdr.branch_id,
dbo.invoice_hdr.disputed_f lag, dbo.invoice_hdr.statement_ period, dbo.invoice_hdr.statement_ year, dbo.invoice_hdr.other_char ge_amount,
dbo.invoice_hdr.tax_amount , dbo.invoice_hdr.original_d ocument_ty pe, dbo.invoice_hdr.consolidat ed, dbo.invoice_hdr.sold_to_ah _uid,
dbo.invoice_hdr.sold_to_cu stomer_id, dbo.invoice_hdr.ship_to_ph one, dbo.invoice_hdr.invoice_ba tch_uid, dbo.invoice_hdr.freight_co de_uid,
dbo.invoice_hdr.shipping_r oute_uid, dbo.invoice_hdr.transmissi on_method, dbo.invoice_hdr.terms_amou nt, dbo.invoice_hdr.sales_loca tion_id,
dbo.invoice_hdr.source_typ e_cd AS Expr5, dbo.invoice_hdr.ship2_emai l_address, dbo.invoice_hdr.currency_l ine_uid, dbo.invoice_hdr.created_by AS Expr6,
dbo.invoice_hdr.tax_terms_ amt, dbo.invoice_hdr.tax_terms_ taken, dbo.invoice_hdr.carrier_in surance_am t, dbo.invoice_hdr.inv_no_dis play,
dbo.invoice_hdr.iva_exempt ion_number , dbo.invoice_hdr.iva_taxabl e_flag AS Expr7, dbo.invoice_hdr.tax_amount _paid, dbo.invoice_hdr.job_id,
dbo.invoice_hdr.invoice_pa id_period, dbo.invoice_hdr.invoice_pe riod, dbo.invoice_hdr.credit_mem o_for_term s_flag, dbo.invoice_hdr.record_typ e_cd,
dbo.invoice_hdr.commission _cost, dbo.invoice_hdr.receiver_n ame, dbo.invoice_hdr.carton_cou nt, dbo.invoice_hdr.strategic_ freight_in ,
dbo.invoice_hdr.strategic_ freight_ou t, dbo.invoice_hdr.affiliated _training_ center, dbo.invoice_hdr.external_r eference_n o, dbo.invoice_hdr.total_frei ghtcharge_ weight,
dbo.invoice_hdr.cardlock_c ons_invoic e_flag, dbo.invoice_hdr.remove_fro m_open_def _rev_windo w, dbo.invoice_hdr.record_typ e_referenc e_no,
dbo.invoice_hdr.rebill_inv oice_reaso n_uid, dbo.invoice_hdr.edi_order_ printed_fl ag, dbo.invoice_hdr.downpaymen t_applied,
dbo.invoice_hdr.reverse_re demption_f lag
Well here goes.... The list of all fields in both tables...
dbo.inv_mast.inv_mast_uid,
dbo.inv_mast.date_created,
dbo.inv_mast.date_inactive
dbo.inv_mast.class_id2, dbo.inv_mast.class_id3, dbo.inv_mast.class_id4, dbo.inv_mast.class_id5, dbo.inv_mast.upc_or_ean, dbo.inv_mast.upc_or_ean_id
dbo.inv_mast.serialized, dbo.inv_mast.product_type,
dbo.inv_mast.short_code, dbo.inv_mast.track_lots, dbo.inv_mast.requisition, dbo.inv_mast.price1, dbo.inv_mast.price2, dbo.inv_mast.price3, dbo.inv_mast.price4,
dbo.inv_mast.price5, dbo.inv_mast.price6, dbo.inv_mast.price7, dbo.inv_mast.price8, dbo.inv_mast.price9, dbo.inv_mast.price10,
dbo.inv_mast.default_produ
dbo.inv_mast.sales_pricing
dbo.inv_mast.commission_cl
dbo.inv_mast.update_via_pr
dbo.inv_mast.haz_mat_flag,
dbo.inv_mast.keywords, dbo.inv_mast.fulltext_time
dbo.inv_mast.source_type_c
dbo.inv_mast.auto_allocati
dbo.inv_mast.currency_id, dbo.inv_mast.service_terms
dbo.inv_mast.sold_outside_
dbo.inv_mast.default_price
dbo.inv_mast.cust_parent_i
dbo.inv_mast.aia_remnant_q
dbo.inv_mast.width, dbo.inv_mast.height, dbo.inv_mast.use_oc_tax_ru
dbo.inv_mast.oc_print_on_i
dbo.inv_mast.unitconv_over
dbo.inv_mast.nmfc_hdr_uid,
dbo.inv_mast.redemption_it
dbo.inv_mast.apply_state_f
dbo.invoice_hdr.invoice_da
dbo.invoice_hdr.bill2_addr
dbo.invoice_hdr.ship2_cont
dbo.invoice_hdr.ship2_post
dbo.invoice_hdr.salesrep_i
dbo.invoice_hdr.gl_freight
dbo.invoice_hdr.year_for_p
dbo.invoice_hdr.total_amou
dbo.invoice_hdr.paid_by_ch
dbo.invoice_hdr.customer_i
dbo.invoice_hdr.last_maint
dbo.invoice_hdr.shipping_c
dbo.invoice_hdr.invoice_ad
dbo.invoice_hdr.invoice_cl
dbo.invoice_hdr.cumulative
dbo.invoice_hdr.disputed_f
dbo.invoice_hdr.tax_amount
dbo.invoice_hdr.sold_to_cu
dbo.invoice_hdr.shipping_r
dbo.invoice_hdr.source_typ
dbo.invoice_hdr.tax_terms_
dbo.invoice_hdr.iva_exempt
dbo.invoice_hdr.invoice_pa
dbo.invoice_hdr.commission
dbo.invoice_hdr.strategic_
dbo.invoice_hdr.cardlock_c
dbo.invoice_hdr.rebill_inv
dbo.invoice_hdr.reverse_re
Hi;
I cannot see a direct relationship between the two tables (unless I am missing something) and this is what is causing your cross join
Do you have a database diagram / schema you can look at to see how the two tables may be related and if perhaps another table or two need to be added to the mix.
You are correct it should be quite simple but I do believe you have a missing piece to you puzzle, possibly invoice_det (detail)
Regards
Simon
I cannot see a direct relationship between the two tables (unless I am missing something) and this is what is causing your cross join
Do you have a database diagram / schema you can look at to see how the two tables may be related and if perhaps another table or two need to be added to the mix.
You are correct it should be quite simple but I do believe you have a missing piece to you puzzle, possibly invoice_det (detail)
Regards
Simon
ASKER
You are correct, there are no similiar fields between the two tables. I guess I could create a new view and then try adding another table that might link them all up.
Hi;
Yes; I would have expected a relationship to exist probably a one to many and it is that link the will help you end you duplication issue
Regards
Simon
Yes; I would have expected a relationship to exist probably a one to many and it is that link the will help you end you duplication issue
Regards
Simon
select a,b
from tableX, tableY
IS a "cross join", it is precisely the same as writing
select a,b
from tableX
cross join tableY
from tableX, tableY
IS a "cross join", it is precisely the same as writing
select a,b
from tableX
cross join tableY
please try this:
select count(*) from dbo.inv_mast
;
select count(*) from dbo.invoice_hdr
;
select count(*)
from dbo.inv_mast as M
inner join dbo.invoice_hdr as H on M.inv_mast_uid = H.invoice_no
;
ASKER
Ok. I ran what you told me to and it returned the following
1 no column name 17361
1 no column name 51205
1 no column name 0
1 no column name 17361
1 no column name 51205
1 no column name 0
>>I guess I could create a new view and then try adding another table that might link them all up.
goodness! NO, please don't
100,000 * 100,000 * something else = much worse
goodness! NO, please don't
100,000 * 100,000 * something else = much worse
well at least it's only 888,970,005 :(
ok, so that didn't work. try this, it should narrow down the possibilities
select
table_name
, column_name
, ordinal_position
, column_default
, is_nullable
, data_type
, character_maximum_length
from information_schema.columns as m
inner join (
select
data_type
, character_maximum_length
from information_schema.columns
where table_name = 'invoice_hdr'
and column_name = 'invoice_no'
) as h where m.data_type = h.data_type and m.character_maximum_length = h.character_maximum_length
where m.table_name = 'inv_mast'
ok, so that didn't work. try this, it should narrow down the possibilities
select
table_name
, column_name
, ordinal_position
, column_default
, is_nullable
, data_type
, character_maximum_length
from information_schema.columns
inner join (
select
data_type
, character_maximum_length
from information_schema.columns
where table_name = 'invoice_hdr'
and column_name = 'invoice_no'
) as h where m.data_type = h.data_type and m.character_maximum_length
where m.table_name = 'inv_mast'
sorry - too much haste, this one:
select
m.table_name
, m.column_name
, m.ordinal_position
, m.column_default
, m.is_nullable
, m.data_type
, m.character_maximum_length
from information_schema.columns as m
inner join (
select
data_type
, character_maximum_length
from information_schema.columns
where table_name = 'invoice_hdr'
and column_name = 'invoice_no'
) as h on m.data_type = h.data_type and m.character_maximum_length = h.character_maximum_length
where m.table_name = 'inv_mast'
ASKER
Tried to run it but getting an error near where...
ASKER
Ok. It ran successfully but returned no results.
yes, use the second one - the first one has errors
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok well thank you for your help. I do appreciate it. I think I just need to get more familiar with these tables and the database and then hopefully from there I can come up with a query to produce the results I am looking for. I will also stray from cross joins :)
ASKER
FYI. Got it to work with the following statement.
SELECT IM.item_id, IM.class_id4, MAX(IH.invoice_date) AS last_date_pruchased
FROM dbo.inv_mast AS IM INNER JOIN
dbo.invoice_line AS IL ON IM.inv_mast_uid = IL.inv_mast_uid INNER JOIN
dbo.invoice_hdr AS IH ON IH.invoice_no = IL.invoice_no
WHERE (IH.invoice_date >= DATEADD(year, - 1, GETDATE())) AND (IM.class_id4 IS NULL)
GROUP BY IM.item_id, IM.class_id4
SELECT IM.item_id, IM.class_id4, MAX(IH.invoice_date) AS last_date_pruchased
FROM dbo.inv_mast AS IM INNER JOIN
dbo.invoice_line AS IL ON IM.inv_mast_uid = IL.inv_mast_uid INNER JOIN
dbo.invoice_hdr AS IH ON IH.invoice_no = IL.invoice_no
WHERE (IH.invoice_date >= DATEADD(year, - 1, GETDATE())) AND (IM.class_id4 IS NULL)
GROUP BY IM.item_id, IM.class_id4
congratulations
it does work better without cross joins :)
it does work better without cross joins :)
I see you are using 'select distinct', quite often peoples expectation of this syntax does not match the technical definition. 'Distinct' will evaluate the whole row (all columns!) and then decide is the row is different to all other rows.
As our code is embedded into an image it will take more time - back soon I hope.
Code blocks by the way look like this
Open in new window