I try to get two different table values to insert one declare table but the employee ID is duplicate,
DECLARE #WSotable TABLE
(
Employee int,
ChargedQty int,
ReturnQty int
)
--insert first table data
INSERT INTO #WSotable
SELECT DISTINCT
MD_Employee.ID,
SUM(TR_InvoiceDetails.InvoiceQuantity) AS ChargedQty,
0 AS ChargedQty
FROM
TR_SalesOrderHeader
INNER JOIN
TR_InvoiceHeader ON TR_InvoiceHeader.SalesOrderID = TR_SalesOrderHeader.ID
INNER JOIN
TR_InvoiceDetails ON TR_InvoiceDetails.InvoiceID = TR_InvoiceHeader.ID
INNER JOIN
MD_ItemMaster ON TR_InvoiceDetails.ItemID = MD_ItemMaster.ID
INNER JOIN
MD_Supplier ON MD_ItemMaster.SupplierID= MD_Supplier.ID
INNER JOIN
MD_Employee ON TR_SalesOrderHeader.CreatedBy = MD_Employee.ID
WHERE
MD_Supplier.ID = 3
AND MD_ItemMaster.ID = 572
GROUP BY
MD_Employee.ID
--insert second table data
INSERT INTO #WSotable
SELECT DISTINCT
MD_Employee.ID,
0 AS ChargedQty,
SUM(TR_SalesReturnDetails.ReturnQuantity) AS ReturnQty
FROM
TR_SalesReturnHeader
INNER JOIN
TR_SalesReturnDetails ON TR_SalesReturnDetails.SalesReturnID = TR_SalesReturnDetails.ID
INNER JOIN
MD_ItemMaster ON TR_SalesReturnDetails.ItemID = MD_ItemMaster.ID
INNER JOIN
MD_Supplier ON MD_ItemMaster.SupplierID= MD_Supplier.ID
INNER JOIN
MD_Employee ON TR_SalesReturnHeader.CreatedBy = MD_Employee.ID
WHERE
MD_Supplier.ID = 3
AND MD_ItemMaster.ID = 572
GROUP BY
MD_Employee.ID
Result:
I need to stop EmployeeID duplicates
Return the data with a final aggregation:
select Employee, sum(ChargedQty) ChargedQty, sum(ReturnQty) ReturnQty
from #WSotable
group by Employee
I have two tables where one table has list of issues and another table has its parent issue in form of issue id.
I am only mentioning required columns
1st table
ID, ISSUENUM, PROJECTKEY
2nd Table
JSON_VALUE has parent_id which is ID from 1st table
END result expectation
ID, ISSUENUM, PROJECT KEY, PARENT_ISSUENUM, PARENT_PROJECT_KEY
I have query till this point, but unable to make hierarchical/ recursive query
(select json_value(EP.JSON_VALUE, '$.parent_id' RETURNING NUMBER)as xyz
from JIR.ENTITY_PROPERTY EP where EP.ENTITY_ID = JI.ID AND EP.ENTITY_NAME = 'IssueProperty'
AND json_exists(EP.JSON_VALUE, '$.parent_id'))as parent_id
from jir.project p,jir.jiraissue JI
where p.id = ji.project
and (p.pkey = 'ABCDEF' OR p.pkey = 'UVWXYZ')
How to use this ID which I have as column parent_id and use it to get parent_issue_num and parent_project_key
I solved it myself. I improved the query on getting parent_id and get parent_key using column query itself
select JI.ID,
(select P.PKEY||'-'||ji2.issuenum
from JIR.ENTITY_PROPERTY EP
inner join jir.jiraissue JI2 on ji2.id=json_value(EP.JSON_VALUE, '$.parent_id' RETURNING NUMBER)
inner join jir.project p2 on p2.id=ji2.project
where EP.ENTITY_ID = JI.ID AND EP.ENTITY_NAME = 'IssueProperty'
AND json_exists(EP.JSON_VALUE, '$.parent_id'))as parent_id
from jir.project p,jir.jiraissue JI
where p.id = ji.project
and (p.pkey = 'ABCDEF' OR p.pkey = 'UVWXYZ')
This small change resolved the issue
inner join jir.jiraissue JI2 on ji2.id=json_value(EP.JSON_VALUE, '$.parent_id' RETURNING NUMBER)
inner join jir.project p2 on p2.id=ji2.project
I am using SQL Server 2019 and looking for a way to show ONLY the latest value for each AOC based on its latest FW_Version. Here is my query I have so far but this shows everything:
SELECT DISTINCT
dbo.Model.ModelName AS AOC,
dbo.Chipset.Chipset,
dbo.FormFactor.FormFactor AS Form_Factor,
dbo.ProductRelease.ECO AS Release,
dbo.ProductRelease.Date AS Release_Date,
dbo.Intel.FWVersion AS FW_Version,
dbo.Intel.ETRACKID
FROM
dbo.Intel
INNER JOIN dbo.ProductRelease ON dbo.Intel.ProductReleaseID = dbo.ProductRelease.ID
INNER JOIN dbo.Model ON dbo.ProductRelease.ModelID = dbo.Model.ID
INNER JOIN dbo.FormFactor ON dbo.Model.FormFactorID = dbo.FormFactor.ID
INNER JOIN dbo.Chipset ON dbo.Intel.ControllerID = dbo.Chipset.ControllerID AND dbo.Intel.ChipsetID = dbo.Chipset.ID
ORDER BY
dbo.Model.ModelName,
dbo.Intel.FWVersion DESC,
dbo.ProductRelease.Date DESC
What I would like to show ONLY items marked in yellow... How can I make this happen?
Here is the list of my Tables which are joined based on their corresponding ID's
Table 1: Model
ID (Primary Key)
ModelName nvarchar(50)
FormFactorID int
Table 2: FormFactor
ID (Primary Key)
FormFactor nvarchar(15)
Table 3: ProductRelease
ID (Primary Key)
ModelID int
Date date
ECO nvarchar(10)
Table 4: Intel
ID (Primary Key)
ProductReleaseID int
ChipsetID int
FWVersion nvarchar(10)
ETRACKID nvarchar(15)
Table 5: Chipset
ID (Primary Key)
Chipsetnvarchar(20)
One method uses window functions:
SELECT *
FROM (
SELECT
dbo.Model.ModelName AS AOC,
dbo.Chipset.Chipset,
dbo.FormFactor.FormFactor AS Form_Factor,
dbo.ProductRelease.ECO AS Release,
dbo.ProductRelease.Date AS Release_Date,
dbo.Intel.FWVersion AS FW_Version,
dbo.Intel.ETRACKID,
ROW_NUMBER() OVER(PARTITION BY dbo.Model.ModelName ORDER BY dbo.ProductRelease.Date DESC) rn
FROM
dbo.Intel
INNER JOIN dbo.ProductRelease ON dbo.Intel.ProductReleaseID = dbo.ProductRelease.ID
INNER JOIN dbo.Model ON dbo.ProductRelease.ModelID = dbo.Model.ID
INNER JOIN dbo.FormFactor ON dbo.Model.FormFactorID = dbo.FormFactor.ID
INNER JOIN dbo.Chipset ON dbo.Intel.ControllerID = dbo.Chipset.ControllerID AND dbo.Intel.ChipsetID = dbo.Chipset.ID
) t
WHERE rn = 1
ORDER BY AOC
I have a complicated stored procedure that worked great until the client wanted to change it.
I am not great with complicated TSQL so I have no idea what is wrong with my code.
Here is the situation. I have three temp tables, Cost, Adjustments, and Payments.
In the end I merge all these tables together in a report table. The problem I am having is even if one or even two of these tables are null, as long as one table has data I need that data to show. I currently have it set up with full outer joins but I'm still not getting the full list, I'm missing probably....50 ish records that should be there.
Can anyone look at this code and tell me what the heck I'm doing wrong? I'm bringing all the data together on #ThisReportAll
UPDATE: So I removed the having clause to see what was going on, and the data for the overdue balance is returning null. So the math isn't...mathing correctly, any ideas?
CODE
CREATE TABLE #BalanceAdjustmentsAll (CustomerId int, Amount decimal(20,2));
CREATE TABLE #AnimalCostsAll (thisIndex int IDENTITY(1,1), AnimalTypeId int, Cost decimal(20,2));
CREATE TABLE #TotalAnimalCostAll (thisIndex int IDENTITY(1,1), YearSetupId int, AnimalTypeId int, AnimalType varchar(max), OwnerId int, CustomerId int, AnimalCount int, TtlSpeciesCost decimal(20,2));
CREATE TABLE #CustomerPaymentsAll (thisIndex int IDENTITY(1,1), CustomerID nvarchar(max), TtlPayments decimal(20,2));
CREATE TABLE #CustomerInfoAll (thisIndex int IDENTITY(1,1), OwnerId int, CustomerId int, FName nvarchar(200), LName nvarchar(200),BName nvarchar(200));
CREATE TABLE #ThisReportAll (thisIndex int IDENTITY(1,1), CustomerID nvarchar(max), Year char(4), OverdueBalance decimal(20,2), YearSetupId int);
INSERT INTO #BalanceAdjustmentsAll (CustomerId, Amount)
SELECT CustomerId, SUM(Amount)
FROM BalanceAdjustment
WHERE YearSetupId = 3
GROUP BY CustomerId;
/* GET Costs per Animal for 'This' yearID */
INSERT INTO #AnimalCostsAll (AnimalTypeId, Cost)
SELECT AnimalTypeId, Cost
FROM PerCapitaFee
WHERE YearSetupId = 3;
/* GET animal type totals for owner per year */
INSERT INTO #TotalAnimalCostAll (yearSetupId,AnimalTypeId,AnimalType,OwnerId,CustomerId,AnimalCount,TtlSpeciesCost)
SELECT YearSetup.YearSetupId,AnimalCount.AnimalTypeId,AnimalType.ShortDescription,Owner.OwnerId,Report.CustomerId,AnimalCount.Count,(ac.Cost * AnimalCount.Count)
FROM AnimalCount
INNER JOIN #AnimalCostsAll as ac
ON ac.AnimalTypeId = AnimalCount.AnimalTypeId
INNER JOIN AnimalType
ON AnimalCount.AnimalTypeId=AnimalType.AnimalTypeId
INNER JOIN AnimalLocation
ON AnimalLocation.AnimalLocationid=AnimalCount.AnimalLocationId
INNER JOIN Owner
ON Owner.OwnerId=AnimalLocation.OwnerId
AND Owner.OwnerType = 'P'
INNER JOIN Report
ON Report.ReportId=Owner.ReportId
INNER JOIN YearSetup
ON Report.YearSetupId=YearSetup.YearSetupId
INNER JOIN County
ON County.CountyId=AnimalLocation.CountyId
WHERE YearSetup.YearSetupId = 3 AND Report.Completed IS NOT NULL AND Report.CustomerId IS NOT NULL
/* Get The total payments a customer has made */
INSERT INTO #CustomerPaymentsAll (CustomerID,TtlPayments)
SELECT BPS.CustomerId,SUM(BPS.Amount)
FROM BatchPaymentSplit BPS
LEFT JOIN BatchPayment bp ON BPS.BatchPaymentId=bp.BatchPaymentId
LEFT JOIN Batch b ON bp.BatchId=b.BatchId
WHERE BPS.CustomerId IS NOT NULL
AND
(
((b.BatchTypeId = 'M' OR b.BatchTypeId = 'C' OR b.BatchTypeId = 'E') AND (b.BatchStatusId = 'S'))
OR
((b.BatchTypeId = 'B' OR b.BatchTypeId = 'N' OR b.BatchTypeId = 'R' OR b.BatchTypeId = 'T') AND (b.BatchStatusId = 'S' OR b.BatchStatusId='C'))
)
AND
BPS.YearSetupId = 3
GROUP BY BPS.CustomerId;
/* Deal with the name/id stuff */
INSERT INTO #CustomerInfoAll(FName, LName, BName, OwnerId, CustomerId)
SELECT
o.FirstName AS FName,
o.LastName AS LName,
o.BusinessName AS BName,
o.OwnerId AS OwnerId,
r.CustomerId AS CustomerId
FROM Owner o
INNER JOIN Report r
ON o.ReportId = r.ReportId
AND o.OwnerType = 'P'
WHERE r.CustomerId IN (SELECT CustomerId FROM #TotalAnimalCostAll)
AND r.Completed IS NOT NULL
AND r.YearSetupId = 3
AND NOT EXISTS(
SELECT 1 FROM Report
WHERE r.CustomerId = Report.CustomerId
AND Report.Completed IS NOT NULL
AND r.ReportId != Report.ReportId
AND r.YearSetupId = Report.YearSetupId
AND (
r.Completed < Report.Completed
OR (
r.Completed = Report.Completed
AND r.ReportId < Report.ReportId
)
)
)
ORDER BY CustomerId;
/** MAKE IT SO #1 **************************************************/
/* Simply Joining The Customer Info to the calculated totals to avoid any aggregation shenanigans... */
INSERT INTO #ThisReportAll (CustomerID,Year,OverdueBalance,YearSetupId)
SELECT COALESCE(t.CustomerId,cp.CustomerId,ba.CustomerID), ys.Name AS Year,
CASE
WHEN (SUM(t.TtlSpeciesCost) < 5 AND SUM(t.TtlSpeciesCost) > 0) AND (ys.Name='2015' OR ys.Name='2016')
THEN (5) - Isnull(cp.TtlPayments,0) + Isnull(ba.Amount,0)
ELSE SUM(t.TtlSpeciesCost) - Isnull(cp.TtlPayments,0) + Isnull(ba.Amount,0)
END
AS TtlOwnerCost, t.YearSetupId AS YearSetupId
FROM #TotalAnimalCostAll t
FULL OUTER JOIN #CustomerPaymentsAll cp ON t.CustomerId=cp.CustomerID
FULL OUTER JOIN #BalanceAdjustmentsAll ba ON COALESCE(t.CustomerId,cp.CustomerId)=ba.CustomerID
LEFT JOIN YearSetup ys ON COALESCE(t.CustomerId,cp.CustomerId,ba.CustomerID) = ys.YearSetupId
GROUP BY COALESCE(t.CustomerId,cp.CustomerId,ba.CustomerID),ys.Name,cp.TtlPayments, ba.Amount, t.YearSetupId
HAVING
CASE WHEN (SUM(t.TtlSpeciesCost) < 5 AND SUM(t.TtlSpeciesCost) > 0) AND (ys.Name='2015' OR ys.Name='2016')
THEN SUM(5) - Isnull(cp.TtlPayments,0) + Isnull(ba.Amount,0)
ELSE SUM(t.TtlSpeciesCost) - Isnull(cp.TtlPayments,0) + Isnull(ba.Amount,0)
END < 0;
/* Return some meaningful report data */
SELECT r.Year AS [YearName],r.CustomerID,left(ci.FName,20) AS [FirstName], left(ci.LName,40) AS [LastName], left(ci.BName,40) AS [BusinessName],r.OverdueBalance AS [Balance],r.YearSetupId
FROM #ThisReportAll r
LEFT JOIN #CustomerInfoAll ci ON r.CustomerID = ci.CustomerId
ORDER BY CAST(r.CustomerID as int) ASC;
DROP TABLE #BalanceAdjustmentsAll;
DROP TABLE #AnimalCostsAll;
DROP TABLE #TotalAnimalCostAll;
DROP TABLE #CustomerPaymentsAll;
DROP TABLE #CustomerInfoAll;
DROP TABLE #ThisReportAll;
Found it. I didn't have a default value for t.TtlSpeciesCost if it was null
SUM(t.TtlSpeciesCost) - Isnull(cp.TtlPayments,0) + Isnull(ba.Amount,0)
to
SUM(ISNULL(t.TtlSpeciesCost,0)) - Isnull(cp.TtlPayments,0) + Isnull(ba.Amount,0)
Some missing records may be found here:
by adjusting /* Get The total payments a customer has made */
INSERT INTO #CustomerPaymentsAll (CustomerID,TtlPayments)
SELECT BPS.CustomerId,SUM(BPS.Amount)
FROM BatchPaymentSplit BPS
LEFT JOIN BatchPayment bp
ON BPS.BatchPaymentId=bp.BatchPaymentId
LEFT JOIN Batch b
ON bp.BatchId=b.BatchId
AND ((b.BatchTypeId IN ('M', 'C', 'E') AND b.BatchStatusId = 'S')
OR (b.BatchTypeId IN ('B','N','R','T') AND (b.BatchStatusId IN ('S','C')))
WHERE BPS.CustomerId IS NOT NULL
AND BPS.YearSetupId = 3
GROUP BY BPS.CustomerId;
The WHERE on B would have negated the left join causing null records to be omitted. or made the left join to behave like an inner join.
To know for certain we need sample data from your tables showing which records are being omitted that you need to retain.
I also refactored the OR's and made them "IN"s to improve readability.
I have a query that I am using to pull in data from different tables. I have broken downthe query into a few different parts to make like a little easier. The problem is that when an ID number exists for sure in both places the LEFT JOIN does not always match them up, somtimes it does sometimes it does not.
All of the ID numbers are of type INT so I know all the data types are the same. I tried to use COLLATE SQL_Latin_General_Pref_CP1_CI_AS in the join statement but that is invalid for data of type INT was the error I received. OK fine, but I am really stuck as I don't know where to proceede from here other than dumping into excel and doing a vlookup.
Here is what I have:
DECLARE #sd DATETIME;
DECLARE #ed DATETIME;
SET #sd = '2014-01-01';
SET #ed = '2015-10-01';
DECLARE #denials_write_offs TABLE (
pk INT IDENTITY(1, 1) PRIMARY KEY
, pt_id INT
, bill_no INT
, denials FLOAT
)
INSERT INTO #denials_write_offs
SELECT a.pt_id
, a.bill_no
, a.denials_woffs
FROM (
SELECT CAST(pt_id AS INT) pt_id
, CAST(bill_no AS INT) bill_no
, SUM(tot_pay_adj_amt) AS denials_woffs
FROM smsmir.mir_pay
JOIN smsdss.c_Softmed_Denials_Detail_v
ON smsmir.mir_pay.pt_id = smsdss.c_Softmed_Denials_Detail_v.bill_no
WHERE discharged >= #sd
AND discharged < #ed
AND LEFT(smsmir.mir_pay.pay_cd, 4) = '0974'
GROUP BY pt_id
, bill_no
) A
--------------------------------------
DECLARE #EDTBL TABLE (
ACCOUNT INT
, ED_MD VARCHAR(MAX)
)
INSERT INTO #EDTBL
SELECT Z.*
FROM (
SELECT CAST(ACCOUNT AS INT) ACCOUNT
, ED_MD
FROM SMSDSS.c_Wellsoft_Rpt_tbl
) Z
----------------------------------------
DECLARE #TmpDenialsTbl TABLE (
PK INT IDENTITY(1, 1) PRIMARY KEY
, BILL_NO INT
...
...
...
)
INSERT INTO #TmpDenialsTbl
SELECT *
FROM (
SELECT bill_no
...
...
...
)
So as you can see, from the above every pt_id or bill_no is put into a table as an INT but I cannot for the life of me figure out why sometimes I get a match on my LEFT JOIN and why sometimes I do not. I did pull data from the #EDMD table and get the account number I was looking for but it did not hit on the left join below:
FROM #TmpDenialsTbl A
LEFT OUTER JOIN #denials_write_offs D
ON A.bill_no = d.pt_id
LEFT OUTER JOIN #EDTBL C
ON C.Account = D.bill_no
LEFT OUTER JOIN #USERTBL F
ON A.CERM_RVWR_ID = F.login_id
AND F.RN = 1
Example of what I am doing and what I get back
DECLARE #TmpDenials TABLE (
PT_ID INT
)
INSERT INTO #TmpDenials
SELECT A.*
FROM (
SELECT CAST(PT_ID AS INT) PT_ID
FROM SOME_TABLE
) A
DECLARE #EDMD TABLE (
PT_ID INT
EDMD VARCHAR(MAX)
)
INSERT INTO #EDMD
SELECT B.*
FROM (
SELECT CAST(PT_ID AS INT) PT_ID
EDMD
FROM SOME_OTHER_TABLE
)B
SELECT * FROM #TmpDenials
PT_ID |
123456789 |
SELECT * FROM #EDMD
PT_ID | ED_MD
123456789 | Dr. Emergency Room
SELECT *
FROM #TmpDenials A
LEFT OUTER JOIN #EDMD B
ON A.PT_ID = B.PT_ID
A.PT_ID | B.PT_ID | ED MD
123456789 | NULL | NULL
I'm not sure if this is the cause of your problem or not, but this condition is potentially suspicious:
FROM #TmpDenialsTbl A LEFT OUTER JOIN
#denials_write_offs D
ON A.bill_no = d.pt_id LEFT OUTER JOIN
#EDTBL C
ON C.Account = D.bill_no LEFT OUTER JOIN
--------^
#USERTBL F
ON A.CERM_RVWR_ID = F.login_id AND F.RN = 1
If there is no match to the C table form A, then this will always fail to match. I'm not sure if this is the intended behavior.
The other option is that a WHERE clause is filtering out the rows that you want.
Note: When you use table aliases, you should use abbreviations for the table names, so i, wo, e, and u are better than the arbitrary letters a, d, c, and f.