I am looking for a way to perform this query in DQL :
SELECT
t0_.product, t0_.creation_date, t0_.id pvId,
t0_.product AS product,
t0_.file_path AS file_path_3,
t0_.product AS product_11
FROM table0 t0_
join (
select max(creation_date) as createdDate, id, product
from table0
group by product
) t0b on (t0b.createdDate = t0_.creation_date and t0_.product = t0b.product)
LEFT JOIN table1 t1_ ON t0_.product = t1_.id
LEFT JOIN table2 t2_ ON t1_.id = t2_.otherId
LEFT JOIN table3 t3_ ON t2_.id = t3_.otherId
LEFT JOIN table4 t4_ ON t2_.id = t4_.otherId
LEFT JOIN table5 t5_ ON t4_.id = t5_.otherId
LEFT JOIN table6 t6_ ON t4_.id = t6_.otherId
LEFT JOIN table7 t7_ ON t6_.id = t7_.otherId
WHERE t6_.id = :identifier
GROUP BY p0_.product;
explication : I have in table 'table0' several rows linked to table 'table1', I want to keep only the most recent one (column creation_date)
tableA.ID
tableA.propertyTableB.ID
tableA.createdAt
1
1
-4days
2
3
-3days
3
1
yesterday
4
3
today
5
1
today
6
1
-5 days
7
2
yesterday
8
2
today
I need to keep the rows : 4, 5, 8,
I need to get a queryBuilder object in order to do other operations behind it.
Has anyone already succeeded in this feat?
Thanks in advance <3
I need to keep the most recents rows from tableA related to tableB
EDIT : Maybe I find another way
no Select in the Join, just another JOIN like this :
FROM table0 t0
LEFT JOIN table0Bis AS t0B ON t0B.identifier = t0.identifier AND t0B.creation_date > t0.creation_date
LEFT JOIN table1 t1 ON t1.id = t0.foo
LEFT JOIN table2 t2 ON t1.id = t2.bar
LEFT JOIN table3 t3 ON t2.id = t3.rab
LEFT JOIN table4 t4 ON t2.id = t4.oof
LEFT JOIN table5 t5 ON t5.id = t4.toto
LEFT JOIN table6 t6 ON t5.id = t6.lorem
LEFT JOIN table7 t7 ON t6.id = t7.ipsum
where t7.id = :id AND t0B.creation_date IS NULL
group by t0.product;
$queryBuilder = $this->createQueryBuilder('t0');
$queryBuilder->leftJoin(
'App\Entity\Table0',
't0b',
Join::WITH,
't0b.product = t0.product AND t0b.creationDate > t0.creationDate')
->leftJoin('t0.foo', 't1')
->leftJoin('t1.bar', 't2')
->leftJoin('t2.bar', 't3')
->leftJoin('t3.rab', 't4')
->andWhere('t4.id = :ident')->setParameter('ident', $user->getId())
->andWhere('t0b.creationDate IS NULL')
->groupBy('t0.id', 't0.foobar')
->orderBy('t0.foobar', 'DESC')
;
Related
I am looking to do some querying on two partitions (different partition date) on BQ.
Table format is the following
crm_id, attribute#1, attribute#2,attribute#3
For the IDs that are common from week 1 and week 2, I want to see how many changed attribute#1, attribute#2 and attribute# 3
I started with the inner join using:
WITH t1 AS
(SELECT crm_id AS w1_crm
FROM `table`
WHERE DATE(_PARTITIONTIME) = "date1"
)
SELECT crm_id
FROM `table`
WHERE DATE(_PARTITIONTIME) = "date2"
INNER JOIN
t1
ON
w1_crm = crm_id
I am getting an error on the INNER JOIN
Try below
WITH t1 AS (
SELECT crm_id AS w1_crm
FROM `table`
WHERE DATE(_PARTITIONTIME) = "date1"
), t2 AS (
SELECT crm_id
FROM `table`
WHERE DATE(_PARTITIONTIME) = "date2"
)
SELECT crm_id
FROM t2
INNER JOIN t1
ON w1_crm = crm_id
I have two instances of the same database from different days. All tables from one day are called tableA* and from the other tableB*. I would like to compare data to see what have changed. I would like to select all rows that don't match exactly. So for example if one value is different in tables tableA1 and tableB1 I would like to select a corresponding row from table A and mark it as 'new' and from table B and mark it as 'deleted'. I tried with a query like this:
SELECT 'new', ta1.name, ta2.name, ta3.name, ta4.name, ta5.name
FROM tableA1 ta1
LEFT JOIN tableA2 ta2 ON ta1.ta2_id = ta2.id
LEFT JOIN tableA3 ta3 ON ta1.ta3_id = ta3.id
LEFT JOIN tableA4 ta4 ON ta1.ta4_id = ta4.id
LEFT JOIN tableA5 ta5 ON ta5.ta1_id = ta1.id WHERE NOT EXISTS
(SELECT tb1.name, tb2.name, tb3.name, tb4.name, tb5.name
FROM tableB1 tb1
LEFT JOIN tableB2 tb2 ON tb1.tb2_id = tb2.id
LEFT JOIN tableB3 tb3 ON tb1.tb3_id = tb3.id
LEFT JOIN tableB4 tb4 ON tb1.tb4_id = tb4.id
LEFT JOIN tableB5 tb5 ON tb5.tb1_id = tb1.id WHERE
tb1.name = ta1.name AND
tb2.name = ta2.name AND
tb3.name = ta3.name AND
tb4.name = ta4.name AND
tb5.name = ta5.name)
UNION
SELECT 'deleted', tb1.name, tb2.name, tb3.name, tb4.name, tb5.name
FROM tableB1 tb1
LEFT JOIN tableB2 tb2 ON tb1.tb2_id = tb2.id
LEFT JOIN tableB3 tb3 ON tb1.tb3_id = tb3.id
LEFT JOIN tableB4 tb4 ON tb1.tb4_id = tb4.id
LEFT JOIN tableB5 tb5 ON tb5.tb1_id = tb1.id WHERE NOT EXISTS
(SELECT ta1.name, ta2.name, ta3.name, ta4.name, ta5.name
FROM tableA1 ta1
LEFT JOIN tableA2 ta2 ON ta1.ta2_id = ta2.id
LEFT JOIN tableA3 ta3 ON ta1.ta3_id = ta3.id
LEFT JOIN tableA4 ta4 ON ta1.ta4_id = ta4.id
LEFT JOIN tableA5 ta5 ON ta5.ta1_id = ta1.id WHERE
tb1.name = ta1.name AND
tb2.name = ta2.name AND
tb3.name = ta3.name AND
tb4.name = ta4.name AND
tb5.name = ta5.name)
Hoping that if I created the same stuructre and compare all the values I would get the anticipated result. Even if databases are the same I get a lot row selected.
Found the problem. When comparing two NULL values the result is FALSE, the query itself should be fine. So I should have added conditions to check whether values are NULL.
I am writing some custom SSRS reports for our QA teams and I need to add test case "Tags" to my query. It is my understanding these are not in the TFS_Warehouse. I have found dbo.tbl_TagDefinition in the operational store but cannot figure out how to join it to a work item.
You can add a tag to work item query directly if your TFS contains the updates added with TFS 2013 Update 2 or later.
If you want to query from database, check this case:
TFS 2013 and up:
SELECT DISTINCT workItem.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 workItem 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 workItem.ID = PROP_CNT.ArtifactId
) > 0
Prior to TFS 2013:
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
The TFS 2015 db is convoluted, but here's how you query for a tag to find all it's matching work Items
select * from tbl_WorkItemCoreLatest
where
id in
(
select CONVERT(INT, artifactid) from tbl_PropertyValue
where
PropertyId in
(
select PropertyId from tbl_PropertyDefinition
where
name in
(
select ltrim(rtrim('Microsoft.TeamFoundation.Tagging.TagDefinition.' + cast(tagid as varchar(1000))))
from tbl_TagDefinition
where name like '%<TAG NAME TO SEARCH FOR>%' -- Tag name you're looking for
)
)
)
Hope this helps :-)
TFS #TFS2015
-R
This is based off of #Robby's answer and still seems to work as of Azure DevOps 2019:
select distinct
wcl.id,
td.Name from tbl_WorkItemCoreLatest wcl
left outer join tbl_PropertyValue pv on wcl.id = pv.artifactid
left outer join tbl_PropertyDefinition pd on pv.PropertyId = pd.PropertyId
left outer join tbl_TagDefinition td on pd.Name = ltrim(rtrim('Microsoft.TeamFoundation.Tagging.TagDefinition.' + cast(td.TagId as varchar(1000))))
where td.Name is not null
I have two tables table1 and table2. Each table contains a column with itemPrice. I need to add the two columns together.
The SQL query below returns the correct SUM.
SELECT SUM(item1+ item2) FROM
(select SUM(t1.itemPrice) item1 from table1 t1 WHERE t1.userid=='jonh') tableA
CROSS JOIN
(select SUM(t2.itemPrice) item2 from table2 t2 WHERE t1.userid=='jonh') tableB
I am not been lazy but the above query has so many SUM functions that I don't know where to start to write LINQ queries.
Can anyone help?
Ceci,
Hopefully this will give you what you want...
from f in (
from x in ( from t1 in Table1
where t1.Userid.Equals("John")
select new { Userid = t1.Userid }
).Distinct()
select new { item1 = ( from z in Table1
where z.Userid.Equals("John")
select z.ItemPrice ).Sum() ??0 ,
item2 = ( from z in Table2
where z.Userid.Equals("John")
select z.ItemPrice ).Sum() ??0 }
) select new { total = f.item1 + f.item2 }
In the case where there are no records for "john" in one table, it will bring back a 0 and sum up the other tables.
hope this helps.
I currently have a stored procedure which returns data and displayed in my report viewer but my issue is that i check to see if students attended to class or not and i have
COALESCE(A.Attended, 0)AS Attended
This returns 1 if they attended and 0 if not - in my report it only shows 1 or 0 even though they could have attended more than once. How can i cast this to an int to get the right total.
thanks
WHOLE QUERY:
SELECT
P.PartyId,
COUNT(COALESCE(A.Attended, 0))AS Attended,
COUNT(DISTINCT H.HearingId) AS Hearings,
O.OfficeName As OfficeName,
CO.Name,
P.FirstName AS FirstName,
P.LastName AS LastName,
P.BirthDate AS DOB
FROM Activity A
INNER JOIN ActivityType AT On A.ActivityTypeId = AT.ActivityTypeId
INNER JOIN ActivityEntry AE ON A.ActivityEntryId = AE.ActivityEntryId
INNER JOIN HearingEntry HE ON CAE.HearingEntryId = HE.HearingEntryId
INNER JOIN Hearing H ON HE.HearingEntryId = H.HearingEntryId
INNER JOIN [Case] C ON H.CaseId = C.CaseId
INNER JOIN CaseOffice CO ON C.CaseId = CO.CaseId AND AE.OfficeId = CO.OfficeId
INNER JOIN Office O ON CO.OfficeId = O.OfficeId
INNER JOIN Attended A ON H.HearingId = A.HearingId
INNER JOIN Party P ON A.PartyId = P.PartyId
WHERE HP.PartyId = P.PartyId AND AE.OfficeId = #OfficeId AND(H.HearingDate >= #BeginDate AND (H.HearingDate <= #EndDate OR H.HearingDate IS NULL)) AND HE.HearingEntryId = CAE.HearingEntryId
GROUP BY P.PartyId, A.Attended, O.OfficeName,CO.Name,P.FirstName, P.LastName,P.BirthDate
I guess you mean that A.Attented is a bit and you want it to be a int, so that you can aggregate later?
You can cast to a bit to an int like this:
CAST(A.Attented AS INT)
Or in this case:
COALESCE(CAST(A.Attended AS INT), 0) AS Attended
You need to SUM by Attended .
SELECT
P.PartyId,
(SUM(COALESCE(A.Attended, 0)))AS Attended,
COUNT(DISTINCT H.HearingId) AS Hearings,
O.OfficeName As OfficeName,
CO.Name,
P.FirstName AS FirstName,
P.LastName AS LastName,
P.BirthDate AS DOB
FROM Activity A
INNER JOIN ActivityType AT On A.ActivityTypeId = AT.ActivityTypeId
INNER JOIN ActivityEntry AE ON A.ActivityEntryId = AE.ActivityEntryId
INNER JOIN HearingEntry HE ON CAE.HearingEntryId = HE.HearingEntryId
INNER JOIN Hearing H ON HE.HearingEntryId = H.HearingEntryId
INNER JOIN [Case] C ON H.CaseId = C.CaseId
INNER JOIN CaseOffice CO ON C.CaseId = CO.CaseId AND AE.OfficeId = CO.OfficeId
INNER JOIN Office O ON CO.OfficeId = O.OfficeId
INNER JOIN Attended A ON H.HearingId = A.HearingId
INNER JOIN Party P ON A.PartyId = P.PartyId
WHERE HP.PartyId = P.PartyId AND AE.OfficeId = #OfficeId AND(H.HearingDate >= #BeginDate AND (H.HearingDate <= #EndDate OR H.HearingDate IS NULL)) AND HE.HearingEntryId = CAE.HearingEntryId
GROUP BY P.PartyId, O.OfficeName,CO.Name,P.FirstName, P.LastName,P.BirthDate