Subject Re: [firebird-support] How to get different Maximum Data for every single date in the table?
Author setysvar
Hi again, Vishal!

Here's a different query that will reasonably often give you what you want:

with CopyToTals( Order_ID, Order_Date, Salesman_ID, Copy_Sold,        ToTal_Amount      ) as
( select         Order_ID, Order_Date, Salesman_ID, Sum( Copy_Sold ), Sum( ToTal_Amount )
  from Ordered_Book_Details
  group by Order_ID, Order_Date, Salesman_ID ),
Orders(  Order_ID,       Order_Date, Salesman_ID, Order_Count ) as
( select list(Order_ID), Order_Date, Salesman_ID, count( * )
  from Ordered_Book_Details
  group by Order_Date, Salesman_ID
),
Maxes( Order_Date, Max_Copy_Sold, Max_ToTal_Amount, Max_Order_Count ) as
( select ct.Order_Date, max( ct.Copy_Sold ), max( ct.ToTal_Amount ), max( o.Order_Count )
  from CopyToTals ct
  join Orders o on ct.Order_Date = o.Order_Date
  group by ct.Order_Date
)
select c.Order_ID, c.Order_Date, c.Salesman_ID, c.Copy_Sold, c.ToTal_Amount,
       t.Order_ID,               t.Salesman_ID, t.Copy_Sold, t.ToTal_Amount,
       o.Order_ID,               o.Salesman_ID, o.Order_Count
from Maxes m
join CopyToTals c on m.Order_Date = c.Order_Date and m.Max_Copy_Sold    = c.Copy_Sold
join CopyToTals t on m.Order_Date = t.Order_Date and m.Max_ToTal_Amount = t.ToTal_Amount
join Orders     o on m.Order_Date = o.Order_Date and m.Max_Order_Count  = o.Order_Count

I say reasonably often because you may have ties, i.e. the maximum of Copy_Sold, ToTal_Amount and/or Order_Count being reached by different Salesman_ID. If there's only one tie, then no problem (you just get two rows), but if you have two ties, e.g. both S001 and S002 selling 500 copies for a total of 200000, then you will get both of them for both c and t and hence, four rows.

Moreover, this query works nicely on the 16 rows in the test table, but I suspect it may be too slow if there are many rows. Hence, this is a query that I'd recommend you to consider replacing with an EXECUTE BLOCK (or a stored procedure) if Ordered_Book_Details is a huge table.

I didn't understand why you wanted the Order_Date repeated three times for each row, so I removed two of them.

HTH,
Set

Den 21.07.2017 10:05, skrev Vishal Tiwari vishualsoft@... [firebird-support]:
First of all Thank You for the SQL. Let me go through.

I am ready if you get enough time to look into good solution for this, and I would wait for you.
This is also different type of SQL I am seeing and feeling happy to learn valuable things from it.
Thank You Sooooo much SET. But I would also prefer to wait for another solution. But no hurry, take your time.
Whenever you are free to look into this, please have a look.

