Querying emails that contains an individual - sql-server

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

Related

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

How to get student maximum interm marks

i want to get maximum student interm marks from below table.
Student name Interm1 marks Interm2 marks Interm3 marks
Raj 60 75 89
raju 78 74 67
ram 67 79 65
balaji 91 89 93
My required Output is:
Balaji 93
option:
raju 78
raj 89 etc..
like this i need output.
can any body help me here for this query.
You'll need to first unpivot your data, and then get the MAX value. I prefer using VALUES to unpivot data, rather than the UNPIVOT operator:
SELECT YT.StudentName,
MAX(IM.ItermMark) AS MaxItermMark
FROM YourTable YT
CROSS APPLY(VALUES(Interm1Mark),(Interm2Mark),(Interm3Nark))IM(ItermMark)
GROUP BY StudentName;
Have a look at this. This should do what you want to do
SELECT [Student name], MAX(MaxMark)
FROM
(
SELECT [Student name],
(SELECT MAX(v) FROM (VALUES ([Interm1 marks]), ([Interm2 marks]), ([Interm3 marks])) AS value(v)
) AS [MaxMark]
) AS subquery
GROUP BY [Student name]

Get a row value based on criterias - T-SQL

Sample Data in Yellow the desired output
Just need your expert help on this.
I need to get the output based on T-SQL. Whenever the invoice_line_id_link is 0 put the product_id value if not look into the id column and get the product_id of that row.
Here's the script.
declare #t table
(id int, invoice_id int, product_id int, invoice_line_id_link int);
insert into #t values
(53,10,383,0),
(54,10,344,53),
(55,10,920,53),
(57,10,384,0),
(58,10,359,57),
(59,10,242,57),
(60,10,284,0);
select id, invoice_id, product_id,invoice_line_id_link, null desiredoutput from #t
based on the image if possible to populate using TSQL
Based on the image you have provided , here is the piece of code producing desired output.
Table Populate
declare #t table (id int, invoice_id int, product_id int, invoice_line_id_link int);
insert into #t values (53,10,383,0), (54,10,344,53), (55,10,920,53), (57,10,384,0), (58,10,359,57), (59,10,242,57), (60,10,284,0);
Code for Output
select *,CASE WHEN invoice_line_id_link = 0 THEN product_id
ELSE (select t1.product_id from #t t1 where t1.id = t2.invoice_line_id_link)
END from #t t2
Output
id invoice_id product_id invoice_line_id_link desiredoutput
53 10 383 0 383
54 10 344 53 383
55 10 920 53 383
57 10 384 0 384
58 10 359 57 384
59 10 242 57 384
60 10 284 0 284
*/

SQL Server Calculated Column using greater than or lesser than

I have this table in sql server i wanted to create a calculated field base on the result of the column that i have, i want the result to display this.
column names are
Key,Homework,Quiz,Exam,Result,Grade
Result=>90 "A+,"Result=>80 "A",Result=>70 "B+",Result=>60 "B"
CREATE TABLE dbo.T
(
Val INT,
Chr AS (
CASE
WHEN Val <= 59 THEN ''
WHEN Val BETWEEN 60 AND 69 THEN 'B'
WHEN Val BETWEEN 70 AND 79 THEN 'B+'
WHEN Val BETWEEN 80 AND 89 THEN 'A'
WHEN Val >= 90 THEN 'A+'
END
)
)
INSERT INTO dbo.T(Val) VALUES (100),(80),(75)
SELECT * FROM dbo.T
DROP TABLE IF EXISTS dbo.T
You need to use concept of derived table like below. I am using result column to derive a new column grade using case statements.
create table #tbl(col1 varchar(2), col2 varchar(2), col3 varchar(2), result int)
insert into #tbl(col1,col2,col3,result) values
('A','A','A',10),
('A','A','B',62),
('A','A','C',83),
('A','A','D',94)
SELECT a.col1, a.col2, a.col3, a.result,
case when a.result < 60 then ''
when a.result between 60 AND 69 then 'B'
when a.result between 70 AND 79 then 'B+'
when a.result between 80 AND 89 then 'A'
when a.result > 89 then 'A+'
else NULL
end as grade
FROM
(
Select col1, col2, col3, result
from #tbl
) a
drop table #tbl;
As a computed column...
ALTER TABLE MyTable ADD Grade AS
CASE
WHEN Result < 60 THEN ''
WHEN Result < 70 THEN 'B'
WHEN Result < 80 THEN 'B+'
WHEN Result < 90 THEN 'A'
ELSE 'A+'
END
If you have fractions (which I've seen at the British Open University) you need to rely on CASE evaluation order and <
Using BETWEEN (like other answers) will means a score of 69.5 will not be graded correctly
SQL Server Docs reference
A CASE example

Conditional display of calculated fields

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

Resources