SQL: Alternating values of a field - sql

So, I'm trying to get a interesting thing to happen, and haven't been able to really get it to work.
So, my query:
SELECT a.lead_id
,a.status
,a.[user]
, a.list_id
, a.last_local_call_time
, a.owner
FROM [LEADS].[dbo].[LDS_VICIDIAL_LIST] a
inner join leads.dbo.LDS_CONTACT_STATUS b on a.[status]=b.[status]
where list_id in ('2557','2562','2559') and Dialable='Y' and a.status<>'5NI' and a.called_since_last_reset = 'n'
order by a.last_local_call_time
and basically what I am trying to do is get it to display 1 record from list 2557, 1 record from list 2562, and 1 record from 2559 and repeating as such.

SELECT a.lead_id
,a.status
,a.[user]
, a.list_id
, a.last_local_call_time
, a.owner
FROM [LEADS].[dbo].[LDS_VICIDIAL_LIST] a
inner join leads.dbo.LDS_CONTACT_STATUS b on a.[status]=b.[status]
where list_id in ('2557','2562','2559') and Dialable='Y' and a.status<>'5NI' and a.called_since_last_reset = 'n'
order by ROW_NUMBER() OVER (PARTITION BY a.list_id order by a.list_id)
Got me what I wanted.

Related

Grouping Multiple Rows into one Row

Here its my script to read stores QTY depending on ITEM SID, I need to group all records which are related to same ITEM SID to be merged together in one line, find below example images for result and expected results.
SELECT DISTINCT
i.SBS_NO,
to_char(i.ITEM_SID) as ITEMSID,
i.ALU ,
a.LOT_NUMBER ,
a.LOT_NAME,
a.LOT_NOTE,
a.EXPIRY_DATE,
a.ACTIVE ,
--s.STORE_NO,
--s.QTY,
DECODE(S.STORE_NO,1,S.QTY,'0') STR1QTY,
DECODE(S.STORE_NO,2,S.QTY,'0') STR2QTY,
DECODE(S.STORE_NO,3,S.QTY,'0') STR3QTY,
DECODE(S.STORE_NO,4,S.QTY,'0') STR4QTY,
DECODE(S.STORE_NO,5,S.QTY,'0') STR5QTY,
DECODE(S.STORE_NO,6,S.QTY,'0') STR6QTY,
DECODE(S.STORE_NO,7,S.QTY,'0') STR7QTY,
DECODE(S.STORE_NO,0,S.QTY,'0') STR0QTY,
DECODE(S.STORE_NO,99,S.QTY,'0') WHQTY,
i.DESCRIPTION2,
i.DESCRIPTION3,
i.DESCRIPTION4,
i.DCS_CODE,
i.ATTR,
i.SIZ,
i.FST_RCVD_DATE,
i.UDF1_DATE,
i.QTY_PER_CASE,
i.ACTIVE,
i.MARKDOWN_PRICE,
i.UDF2_VALUE as ITEM_STATUS
from inventory_v i
inner join LOT a
on i.ITEM_SID = a.ITEM_SID
inner join LOT_QTY s
on a.ITEM_SID=s.ITEM_SID
where i.sbs_no=1
and i.ITEM_SID=a.ITEM_SID
--and i. ALU='358N690175'
and a.ITEM_SID=s.ITEM_SID
and i.SBS_NO=a.SBS_NO
and a.SBS_NO=s.SBS_NO
and s.STORE_No in (0,1,2,3,4,5,6,7,99)
and a.ACTIVE=1
and i.ACTIVE=1
GROUP BY
i.SBS_NO,
i.ITEM_SID,
i.ALU ,
a.LOT_NUMBER ,
a.LOT_NAME,
a.LOT_NOTE,
a.EXPIRY_DATE,
a.ACTIVE ,
s.STORE_NO,
s.QTY,
i.DESCRIPTION2,
i.DESCRIPTION3,
i.DESCRIPTION4,
i.DCS_CODE,
i.ATTR,
i.SIZ,
i.FST_RCVD_DATE,
i.UDF1_DATE,
i.QTY_PER_CASE,
i.ACTIVE,
i.MARKDOWN_PRICE,
i.UDF2_VALUE
;
THANKS
[here the current result ]
[expected result]

Join 2 oracle queries

