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 |
|
abhit_kumar
Posting Yak Master
147 Posts |
Posted - 2010-02-04 : 00:15:02
|
Hi frnds,Please see below code its working for ITEM 1 and ITEM 2, but fails for ITEM 3.Total quantity should compare with last quanity first and then before that per item wise.DROP TABLE #TESTDROP TABLE #TEMPTESTGOCREATE TABLE #TEST (ITEM VARCHAR(10),TOTAL INT, GRPO INT, REST INT)CREATE TABLE #TEMPTEST (ID INT IDENTITY(1,1),ITEM VARCHAR(10),TOTAL INT, GRPO INT, REST INT, QSUM INT)GOINSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,40)INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,7)INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,3)INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM2',60,60)INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM2',60,15)INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM2',60,20)INSERT INTO #TEMPTEST(ITEM,TOTAL,GRPO) VALUES ('ITEM3',10,5)INSERT INTO #TEMPTEST(ITEM,TOTAL,GRPO) VALUES ('ITEM3',10,15)GOINSERT INTO #TEMPTEST(ITEM,TOTAL,GRPO) SELECT ITEM,TOTAL,GRPO FROM #TESTGODECLARE @ID INTDECLARE @COUNT INTDECLARE @ITEM VARCHAR(20)DECLARE @GRPO INTDECLARE @TOTAL INTDECLARE @SUM INTSELECT @COUNT = COUNT(*) FROM #TEMPTESTSET @ID =1WHILE (@ID <= @COUNT)BEGIN SELECT @ITEM=ITEM, @GRPO=GRPO,@TOTAL=TOTAL FROM #TEMPTEST WHERE ID=@ID IF (@TOTAL > @GRPO) UPDATE #TEMPTEST SET REST = @GRPO WHERE ID=@ID ELSE BEGIN SELECT @SUM = SUM(GRPO) FROM #TEMPTEST WHERE ITEM=@ITEM AND TOTAL=@TOTAL AND GRPO<>@GRPO AND ID > @ID UPDATE #TEMPTEST SET REST = (TOTAL-@SUM) WHERE ID=@ID END SET @ID = @ID + 1ENDGOSELECT * FROM #TEMPTEST |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-02-04 : 00:25:41
|
maybe you can explain what are you trying to do here ? And what is the required result ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
abhit_kumar
Posting Yak Master
147 Posts |
Posted - 2010-02-04 : 00:43:01
|
| I want to calculate rest qty, based upon the data inserted in database.For example every item has TOtal qty and based upon this multiple records are for GRP Qty.So first total qty compare with last GRPO qty and if its more then total qty then its should display GRPO qty next to rest qty col.and then compare with second last GRPO qty, means at all GRPO qtys it should not be more then Total qty.Please see the result of ITEM 1 and ITEM 2,its desired result, but failed to do ITEM 3. |
 |
|
|
mymatrix
Starting Member
24 Posts |
Posted - 2010-02-04 : 00:45:11
|
| Abhit, there is some mistake in your last condition which says AND ID > @IDsince 1st record is having id as 1 and 2nd record has id 2. and it doesn't pick any of item3 records. This condition has to be removed and only then it would update REST as 5 (value of record1 of ITEM3 item).On second thought, Can you elaborate your requirement. This may help in further optimising the query.**************************************Even my blood group says be -ve to all the negatives. |
 |
