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
Related
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')
;
I have written a stored procedure as follows:
ALTER PROCEDURE [dbo].[Transaction]
(
#date_start date,
#date_end date
)
AS
BEGIN
with
rntexportbatch as (
Select
fb.BatchId,
rt.TransactionId,
count(*) as line_Count
from CxWarehouse.dbo.FinancialBatchPostingDetail fbpd
INNER JOIN CxWarehouse.dbo.FinancialBatchPosting fb
on (fb.BatchPostingId = fbpd.BatchPostingId and NominalAccount='12000' ) ---and NominalAccount='12000'
INNER JOIN CxWarehouse.dbo.FinancialBatch finb
on (finb.BatchId = fb.BatchId)
INNER JOIN CxWarehouse.dbo.SystemLookup l
on (l.LookupReference = finb.TransactionTypeId and LookupTypeId = 42)
INNER JOIN CxWarehouse.dbo.Financialtransaction ft
on (ft.TransactionId = fbpd.TransactionId)
LEFT JOIN CxWarehouse.dbo.RentTransactionElement rte
on (rte.TransactionElementId = ft.EntityId)
LEFT JOIN CxWarehouse.dbo.RentElement re
ON (re.ElementId = rte.ElementId)
LEFT JOIN CxWarehouse.dbo.RentTransaction rt
ON (rt.TransactionId = rte.TransactionId)
Group by rt.TransactionId,fb.BatchId
)
Select
ast.AssetId,
ast.AssetReference,
ast.[Agreement Description],
ast.CompanyIds,
rntAcc.AccountId,
rntAcc.AccountReference,
rntAcc.PaymentReference,
rt.TransactionId,
rt.AccountId,
rt.TransactionDate,
rt.TransactionTypeId,
[Transaction Type Id Description],
rt.PostingDate,
rt.PeriodNumber,
rt.Description as 'Rent Transaction Description',
rt.Notes,
[ElementId Description] ,
rntPay.BatchReference as 'Import BatchId',
rntPay.[Import Value],
rntexportbatch.BatchId as 'Export BatchId',
final.Value as 'Export Value'
from CxWarehouse.dbo.RentTransaction as rt
LEFT Join (
Select LookupId,
LookupTypeId,
LookupReference,
Description as 'Transaction Type Id Description'
from CxWarehouse.dbo.SystemLookup
where LookupTypeId = 46
) lu
on rt.TransactionTypeId = lu.LookupReference
LEFT Join (
Select
te.TransactionId,
te.TransactionElementId,
te.ElementId,
te.Value,
rntElm.Description as 'ElementId Description'
from CxWarehouse.dbo.RentTransactionElement as te
LEFT Join (
Select * from CxWarehouse.dbo.RentElement
) rntElm
on te.ElementId = rntElm.ElementId
) final
on rt.TransactionId = final.TransactionId
LEFT JOIN (
Select
t.AccountId,
t.AccountReference,
t.PaymentReference,
typ.Description as 'AccountType Description'
from CxWarehouse.dbo.RentAccount as t
left Join CxWarehouse.dbo.RentAccountType as typ
on t.AccountTypeId = typ.AccountTypeId
) rntAcc
on rt.AccountId = rntAcc.AccountId
Left Join (
Select
ast.AssetId,
AssetReference,
rntInf.AccountId,
rntInf.[Agreement Description],
rntInf.CompanyIds
from CxWarehouse.dbo.Asset as ast
left join (
Select
rntAgAs.AgreementAssetId,
rntAgAs.AgreementId,
rntAgAs.AssetId,
rntAgr.AccountId,
rntAgr.[Agreement Description],
rntAgr.CompanyIds
from CxWarehouse.dbo.RentAgreementAsset as rntAgAs
left join (
Select
rntAgmt.AgreementId,
rntAgmt.AgreementReference,
rntAgmt.AgreementTypeId,
rntAgmtTyp.Description as 'Agreement Description',
rntAgmtTyp.CompanyIds,
accEpi.AccountId
from CxWarehouse.dbo.RentAgreement as rntAgmt
LEFT JOIN CxWarehouse.dbo.RentAgreementType rntAgmtTyp
on rntAgmt.AgreementTypeId = rntAgmtTyp.AgreementTypeId
Left Join (
Select
rntAgEp.AgreementEpisodeId,
rntAgEp.AgreementId,
rntAgAc.AccountId
from CxWarehouse.dbo.RentAgreementEpisode as rntAgEp
Left Join CxWarehouse.dbo.RentAgreementAccount as rntAgAc
on rntAgEp.AgreementEpisodeId =rntAgAc.AgreementEpisodeId
) accEpi
on rntAgmt.AgreementId = accEpi.AgreementId
) rntAgr
on rntAgAs.AgreementId = rntAgr.AgreementId
) rntInf
on ast.AssetId = rntInf.AssetId
) ast
on rntAcc.AccountId = ast.AccountId
Left Join (
Select
rp.PaymentId,
rp.BatchReference,
rpd.PaymentDetailId,
rpd.Value as 'Import Value',
GeneratedTransactionId
from CxWarehouse.dbo.RentPayment as rp
left Join CxWarehouse.dbo.RentPaymentDetail rpd
on rp.PaymentId = rpd.PaymentId
left join CxWarehouse.dbo.RentPaymentPosting rpp
on rpd.PaymentDetailId = rpp.PaymentDetailId
) rntPay
on rt.TransactionId = rntPay.GeneratedTransactionId
LEFT JOIN rntexportbatch
on rntexportbatch.TransactionId = rt.TransactionId
where rt.PostingDate between #date_start and #date_end
END
I used that stored procedure in SSRS report. When I clicked on the dataset option of the report and refresh the data set, it gave following error:
I looked at the forum and found that error comes due to two or more columns have same name. I looked at the code and made sure that no column have same name. However, the problem still exist. Could anyone help me where I am making the mistake?
You have two columns both called AccountID being returned from your query
...
rntAcc.AccountId,
rntAcc.AccountReference,
rntAcc.PaymentReference,
rt.TransactionId,
rt.AccountId,
....
Alias or remove one of these columns.
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 am giving here part of the query I am executing:
SELECT SUM(ParentTable.Field1),
(SELECT SUM(ChildrenTable.Field1)
FROM ChildrenRable INNER JOIN
GrandChildrenTable ON ChildrenTable.Id = GrandChildrenTable.ChildrenTableId INNER JOIN
AnotherTable ON GrandChildrenTable.AnotherTableId = AnotherTable.Id
WHERE ChildrenTable.ParentBaleId = ParentTable.Id
AND AnotherTable.Type=1),
----
FROM ParentTable
WHERE some_conditions
Relationships:
ParentTable -> ChildrenTable = 1-to-many
ChildrenTable -> GrandChildrenTable = 1-to-many
GrandChildrenTable -> AnotherTable = 1-to-1
I am executing this query three times, while changing only the Type condition, and here are the results:
Number of records that are returned:
Condition Total execution time (ms)
Type = 1 : 973
Type = 2 : 78810
Type = 3 : 648318
If I execute just the inner join query, here is the count of joined records:
SELECT p.Type, COUNT(*)
FROM CycleActivities ca INNER JOIN
CycleActivityProducts cap ON ca.Id = CAP.CycleActivityId INNER JOIN
Products p ON cap.ProductId = p.Id
GROUP BY p.Type
Type
---- -----------
1 55152
2 13401
4 102730
So, why would the query with Type = 1 condition execute much faster than the query with Type = 2, although it is querying 4x larger resultset (Type is tinyint)?
The way your query is written instructs SQL Server to execute the sub-query with JOIN for every row of the output.
This way it should be faster, if I understand what you want correctly (UPDATED):
with cte_parent as (
select
Id,
SUM (ParentTable.Field1) as Parent_Sum
from ParentTable
group by Id
),
cte_child as (
SELECT
Id,
SUM (ChildrenTable.Field1) as as Child_Sum
FROM ChildrenRable
INNER JOIN
GrandChildrenTable ON ChildrenTable.Id = GrandChildrenTable.ChildrenTableId
INNER JOIN
AnotherTable ON GrandChildrenTable.AnotherTableId = AnotherTable.Id
WHERE
AnotherTable.Type=1
AND
some_conditions
GROUP BY Id
)
select cte_parent.id, Parent_Sum, Child_Sum
from parent_cte
join child_cte on parent_cte.id = child_cte.id
My Stored Procedure takes a very long time to execute.
Can anyone suggest me what I can do to speed up the stored procedure, apart from using some good practices for writing down the queries.
I've heard about creating indices, but I'm not sure what are they.
Please suggest all the best ways to speed up my queries.
Thanks
CREATE PROCEDURE [dbo].[usp_GetAlternates]
(
#NNumber CHAR(11) ,
#pid INT ,
#pbmid INT
)
AS
BEGIN
TRUNCATE TABLE TempTherapeuticAlt
INSERT INTO TempTherapeuticAlt
SELECT NULL AS MedicationID ,
PR.ePrescribingName AS MedicationName ,
U.Strength AS MedicationStrength ,
FRM.FormName AS MedicationForm ,
PR.DEAClassificationID AS DEASchedule ,
NULL AS NDCNumber
FROM Product PR
JOIN ( SELECT MP.MarketedProductID
FROM table2 TCTSP
JOIN table3 MP ON MP.SpecificProductID = TCTSP.SpecificProductID
JOIN ( SELECT TCTSP.TherapeuticConceptTreeID
FROM table3 MP
JOIN table2 TCTSP ON MP.SpecificProductID = TCTSP.SpecificProductID
JOIN ( SELECT
PR.MarketedProductID
FROM
table4 PA
JOIN Product PR ON PA.ProductID = PR.ProductID
WHERE
PA.NDC11 = #NNumber
) PAPA ON MP.MarketedProductID = PAPA.MarketedProductID
) xxx ON TCTSP.TherapeuticConceptTreeID = xxx.TherapeuticConceptTreeID
) MPI ON PR.MarketedProductID = MPI.MarketedProductID
JOIN ( SELECT P.ProductID ,
O.Strength ,
O.Unit
FROM Product AS P
INNER JOIN table3 AS M ON P.MarketedProductID = M.MarketedProductID
INNER JOIN table5 AS S ON M.SpecificProductID = S.SpecificProductID
LEFT OUTER JOIN table6 AS O ON S.SpecificProductID = O.SpecificProductID
GROUP BY P.ProductID ,
O.Strength ,
O.Unit
) U ON PR.ProductID = U.ProductID
JOIN ( SELECT PA.ProductID ,
S.ScriptFormID ,
F.Code AS NCPDPScriptFormCode ,
S.FormName
FROM table4 AS PA
INNER JOIN table7 AS S ON PA.NCPDPScriptFormCode = S.NCPDPScriptFormCode
INNER JOIN table8 AS F ON S.FormName = F.FormName
GROUP BY PA.ProductID ,
S.ScriptFormID ,
F.Code ,
S.FormName
) FRM ON PR.ProductID = FRM.ProductID
GROUP BY PR.ePrescribingName ,
U.Strength ,
FRM.FormName ,
PR.DEAClassificationID
ORDER BY pr.ePrescribingName
SELECT LL.ProductID AS MedicationID ,
temp.MedicationName ,
temp.MedicationStrength ,
temp.MedicationForm ,
temp.DEASchedule ,
temp.NDCNumber ,
fs.[ReturnFormulary] AS FormularyStatus ,
copay.CopaTier ,
copay.FirstCopayTerm ,
copay.FlatCopayAmount ,
copay.PercentageCopay
FROM TempTherapeuticAlt temp
OUTER APPLY ( SELECT TOP 1
ProductID
FROM Product
WHERE ePrescribingName = temp.MedicationName
) AS LL
OUTER APPLY function1(#pid, LL.ProductID, #pbmid) AS fs
OUTER APPLY function2(LL.ProductID, #pbmid) AS copay
ORDER BY LL.ProductID
TRUNCATE TABLE TempTherapeuticAlt
END
GO
Here are a few:
You should have indexes for every column in a WHERE clause. See
your SQL language for how to do it.
Learn how to EXPLAIN PLAN and see what's slow.
Stored procedure languages are functional, not set based. Use JOIN and don't fall into the (n+1) query/iteration trap.
Understand how using certain functions force you to TABLE SCAN in a WHERE clause.