Conditional display of calculated fields - sql-server

I'm working on a table named FCT_HISTO_PORTES that contains ID_MVT_EXPL and ID_PTE fields which are integers. In this table a ID_MVT_EXPL is associated with one, two or three ID_PTE.
Here's an example:
select top 1000
ID_MVT_EXPL,
ID_PTE
from FCT_HISTO_PORTES
ID_MVT_EXPL ID_PTE
3945546 6
3945547 25
3945548 56
3945548 57
3945549 25
3945550 52
3945551 57
3945551 58
3945553 56
3945557 51
3945558 57
3945558 58
You can see that "3945546" has one ID_PTE and "3945548" has two different ID_PTE.
The aim of the game is that I can display a single line for each ID_MVT_EXPL with named fields ID_PTE_1, ID_PTE_2, ID_PTE_3.
I tried with this SELECT but the result is incorrect because all the ID_PTE fit into ID_PTE_1 and leave the other two to NULL.
select top 1000
ID_MVT_EXPL,
MIN(id_pte) as ID_PTE_1,
case
when COUNT(id_pte) = 2
then MAX(id_pte)
when COUNT(id_pte) = 3
then SUM(id_pte)-(MIN(ID_PTE)+MAX(ID_PTE))
end as ID_PTE_2,
case
when COUNT(id_pte) = 3
then MAX(id_pte)
end as ID_PTE_3
from FCT_HISTO_PORTES
group by ID_MVT_EXPL, ID_PTE
ID_MVT_EXPL ID_PTE_1 ID_PTE_2 ID_PTE_3
3945546 6 NULL NULL
3945547 25 NULL NULL
3945548 56 NULL NULL
3945548 57 NULL NULL
3945549 25 NULL NULL
3945550 52 NULL NULL
3945551 57 NULL NULL
3945551 58 NULL NULL
3945553 56 NULL NULL
3945557 51 NULL NULL
3945558 57 NULL NULL
3945558 58 NULL NULL
So what needs to change in my SELECT?

My suggestion is a little bit different. But if I understand you correct. You could do this:
Test data:
DECLARE #tbl TABLE(ID_MVT_EXPL INT, ID_PTE INT)
INSERT INTO #tbl
VALUES
(3945546,6),(3945547,25),(3945548,56),(3945548,57),
(3945549,25),(3945550,52),(3945551,57),(3945551,58),
(3945553,56),(3945557,51),(3945558,57),(3945558,58)
Query:
SELECT
pvt.ID_MVT_EXPL,
pvt.[1] AS ID_PTE_1,
pvt.[2] AS ID_PTE_2,
pvt.[3] AS ID_PTE_3
FROM
(
SELECT
ROW_NUMBER() OVER(PARTITION BY tbl.ID_MVT_EXPL ORDER BY ID_PTE) AS RowNbr,
tbl.ID_MVT_EXPL,
tbl.ID_PTE
FROM
#tbl AS tbl
) AS SourceTable
PIVOT
(
MAX(ID_PTE)
FOR RowNbr IN([1],[2],[3])
) AS pvt
Output:
3945546 6 NULL NULL
3945547 25 NULL NULL
3945548 56 57 NULL
3945549 25 NULL NULL
3945550 52 NULL NULL
3945551 57 58 NULL
3945553 56 NULL NULL
3945557 51 NULL NULL
3945558 57 58 NULL
Reference:
ROW_NUMBER (Transact-SQL)
Using PIVOT and UNPIVOT

Related

How to get the value from another row in my query