|
|
abhit_kumar
Posting Yak Master
147 Posts |
Posted - 2010-02-04 : 00:57:16
|
| PLEASE NOTE THAT AT REST 5 IS COMING WHICH IS INCORRECT, BECAUSE IT SHOULD DISPLAY 10, NOT 5 BECAUSE 10<15, HENCE 10 SHOULD COME THERE, AND THEN AT ABOVE FOR ITEM 3 IT SHOULD BE ZERO, BECAUSE TOTAL 10 ALREADY USED AT BELOW ITEM 3.IN ANY CASE, GRPO CAN NOT BE MORE THEN TOTAL.I WANT RESULT FOR ITEM 3 LIKE AS BELOW:-ITEM 3 10 5 0 15 10 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-02-04 : 01:06:55
|
i am still a bit lost on your requirement.It seems that the ordering of your record is important ? Do you have another column that determine the sequence or ordering of the records ?What happen if your data is like this ? What will be the expected result ?INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,40)INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,27)INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,3) KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
abhit_kumar
Posting Yak Master
147 Posts |
Posted - 2010-02-04 : 01:13:44
|
| INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,1,40)INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,2,27)INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,3,3)one modification in above insert stmt is i have one col of ID i.e. 1,2,3 so alyways first we have to compare with last ID i.e 3 with their qty and then ID 2 and ID 1.Expected result should be like this:-=========================================== 0 25 3 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-04 : 03:55:24
|
quote: Originally posted by khtan i am still a bit lost on your requirement.It seems that the ordering of your record is important ? Do you have another column that determine the sequence or ordering of the records ?What happen if your data is like this ? What will be the expected result ?INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,40)INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,27)INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,3) KH[spoiler]Time is always against us[/spoiler]
I've been asking him about this for two days now still no answer http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=139283http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=139211 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-04 : 04:07:24
|
quote: Originally posted by abhit_kumar INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,1,40)INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,2,27)INSERT INTO #TEST(ITEM,TOTAL,GRPO) VALUES ('ITEM1',28,3,3)one modification in above insert stmt is i have one col of ID i.e. 1,2,3 so alyways first we have to compare with last ID i.e 3 with their qty and then ID 2 and ID 1.Expected result should be like this:-=========================================== 0 25 3
this was what i was asking you in all the other threads.Now that you've the column use the belowSELECT t.ITEM,t.TOTAL,t.GRPO, CASE WHEN t.TOTAL > (t.GRPO +COALESCE( t1.Total,0)) THEN t.GPROELSE CASE WHEN SIGN((t.TOTAL-COALESCE( t1.Total,0)))> 0.0 THEN (t.TOTAL-COALESCE( t1.Total,0)) ELSE 0 ENDEND AS RestQtyFROM YourTable tOUTER APPLY (SELECT SUM(GPRO) AS Total FROM YourTable WHERE Item= t.Item AND ID >t.ID)t1 |
 |
|
|
abhit_kumar
Posting Yak Master
147 Posts |
Posted - 2010-02-04 : 04:42:39
|
| thanks alot visakh...its working now..sorry i confused alot and thanks to all of them who shared there opinion... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-04 : 04:54:24
|
quote: Originally posted by abhit_kumar thanks alot visakh...its working now..sorry i confused alot and thanks to all of them who shared there opinion...
you're welcome for future, please keep in mind following things1. post proper data and table structure and also sample output in below formathttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx2. Answer any more follow up questions correctly3.Dont keep on adding new threads asking on same questions. That will only help in adding to the confusion |
 |
|
|
abhit_kumar
Posting Yak Master
147 Posts |
Posted - 2010-02-05 : 07:49:03
|
| Hello Visakh,Thanx for the valuable suggestion.I have tested your given code, its working good, except itsfailing in one case where the GRPOid is duplicating for particular item.ID ITEM TOTALQTY GRPOID GRPOQTY RESTQTY50724 ZBR-R1 26 972 20 2050725 ZBR-R1 26 972 30 26Note :- First column ID is IDENTITY columnI have used GRPOID column for the condition in Outer Apply subquery like GRPOID>t.GRPOIDLook the output, for particular item there are two GRPO ID, item with different quantity.because there is two times entry of the item in GRPO with different GRPO qty.The entry has been done by end user in the software, so we cant modify it.please guide me. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-05 : 09:00:26
|
quote: Originally posted by abhit_kumar Hello Visakh,Thanx for the valuable suggestion.I have tested your given code, its working good, except itsfailing in one case where the GRPOid is duplicating for particular item.ID ITEM TOTALQTY GRPOID GRPOQTY RESTQTY50724 ZBR-R1 26 972 20 2050725 ZBR-R1 26 972 30 26Note :- First column ID is IDENTITY columnI have used GRPOID column for the condition in Outer Apply subquery like GRPOID>t.GRPOIDLook the output, for particular item there are two GRPO ID, item with different quantity.because there is two times entry of the item in GRPO with different GRPO qty.The entry has been done by end user in the software, so we cant modify it.please guide me.
so in this case what should be your output? |
 |
|
|
abhit_kumar
Posting Yak Master
147 Posts |
Posted - 2010-02-05 : 23:21:08
|
| currently output is coming as:-ID ITEM TOTALQTY GRPOID GRPOQTY RESTQTY50724 ZBR-R1 26 972 20 2050725 ZBR-R1 26 972 30 26and i would like to expect as:-ITEM TotalQTY GRPOID GRPOQTY RESTQTYZBR-R1 26 972 20 0ZBR-R1 26 972 30 26I HAVE NOTED THAT WHENEVER THERE IS AN DUPLICATING OF GRPOID OR IN SOME CASES OF DUPLICATING GRPOQTY ITS OCCURING PROBLEM, ELSE IN ALL OTHER CASES ITS WORKING GOOD. |
 |
|
|
abhit_kumar
Posting Yak Master
147 Posts |
Posted - 2010-02-06 : 05:24:07
|
| whenever gproid is duplicating i have taken the sum of quantity and now its work as per the requirement.Thanks, |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-06 : 05:54:33
|
| ok great |
 |
|
|
|
|
|
|
|