someone, please help to join the below queries.
I have tried my best but not able to join with the condition.
PLN_ID is the common column on both the tables.
Query 1-
SELECT PLN_ID
, ASSORTMENT_GROUP
, STORE
, PLANOGRAM
, STATUS
FROM ACN_PLANOGRAMS
WHERE PLANOGRAM not like '%<Untitled>%'
;
​
Query 2
SELECT distinct(PLN_ID)
, count(*)
, (sum(WIDTH)) AS width
FROM ACN_FIXEL
WHERE type='0'
GROUP BY PLN_ID
HAVING count(*) > 1
;
Please changes join what you want you. Try this query:
SELECT
DISTINCT(a.PLN_ID),
(SUM(a.WIDTH)) AS width,
b.PLN_ID,
b.ASSORTMENT_GROUP,
b.STORE,
b.PLANOGRAM,
b.STATUS
FROM
ACN_FIXEL a
INNER JOIN
ACN_PLANOGRAMS b ON a.PLN_ID = b.PLN_ID
WHERE
a.type = '0'
AND b.PLANOGRAM NOT LIKE '%<Untitled>%'
GROUP BY
a.PLN_ID,
b.PLN_ID,
b.ASSORTMENT_GROUP,
b.STORE,
b.PLANOGRAM,
b.STATUS
HAVING
COUNT(*) > 1
There are several ways to solve this. Without understanding your data model or business logic I offer the simplest solution, a derived table (inline view):
SELECT p.PLN_ID
, p.ASSORTMENT_GROUP
, p.STORE
, p.PLANOGRAM
, p.STATUS
, f.fixel_count
, f.fixel_width
FROM ACN_PLANOGRAMS p
inner join (SELECT PLN_ID
, count(*) as fixel_count
, (sum(WIDTH)) AS fixel_width
FROM ACN_FIXEL
WHERE type='0'
GROUP BY PLN_ID
HAVING count(*) > 1 ) f
on f.pln_id = p.pln_id
WHERE p.PLANOGRAM not like '%<Untitled>%'
;
This solution only returns results for PLN_ID in both result sets. If you have a different logic you may need to use LEFT OUTER JOIN instead.
Make Query 2 a subquery:
SELECT ap.PLN_ID
, ap.ASSORTMENT_GROUP
, ap.STORE
, ap.PLANOGRAM
, ap.STATUS
, sq.cnt
, sq.width
FROM ACN_PLANOGRAMS ap
JOIN (
SELECT PLN_ID
, count(*) AS cnt
, sum(WIDTH) AS width
FROM ACN_FIXEL
WHERE type='0'
GROUP BY PLN_ID
HAVING count(*) > 1
) sq
ON ( sq.PLN_ID = ap.PLN_ID )
WHERE ap.PLANOGRAM not like '%<Untitled>%'
;

Can't order query correctly