How to get value from another row in SQL?
ID DeletedPersonID ProductKey PartyID RespID
9 461 17 33 95
10 95 17 34 95
and I have this select statement
select
drp.ID,
drp.DeletedPersonID,
drp.ProductID,
case when drp.DeletedPersonID != drp.RespID then (Get Party Key by RespID) else drp.PartyID end as 'PartyID',
case when drp.DeletedPersonID != drp.RespID then (Get Party Key by RespID) else drp.PartyName end as 'PartyName',
case when drp.DeletedPersonID = drp.RespID then null else drp.PartyID end as 'SubPartyID',
from dbo.RespHistory drp
In above example since ID = 10 has the same DeletedPersonID and RespID value I like to use the same PartyID but in ID = 9 or in the first line Since DeletedPersonID and RespID are difference I want to use PartyID of ID = 10
I am looking a view similar to shown below
ID DeletedPersonID ProductKey PartyID PartyName SubPartyID
9 461 17 34 ABC 33
10 95 17 34 XYZ null
Something like this
with equals_cte (RespID) as (
select RespID
from dbo.RespHistory
where DeletedPersonID = RespID
)
select
drp.ID, drp.DeletedPersonID, drp.ProductKey, coalesce(ec.RespID, drp.RespID) PartyID
, drp.PartyName, drp.RespID
, iif(drp.DeletedPersonID = drp.RespID, null, drp.RespID) SubPartyID
from dbo.RespHistory drp
left join equals_cte ec on drp.RespID = ec.RespID;

Trying to Replace While loop that executes based on a value in a row from a view to a Set query

