MS sql server
ideally it could merge three selections into one and return one data set.
the pseudo code is :
select this_line, this_trimSeq, this_paintSeq
from tableA
join tableB on tableA.id = tableB.id
where control_key = '11'
select max(paintSeq) as maxPaintSeq
from tableA
join tableB on tableA.id = tableB.id
join tableC on tableB.zone_name = tableC.zone_name
where zone_no between 54 and 121
and line =this_line (the result from the first selection)
and paintSeq<=this_paintSeq (the result from the first selection)
select max(trimSeq) as MaxTrimSeq
from tableA
join tableB on tableA.id = tableB.id
join tableC on tableB.zone_name = tableC.zone_name
where zone_no between 92 and 141
and line =this_line (the result from the first selection)
and trimSeq<=this_trimSeq (the result from the first selection)
any tidy way to do this? if not use CTE. thanks
Related
this is my code :
SELECT DISTINCT Emps.name, Degrees.Name AS degree, Degrees.Date AS degree_date
FROM Emps INNER JOIN
Degrees ON Emps.id = Degrees.empId
but distict dosn't work , i want this result
i want distict name with degree which has max id or max date
thanks for all i found the solution
SELECT e.name, d.Name AS degree
FROM Emps AS e
full JOIN (
SELECT t.*, ROW_NUMBER() OVER (PARTITION BY t.empId ORDER BY t.id DESC) AS rn FROM Degrees AS t
) AS d ON e.id = d.empId
WHERE d.rn = 1
I have a recent request to include Work ItemTag information in a daily SSRS report that I generate. I was led to the SQL query below. The problem is that I cannot find a table or a view with the "WorkItemsAre" in the name. So giving that I have the WorkItemSK, System_Id, ProjectSK and CollectionGUID among many other things, how and where can I get the tags querying directly against the TFS database (I know querying against the TFS databases is not recommended but It's in my requirements)?
SELECT DISTINCT WorkItemsAre.ID, WorkItemsAre.Title, tbl_TagDefinition.Name
--,tbl_PropertyValue.ArtifactId, *
FROM tbl_TagDefinition
LEFT JOIN tbl_PropertyDefinition ON tbl_PropertyDefinition.Name =
'Microsoft.TeamFoundation.Tagging.TagDefinition.' + CONVERT(NVARCHAR(400),
tbl_TagDefinition.TagId)
LEFT JOIN tbl_PropertyValue ON tbl_PropertyValue.PropertyId =
tbl_PropertyDefinition.PropertyId
--LEFT JOIN WorkItemLongTexts ON WorkItemLongTexts.ID =
tbl_PropertyValue.ArtifactId
left join WorkItemsAre on WorkItemsAre.ID = tbl_PropertyValue.ArtifactId
WHERE
(
SELECT SUM(CASE WHEN IntValue = 0 THEN 1 ELSE -1 END) NB
FROM tbl_PropertyValue PROP_CNT
WHERE PROP_CNT.PropertyId = tbl_PropertyDefinition.PropertyId
AND WorkItemsAre.ID = PROP_CNT.ArtifactId
) > 0
TFS warehouse does not contain tags. For TFS 2018 (maybe also for tfs 2017) you can find information in table tbl_WorkItemCoreLatest in operational database (in my case: Tfs_DefaultCollection)
SELECT DISTINCT tbl_WorkItemCoreLatest.Id,tbl_TagDefinition.Name
--,tbl_PropertyValue.ArtifactId, *
FROM tbl_TagDefinition
LEFT JOIN tbl_PropertyDefinition ON tbl_PropertyDefinition.Name = 'Microsoft.TeamFoundation.Tagging.TagDefinition.' + CONVERT(NVARCHAR(400), tbl_TagDefinition.TagId)
LEFT JOIN tbl_PropertyValue ON tbl_PropertyValue.PropertyId = tbl_PropertyDefinition.PropertyId
--LEFT JOIN WorkItemLongTexts ON WorkItemLongTexts.ID = tbl_PropertyValue.ArtifactId
left join tbl_WorkItemCoreLatest on tbl_WorkItemCoreLatest.Id = tbl_PropertyValue.ArtifactId
WHERE
(
SELECT SUM(CASE WHEN IntValue = 0 THEN 1 ELSE -1 END) NB
FROM tbl_PropertyValue PROP_CNT
WHERE PROP_CNT.PropertyId = tbl_PropertyDefinition.PropertyId
AND tbl_WorkItemCoreLatest.Id = PROP_CNT.ArtifactId
) > 0
I have to join two tables using left outer join method in Hive. Please find my table details and required output below,
Apologize for big explanation. I gave all the information for more clarity in my doubts.
Table1 columns:
date1, srcid, claimno1
Date1 srcid claimno1
31-01-2015 ABC 432
31-01-2015 ABC 451
31-01-2015 ABC 435
01-02-2015 ABC 451
Table2 columns:
date2,claimno2,amount
Date2 claimno1 amount
31-01-2015 432 150.51
01-02-2015 451 250.61
31-02-2015 451 320.72
01-02-2015 432 381.65
Required Output:
Date1 srcid claimno1 amount
31-01-2015 ABC 432 381.65
31-01-2015 ABC 451 250.61
31-01-2015 ABC 432 381.65
01-02-2015 ABC 451 NULL
Condition:
1) from table1 need to select the record with date as 31-01-2015
2) from table2 need to select the record with date as 01-02-2015
Assumption:
1) claimno1 from table1 is not unique and occur many time for same date1
2) claimno2 from table2 is unique for particular date2
Doubts:
1) Do I need to give all the selected columns in GROUP BY ? (else getting SemanticException error)
2) What is the difference when i used the same condition in WHERE clause and ON clause?
I have used below queries but couldn't able to get required output,
SELECT
a.date1, a.srcid, a.claimno1, b.amount
FROM
table1 a
LEFT OUTER JOIN
tableb ON (a.claimno1 = b.claimno2)
WHERE
a.date1 = '31-01-2015'
AND b.date2 = '01-02-2015'
GROUP BY a.date1 , a.srcid;
FAILED: SemanticException [Error 10002]: Invalid column reference 'claimno1'
SELECT
a.date1, a.srcid, a.claimno1, b.amount
FROM
table1 a
LEFT OUTER JOIN
tableb ON (a.claimno1 = b.claimno2)
WHERE
a.date1 = '31-01-2015'
AND b.date2 = '01-02-2015'
GROUP BY a.date1 , a.srcid , a.claimno1;
FAILED: SemanticException [Error 10002]: Invalid column reference 'amount'
SELECT
a.date1, a.srcid, a.claimno1, b.amount
FROM
table1 a
LEFT OUTER JOIN
tableb ON (a.claimno1 = b.claimno2)
WHERE
a.date1 = '31-01-2015'
AND b.date2 = '01-02-2015'
GROUP BY a.date1 , a.srcid , a.claimno1 , b.amount;
Result: No records selected
SELECT
a.date1, a.srcid, a.claimno1, b.amount
FROM
table1 a
LEFT OUTER JOIN
tableb ON (a.claimno1 = b.claimno2
AND a.date1 = '31-01-2015'
AND b.date2 = '01-02-2015')
GROUP BY a.date1 , a.srcid , a.claimno1 , b.amount;
Result: records selected but amount values are NULL
Apologize for big explanation. I gave all the information for more clarity in my doubts.
Thanks in advance.
I am trying to convert the following join statement into PIG.
from EXCH e
JOIN (select a,p, count(buyer) from EXCH group by a, p) sq on e.a = sq.a and e.p = sq.p
left outer join myimplocal i on e.a = i.a and e.p = i.p and e.f = i.f and i.imp > 0 and e.iswin = 1
Currently, I have this:
EXCH2 = FOREACH EXCH GENERATE a, p, buyer;
EXCH2_groupby = GROUP EXCH2 BY (a, p);
EXCH_alias_sq = FOREACH EXCH2_groupby GENERATE EXCH2.a, EXCH2.p, COUNT(EXCH2.buyer);
join_EXCH_and_EXCH_alias_sq = JOIN EXCH by (a, p), EXCH_alias_sq by (a, p);
myimplocal_filterby_imp_notZero = FILTER myimplocal BY imp > 0;
Should I filter e.iswin=1 before doing the left outer join? Should I apply the filter to join_EXCH_and_EXCH_alias_sq, then do an outer join on myimplocal_filterby_imp_notZero
and my filter alias?
Any help would be greatly appreciated.
In my question - as e.iswin is performed during the outerjoin on the result of the inner join tables, I had to use Union to give me the correct result.
This is what I did:
Data Set 1:
1) inner join EXCH_and_EXCH_alias_sq
2) filter the result with e.iswin =1,
3)left outer join the result in step 2 with myimplocal
Data set 2:
1) inner join EXCH_and_EXCH_alias_sq (same as (1)above)
2) filter the result with e.iswin =0
UNION ONSCHEMA (alias for step 3 from Data set 1) (alias for step 2 from Data set 2)
This gave me the correct result.
I'm struggling to figure out the issue with my SQL table using compute sum.
All that is displayed where the sum of the column should be is a blank box!
Code Below:
TTITLE CENTER ==================== SKIP 1-
CENTER 'U T O O L' skip 1-
CENTER ==================== SKIP 1 -
LEFT 'Tool Report 1.03' SKIP 1 -
LEFT ============ SKIP 2-
RIGHT 'Page:' -
FORMAT 999 SQL.PNO SKIP 2
set pagesize 50
column MEMBERNAME HEADING 'Member Name' format a20
compute sum of TOTAL on Rental_ID
Break on RENTAL_ID
select Member.Member_ID, SUBSTR(Member.FName,0,10) || SUBSTR(' ',0,10) ||
SUBSTR(Member.SName,0,15) as MEMBERNAME,
Rental.Rental_ID,
Tool.Name,
Rental_Line.Qty,
Rental_Line.Price,
TO_Char(Rental_Line.Qty*Rental_Line.Price,'L9,999.99') TOTAL
from Rental_Line
INNER JOIN Rental
on Rental.Rental_ID = Rental_Line.Rental_ID
INNER JOIN Member
on Rental.Member_ID = Member.Member_ID
INNER JOIN Tool_Instance
on Rental_Line.Tool_Instance_ID = Tool_Instance.Tool_Instance_ID
INNER JOIN Tool
on Tool_Instance.Tool_ID = Tool.Tool_ID
where Rental.Rental_ID = '&Rental_ID';
may be this help you, as I understood you need SUM(Rental_Line.Qty) OVER (PARTITION BY Rental.Rental_ID)
select Member.Member_ID,
SUBSTR(Member.FName, 0, 10) || SUBSTR(' ', 0, 10) ||
SUBSTR(Member.SName, 0, 15) as MEMBERNAME,
Rental.Rental_ID,
Tool.Name,
Rental_Line.Qty,
Rental_Line.Price,
TO_Char(Rental_Line.Qty * Rental_Line.Price, 'L9,999.99') TOTAL,
SUM(Rental_Line.Qty) OVER (PARTITION BY Rental.Rental_ID) TOTAL_QTY,
SUM(Rental_Line.Qty * Rental_Line.Price) OVER (PARTITION BY Rental.Rental_ID) TOTAL_SUM
from Rental_Line
INNER JOIN Rental on Rental.Rental_ID = Rental_Line.Rental_ID
INNER JOIN Member on Rental.Member_ID = Member.Member_ID
INNER JOIN Tool_Instance on Rental_Line.Tool_Instance_ID =
Tool_Instance.Tool_Instance_ID
INNER JOIN Tool on Tool_Instance.Tool_ID = Tool.Tool_ID
where Rental.Rental_ID = '&Rental_ID';