I have a fairly complex Stored Procedure that is joining several tables together, but I need yet another column called in from a table that is yet to be joined.
Here's the Stored Procedure as it stands:
CREATE PROCEDURE [rpt].[PlannerShipToLocations_ds1]
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sql nvarchar(4000)
,#DateStart nvarchar(10) = '2015-01-01'
DECLARE #t TABLE (
[PCN] int
,[Part_Key] int
,[Customer_Address_No] int
PRIMARY KEY CLUSTERED (
[Part_Key]
,[Customer_Address_No]
,[PCN]
)
)
SET #sql =
'SELECT *
FROM OPENQUERY (PLEXRPTSVR,
''SELECT DISTINCT
s.[PCN]
,sl.[Part_Key]
,s.[Customer_Address_No]
FROM [Sales_v_Shipper_e] AS s
INNER JOIN [Sales_v_Shipper_Status_e] AS ss
ON s.[PCN] = ss.[PCN]
AND s.[Shipper_Status_Key] = ss.[Shipper_Status_Key]
INNER JOIN [Sales_v_Shipper_Line_e] AS sl
ON s.[PCN] = sl.[PCN]
AND s.[Shipper_Key] = sl.[Shipper_Key]
WHERE 1 = 1
AND ss.[Shipped] = 1
AND s.[Ship_Date] >= ''''' + #DateStart + '''''
;'')'
INSERT INTO #t
EXECUTE sp_executesql #sql
;WITH base AS (
SELECT DISTINCT u.[Last_Name] + ', ' + u.[First_Name] AS [Planner]
,c.[Customer_Code] AS [Customer Code]
,c.[Name] AS [Customer Name]
,a.[Customer_Address_Code] AS [Customer Address Code]
**/* xxx.[Country] AS [Country] */**
FROM [plx].[Part_v_Customer_Part_e] cp
INNER JOIN [plx].[Part_v_Part_e] p
ON cp.[Plexus_Customer_No] = p.[Plexus_Customer_No]
AND cp.[Part_Key] = p.[Part_Key]
INNER JOIN [plx].[Common_v_Customer_e] c
ON cp.[Plexus_Customer_No] = c.[Plexus_Customer_No]
AND cp.[Customer_No] = c.[Customer_No]
INNER JOIN [plx].[Plexus_Control_v_Plexus_User_e] u
ON p.[Plexus_Customer_No] = u.[Plexus_Customer_No]
AND p.[Planner] = u.[Plexus_User_No]
OUTER APPLY (
SELECT [Customer_Address_Code], **/*[Country]*/**
FROM [plx].[Common_v_Customer_Address_e] a
INNER JOIN #t t
ON a.[Plexus_Customer_No] = t.[PCN]
AND a.[Customer_Address_No] = t.[Customer_Address_No]
**/* INNER JOIN [plx].[Common_v_Country] xxx
ON a.[Country_Key] = xxx.[Country_Key] */**
WHERE a.[Plexus_Customer_No] = p.[Plexus_Customer_No]
AND a.[Customer_No] = c.[Customer_No]
AND t.[Part_Key] = p.[Part_Key]
AND a.[Ship_To] = 1
) a
**
/* INNER JOIN [plx].[Common_v_Country] xxx
ON a.[Country_Key] = xxx.[Country_Key] */
/*
OUTER APPLY (
SELECT [Country]
FROM [plx].[Common_v_Country] xxx
INNER JOIN #t t
ON a.[Country_Key] = xxx.[Country_Key]
WHERE a.[Plexus_Customer_No] = p.[Plexus_Customer_No]
AND a.[Customer_No] = c.[Customer_No]
AND t.[Part_Key] = p.[Part_Key]
AND a.[Ship_To] = 1
) xxx
*/**
WHERE 1 = 1
AND u.[Lockout] = 0
AND p.[Part_Status] IN ('Production', 'Production - Near EOP', 'Pre-Production')
AND cp.[Active] = 1
)
SELECT t1.[Planner]
,t1.[Customer Code]
,t1.[Customer Name]
,STUFF(
(SELECT
' | ' + t2.[Customer Address Code]
FROM base t2
WHERE t1.[Planner] = t2.[Planner]
and t1.[Customer Code] = t2.[Customer Code]
ORDER BY t2.[Customer Address Code]
FOR XML PATH(''), TYPE
).value('.','varchar(max)')
,1,3,'') AS [Customer Address Code(s)]
FROM base t1
GROUP BY t1.[Planner]
,t1.[Customer Code]
,t1.[Customer Name]
ORDER BY [Customer Code]
,[Planner]
,[Customer Address Code(s)]
END
GO
I've bolded the sections that are my best guesses about how to go about joining this additional table, I recognize that I wouldn't use all of them but I wanted to show my thoughts. To break it down:
1.) [plx].[Common_v_Customer_Address_e] a AND [plx].[Common_v_Country] xxx are the two tables I need in order to call [Country] out by name. I essentially need to add this as a column displayed on the report and eventually will need to sort on it as well (I'll probably add it to ORDER BY at the end).
2.) I'm not sure if I need to be joining [plx].[Common_v_Country] xxx within the OUTER APPLY or if it needs its own separate INNER JOIN or what. I've illustrated and commented out both here. So far everything I've tried results in "The multi-part identifier 'xxx.Country' could not be bound."
Thanks for the help.
I've tried modifying the OUTER APPLY to include the new table. I've tried creating my own new INNER JOIN. I've tried creating my own new OUTER APPLY.
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 table with column name policy_refer, client name and issue_date
policy_refer Client_Name issue_date(entry_date)
0001 Ajaz 01-Jan-2019
0001 Ajaz 05-Jan-2019
0001 Anita 10-Jan-2019
i want to select last update/insert client_name where policy_refer = 0001 , in my select/join query ....
select policy_master.CLIENT_NAME
,POLICY_INSURER_DETAIL.INSURER_NAME
,POLICY_INSURER_DETAIL.INSURER_BRANCH
,POLICY_INSURER_DETAIL.policy_number
,policy_master.policy_refer
,policy_master.POLICY_CLASS
,policy_master.POLICY_PRODUCT
,policy_master.P_ISSUE_DATE
,policy_master.EXPIRY_DATE
,sum(policy_master.TOTAL_SUMINSURED)
,sum(policy_master.GROSS)
,sum(policy_master.PERMIUM)
from POLICY_MASTER,POLICY_INSURER_DETAIL
where policy_master.policy_refer = POLICY_INSURER_DETAIL.POLICY_REFER
and POLICY_MASTER.POL_ID = POLICY_INSURER_DETAIL.POL_ID
and POLICY_MASTER.EXPIRY_DATE ='19-AUG-20'
and POLICY_MASTER.DOC_STATUS ='Posted'
group by POLICY_MASTER.policy_refer
,POLICY_INSURER_DETAIL.INSURER_NAME
,POLICY_INSURER_DETAIL.INSURER_BRANCH
,POLICY_INSURER_DETAIL.policy_number
,policy_master.policy_refer
,policy_master.EXPIRY_DATE
,policy_master.CLIENT_NAME
,policy_master.POLICY_CLASS
,policy_master.POLICY_PRODUCT
,policy_master.P_ISSUE_DATE;
One option is to find which one's the first (using analytic functions), and then fetch that row. For example:
select column_list_goes_here
from (
--> this is your current query, with the ROW_NUMBER addition
select all your columns here,
--
row_number() over (partition by policy_refer order by issue_date desc) rn
--
from your_tables
where ...
group by ...
--> end of your current query
)
where rn = 1
It appears the table you are talking about is the POLICY_MASTER table and you want the latest row (by ISSUE_DATE) for each policy.
Then you want to filter something like this:
select pm.CLIENT_NAME
,POLICY_INSURER_DETAIL.INSURER_NAME
,POLICY_INSURER_DETAIL.INSURER_BRANCH
,POLICY_INSURER_DETAIL.policy_number
,pm.policy_refer
,pm.POLICY_CLASS
,pm.POLICY_PRODUCT
,pm.P_ISSUE_DATE
,pm.EXPIRY_DATE
,sum(pm.TOTAL_SUMINSURED)
,sum(pm.GROSS)
,sum(pm.PERMIUM)
from (
SELECT *
FROM (
SELECT POL_ID,
CLIENT_NAME,
policy_refer,
POLICY_CLASS,
POLICY_PRODUCT,
P_ISSUE_DATE,
EXPIRY_DATE,
TOTAL_SUMINSURED,
GROSS,
PERMIUM,
ROW_NUMBER() OVER ( PARTITION BY policy_refer /*, pol_id */
ORDER BY ISSUE_DATE DESC ) AS rn
FROM POLICY_MASTER
WHERE EXPIRY_DATE = DATE '2020-08-19'
AND DOC_STATUS ='Posted'
)
WHERE rn = 1
) pm
INNER JOIN POLICY_INSURER_DETAIL pid
ON ( pm.policy_refer = pid.POLICY_REFER
AND pm.POL_ID = pid.POL_ID )
GROUP BY pm.policy_refer
,pid.INSURER_NAME
,pid.INSURER_BRANCH
,pid.policy_number
,pm.policy_refer
,pm.EXPIRY_DATE
,pm.CLIENT_NAME
,pm.POLICY_CLASS
,pm.POLICY_PRODUCT
,pm.P_ISSUE_DATE;
I guess you need the first row after sorting them in descending from issue date -
select policy_master.CLIENT_NAME
,POLICY_INSURER_DETAIL.INSURER_NAME
,POLICY_INSURER_DETAIL.INSURER_BRANCH
,POLICY_INSURER_DETAIL.policy_number
,policy_master.policy_refer
,policy_master.POLICY_CLASS
,policy_master.POLICY_PRODUCT
,policy_master.P_ISSUE_DATE
,policy_master.EXPIRY_DATE
,sum(policy_master.TOTAL_SUMINSURED)
,sum(policy_master.GROSS)
,sum(policy_master.PERMIUM)
from POLICY_MASTER,POLICY_INSURER_DETAIL
where policy_master.policy_refer = POLICY_INSURER_DETAIL.POLICY_REFER
and POLICY_MASTER.POL_ID = POLICY_INSURER_DETAIL.POL_ID
and POLICY_MASTER.EXPIRY_DATE ='19-AUG-20'
and POLICY_MASTER.DOC_STATUS ='Posted'
and rownum = 1
group by POLICY_MASTER.policy_refer
,POLICY_INSURER_DETAIL.INSURER_NAME
,POLICY_INSURER_DETAIL.INSURER_BRANCH
,POLICY_INSURER_DETAIL.policy_number
,policy_master.policy_refer
,policy_master.EXPIRY_DATE
,policy_master.CLIENT_NAME
,policy_master.POLICY_CLASS
,policy_master.POLICY_PRODUCT
,policy_master.P_ISSUE_DATE
ORDER BY P_ISSUE_DATE DESC;
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