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 |
scott_hanebutt
Starting Member
10 Posts |
Posted - 2009-05-14 : 08:06:08
|
I have a table that contains two fields.FPARTNO – unique part numberFADJUST1 – price of partMy part numbers contain two types of parts, the 21 and 23 series. The part numbers in each series match each other except for the series number.For example these are some of my part numbersTM2172L-5ML-P - 21 seriesTM2372L-5ML-P - 23 seriesUM216630R-EHFM-3L-P - 21 seriesUM236630R-EHFM-3L-P - 23 seriesThe 21 series part should always cost the same or less then the corresponding 23 series part.How can I write a query that would check this and then give me a list of parts where this was not true?What query would give me a list of parts that did not have a corresponding part in the other series?Thank you,Scott |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-05-14 : 09:33:08
|
Try thisselect FPARTNO from your_tableGroup by FPARTNOHaving min(FADJUST1)='21 series' and max(FADJUST1)='21 series' MadhivananFailing to plan is Planning to fail |
|
|
scott_hanebutt
Starting Member
10 Posts |
Posted - 2009-05-14 : 09:56:44
|
Thanks for the reply.I made a few typo’s in my post.The table name is INPRSCFields:FSCHECODE – is the field name for the unique part number, not FPARTNOFADJUST1 – price of partThe example part numbers I listed do not contain the word ‘series’. I but the series name after the part number so you would know what series they are in. The following are examples of part numbers.TM2172L-5ML-PTM2372L-5ML-PUM216630R-EHFM-3L-PUM236630R-EHFM-3L-PPCUMBT2172LC-EHFP-4LPCUMBT2372LC-EHFP-4LI tried modifying your query to the following two but neither one returned any results.select FSCHECODE from INPRSCGroup by FSCHECODEHaving min(FADJUST1)='23' and max(FADJUST1)='21'select FSCHECODE from INPRSCGroup by FSCHECODEHaving min(FADJUST1)='21' and max(FADJUST1)='23' |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-05-14 : 09:59:58
|
select FSCHECODE from INPRSCGroup by FSCHECODEHaving min(FADJUST1)='21' and max(FADJUST1)='21'MadhivananFailing to plan is Planning to fail |
|
|
scott_hanebutt
Starting Member
10 Posts |
Posted - 2009-05-14 : 10:12:43
|
I tried your query and got no results.Is your query to answer my first or second question?I am not sure what your query is doing. It seems to be grouping by the part number, but the part number is always unique because of '21' or '23' being incorporated into the part number. |
|
|
yonabout
Posting Yak Master
112 Posts |
Posted - 2009-06-11 : 11:11:33
|
Hi,First off, it doesn't look like your database design is set up to deal with this query at all.I've come up with something that might work, but its really dodgy.Basically I created a couple of temp tables with the parts code, price and a 'newcode' column, one for the 21 codes and one for the 23 codes.I selected all the 21 codes into the #21s table and all the 23 codes into the #23s table, and stripped out the 21 and 23 values from the part code and stuck that in the newcode column.Once you've got that, the values in the newcode column should be the same in both the temp tables so you can use it as a key to join the tables and do comparisons.You may have issues with this if 21 or 23 appears more than once in a part number, and If possible I'd recommend revisiting your database design, but here's the code anyway, it might help.create table #temp (FSCHECODE varchar(50), FADJUST1 decimal(10,2)) insert into #tempselect 'TM2172L-5ML-P', 50.00 unionselect 'TM2372L-5ML-P', 10.00 unionselect 'UM216630R-EHFM-3L-P', 10.00 unionselect 'UM236630R-EHFM-3L-P', 10.00 unionselect 'PCUMBT2172LC-EHFP-4L', 10.00 create table #21s (FSCHECODE varchar(50), Newcode varchar(50), FADJUST1 decimal(10,2)) insert into #21sselectFSCHECODE,replace(FSCHECODE,'21', ''),FADJUST1from #temp where FSCHECODE like '%21%'create table #23s (FSCHECODE varchar(50), Newcode varchar(50), FADJUST1 decimal(10,2)) insert into #23sselectFSCHECODE,replace(FSCHECODE,'23', ''),FADJUST1from #temp where FSCHECODE like '%23%'--gets parts where the 21 version costs more than the 23 versionselect#21s.fschecode,#21s.fadjust1from #21s join #23son #21s.newcode = #23s.newcodewhere #21s.fadjust1 > #23s.fadjust1 --gets parts where there is a 21 version but no 23 versionselect#21s.fschecode,#23s.fschecode,#21s.fadjust1from #21s left join #23son #21s.newcode = #23s.newcodewhere #23s.fschecode is nulldrop table #21sdrop table #23sdrop table #temp Cheers,Yonabout |
|
|
scott_hanebutt
Starting Member
10 Posts |
Posted - 2009-06-11 : 13:20:26
|
Thanks for the Post. I modified your sugestion and got it to work. Below is the query I used.SELECT LINE21.PARTFROM (SELECT REPLACE(REPLACE(RTRIM(INPRSC.FSCHECODE),'21','-LINE-'),'-P','-MATERIAL') AS PART, INPRSC.FADJUST1 AS Z1 FROM INPRSC, INMAST WHERE INPRSC.FSCHECODE = INMAST.FPARTNO AND INMAST.FPRODCL = '21' AND INMAST.FCSTSCODE = 'A') AS LINE21, (SELECT REPLACE(REPLACE(RTRIM(INPRSC.FSCHECODE),'23','-LINE-'),'-W','-MATERIAL') AS PART, INPRSC.FADJUST1 AS Z1 FROM INPRSC, INMAST WHERE INPRSC.FSCHECODE = INMAST.FPARTNO AND INMAST.FPRODCL = '23' AND INMAST.FCSTSCODE = 'A') AS LINE23WHERE LINE21.PART = LINE23.PART AND LINE21.Z1 > LINE23.Z1 |
|
|
|
|
|
|
|