Hi SET,
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.
:)
Thanking You And With Best Regards.
Vishal
On Friday, 21 July 2017 2:26 AM, "setysvar [email protected]
[firebird-support]" <[email protected]> wrote:
Den 17.07.2017 12:30, skrev Vishal Tiwari [email protected]
[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
#yiv0475203540 #yiv0475203540 -- #yiv0475203540ygrp-mkp {border:1px solid
#d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv0475203540
#yiv0475203540ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv0475203540
#yiv0475203540ygrp-mkp #yiv0475203540hd
{color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px
0;}#yiv0475203540 #yiv0475203540ygrp-mkp #yiv0475203540ads
{margin-bottom:10px;}#yiv0475203540 #yiv0475203540ygrp-mkp .yiv0475203540ad
{padding:0 0;}#yiv0475203540 #yiv0475203540ygrp-mkp .yiv0475203540ad p
{margin:0;}#yiv0475203540 #yiv0475203540ygrp-mkp .yiv0475203540ad a
{color:#0000ff;text-decoration:none;}#yiv0475203540 #yiv0475203540ygrp-sponsor
#yiv0475203540ygrp-lc {font-family:Arial;}#yiv0475203540
#yiv0475203540ygrp-sponsor #yiv0475203540ygrp-lc #yiv0475203540hd {margin:10px
0px;font-weight:700;font-size:78%;line-height:122%;}#yiv0475203540
#yiv0475203540ygrp-sponsor #yiv0475203540ygrp-lc .yiv0475203540ad
{margin-bottom:10px;padding:0 0;}#yiv0475203540 #yiv0475203540actions
{font-family:Verdana;font-size:11px;padding:10px 0;}#yiv0475203540
#yiv0475203540activity
{background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv0475203540
#yiv0475203540activity span {font-weight:700;}#yiv0475203540
#yiv0475203540activity span:first-child
{text-transform:uppercase;}#yiv0475203540 #yiv0475203540activity span a
{color:#5085b6;text-decoration:none;}#yiv0475203540 #yiv0475203540activity span
span {color:#ff7900;}#yiv0475203540 #yiv0475203540activity span
.yiv0475203540underline {text-decoration:underline;}#yiv0475203540
.yiv0475203540attach
{clear:both;display:table;font-family:Arial;font-size:12px;padding:10px
0;width:400px;}#yiv0475203540 .yiv0475203540attach div a
{text-decoration:none;}#yiv0475203540 .yiv0475203540attach img
{border:none;padding-right:5px;}#yiv0475203540 .yiv0475203540attach label
{display:block;margin-bottom:5px;}#yiv0475203540 .yiv0475203540attach label a
{text-decoration:none;}#yiv0475203540 blockquote {margin:0 0 0
4px;}#yiv0475203540 .yiv0475203540bold
{font-family:Arial;font-size:13px;font-weight:700;}#yiv0475203540
.yiv0475203540bold a {text-decoration:none;}#yiv0475203540 dd.yiv0475203540last
p a {font-family:Verdana;font-weight:700;}#yiv0475203540 dd.yiv0475203540last p
span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv0475203540
dd.yiv0475203540last p span.yiv0475203540yshortcuts
{margin-right:0;}#yiv0475203540 div.yiv0475203540attach-table div div a
{text-decoration:none;}#yiv0475203540 div.yiv0475203540attach-table
{width:400px;}#yiv0475203540 div.yiv0475203540file-title a, #yiv0475203540
div.yiv0475203540file-title a:active, #yiv0475203540
div.yiv0475203540file-title a:hover, #yiv0475203540 div.yiv0475203540file-title
a:visited {text-decoration:none;}#yiv0475203540 div.yiv0475203540photo-title a,
#yiv0475203540 div.yiv0475203540photo-title a:active, #yiv0475203540
div.yiv0475203540photo-title a:hover, #yiv0475203540
div.yiv0475203540photo-title a:visited {text-decoration:none;}#yiv0475203540
div#yiv0475203540ygrp-mlmsg #yiv0475203540ygrp-msg p a
span.yiv0475203540yshortcuts
{font-family:Verdana;font-size:10px;font-weight:normal;}#yiv0475203540
.yiv0475203540green {color:#628c2a;}#yiv0475203540 .yiv0475203540MsoNormal
{margin:0 0 0 0;}#yiv0475203540 o {font-size:0;}#yiv0475203540
#yiv0475203540photos div {float:left;width:72px;}#yiv0475203540
#yiv0475203540photos div div {border:1px solid
#666666;min-height:62px;overflow:hidden;width:62px;}#yiv0475203540
#yiv0475203540photos div label
{color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv0475203540
#yiv0475203540reco-category {font-size:77%;}#yiv0475203540
#yiv0475203540reco-desc {font-size:77%;}#yiv0475203540 .yiv0475203540replbq
{margin:4px;}#yiv0475203540 #yiv0475203540ygrp-actbar div a:first-child
{margin-right:2px;padding-right:5px;}#yiv0475203540 #yiv0475203540ygrp-mlmsg
{font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv0475203540
#yiv0475203540ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv0475203540
#yiv0475203540ygrp-mlmsg select, #yiv0475203540 input, #yiv0475203540 textarea
{font:99% Arial, Helvetica, clean, sans-serif;}#yiv0475203540
#yiv0475203540ygrp-mlmsg pre, #yiv0475203540 code {font:115%
monospace;}#yiv0475203540 #yiv0475203540ygrp-mlmsg *
{line-height:1.22em;}#yiv0475203540 #yiv0475203540ygrp-mlmsg #yiv0475203540logo
{padding-bottom:10px;}#yiv0475203540 #yiv0475203540ygrp-msg p a
{font-family:Verdana;}#yiv0475203540 #yiv0475203540ygrp-msg
p#yiv0475203540attach-count span {color:#1E66AE;font-weight:700;}#yiv0475203540
#yiv0475203540ygrp-reco #yiv0475203540reco-head
{color:#ff7900;font-weight:700;}#yiv0475203540 #yiv0475203540ygrp-reco
{margin-bottom:20px;padding:0px;}#yiv0475203540 #yiv0475203540ygrp-sponsor
#yiv0475203540ov li a {font-size:130%;text-decoration:none;}#yiv0475203540
#yiv0475203540ygrp-sponsor #yiv0475203540ov li
{font-size:77%;list-style-type:square;padding:6px 0;}#yiv0475203540
#yiv0475203540ygrp-sponsor #yiv0475203540ov ul {margin:0;padding:0 0 0
8px;}#yiv0475203540 #yiv0475203540ygrp-text
{font-family:Georgia;}#yiv0475203540 #yiv0475203540ygrp-text p {margin:0 0 1em
0;}#yiv0475203540 #yiv0475203540ygrp-text tt {font-size:120%;}#yiv0475203540
#yiv0475203540ygrp-vital ul li:last-child {border-right:none
!important;}#yiv0475203540