Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7802789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:21:41+00:00 2026-06-02T01:21:41+00:00

i have a table Transaction with following column and data id transaction_date trans_type account_id

  • 0

i have a table Transaction with following column and data

id  transaction_date trans_type account_id agents_id transaction_date price miles
1   2012-02-08       Buy        1          1         2010-02-08       0.016 12000
2   2012-03-01       Sell       2          2         2012-03-10       0.256 -2000
3   2012-03-27       Buy        3          3         2012-03-27       0.256 10000
4   2012-03-28       Sell       4          4         2012-03-28       0.589 -11000
5   2012-03-29       Buy        5          5         2012-03-29       0.87  25000
6   2012-03-29       Sell       6          6         2012-02-29       0.879 -12000
7   2012-04-01       Sell       7          7         2012-04-01       0.058 -15000

  Account Table
  id    Program_id
  1     1
  2     1
  3     2

  Program table
  id      Abbreviation
  1       AA
  2       AC

  Agents table
  id      Name
  1       Bob
  2       Ben

I want to get first sell date and first buy date to get average days before a transaction is sold, to get days transaction is in inventory, so it should be

  (Sell date)2012-03-01 - (Buy date)2012-02-08

i m trying this

SELECT 
    case when t.trans_type ='Sell' then transaction_date end as SellDate
   ,case when t.trans_type ='Buy' then transaction_date end as BuyDate
   ,DATEDIFF(case when t.trans_type ='Sell' then transaction_date end
            ,case when t.trans_type ='Buy' then transaction_date end) as Date
   ,transaction_date
FROM transactions t
order by transaction_date

But always getting NULL in Date

Here is the complete query

SELECT p.abbreviation,ag.name
  ,sum(-1.00 * t.miles * t.price - coalesce(t.fees,0) - coalesce(c.cost,0)) as profit
  ,sum(t.miles) 'Totakl Miles'
  ,avg(price / miles) 'Average'
  ,transaction_date
FROM transactions t
inner join accounts a on t.account_id = a.id
inner join programs p on a.program_id = p.id
inner join agents ag on t.agent_id = ag.id
LEFT JOIN (
           SELECT rp.sell_id, sum(rp.miles * t.price) as cost
           from report_profit rp
           inner join transactions t on rp.buy_id = t.id
           where t.miles > 50000
           group by rp.sell_id
           order by rp.sell_id
          ) c on t.id = c.sell_id
where t.transaction_date BETWEEN '2012-03-14' AND '2012-04-14'
Group by p.id , ag.id

EDIT

I tried liquorvicar answer, but it is giving error “Sub-query return more than one record” because of the Group by i added

Any one can guide me on this?

Thanks in advance…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:21:43+00:00Added an answer on June 2, 2026 at 1:21 am

    first of all thanks all for every help

    Here is the query which return exact result

    select p_id,ag_id,
         p_abb,ag_name
        ,sum(-1.00 * miles * price - coalesce(fees,0) - coalesce(cost,0)) as profit
        ,sum(miles) 'Total Miles',avg(price / miles) 'Average'
        ,DATEDIFF(min(buy_dt),min(sell_dt)) as 'Days'
         From
         (
             SELECT p.id 'p_id',ag.id 'ag_id',p.abbreviation 'p_abb',ag.name 'ag_name'
             ,miles
             ,price
             ,fees
             ,c.cost
             ,case when t.trans_type ='Sell' then transaction_date end 'sell_dt'
             ,case when t.trans_type ='Buy' then transaction_date end 'buy_dt'
             ,transaction_date
           FROM transactions t
           inner join accounts a on t.account_id = a.id
           inner join programs p on a.program_id = p.id
           inner join agents ag on t.agent_id = ag.id
           LEFT JOIN (
                SELECT rp.sell_id, sum(rp.miles * t.price) as cost
               from report_profit rp
               inner join transactions t on rp.buy_id = t.id
               where t.miles > 50000
               group by rp.sell_id
               order by rp.sell_id
              ) c on t.id = c.sell_id
    
      ) t1
      group by p_id, ag_id
    

    Thanks all again..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following transaction: SQL inserts a 1 new record into a table
Ok I have a data table containing duplicate Reciept numbers and a transaction value
I have an XML structure like the following: <tables> <table name=tableName1> <row ID=34 col1=data
I have two tables as following table one named tbooking having column fields as
I have a table with the following column: NOTEID NUMBER NOT NULL, For all
I have the following columns in Table A which records users fingerprint "transaction" every
I have the following requirement. Ex: There is a transaction table where it has
I have a table transaction which has duplicates. i want to keep the record
I have a transaction table like so id , name , code , flag
I have a transaction table like so id , name , code , flag

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.