| Author |
Topic |
|
skiabox
Posting Yak Master
169 Posts |
Posted - 2008-02-12 : 03:27:21
|
| I have the following view in my database :SELECT bedalog, tmpRm, SUM(bedaposo) AS bedaposoFROM ErmisUser.QBEPROKWHERE (bedaflarr = '1') AND (LEFT(bedakind, 1) <> 'P')GROUP BY tmpRm, bedalogI want to create a stored procedure that will show only bedaposo column as an output from this code above.Is this possible?Thank you for your help. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-02-12 : 03:32:43
|
[code]select bedaposofrom(SELECT bedalog, tmpRm, SUM(bedaposo) AS bedaposoFROM ErmisUser.QBEPROKWHERE (bedaflarr = '1') AND (LEFT(bedakind, 1) <> 'P')GROUP BY tmpRm, bedalog) d[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
skiabox
Posting Yak Master
169 Posts |
Posted - 2008-02-12 : 03:37:42
|
| thnx a lot for the rapid answer! d is a temporary table? which book do you suggest to learn more about sql server 2005 programming? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-12 : 03:38:02
|
SELECT bedalog, tmpRm, SUM(bedaposo) AS bedaposoFROM ErmisUser.QBEPROKWHERE (bedaflarr = '1') AND (LEFT(bedakind, 1) <> 'P')GROUP BY tmpRm, bedalog E 12°55'05.25"N 56°04'39.16" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-02-12 : 03:38:20
|
d is the table alias given to the derived table KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-12 : 04:31:59
|
| Also instead of LEFT(bedakind, 1) <> 'P' use bedakind not like 'P%' to make use of index(if defined)MadhivananFailing to plan is Planning to fail |
 |
|
|
skiabox
Posting Yak Master
169 Posts |
Posted - 2008-02-14 : 05:13:42
|
| khtan : how can I select another field from another view in the same query and add them? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-14 : 05:16:55
|
| which is other field and other view. On what field you want to link the two views? |
 |
|
|
skiabox
Posting Yak Master
169 Posts |
Posted - 2008-02-14 : 05:48:35
|
| The 1st view is stkef3room :bedalog tmpRm rbedaposo8309 520 58375 520 888203 501 70(this is a small sample)Code : SELECT bedalog, tmpRm, SUM(bedaposo) AS rbedaposoFROM ErmisUser.QBEPROKWHERE (bedaflarr = '1') AND (LEFT(bedakind, 1) <> 'P')GROUP BY bedalog, tmpRmThe next view is also producing 3 columns :SELECT bedalog, tmpRm, SUM(bedaposo) AS rbedaposo1FROM ErmisUser.QBEPROKWHERE (bedaflarr <> '1') AND (LEFT(bedakind, 1) <> 'P')GROUP BY bedalog, tmpRmI want to get in another table or view the sum rbedaposo + rbedaposo1 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-14 : 06:02:00
|
| SELECT SUM(rbedaposo) as rbedaposo from(SELECT bedalog, tmpRm, SUM(bedaposo) AS rbedaposoFROM ErmisUser.QBEPROKWHERE (bedaflarr = '1') AND (LEFT(bedakind, 1) <> 'P')GROUP BY bedalog, tmpRmUNION ALLSELECT bedalog, tmpRm, SUM(bedaposo) AS rbedaposo1FROM ErmisUser.QBEPROKWHERE (bedaflarr <> '1') AND (LEFT(bedakind, 1) <> 'P')GROUP BY bedalog, tmpRm) as tMadhivananFailing to plan is Planning to fail |
 |
|
|
skiabox
Posting Yak Master
169 Posts |
Posted - 2008-02-14 : 06:12:14
|
| I need the total rbedaposo + rebedaposo1 displayed for every room in each account.Can it be done? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-14 : 06:19:50
|
| SELECT bedalog, tmpRm,SUM(rbedaposo) as rbedaposo from(SELECT bedalog, tmpRm, SUM(bedaposo) AS rbedaposoFROM ErmisUser.QBEPROKWHERE (bedaflarr = '1') AND (LEFT(bedakind, 1) <> 'P')GROUP BY bedalog, tmpRmUNION ALLSELECT bedalog, tmpRm, SUM(bedaposo) AS rbedaposo1FROM ErmisUser.QBEPROKWHERE (bedaflarr <> '1') AND (LEFT(bedakind, 1) <> 'P')GROUP BY bedalog, tmpRm) as tgroup by bedalog, tmpRmMadhivananFailing to plan is Planning to fail |
 |
|
|
skiabox
Posting Yak Master
169 Posts |
Posted - 2008-02-14 : 06:31:39
|
| Wow!It worked!Can it be used to construct a view?I mean CREATE VIEW testview AS( SELECT bedalog, tmpRm,SUM(rbedaposo) as rbedaposo from(SELECT bedalog, tmpRm, SUM(bedaposo) AS rbedaposoFROM ErmisUser.QBEPROKWHERE (bedaflarr = '1') AND (LEFT(bedakind, 1) <> 'P')GROUP BY bedalog, tmpRmUNION ALLSELECT bedalog, tmpRm, SUM(bedaposo) AS rbedaposo1FROM ErmisUser.QBEPROKWHERE (bedaflarr <> '1') AND (LEFT(bedakind, 1) <> 'P')GROUP BY bedalog, tmpRm) as tgroup by bedalog, tmpRm) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-14 : 06:32:58
|
| CREATE VIEW testview ASSELECT bedalog, tmpRm,SUM(rbedaposo) as rbedaposo from(SELECT bedalog, tmpRm, SUM(bedaposo) AS rbedaposoFROM ErmisUser.QBEPROKWHERE (bedaflarr = '1') AND (LEFT(bedakind, 1) <> 'P')GROUP BY bedalog, tmpRmUNION ALLSELECT bedalog, tmpRm, SUM(bedaposo) AS rbedaposo1FROM ErmisUser.QBEPROKWHERE (bedaflarr <> '1') AND (LEFT(bedakind, 1) <> 'P')GROUP BY bedalog, tmpRm) as tgroup by bedalog, tmpRmMadhivananFailing to plan is Planning to fail |
 |
|
|
skiabox
Posting Yak Master
169 Posts |
Posted - 2008-02-14 : 07:04:36
|
| ok.This first step worked.This rbedaposo calculation is used under the Credit column in my crystal report.Can now this result for every room in the account(bedalog) be compared to the total credit for this account?I calculate the total credit for every account using this view (stkef7):SELECT bedalog, SUM(bedaposo) AS sbedaposo2FROM ErmisUser.QBEPROKWHERE (LEFT(bedakind, 1) = 'P')GROUP BY bedalogThis view is giving a negative number as the total credit for each account.I want to compare the above calculation with the absolute value of the total credit (stkef7.sbedaposo2)so that I display the calculation only when the rbedaposo is lower than total credit.Tell me to give you more details if there is something that is not so clear.Thnx again for your help! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-14 : 07:12:40
|
| [code]SELECT t.bedalog,t.tmpRm,CASE WHEN ABS(s.sbedaposo2)-t.rbedaposo >0 THEN ABS(s.sbedaposo2)-t.rbedaposo ELSE 0AS DifferenceFROM testview tINNER JOIN stkef7 sON s.bedalog=t.bedalog[/code] |
 |
|
|
skiabox
Posting Yak Master
169 Posts |
Posted - 2008-02-14 : 07:19:19
|
| Msg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword 'AS' |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-14 : 07:22:31
|
| [code]SELECT t.bedalog,t.tmpRm,CASE WHEN ABS(s.sbedaposo2)-t.rbedaposo >0 THEN ABS(s.sbedaposo2)-t.rbedaposo ELSE 0ENDAS DifferenceFROM testview tINNER JOIN stkef7 sON s.bedalog=t.bedalog[/code]MadhivananFailing to plan is Planning to fail |
 |
|
|
skiabox
Posting Yak Master
169 Posts |
Posted - 2008-02-14 : 07:53:23
|
| Is there any way I can choose a specific bedalog from this query? |
 |
|
|
skiabox
Posting Yak Master
169 Posts |
Posted - 2008-02-14 : 08:05:36
|
| ok.I put an ORDER BY t.bedalog, t.tmpRm in the end of the query. |
 |
|
|
skiabox
Posting Yak Master
169 Posts |
Posted - 2008-02-14 : 08:30:43
|
| ok.We're getting somewhere now.First see these 2 accounts (from the last query) :8910 406 0.008910 407 0.008910 408 0.008910 409 0.008910 431 0.008910 432 0.008910 433 0.008910 434 0.008910 435 0.008910 437 0.008910 438 0.008910 439 0.008910 443 26.008910 444 0.008910 445 26.008910 446 0.008910 451 0.008910 452 0.008910 453 0.008910 530 26.008910 539 0.008910 548 0.008932 502 301.008932 507 320.008932 508 320.008932 509 320.008932 510 301.508932 511 320.008932 514 320.008932 515 320.008932 522 320.008932 541 301.008932 543 320.008932 544 320.008932 545 320.008932 546 320.008932 547 320.008932 549 320.008932 602 335.008932 604 335.008932 605 335.008932 606 320.008932 607 301.008932 608 320.008932 609 320.008932 610 320.008932 611 320.008932 613 320.008932 614 320.008932 615 320.00And now see what I want to get in my report :Account Room Arrang Extras Credit Remaining8910 406 264 0 230 348910 407 264 0 0 2648910 408 264 0 0 2648910 409 264 9 0 2738910 431 264 15,75 0 279,75. . . . . .. . . . . .Totals 5568 117,20 230 5455,20The second example is when total credit is bigger than the first sum of arrangements + extras :Account Room Arrang Extras Credit Remaining8932 502 69 0 69 08932 507 50 0 50 08932 508 50 0 50 08932 509 50 0 50 08932 510 50 18,50 68,50 08932 511 50 0 50 08932 514 50 0 32,50 17,508932 515 50 0 0 508932 522 50 0 0 50. . . . . .. . . . . .Totals 1412 18,50 370 1060,5 |
 |
|
|
Next Page
|