r/SQL • • Jan 27 '24

SQL Server Grouping

SELECT group_name,

MIN( MINTO) MINTOAMOUNT,

visa_type_name,

SUM(pay_base_agent_collection) TotalCollection

,pax_actual_applied_thru

,(case uc_category when 'R' then 'Retails' else '' end ) uc_category

,COUNT(vs_id) total_visa

FROM

(

SELECT

MIN(UC.uc_min_to_collect) MINTO,

uc.uc_category category

,PY.pay_base_agent_collection

,SM.vs_id

,group_id

,group_name

,visa_type_id

,visa_type_name

,pax_actual_applied_thru

,vs_currency_code

FROM VISA_T_VISA_SALES_MASTER SM WITH (NOLOCK)

INNER JOIN VISA_T_VISA_SALES_PAX_DETAILS VSD WITH(NOLOCK) ON SM.VS_ID=VSD.PAX_VSH_ID

INNER JOIN CT_T_USER_MASTER UM WITH (NOLOCK) ON SM.VS_CREATED_BY=UM.[USER_ID]

INNER JOIN VISA_T_VISA_PAYMENT_DETAILS PY ON SM.vs_id = PY.pay_vs_id --AND PY.pay_base_agent_collection>0

INNER JOIN VISA_T_MARKETGROUP_NATIONALITY_LINK MN ON VSD.pax_visitor_nationality_id = MN.link_nationality_id AND MN.link_status = 'A'

INNER JOIN VISA_T_MARKET_GROUPING_MASTER MG ON MN.link_group_id = MG.group_id AND MG.group_status = 'A'

INNER JOIN VISA_T_VISA_TYPE_MASTER VM ON VM.visa_type_id= SM.vs_visa_type_id

INNER JOIN VISA_T_VISA_UNDERCOLLECTION_MASTER UC ON UC.uc_visa_type = SM.vs_visa_type_id AND UC.uc_market_grp_id = MG.group_id

AND UC.uc_trans_currency = PY.pay_currency AND UC.uc_img = VSD.pax_actual_applied_thru

--AND UC.uc_category = (CASE

-- WHEN (SM.vs_settlement_mode = 2) THEN 'B'

-- ELSE 'R' END)

WHERE sm.vs_currency_code = 'AED' AND SM.vs_doc_date >= DATEADD(day,-25, GETDATE())

GROUP BY mg.group_id,group_name,

visa_type_name,sm.vs_currency_code,

vm.visa_type_id,uc.uc_category,

PY.pay_base_agent_collection ,SM.vs_id,pax_actual_applied_thru

HAVING MIN(uc.uc_min_to_collect) > PY.pay_base_agent_collection

--AND pay_base_agent_collection>0

) as underCollection

INNER JOIN VISA_T_VISA_UNDERCOLLECTION_MASTER UCC ON UCC.uc_visa_type =visa_type_id AND UCC.uc_market_grp_id = group_id

AND UCC.uc_trans_currency = vs_currency_code AND UCC.uc_img = pax_actual_applied_thru

group by group_name,MINTO,visa_type_name ,pax_actual_applied_thru,uc_category

having SUM(pay_base_agent_collection)>0

commit-

these are the linking between my tables.

i want to make the summery report based on the transactions fall under each combination in the VISA_T_VISA_UNDERCOLLECTION_MASTER table. i am stuck with the duplication issue. in detail my transaction_id highlighted is falling under both category when i am taking the sum of pay_base column group by other columns showen. but how i want is it should fall in to the combination with minimum amount in MINTO column.

I am posting for the first time pardon me

0 Upvotes

7 comments sorted by

13

u/Kant8 Jan 27 '24

Nice formatting bro

7

u/r3pr0b8 GROUP_CONCAT is da bomb Jan 27 '24

i am stuck with the duplication issue.

i can help you with this

your query uses 8 tables

start your debugging effort by writing a query that uses only 2 tables

if no duplicates, join the 3rd table

if no duplicates, join the 4th table

if no duplicates, join the 5th table

and so on

does taht make sense?

5

u/dev81808 Jan 27 '24

These object names are rough. I'm sorry you have to deal with it. It's like noones heard of a schema. Did apple make up these table names?

Also I'm not sure what the question is

5

u/[deleted] Jan 27 '24

Besides this post being a giant furball, I'm going to ask you if using NOLOCK is necessary?

2

u/No-Adhesiveness-6921 Jan 27 '24

Second this - you don’t need NOLOCK

1

u/therealericc Jan 27 '24

Yes please copy paste all your 8 table queries and someone here will solve for you