|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Subquery in Columns/Fields
I tried to post earlier so I apologize if my other post finally comes up and there are duplicates.
I am fairly new to SQL and am having problems trying to put together a subquery. I have two queries that have so much in common that it seems like an easy join. My problem is that each query is looking at each line of each transaction for a certain line object (111 is for purchases and 444 is for markdowns). Right now I am running the two and merging outside my current application. I'm not sure if I can use a SELECT statement (using the Markdowns statement as a field in the Purchases statement) in the columns/fields portion but I have tried and failed at that. Ideally I'd like to be able to take the markdowns and divide them by the purchases to get the % discount and filter only those that are greater than 60% discount but right now I would be happy if I could get a solid subquery going. Below is are the two SQL statements I am running. SELECT a.store_no as Field_a, a.transaction_date as Field_b, a.transaction_no as Field_c, a.register_no as Field_d, a.cashier_no as Field_e, SUM(b.gross_line_amount) as Field_f FROM DevDB.transaction_header a, DevDB.transaction_line b, DevDB.store c WHERE a.transaction_id=b.transaction_id AND a.store_no=c.store_no AND (a.transaction_date Between {{From date is *}} and {{To date is *}} AND b.line_object = 111 AND b.line_action = 11) AND EXISTS (SELECT bb.gross_line_amount FROM DevDB.transaction_header aa, DevDB.transaction_line bb, DevDB.store cc WHERE aa.transaction_id=bb.transaction_id AND aa.store_no = cc.store_no AND aa.transaction_id = a.transaction_id AND (aa.transaction_date Between {{From date is *}} and {{To date is *}} AND bb.line_object = 444 AND bb.line_action = 44)) GROUP BY a.store_no,a.transaction_date,a.transaction_no,a.r egister_no,a.cashier_no SELECT a.store_no as Field_a, a.transaction_date as Field_b, a.transaction_no as Field_c, a.register_no as Field_d, a.cashier_no as Field_e, SUM(b.gross_line_amount) as Field_f FROM DevDB.transaction_header a, DevDB.transaction_line b, DevDB.store c WHERE a.transaction_id=b.transaction_id AND a.store_no=c.store_no AND (a.transaction_date Between {{From date is *}} and {{To date is *}} AND b.line_object = 444 AND b.line_action = 44) GROUP BY a.store_no,a.transaction_date,a.transaction_no,a.r egister_no,a.cashier_no Thank you to any help you can give me. |
![]() |
| Viewing: Dev Articles Community Forums > Databases > General SQL Development > Subquery in Columns/Fields |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|