I am trying to create a query that will give me random rows from a view (view_XYZ) based on the value in the SampleSizeByRow (in view_SampleSizeByRow) then it inserts the random value from view_XYZ into tbl_Randomization.
view_SampleSizeByRow looks like
ID_x SampleSizeByRow
49 1
87 1
47 6
41 2
247 13
96 3
99 1
31 1
91 13
DECLARE #CurrSize AS INT
DECLARE #CurrID AS INT
DECLARE #SampleByValTbl AS TABLE(
Samplesize INT,
ID INT
)
INSERT INTO #SampleByValTbl (
Samplesize
, ID
)
SELECT SampleSizeByRow, ID_x
FROM view_SampleSizeByRow
ORDER BY ID_x;
WHILE(1=1) -- Infinite Loop until it is broken
BEGIN
SET #CurrID = NULL -- Reset pointer variables
SET #CurrSize = NULL
SELECT TOP(1) #CurrID = ID
FROM #SampleByValTbl
SELECT TOP(1) #CurrSize = Samplesize
FROM #SampleByValTbl
IF (#CurrID IS NULL OR #CurrSize IS NULL) -- End loop if there are no ID's to go through
BREAK
IF #CurrSize <= 0
BEGIN
DELETE TOP(1) FROM #SampleByValTbl
CONTINUE
END
ELSE
BEGIN
WHILE #CurrSize > 0
BEGIN
INSERT INTO tbl_Randomization (
ID_x
, Num_y
)
SELECT TOP(1) ID_x
, Num_y
FROM view_XYZ
ORDER BY NEWID() -- Random Selection
SET #CurrSize = #CurrSize - 1
END
DELETE TOP(1) FROM #SampleByValTbl -- To shorten the size of the table each time we parse through it
END
END
Results should be something like the table below
ID_x Num_y
41 4888084
41 4895898
46 4889509
46 4889493
47 4891612
47 4898679
47 4889485
47 4902432
49 4893435
91 4898447
91 4898892
91 4895738
91 4888350
91 4898920
91 4886422
91 4899137
91 4895741
91 4886918
91 4894146
91 4888301
91 4888383
91 4882095
91 4898402
96 4893927
96 4893738
96 4887504
99 4893713
247 4897332
247 4902343
247 4897769
247 4895529
247 4885333
247 4895512
247 4895488
247 4885279
247 4893031
247 4891872
247 4896523
247 4901417
247 4885181
247 4897541
...
I have tried to use CTE and various joins but I can't figure out how to code them so they return the same results.
If I followed you correctly, you want to select sample_size random values from view_XYZ for each id_x listed in view_SampleSizeByRow.
Here is an approach that uses a recursive query to generate the rows, then a correlated subquery to retrieve the random value:
with cte as (
select id_x, sample_size - 1 from view_SampleSizeByRow where sample_size > 0
union all
select id_x, sample_size - 1 from cte where sample_size > 0
)
insert into tbl_Randomization (id_x, num_y)
select
id_x,
(select top (1) num_y from view_XYZ v where v.id_x = c.id_x order by newid())
from cte c

Copy data from one table #1 into table #2 then remove the remaining rows of table # 2 for particular Sales Invoice ID

Sorry to say i have not in depth knowledge of SQL queries. I have to modify an existing application that records and maintain the customers installments payments.
For this purpose I have a requirement in which i want to copy date of
TableNo1 into TableNo2 except New_Amount(TableNo1) column data, and then remove the remaining rows of TableNo2 for each SalesInvoiceID.
Actually TablNo1 has modified installment payment plan and that is why it is required to modify TableNo2 accordingly.
TableNo1
New_ID SalesInvoiceID InsttNo DueDate New_Amount
1 30 1 2019-05-02 12000
2 30 2 2019-06-02 12000
3 30 3 2019-09-02 4000
4 30 4 2019-12-02 4000
TableNo2
Instt_ID SalesInvoiceID InsttNo DueDate PaymentDate Amount Status
51 30 1 2019-05-02 NULL 0 Up-Coming
52 30 2 2019-06-02 NULL 0 Up-Coming
53 30 3 2019-07-02 NULL 0 Up-Coming
54 30 4 2019-08-02 NULL 0 Up-Coming
55 30 5 2019-09-02 NULL 0 Up-Coming
56 30 6 2019-10-02 NULL 0 Up-Coming
57 30 7 2019-11-02 NULL 0 Up-Coming
58 30 8 2019-12-02 NULL 0 Up-Coming
Required Output (TableNo2)
Instt_ID SalesInvoiceID InsttNo DueDate PaymentDate Amount Status
51 30 1 2019-05-02 NULL 0 Up-Coming
52 30 2 2019-06-02 NULL 0 Up-Coming
53 30 3 2019-09-02 NULL 0 Up-Coming
54 30 4 2019-12-02 NULL 0 Up-Coming
If I'm guessing at the relationship between the tables correctly, this should be all you need, but before you delete any production data you should test this on copies of the tables.
UPDATE t2
SET t2.DueDate = t1.DueDate
FROM
dbo.TableNo2 AS t2
JOIN
dbo.TableNo1 AS t1
ON t1.SalesInvoiceID = t2.SalesInvoiceID
AND t1.InsttNo = t2.InsttNo;
DELETE t2
FROM
dbo.TableNo2 AS t2
WHERE
NOT EXISTS
(
SELECT 1
FROM dbo.TableNo1 AS t1
WHERE t1.SalesInvoiceID = t2.SalesInvoiceID
AND t1.InsttNo = t2.InsttNo
);
CREATE TABLE #Table1 (New_ID INT IDENTITY (1,1) PRIMARY KEY,SalesInvoiceID
INT,InsttNo INT,PaymentDate DATE,New_Amount INT )
CREATE TABLE #Table2 (New_ID INT IDENTITY (1,1) PRIMARY KEY,Instt_ID INT,
SalesInvoiceID INT,InsttNo INT,DueDate DATE,PaymentDate DATE,Amount INT,Status
VARCHAR(20))
INSERT INTO #Table1 (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30
,1,'2019-05-02','12000')
INSERT INTO #Table1 (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30
,2,'2019-06-02','12000')
INSERT INTO #Table1 (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30
,3,'2019-09-02','4000')
INSERT INTO #Table1 (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30
,4,'2019-12-02','4000')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(51,30,1,'2019-05-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(52,30,2,'2019-06-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(53,30,3,'2019-07-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(54,30,4,'2019-08-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(55,30,5,'2019-09-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(56,30,6,'2019-10-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(57,30,7,'2019-11-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(58,30,8,'2019-12-02',NULL,0,'Up-Coming')
SELECT
T2.Instt_ID
,T1.SalesInvoiceID
,T1.InsttNo
,T1.PaymentDate DueDate
,T2.PaymentDate
,T2.Amount
,T2.Status
FROM #Table1 t1
INNER JOIN #Table2 T2 ON t1.New_ID = t2.New_ID
DROP TABLE #Table1,#Table2

Querying emails that contains an individual

I want to pull a conversation for a certain order that a person has ever read a message on. For instance:
Order SentTOID ReadBy
A 111 55
A 55 55
A 111 89
B 111 89
C 111 55
C 55 55
D 111 99
D 99 99
Results
A 111 55
A 55 55
A 111 89
C 111 55
C 55 55
My code will only pull all the cases that were read by 55 but I want the whole converstaion.
Order SentTOID ReadBy
A 111 55
A 55 55
C 111 55
C 55 55
Code used.
Select *
from conversation
where readby = 55.
first get Order and apply IN clause
declare #temp table
(Orders nvarchar(7), SentTOID int, ReadBy int)
insert into #temp values ('A',111,55)
insert into #temp values ('A',55 ,55)
insert into #temp values ('A',111,89)
insert into #temp values ('B',111,89)
insert into #temp values ('C',111,55)
insert into #temp values ('C',55 ,55)
insert into #temp values ('D',111,99)
insert into #temp values ('D',99 ,99)
select * from #temp
where Orders in (select Orders from #temp where ReadBy = 55)
RESULT

How to PIVOT this data in SQL Server?

I have this query below
select * from (
SELECT tjdm.ID as id, cjb.CORPORATE_ID,
tjdm.JOB_TICKET_VALUE, cjb.DISPLAY_NAME
from Cjd cjb
join Tjdm tjdm
on cjb.CUSTOM_JOB_DATA_ID = tjdm.CUSTOM_JOB_DATA_ID
where cjb.CUSTOM_DATA_TYPE in (1,2) and cjb.DISPLAY_IS_ACTIVE = 1
) AS PivotData
PIVOT (
max(JOB_TICKET_VALUE)
FOR DISPLAY_NAME IN
([OMXAccount],[Consignee],[Col1],[Col2],[Col3],[Col4])
) AS PivotTable
The select * from (.....) returns a data columns as shown below.
id CORPORATE_ID JOB_TICKET_VALUE DISPLAY_NAME
-- ------------ ---------------- ------------
53 1 9 OMXAccount
53 1 199 Consignee
54 6 "No value" OMXAccount
54 6 "No value" Consignee
58 1 "No value" OMXAccount
58 1 "No value" Consignee
But when the "PIVOT" statement being executed, I am getting this result.
id CORPORATE_ID OMXAccount Consignee [Col1] [Col2] [Col3] [Col4]
-- ------------ ---------- --------- ------ ------ ------ ------
53 1 9 199 Null Null Null Null
54 6 Null Null Null Null
58 1 Null Null Null Null
The expected output should be just like below.
id CORPORATE_ID OMXAccount Consignee [Col1] [Col2] [Col3] [Col4]
-- ------------ ---------- --------- ------ ------ ------ ------
53 1 OMXAccount Consignee Null Null Null Null
54 6 OMXAccount Consignee Null Null Null Null
58 1 OMXAccount Consignee Null Null Null Null
What is wrong?
Thanks
RJuy
select * from (
SELECT tjdm.ID as id, cjb.CORPORATE_ID,
cjb.DISPLAY_NAME
from Cjd cjb
join Tjdm tjdm
on cjb.CUSTOM_JOB_DATA_ID = tjdm.CUSTOM_JOB_DATA_ID
where cjb.CUSTOM_DATA_TYPE in (1,2) and cjb.DISPLAY_IS_ACTIVE = 1
) AS PivotData
PIVOT (
max(DISPLAY_NAME)
FOR DISPLAY_NAME IN
([OMXAccount],[Consignee],[Col1],[Col2],[Col3],[Col4])
) AS PivotTable

Resources