| Author |
Topic |
|
KINGOFMPLS
Starting Member
6 Posts |
Posted - 2009-11-30 : 15:23:36
|
| i have two views that im trying to merge toghether as one..both are demand for parts one is a customer order demand view, one is a workorder demand view. the fields are identical in each 3 fields part id, date (date parted as year date ie 332) and qty i would like to merge these so that if the date fields are the same than i would like to add the qty's toghether, that much ive figured out with CASE WHEN CO.DESIREDDAY = WO.DESIREDDAY THEN (CO.ORDER_QTY + WO.WO_QTY) ELSEEND AS MERGEDDEMAND,what im not sure on how to do is to merge the data if the date fields dont equal. i have demand in each and i need to get it down to one view with the part id , qty (could be from either table) and the date (also could be from either table) any help i could get would be extremely appreciatedthanks in advanceMike |
|
|
KINGOFMPLS
Starting Member
6 Posts |
Posted - 2009-11-30 : 15:27:57
|
| (date parted as year DAY ie 332) and qty basically i want three fields in this new view, part_id , demand (from either previous view or a combination of the two if the dates are the same) and date (also from either previous view) |
 |
|
|
KINGOFMPLS
Starting Member
6 Posts |
Posted - 2009-11-30 : 15:47:34
|
| Here's what i have so far, unfortunatly when the dates dont equal its not returning the value its just coming up NullCASE WHEN CO.DESIREDDAY = WO.DESIREDDAY THEN (CO.ORDER_QTY + WO.WO_QTY) WHEN CO.DESIREDDAY <> WO.DESIREDDAY AND CO.ORDER_QTY IS NOT NULL THEN CO.ORDER_QTY WHEN CO.DESIREDDAY <> WO.DESIREDDAY AND WO.WO_QTY IS NOT NULL THEN WO.WO_QTY ELSE NULLEND AS MERGEDDEMAND,CASEWHEN CO.DESIREDDAY = WO.DESIREDDAYTHEN CO.DESIREDDAYWHEN CO.DESIREDDAY <> WO.DESIREDDAYAND CO.DESIREDDAY IS NOT NULLTHEN CO.DESIREDDAYWHEN CO.DESIREDDAY <> WO.DESIREDDAYAND WO.DESIREDDAY IS NOT NULLTHEN WO.DESIREDDAYELSE NULLEND AS DEMANDDAY |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-11-30 : 16:37:40
|
table structuresample datawanted output No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
KINGOFMPLS
Starting Member
6 Posts |
Posted - 2009-12-01 : 09:09:46
|
| sample data Customer Order TablePart id QTY YearDay0106500 5 33401015PB 3 33503100PB 4 336sample data Work order TablePart id QTY YearDay0106500 3 33401015PB 4 33903100PB 5 340what i would like to see is Part id QTY YearDay0106500 8 33401015PB 3 33503100PB 4 33601015PB 4 33903100PB 5 340 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-01 : 09:19:25
|
| Try thisselect [Part id], sum(QTY) as QTY, YearDay from (Select [Part id], QTY, YearDay from customer_orderunion allSelect [Part id], QTY, YearDay from work_order) as tgroup by [Part id], YearDayMadhivananFailing to plan is Planning to fail |
 |
|
|
KINGOFMPLS
Starting Member
6 Posts |
Posted - 2009-12-01 : 11:43:41
|
| GETTING A SYNTAX ERROR BY THE WORD FROM, NOT SURE WHAT I DID WRONG, THANKS FOR YOUR HELP I SEE WHERE YOUR GOING WITH THIS CREATE VIEW DBO.GPSR_PARTDEMANDDAY AS SELECT[PART_ID],SUM(QTY) AS QTY,DESIREDDAYFROM(SELECT [PART_ID],ORDER_QTY AS QTY,DESIREDDAYFROM GPSR_CUSTDEMANDDAYUNION ALLSELECT[PART_ID],WO_QTY AS QTY,DESIREDDAYFROMGPSR_WODEMANDDAY2)AS TFROM GPSR_CUSTDEMANDDAY AS CO WITH(NOLOCK)LEFT OUTER JOIN GPSR_WODEMANDDAY2 AS WO WITH(NOLOCK)ON CO.PART_ID = WO.PART_IDGROUP BY PART_ID , DESIREDDAY |
 |
|
|
DP978
Constraint Violating Yak Guru
269 Posts |
Posted - 2009-12-01 : 12:38:48
|
| After the 'AS T' part it appears you are joining some more tables...if I am reading this correctly you have 2 from statements. The derived table and then another joining CO to WO.The above suggestion from Madhiv was to create one derived table and then pull from it.Edit for readability:Suggested-SELECTt.fieldsFROM(Derived Table) tGroup by ...Yours -SELECTt.fieldsFROM(Derived table) tFROM <---the mistakeCO.tableWO.TableJOIN....WHERE.... |
 |
|
|
KINGOFMPLS
Starting Member
6 Posts |
Posted - 2009-12-02 : 10:18:20
|
| thanks so much everyone, that worked perfectly , just what i wanted, much apprecitated |
 |
|
|
|