A while ago I requested help to code a LEFT JOIN filtering in a particular way that the result postition the desired value in the first row.
Need to retrieve table's last inserted/updated record with some exclusions
The thing now is that there are many cases which are mixing data. The scenario is that on the same table we have 2 values that we need to organize on different columns. The PO_ID is unique, but can have 1 or more values on the other tables, and for this particular case 1 PO_ID has 3 SHIP_ID_CUS values. We only need 1 PO_ID per row (no duplicates) that is way we used the MAX() and GROUP BY.
Here is a piece of the code that I think cause issues.
select
z.po_id,
max(scdc.ship_id) as ship_id_cdc,
max(lscdc.ship_evnt_cd) as last_event_cdc,
max(lscdc.ship_evnt_tms) as event_tms_cdc,
max(scus.SHIP_ID) as ship_id_cus,
max(lscus.ship_evnt_cd) as last_event_cus,
max(lscus.ship_evnt_tms) as event_tms_cus
from TABLE.A z
left join (select distinct po_id, iltc.ship_id, s.ship_to_loc_code from TABLE.B iltc inner join TABLE.C s on iltc.ship_id=s.ship_id and iltc.ship_to_loc_code=s.ship_to_loc_code and s.ship_to_ctry<>' ') AS A ON z.po_id = a.po_id
left JOIN TABLE.C scus ON A.SHIP_ID = scus.SHIP_ID AND A.SHIP_TO_LOC_CODE = scus.SHIP_TO_LOC_CODE and scus.loc_type = 'CUS' AND DAYS(scus.shipment_tms)+10 >= DAYS(z.ship_tms)
left JOIN TABLE.C scdc ON A.SHIP_ID = scdc.SHIP_ID AND A.SHIP_TO_LOC_CODE = scdc.SHIP_TO_LOC_CODE and scdc.loc_type = 'CDC' AND DAYS(scdc.shipment_tms)+10 >= DAYS(z.ship_tms)
left join
( select ship_id_856, ship_to_loc_cd856, ship_evnt_cd, ship_evnt_tms, carr_tracking_num, event_srv_lvl
, row_number() over(partition by ship_id order by updt_job_tms desc) as RN
FROM TABLE.D
WHERE LEFT(ship_evnt_cd, 1) <> '9') lscus
ON lscus.ship_id_856=scus.ship_id and scus.ship_to_loc_code=lscus.ship_to_loc_cd856 and lscus.rn = 1
left join
( select ship_id_856, ship_to_loc_cd856, ship_evnt_cd, ship_evnt_tms, carr_tracking_num, event_srv_lvl
, row_number() over(partition by ship_id order by updt_job_tms desc) as RN
FROM TABLE.D
WHERE LEFT(ship_evnt_cd, 1) <> '9') lscdc
ON lscdc.ship_id_856=scdc.ship_id and lscdc.ship_to_loc_cd856=scdc.ship_to_loc_code and lscdc.rn = 1
WHERE
z.po_id = 'T1DLDC'
GROUP BY z.po_id
By searching that condition we get the following result
The problem is that if we search directly on the TABLE.D, the last event that we need (with last update record tms) is another one (X1) and somehow the date is incorrect.
What is even more weird, is that if we search for the ship_id_cus on the original query, we get the correct code but still with a wrong date...
WHERE
--z.po_id = 'T1DLDC'
scus.ship_id = 'D30980'
GROUP BY z.po_id
I tried other logic changes like modifying the left joins to search on a subquery.
left JOIN ( select * from TABLE.C order by updt_job_tms desc) scus ON A.SHIP_ID = scus.SHIP_ID AND A.SHIP_TO_LOC_CODE = scus.SHIP_TO_LOC_CODE and scus.loc_type = 'CUS' AND DAYS(scus.shipment_tms)+10 >= DAYS(z.ship_tms)
But this is also giving the same exact results by searching either by po_id or ship_id_cus
Any ideas or comment will be much appreciated.
Thanks
------------------------------------UPDATE-----------------------------------
Adding the result of the LEFT JOIN with the row_partition() including all the ship_id_cus for that po_id, and all the codes with the tms. None match here.
Based on all these, it should be the last ship_id_cus with X1 event/tms. If we exclude also the ones starting with 9, we would get the following result.
(I am not applying here ordering by ship_id_cus, which already described before that did not work either the way I implemented)
If you have a table: TBL1
ID APPROVED APPROVER DATE_APPROVED
====== ======== ======== =============
ABC Y JOE 2019-01-13
ABC N ZACK 2018-12-23
ABC N SUE 2019-02-23
And you do SQL:
SELECT ID, MAX(APPROVED) AS APPROVAL
,MAX(APPROVER) AS APPROVED_BY , MAX(DATE_APPROVED) AS APPROVED_ON
FROM TBL1 GROUP BY ID
you will get result:
ID APPROVAL APPROVED_BY APPROVED_ON
====== ======== =========== =============
ABC Y ZACK 2019-02-23
which is correct to the code but is NOT what you want
Try the following:
SELECT T1.ID, T1.APPROVED, T1.APPROVER, T1.DATE_APPROVED
FROM TBL1 AS T1
INNER JOIN (SELECT ID, MAX(DATE_APPROVED) AS APPROVED_ON
FROM TBL1 GROUP BY ID
) AS T2
ON T1.ID =T2.ID
AND T1.DATE_APPROVED = T2.APPROVED_ON
Result:
ID APPROVED APPROVER DATE_APPROVED
====== ======== ======== =============
ABC N SUE 2019-02-23

Query Error and Loading the result Query to new Table