On Friday, 21 July 2017 2:26 AM, "setysvar setysvar@... [firebird-support]" <firebird-support@yahoogroups.com> wrote:


 
Den 17.07.2017 12:30, skrev Vishal Tiwari vishualsoft@...
[firebird-support]:
> Hi All,
> As per the attache files or below specified table DDL, DML and SQL, I need to get below details:
> Get Order_ID, Order_Date, Salesman_ID for different Salesmen :1. Who sold maximum Copy for a day2. Who sold maximum amount of book for a day3. Who got maximum order for a day
> As per below SQL, I am able to fetch the result only in one record that is correct. But I need to get the data for every single date i.e. for all distinct date only, i.e. Date should not repeat in the row, which satisfy above three condition. The result could be for different Salesmen or same Salesman depending on the data in the table.
> So, I need to get data for every distinct date in the table. Please help how to get it.
> One option I see to put "Order_ID" column in Max() function to avoid Group By issue.
>
> Kindly suggest, If possible please correct the above SQL and share.
>
> ...
> ----------
>
> Get Order_ID, Order_Date, Salesman_ID for different Salesmen :
> 1. Who sold maximim Copy for a day
> 2. Who sold maximum amount of book for a day
> 3. Who got maximum order for a day
>
>
>
> With Max_Copy (Order_ID, Order_Date, Salesman_ID, Max_Copy_Sold, ToTal_Amount) As
> (Select Order_ID, Order_Date, Salesman_ID, Sum(Copy_Sold) As Max_Copy_Sold , Sum(ToTal_Amount)
> From Ordered_Book_Details
> Group By Order_ID, Order_Date, Salesman_ID Order By Max_Copy_Sold Desc
> ),
>
> Max_ToTal (Order_ID, Order_Date, Salesman_ID, Copy_Sold, Max_ToTal_Amount) As
> (Select Order_ID, Order_Date, Salesman_ID, Sum(Copy_Sold), Sum(ToTal_Amount) As Max_ToTal_Amount
> From Ordered_Book_Details
> Group By Order_ID, Order_Date, Salesman_ID Order By Max_ToTal_Amount Desc
>
> ),
>
> Max_Orders (Order_ID, Order_Date, Salesman_ID, Max_Orders_Count) As
> (Select List(Order_ID), Order_Date, Salesman_ID, Count(Salesman_ID) As Max_Orders_Count
> From Ordered_Book_Details
> Group By Salesman_ID, Order_Date Order By Max_Orders_Count Desc
> )
>
> Select First 1 Max_Copy.Order_ID, Max_Copy.Order_Date, Max_Copy.Salesman_ID, Max_Copy.Max_Copy_Sold, Max_Copy.ToTal_Amount,
> Max_ToTal.Order_ID, Max_ToTal.Order_Date, Max_ToTal.Salesman_ID, Max_ToTal.Copy_Sold, Max_ToTal.Max_ToTal_Amount,
> Max_Orders.Order_ID, Max_Orders.Order_Date, Max_Orders.Salesman_ID, Max_Orders.Max_Orders_Count
> From
> Max_Copy, Max_ToTal, Max_Orders

Hi Vishal!

This is somewhat different from what you asked for, but I hope it can
serve as a starting point.

With CopyTotals ( Order_ID, Order_Date, Salesman_ID, Copy_Sold,
ToTal_Amount ) As
( Select Order_ID, Order_Date, Salesman_ID, Sum( Copy_Sold ), Sum(
ToTal_Amount )
From Ordered_Book_Details
Group By Order_ID, Order_Date, Salesman_ID
),
Orders (Order_ID, Order_Date, Salesman_ID, Max_Orders_Count) As
( Select List( Order_ID ), Order_Date, Salesman_ID, Count( * ) As
Max_Orders_Count
From Ordered_Book_Details
Group By Salesman_ID, Order_Date
)

Select ct.Order_ID, ct.Order_Date, ct.Salesman_ID, ct.Copy_Sold,
ct.ToTal_Amount,
o.Order_ID, o.Salesman_ID, o.Max_Orders_Count
From CopyTotals ct
Join Orders o on ct.Order_Date = o.Order_Date
Where ( not exists( select * from CopyTotals ct2
where ct.Order_Date = ct2.Order_Date
and ct.Copy_Sold < ct2.Copy_Sold )
or not exists( select * from CopyTotals ct2
where ct.Order_Date = ct2.Order_Date
and ct.Total_Amount < ct2.Total_Amount ) )
and not exists( select * from Orders o2
where o.Order_Date = o2.Order_Date
and o.Max_Orders_Count < o2.Max_Orders_Count )

It returns the max copies and totals in one or two rows depending on
whether or not it is the same Order_ID that is maximum. The query will
probably be slow since the CTEs are also mentioned in the NOT EXISTs.

I'm not convinced this is an acceptable solution, but my head is a bit
too tired to come up with any good suggestion today.

HTH,
Set