Can anybody correct me with the below query where the query is going wrong ?
For the below Query i need to add where clause and get based on client names. When i execute the query i am able to view only first client data. And also i want the result to store in separate database.
select amount,[Account #]as cdn#,[CC ACCT]as CC#,[CardType] as CCType
from [PayPal_staging].[dbo].[VendorFiles] as a
inner join [CUBS].[dbo].[Client] as d
on d.PK_Client=CONVERT(varchar(250),a.client)
Union all
Select [Amount],[CDN #] as cdn#,[Card_No] as CC#,[Card_Type] as CCType
from [PayPal_staging].[dbo].[VirtualFiles] as b
left join [CUBS].[dbo].[Client] as d
on d.PK_Client=CONVERT(varchar(250),b.[Client_Id])
union all
select [SV10_Amt] as Amount,[Cdr_Id] as cdn#,[SV07_Last4] as CC#,[CardType] as CCType
from [PayPal_staging].[dbo].[IVRFiles] as c
left join [CUBS].[dbo].[Client] as d
on d.PK_Client=CONVERT(varchar(250),c.[SV03_Clientnbr])
where PK_Client in ('SWMC600',' SQMC203',' SQMC600',' SBMC203',' SBMC600',
' PROV203',' PROV600',' SWRC203',' SWRC600',' SMCC203',' SMCC600',' SWIS203',' SWIS600')
Can anybody help me with the above query where the query is going wrong and how to get the result for all clients in Where clause and how to store the result in separate database ?
Please write me a complete query for the above task....
It's hard to know for sure, but you have a space preceding each CLIENT ID except for the first one:
where PK_Client in ('SWMC600',' SQMC203',' SQMC600',' SBMC203',' SBMC600',
' PROV203',' PROV600',' SWRC203',' SWRC600',' SMCC203',' SMCC600',
' SWIS203',' SWIS600')
Just remove the spaces:
where PK_Client in ('SWMC600','SQMC203','SQMC600','SBMC203','SBMC600',
'PROV203','PROV600','SWRC203','SWRC600','SMCC203','SMCC600',
'SWIS203','SWIS600')
The where clause only works for the last select. Maybe you want it this way.
SELECT *
FROM (SELECT [Amount]
, [Account #]as cdn#
, [CC ACCT]as CC#
, [CardType] as CCType
, PK_Client
FROM [PayPal_staging].[dbo].[VendorFiles] AS a
INNER JOIN [CUBS].[dbo].[Client] AS d
ON d.PK_Client=CONVERT(varchar(250),a.client)
UNION ALL
SELECT [Amount]
, [CDN #] AS cdn#
, [Card_No] AS CC#
, [Card_Type] AS CCType
, PK_Client
FROM [PayPal_staging].[dbo].[VirtualFiles] AS b
LEFT JOIN [CUBS].[dbo].[Client] as d
ON d.PK_Client=CONVERT(varchar(250),b.[Client_Id])
UNION ALL
SELECT [SV10_Amt] AS Amount
, [Cdr_Id] AS cdn#
, [SV07_Last4] AS CC#
, [CardType] AS CCType
, PK_Client
FROM [PayPal_staging].[dbo].[IVRFiles] AS c
LEFT JOIN [CUBS].[dbo].[Client] AS d
ON d.PK_Client=CONVERT(varchar(250),c.[SV03_Clientnbr])
) t
WHERE t.PK_Client IN ('SWMC600','SQMC203','SQMC600','SBMC203','SBMC600'
,'PROV203','PROV600','SWRC203','SWRC600','SMCC203','SMCC600'
,'SWIS203','SWIS600');

Find duplicates in SQL Server database where one of the columns must differ

I'm trying to write a SQL query to find duplicates. What I can't manage to do is to make my query only select duplicates where one of the columns value must differ. So, I want to find all the duplicates where all the columns are the same, but one of the values must differ.
What I've got at the moment:
SELECT
a.1, underlag.1, f.1, f.2, f.3, f.4, f.5, f.6, f.7, f.8,
COUNT(*) TotalCount
FROM
f
JOIN
a ON a.Id = f.Id
JOIN
underlag ON underlag.Id = f.Id
GROUP BY
a.1, underlag.1, f.1, f.2, f.3, f.4, f.5, f.6, f.7, f.8
HAVING
COUNT(*) > 1
ORDER BY
underlag.1
The column that I want to differ is f.9 but I've no clue on how to do this. Any help or pointers in the right direction would be great!
SELECT *
FROM (
SELECT
a1 = a.[1]
, underlag1 = underlag.[1]
, f.[1], f.[2], f.[3], f.[4], f.[5], f.[6], f.[7], f.[8], f.[9]
, val = SUM(1) OVER (PARTITION BY CHECKSUM(f.[1], f.[2], f.[3], f.[4], f.[5], f.[6], f.[7], f.[8]))
FROM f
JOIN a on a.Id = f.Id
JOIN underlag on underlag.Id = f.Id
) t
WHERE t.val > 1
ORDER BY underlag1