I have to unite more than 12 tables value in one view and I should make it with the help of view. Here are two selects which show us what kind of data I should have in a view, I have tried view's several combination but nothing of them were helpful, they provide too many duplicates.
What should I do to get rid of these duplicates?
Here are my two selects and my views example.
First table
SELECT DISTINCT
,ISNULL(SoxInf.InformationGUID,'') AS EntryGuid
,ISNULL([Entry].EntryGUID,'') AS LineGuid
,ISNULL([Entry].LineNumber,0) AS LineNum
,ISNULL([Entry].productID,0) AS Productid
,ISNULL([Entry].ProductName,'') AS ProductNum
,ISNULL([Entry].CommercialDescription,'') AS ProductDesc
,ISNULL([Entry].Brand,'') AS StyleNum
,ISNULL([Entry].TaxIDOfManufacturer,'') AS ManufacturerID
,ISNULL([Entry].ManufacturerInfo,'') AS ManufacturerName
,ISNULL([Entry].GrossWeight,0) AS GrossWeight
,ISNULL([Entry].NetWeight,0) AS NetWeight
,ISNULL([Entry].NumberOfUnits,0) AS TxnQty
,ISNULL([Entry].TypeOfUnits,'') AS TxnQtyUOM
,ISNULL([Entry].SumValue,0) AS TotalValue
,ISNULL([Entry].StatisticalAmount,0) AS RptQty
,ISNULL(TaxInsurence.Rate,0) AS ExactRate
,ISNULL(TaxInsurence.Amount,0) AS Duty
,ISNULL(TaxInsurence.Amount,0) + ISNULL(Calculate.AddlDuty,0) AS TotalDuty
,ISNULL(BorderTax.Amount,0) AS TotalBorderTaxValue
,ISNULL(CustomerTax.Amount,0) AS TotalCustomerTaxValue
,ISNULL(OtherTax.Amount,0) AS TotalOtherTaxValue
,ISNULL(SoxInf.CountryOfDeparture,0) AS ExportCountry
,ISNULL(SoxInf.Term,'') AS Terms
,ISNULL(SoxInf.RelationshipOfBuyerAndSeller,'') AS RelatedPartyFlag
,LocalPortExpenses + OtherLocalExpenses + FreightAmount + InsuranceAmount + TresspassingFeeAtTheBorder AS Fees
,ISNULL(TotalCalculate.TotalAVeragePayments,0) AS TotalSummedPayments
FROM
[Incoming] Incoming
LEFT JOIN
[Documentinformation] SoxInf ON SoxInf.IncomingGUID = Incoming.IncomingGUID
AND SoxInf.PartnerId = Incoming.PartnerId
LEFT JOIN
[IncomingDocuments] Document ON Document.InformationGUID = SoxInf.InformationGUID
LEFT JOIN
[IncomingEntryDocument] [Entry] ON [Entry].InformationGUID = SoxInf.InformationGUID
LEFT JOIN
(SELECT
SoxInf.InformationGUID AS DocumentExtraInformationGUID
,SoxInf.PartnerId AS PartnerId
,(SoxInf.CounterveilingDuty * (ISNULL([Entry].SumValue,0) / ISNULL(NULLIF(EntrySum.SumSumValue,0),1))) AS AddlDuty
FROM
[Documentinformation] SoxInf
LEFT JOIN
[IncomingEntryDocument] [Entry] ON [Entry].InformationGUID = SoxInf.InformationGUID
AND
LEFT JOIN
(SELECT
DocumentExtraInformationGUID, SUM(SumValue) AS SumSumValue
FROM
[IncomingEntryDocument]
GROUP BY
DocumentExtraInformationGUID) EntrySum ON EntrySum.InformationGUID = SoxInf.InformationGUID
GROUP BY
SoxInf.DocumentExtraInformationGUID
,SoxInf.PartnerId
,SoxInf.CounterveilingDuty
,[Entry].SumValue
,EntrySum.SumSumValue) Calculate ON Calculate.InformationGUID = SoxInf.InformationGUID
AND
LEFT JOIN
(SELECT
SoxInf.InformationGUID AS DocumentExtraInformationGUID
,(ISNULL(TaxInsurence.Amount,0) + (SoxInf.CounterveilingDuty * (ISNULL([Entry].SumValue,0) / ISNULL(NULLIF(EntrySum.SumSumValue,0),1)))
+ ISNULL(BorderTax.Amount,0) + ISNULL(CustomerTax.Amount,0) + ISNULL(OtherTax.Amount,0)
+ ISNULL([Entry].LocalPortExpenses,0) + ISNULL([Entry].OtherLocalExpenses,0)
+ ISNULL([Entry].FreightAmount,0) + ISNULL([Entry].InsuranceAmount,0)
+ ISNULL([Entry].TresspassingFeeAtTheBorder,0)) AS TotalAVeragePayments
FROM [Documentinformation] SoxInf
LEFT JOIN [IncomingEntryDocument] [Entry]
ON [Entry].InformationGUID = SoxInf.InformationGUID AND [Entry].PartnerID = SoxInf.PartnerId
LEFT JOIN (
SELECT DocumentExtraInformationGUID, PartnerID, SUM(SumValue) AS SumSumValue
FROM [IncomingEntryDocument]
GROUP BY DocumentExtraInformationGUID, PartnerID) EntrySum
ON EntrySum.InformationGUID = SoxInf.InformationGUID
LEFT JOIN [EntryTaxInformation] TaxInsurence
ON TaxInsurence.InformationGUID = SoxInf.InformationGUID
AND TaxInsurence.Code = '10'
LEFT JOIN [EntryTaxInformation] BorderTax
ON BorderTax.InformationGUID = SoxInf.InformationGUID
AND BorderTax.Code = '40'
LEFT JOIN [EntryTaxInformation] CustomerTax
ON CustomerTax.InformationGUID = SoxInf.InformationGUID
LEFT JOIN [EntryTaxInformation] OtherTax
ON OtherTax.InformationGUID = SoxInf.InformationGUID
AND OtherTax.Code = '89'
GROUP BY SoxInf.DocumentExtraInformationGUID
,TaxInsurence.Amount
,BorderTax.Amount
,CustomerTax.Amount
,OtherTax.Amount
,SoxInf.CounterveilingDuty
,[Entry].SumValue
,EntrySum.SumSumValue
,[Entry].LocalPortExpenses
,[Entry].OtherLocalExpenses
,[Entry].FreightAmount
,[Entry].InsuranceAmount
,[Entry].TresspassingFeeAtTheBorder
) TotalCalculate
ON TotalCalculate.InformationGUID = SoxInf.InformationGUID
LEFT JOIN EntryTaxInformation TaxInsurence
ON TaxInsurence.InformationGUID = SoxInf.InformationGUID
AND TaxInsurence.Code = '10'
LEFT JOIN [EntryTaxInformation] BorderTax
ON BorderTax.InformationGUID = SoxInf.InformationGUID
AND BorderTax.Code = '40'
LEFT JOIN [EntryTaxInformation] CustomerTax
ON CustomerTax.InformationGUID = SoxInf.InformationGUID
AND CustomerTax.Code = '93'
LEFT JOIN [EntryTaxInformation] OtherTax
ON OtherTax.InformationGUID = SoxInf.InformationGUID
AND OtherTax.Code = '89'
Second Table
SELECT DISTINCT
ISNULL(Inf.InformationGUID,'') AS EntryRecordGuid
,ISNULL(Inf.RegistryNumber,'') AS Num
,ISNULL(Inf.TaxIDOfTheDeclarant,'') AS BrokerID
,ISNULL(Inf.TypeOfDocument,'') AS DocumentType
,ISNULL(Inf.CustomerId,'') AS CustomerID
,ISNULL(Inf.RelatedReference,'') AS ReferenceNum
,CONVERT(DATETIME,(SELECT MAX(ISNULL(Document.DocumentDate,'')) FROM [EntryRecordDocument] Document
WHERE Document.InformationGUID = Inf.InformationGUID
GROUP BY Document.InformationGUID),104) AS Registration
,CONVERT(DATETIME,(SELECT MAX(ISNULL(Document.DocumentDate,'')) FROM [EntryRecordDocument] Document
WHERE Document.InformationGUID = Inf.InformationGUID
GROUP BY Document.InformationGUID),104) AS Eidt
,CONVERT(DATETIME,(SELECT MAX(ISNULL(Document.ApprovalDate,'')) FROM [EntryRecordDocument] Document
WHERE Document.InformationGUID = Inf.InformationGUID
GROUP BY Document.InformationGUID),104) AS ReleaseDate
,ISNULL(Inf.TypeOfVehicleAtTheBorder,'') AS ModeOfTransport
,CONVERT(varchar(6),FLOOR(ISNULL([EntryRecord].LocalPort,0))) AS PortOfFiling
,SUM(DutyCalculate.Duty) AS TotalPaymentValue
,SUM(LocalPortExpenses + OtherLocalExpenses + FreightAmount + InsuranceAmount + TresspassingFeeAtTheBorder) AS TotalFees
,SUM(DeclarationPaymentsCalculate.DeclarationPayments) AS TotalDeclarationPayments
FROM [Incoming] Incoming
LEFT JOIN [Documentinformation] Inf
ON Inf.IncomingGUID = Incoming.IncomingGUID
LEFT JOIN [EntryRecordInformation] [EntryRecord]
ON [EntryRecord].InformationGUID = Inf.InformationGUID
LEFT JOIN (
SELECT Inf.InformationGUID AS InformationGUID
,(ISNULL(Tax.Amount,0) + (Inf.CounterveilingDuty * (ISNULL([EntryRecord].StatisticValue,0) / ISNULL(NULLIF(EntryRecordSum.SumStatisticValue,0),1)))) AS Duty
FROM [Documentinformation] Inf
LEFT JOIN [EntryRecordInformation] [EntryRecord]
ON [EntryRecord].InformationGUID = Inf.InformationGUID
LEFT JOIN (
SELECT InformationGUID, PartnerID, SUM(StatisticValue) AS SumStatisticValue
FROM [EntryRecordInformation]
GROUP BY InformationGUID, PartnerID) EntryRecordSum
ON EntryRecordSum.InformationGUID = Inf.InformationGUID
LEFT JOIN [EntryRecordTax] Tax
ON Tax.InformationGUID = Inf.InformationGUID
AND Tax.Code = '10'
GROUP BY Inf.InformationGUID
,Tax.Amount
,Inf.CounterveilingDuty
,[EntryRecord].StatisticValue
,EntryRecordSum.SumStatisticValue
) DutyCalculate
ON DutyCalculate.InformationGUID = Inf.InformationGUID
LEFT JOIN(
SELECT Inf.InformationGUID AS InformationGUID
,(ISNULL(TaxInsurence.Amount,0) + (Inf.CounterveilingDuty * (ISNULL([EntryRecord].StatisticValue,0) / ISNULL(NULLIF(EntryRecordSum.SumStatisticValue,0),1)))
+ ISNULL(BorderTax.Amount,0) + ISNULL(CustomerTax.Amount,0) + ISNULL(OtherTax.Amount,0)
+ ISNULL([EntryRecord].LocalPortExpenses,0) + ISNULL([EntryRecord].OtherLocalExpenses,0)
+ ISNULL([EntryRecord].FreightAmount,0) + ISNULL([EntryRecord].InsuranceAmount,0)
+ ISNULL([EntryRecord].TresspassingFeeAtTheBorder,0)) AS DeclarationPayments
FROM [Documentinformation] Inf
LEFT JOIN [EntryRecordInformation] [EntryRecord]
ON [EntryRecord].InformationGUID = Inf.InformationGUID AND [EntryRecord].PartnerID = Inf.PartnerId
LEFT JOIN (
SELECT InformationGUID, PartnerID, SUM(StatisticValue) AS SumStatisticValue
FROM [EntryRecordInformation]
GROUP BY InformationGUID, PartnerID) EntryRecordSum
ON EntryRecordSum.InformationGUID = Inf.InformationGUID
LEFT JOIN [EntryRecordTax] TaxInsurence
ON TaxInsurence.InformationGUID = Inf.InformationGUID
AND TaxInsurence.Code = '10'
LEFT JOIN [EntryRecordTax] BorderTax
ON BorderTax.InformationGUID = Inf.InformationGUID
AND BorderTax.Code = '40'
LEFT JOIN [EntryRecordTax] CustomerTax
ON CustomerTax.InformationGUID = Inf.InformationGUID
AND CustomerTax.Code = '93'
LEFT JOIN [EntryRecordTax] OtherTax
ON OtherTax.InformationGUID = Inf.InformationGUID
AND OtherTax.Code = '89'
GROUP BY Inf.InformationGUID
,TaxInsurence.Amount
,BorderTax.Amount
,CustomerTax.Amount
,OtherTax.Amount
,Inf.CounterveilingDuty
,[EntryRecord].StatisticValue
,EntryRecordSum.SumStatisticValue
,[EntryRecord].LocalPortExpenses
,[EntryRecord].OtherLocalExpenses
,[EntryRecord].FreightAmount
,[EntryRecord].InsuranceAmount
,[EntryRecord].TresspassingFeeAtTheBorder
) DeclarationPaymentsCalculate
ON DeclarationPaymentsCalculate.InformationGUID = Inf.InformationGUID
GROUP BY Inf.InformationGUID
,Inf.RegistryNumber
,Inf.TaxIDOfTheDeclarant
,Inf.TypeOfDocument
,Inf.CustomerId
,Inf.RelatedReference
,Inf.TypeOfVehicleAtTheBorder
My view
CREATE VIEW
AS
SELECT DISTINCT
,ISNULL(SoxInf.InformationGUID,'') AS EntryGuid
,ISNULL([Entry].EntryGUID,'') AS LineGuid
,ISNULL([Entry].LineNumber,0) AS LineNum
,ISNULL([Entry].productID,0) AS Productid
,ISNULL([Entry].ProductName,'') AS ProductNum
,ISNULL([Entry].CommercialDescription,'') AS ProductDesc
,ISNULL([Entry].Brand,'') AS StyleNum
,ISNULL([Entry].TaxIDOfManufacturer,'') AS ManufacturerID
,ISNULL([Entry].ManufacturerInfo,'') AS ManufacturerName
,ISNULL([Entry].GrossWeight,0) AS GrossWeight
,ISNULL([Entry].NetWeight,0) AS NetWeight
,ISNULL([Entry].NumberOfUnits,0) AS TxnQty
,ISNULL([Entry].TypeOfUnits,'') AS TxnQtyUOM
,ISNULL([Entry].SumValue,0) AS TotalValue
,ISNULL([Entry].StatisticalAmount,0) AS RptQty
,ISNULL(TaxInsurence.Rate,0) AS ExactRate
,ISNULL(TaxInsurence.Amount,0) AS Duty
,ISNULL(TaxInsurence.Amount,0) + ISNULL(Calculate.AddlDuty,0) AS TotalDuty
,ISNULL(BorderTax.Amount,0) AS TotalBorderTaxValue
,ISNULL(CustomerTax.Amount,0) AS TotalCustomerTaxValue
,ISNULL(OtherTax.Amount,0) AS TotalOtherTaxValue
,ISNULL(SoxInf.CountryOfDeparture,0) AS ExportCountry
,ISNULL(SoxInf.Term,'') AS Terms
,ISNULL(SoxInf.RelationshipOfBuyerAndSeller,'') AS RelatedPartyFlag
,LocalPortExpenses + OtherLocalExpenses + FreightAmount + InsuranceAmount + TresspassingFeeAtTheBorder AS Fees
,ISNULL(TotalCalculate.TotalAVeragePayments,0) AS TotalSummedPayments
,ISNULL(Inf.InformationGUID,'') AS EntryRecordGuid
,ISNULL(Inf.RegistryNumber,'') AS Num
,ISNULL(Inf.TaxIDOfTheDeclarant,'') AS BrokerID
,ISNULL(Inf.TypeOfDocument,'') AS DocumentType
,ISNULL(Inf.CustomerId,'') AS CustomerID
,ISNULL(Inf.RelatedReference,'') AS ReferenceNum
,CONVERT(DATETIME,(SELECT MAX(ISNULL(Document.DocumentDate,'')) FROM [EntryRecordDocument] Document
WHERE Document.InformationGUID = Inf.InformationGUID
GROUP BY Document.InformationGUID),104) AS Registration
,CONVERT(DATETIME,(SELECT MAX(ISNULL(Document.DocumentDate,'')) FROM [EntryRecordDocument] Document
WHERE Document.InformationGUID = Inf.InformationGUID
GROUP BY Document.InformationGUID),104) AS Eidt
,CONVERT(DATETIME,(SELECT MAX(ISNULL(Document.ApprovalDate,'')) FROM [EntryRecordDocument] Document
WHERE Document.InformationGUID = Inf.InformationGUID
GROUP BY Document.InformationGUID),104) AS ReleaseDate
,ISNULL(Inf.TypeOfVehicleAtTheBorder,'') AS ModeOfTransport
,CONVERT(varchar(6),FLOOR(ISNULL([EntryRecord].LocalPort,0))) AS PortOfFiling
,SUM(DutyCalculate.Duty) AS TotalPaymentValue
,SUM(LocalPortExpenses + OtherLocalExpenses + FreightAmount + InsuranceAmount + TresspassingFeeAtTheBorder) AS TotalFees
,SUM(DeclarationPaymentsCalculate.DeclarationPayments) AS TotalDeclarationPayments
FROM [Incoming] Incoming
LEFT JOIN [Documentinformation] Inf
ON Inf.IncomingGUID = Incoming.IncomingGUID
LEFT JOIN [EntryRecordInformation] [EntryRecord]
ON [EntryRecord].InformationGUID = Inf.InformationGUID
--join from another table--
LEFT JOIN (
SELECT SoxInf.InformationGUID AS DocumentExtraInformationGUID
,SoxInf.PartnerId AS PartnerId
,(SoxInf.CounterveilingDuty * (ISNULL([Entry].SumValue,0) / ISNULL(NULLIF(EntrySum.SumSumValue,0),1))) AS AddlDuty
FROM [Documentinformation] SoxInf
LEFT JOIN [IncomingEntryDocument] [Entry]
ON [Entry].InformationGUID = SoxInf.InformationGUID AND
LEFT JOIN (
SELECT DocumentExtraInformationGUID, SUM(SumValue) AS SumSumValue
FROM [IncomingEntryDocument]
GROUP BY DocumentExtraInformationGUID) EntrySum
ON EntrySum.InformationGUID = SoxInf.InformationGUID
GROUP BY SoxInf.DocumentExtraInformationGUID
,SoxInf.PartnerId
,SoxInf.CounterveilingDuty
,[Entry].SumValue
,EntrySum.SumSumValue
) Calculate
ON Calculate.InformationGUID = SoxInf.InformationGUID AND
LEFT JOIN(
SELECT SoxInf.InformationGUID AS DocumentExtraInformationGUID
,(ISNULL(TaxInsurence.Amount,0) + (SoxInf.CounterveilingDuty * (ISNULL([Entry].SumValue,0) / ISNULL(NULLIF(EntrySum.SumSumValue,0),1)))
+ ISNULL(BorderTax.Amount,0) + ISNULL(CustomerTax.Amount,0) + ISNULL(OtherTax.Amount,0)
+ ISNULL([Entry].LocalPortExpenses,0) + ISNULL([Entry].OtherLocalExpenses,0)
+ ISNULL([Entry].FreightAmount,0) + ISNULL([Entry].InsuranceAmount,0)
+ ISNULL([Entry].TresspassingFeeAtTheBorder,0)) AS TotalAVeragePayments
FROM [Documentinformation] SoxInf
LEFT JOIN [IncomingEntryDocument] [Entry]
ON [Entry].InformationGUID = SoxInf.InformationGUID AND [Entry].PartnerID = SoxInf.PartnerId
LEFT JOIN (
SELECT DocumentExtraInformationGUID, PartnerID, SUM(SumValue) AS SumSumValue
FROM [IncomingEntryDocument]
GROUP BY DocumentExtraInformationGUID, PartnerID) EntrySum
ON EntrySum.InformationGUID = SoxInf.InformationGUID
LEFT JOIN [EntryTaxInformation] TaxInsurence
ON TaxInsurence.InformationGUID = SoxInf.InformationGUID
AND TaxInsurence.Code = '10'
LEFT JOIN [EntryTaxInformation] BorderTax
ON BorderTax.InformationGUID = SoxInf.InformationGUID
AND BorderTax.Code = '40'
LEFT JOIN [EntryTaxInformation] CustomerTax
ON CustomerTax.InformationGUID = SoxInf.InformationGUID
LEFT JOIN [EntryTaxInformation] OtherTax
ON OtherTax.InformationGUID = SoxInf.InformationGUID
AND OtherTax.Code = '89'
GROUP BY SoxInf.DocumentExtraInformationGUID
,TaxInsurence.Amount
,BorderTax.Amount
,CustomerTax.Amount
,OtherTax.Amount
,SoxInf.CounterveilingDuty
,[Entry].SumValue
,EntrySum.SumSumValue
,[Entry].LocalPortExpenses
,[Entry].OtherLocalExpenses
,[Entry].FreightAmount
,[Entry].InsuranceAmount
,[Entry].TresspassingFeeAtTheBorder
) TotalCalculate
ON TotalCalculate.InformationGUID = SoxInf.InformationGUID
LEFT JOIN EntryTaxInformation TaxInsurence
ON TaxInsurence.InformationGUID = SoxInf.InformationGUID
AND TaxInsurence.Code = '10'
LEFT JOIN [EntryTaxInformation] BorderTax
ON BorderTax.InformationGUID = SoxInf.InformationGUID
AND BorderTax.Code = '40'
LEFT JOIN [EntryTaxInformation] CustomerTax
ON CustomerTax.InformationGUID = SoxInf.InformationGUID
AND CustomerTax.Code = '93'
LEFT JOIN [EntryTaxInformation] OtherTax
ON OtherTax.InformationGUID = SoxInf.InformationGUID
AND OtherTax.Code = '89'
--end of join on another table--
LEFT JOIN (
SELECT Inf.InformationGUID AS InformationGUID
,(ISNULL(Tax.Amount,0) + (Inf.CounterveilingDuty * (ISNULL([EntryRecord].StatisticValue,0) / ISNULL(NULLIF(EntryRecordSum.SumStatisticValue,0),1)))) AS Duty
FROM [Documentinformation] Inf
LEFT JOIN [EntryRecordInformation] [EntryRecord]
ON [EntryRecord].InformationGUID = Inf.InformationGUID
LEFT JOIN (
SELECT InformationGUID, PartnerID, SUM(StatisticValue) AS SumStatisticValue
FROM [EntryRecordInformation]
GROUP BY InformationGUID, PartnerID) EntryRecordSum
ON EntryRecordSum.InformationGUID = Inf.InformationGUID
LEFT JOIN [EntryRecordTax] Tax
ON Tax.InformationGUID = Inf.InformationGUID
AND Tax.Code = '10'
GROUP BY Inf.InformationGUID
,Tax.Amount
,Inf.CounterveilingDuty
,[EntryRecord].StatisticValue
,EntryRecordSum.SumStatisticValue
) DutyCalculate
ON DutyCalculate.InformationGUID = Inf.InformationGUID
LEFT JOIN(
SELECT Inf.InformationGUID AS InformationGUID
,(ISNULL(TaxInsurence.Amount,0) + (Inf.CounterveilingDuty * (ISNULL([EntryRecord].StatisticValue,0) / ISNULL(NULLIF(EntryRecordSum.SumStatisticValue,0),1)))
+ ISNULL(BorderTax.Amount,0) + ISNULL(CustomerTax.Amount,0) + ISNULL(OtherTax.Amount,0)
+ ISNULL([EntryRecord].LocalPortExpenses,0) + ISNULL([EntryRecord].OtherLocalExpenses,0)
+ ISNULL([EntryRecord].FreightAmount,0) + ISNULL([EntryRecord].InsuranceAmount,0)
+ ISNULL([EntryRecord].TresspassingFeeAtTheBorder,0)) AS DeclarationPayments
FROM [Documentinformation] Inf
LEFT JOIN [EntryRecordInformation] [EntryRecord]
ON [EntryRecord].InformationGUID = Inf.InformationGUID AND [EntryRecord].PartnerID = Inf.PartnerId
LEFT JOIN (
SELECT InformationGUID, PartnerID, SUM(StatisticValue) AS SumStatisticValue
FROM [EntryRecordInformation]
GROUP BY InformationGUID, PartnerID) EntryRecordSum
ON EntryRecordSum.InformationGUID = Inf.InformationGUID
LEFT JOIN [EntryRecordTax] TaxInsurence
ON TaxInsurence.InformationGUID = Inf.InformationGUID
AND TaxInsurence.Code = '10'
LEFT JOIN [EntryRecordTax] BorderTax
ON BorderTax.InformationGUID = Inf.InformationGUID
AND BorderTax.Code = '40'
LEFT JOIN [EntryRecordTax] CustomerTax
ON CustomerTax.InformationGUID = Inf.InformationGUID
AND CustomerTax.Code = '93'
LEFT JOIN [EntryRecordTax] OtherTax
ON OtherTax.InformationGUID = Inf.InformationGUID
AND OtherTax.Code = '89'
GROUP BY Inf.InformationGUID
,TaxInsurence.Amount
,BorderTax.Amount
,CustomerTax.Amount
,OtherTax.Amount
,Inf.CounterveilingDuty
,[EntryRecord].StatisticValue
,EntryRecordSum.SumStatisticValue
,[EntryRecord].LocalPortExpenses
,[EntryRecord].OtherLocalExpenses
,[EntryRecord].FreightAmount
,[EntryRecord].InsuranceAmount
,[EntryRecord].TresspassingFeeAtTheBorder
) DeclarationPaymentsCalculate
ON DeclarationPaymentsCalculate.InformationGUID = Inf.InformationGUID
GROUP BY Inf.InformationGUID
,Inf.RegistryNumber
,Inf.TaxIDOfTheDeclarant
,Inf.TypeOfDocument
,Inf.CustomerId
,Inf.RelatedReference
,Inf.TypeOfVehicleAtTheBorder
I don't face any error, but when i need to have 15 record in my view, because i have 15 id in all my table i got more than 63 ides, what should i manage to get rid of duplicate values?
There are two options:
one is to create a view that aggregates values based on group by, so something like
SELECT t1.something, MAX(t1.somethingElse) as maxElse, MIN(t2.x) as minX
the other is to use RANK (https://learn.microsoft.com/en-us/sql/t-sql/functions/rank-transact-sql) which will return all of the duplicates, but also with rank columns that show their order in the partition. All you have to do then is add another filter:
SELECT * FROM (
SELECT with rank
) t
WHERE t.rank = 1
I think the second one is more correct and more clear, but it depends on your case.
There is a trick to do this in SQL Server. It is really hard to say what the exact syntax is for your query, but you can use top (1) with ties instead of select distinct.
So:
select top (1) with ties . . .
from . . .
. . .
order by row_number() over (partition by <x> order by <y>)
-----------------------------------------^ you'll get one row for each value of this
------------------------------------------------------^ that row will be the first by this
This is really a "hack" solution for what you are doing. If you expect fewer rows, you should fix the underlying query so it returns the rows that you want. It is not clear where those rows come from, and your queries are way to complicated for anyone to figure out.
You will need to simplify, simplify the logic. I would recommend starting with a query that just produces the rows you want. Then add additional joins and columns and other logic to get the columns.
All,
I have multiple left outer join from multiple tables.
My SQL query works fine as below:
select distinct
C.CUST_CD, C.DSC, C.BH_PHONE, A.ADDRESS, CITY.DESCR CITY, ZIP.DESCR ZIP,
CSH.UDT_DT , CSR.DSC
from
dbo.CUST C
inner join dbo.CUST_ADDR A on C.CUST_CD = A.CUST_CD and A.ADDR_CD = 5
left outer join dbo.LOCATION CITY on A.CITY_CD = CITY.LOCATION_CD
left outer join dbo.LOCATION ZIP on A.ZIP_CD = ZIP.LOCATION_CD
left outer join dbo.CUST_STS S on C.CUST_STS_CD = S.CUST_STS_CD
left outer join dbo.CUST_STS_HIS CSH on C.CUST_CD = CSH.CUST_CD
left outer join dbo.CUST_STS_REASON CSR on CSH.REASON_CD = CSR.REASON_CD
where
C.CUST_STS_CD in (5)
and CSH.CUST_STS_CD in (5)
and CSR.STS_CD in (5)
order by C.CUST_CD
My Lambda expression looks like this:
var items = (
from a in db.CUSTs
from b in db.CUST_ADDR
.Where(bb => bb.CUST_CD == a.CUST_CD && bb.ADDR_CD ==5)
from c in db.LOCATION
.Where(cc => cc.LOCATION_CD == b.CITY_CD)
.DefaultIfEmpty() // <== makes outer left join
from d in db.LOCATION
.Where(dd => dd.LOCATION_CD == c.LOCATION_CD)
.DefaultIfEmpty()
from e in db.LOCATION
.Where(ee => ee.LOCATION_CD == b.ZIP_CD)
.DefaultIfEmpty()
from f in db.CUST_STS_HIS
.Where(ff => ff.CUST_STS_CD == a.CUST_STS_CD)
.DefaultIfEmpty()
from g in db.CUST_STS_REASON
.Where(gg => gg.STS_CD == f.CUST_STS_CD)
.DefaultIfEmpty()
where (a.CUST_STS_CD == 5 && f.CUST_STS_CD == 5 && g.STS_CD == 5)
select new
{
CUSTCode = a.CUST_CD,
CUSTDesc = a.DSC,
CUSTPhone = a.BH_PHONE,
CUSTAddr = a.ADDRESS,
CUSTCity = c.DESCR,
CUSTZip = d.DESCR,
CUSTCounty = e.DESCR,
Date = f.UDT_DT,
reason = g.DSC
})
.ToList();
Here's the SQL from the Lamda expression:
SELECT
1 AS [C1],
[Filter1].[CUST_CD1] AS [CUST_CD],
[Filter1].[DSC] AS [DSC],
[Filter1].[BH_PHONE] AS [BH_PHONE],
[Filter1].[ADDRESS] AS [ADDRESS],
[Filter1].[DESCR1] AS [DESCR],
[Filter1].[DESCR2] AS [DESCR1],
[Filter1].[DESCR3] AS [DESCR2],
[Filter1].[UDT_DT] AS [UDT_DT],
[Extent7].[DSC] AS [DSC1]
FROM (SELECT [Extent1].[CUST_CD] AS [CUST_CD1], [Extent1].[DSC] AS [DSC], [Extent1].[BH_PHONE] AS [BH_PHONE], [Extent1].[ADDRESS] AS [ADDRESS], [Extent3].[DESCR] AS [DESCR1], [Extent4].[DESCR] AS [DESCR2], [Extent5].[DESCR] AS [DESCR3], [Extent6].[UDT_DT] AS [UDT_DT], [Extent6].[CUST_STS_CD] AS [CUST_STS_CD1]
FROM [DBO].[CUST] AS [Extent1]
INNER JOIN (SELECT
[CUST_ADDR].[CUST_CD] AS [CUST_CD],
[CUST_ADDR].[ADDR_CD] AS [ADDR_CD],
[CUST_ADDR].[ADDRESS] AS [ADDRESS],
[CUST_ADDR].[CITY_CD] AS [CITY_CD],
[CUST_ADDR].[ZIP_CD] AS [ZIP_CD],
[CUST_ADDR].[STATE_CD] AS [STATE_CD],
[CUST_ADDR].[ADDRESS2] AS [ADDRESS2]
FROM [DBO].[CUST_ADDR] AS [CUST_ADDR]) AS [Extent2] ON ([Extent2].[CUST_CD] = [Extent1].[CUST_CD]) AND (cast(5 as decimal(18)) = [Extent2].[ADDR_CD])
LEFT OUTER JOIN [DBO].[LOCATION] AS [Extent3] ON [Extent3].[LOCATION_CD] = [Extent2].[CITY_CD]
LEFT OUTER JOIN [DBO].[LOCATION] AS [Extent4] ON [Extent4].[LOCATION_CD] = [Extent3].[LOCATION_CD]
LEFT OUTER JOIN [DBO].[LOCATION] AS [Extent5] ON [Extent5].[LOCATION_CD] = [Extent2].[ZIP_CD]
INNER JOIN (SELECT
[CUST_STS_HIS].[CUST_CD] AS [CUST_CD],
[CUST_STS_HIS].[UDT_DT] AS [UDT_DT],
[CUST_STS_HIS].[REASON_CD] AS [REASON_CD],
[CUST_STS_HIS].[CUST_STS_CD] AS [CUST_STS_CD],
[CUST_STS_HIS].[USR_ID] AS [USR_ID],
[CUST_STS_HIS].[NOTES] AS [NOTES]
FROM [DBO].[CUST_STS_HIS] AS [CUST_STS_HIS]) AS [Extent6] ON [Extent6].[CUST_STS_CD] = [Extent1].[CUST_STS_CD]
WHERE (cast(5 as decimal(18)) = [Extent1].[CUST_STS_CD]) AND (cast(5 as decimal(18)) = [Extent6].[CUST_STS_CD]) ) AS [Filter1]
INNER JOIN (SELECT
[CUST_STS_REASON].[STS_CD] AS [STS_CD],
[CUST_STS_REASON].[REASON_CD] AS [REASON_CD],
[CUST_STS_REASON].[DSC] AS [DSC],
[CUST_STS_REASON].[RES_KEY] AS [RES_KEY]
FROM [DBO].[CUST_STS_REASON] AS [CUST_STS_REASON]) AS [Extent7] ON [Extent7].[STS_CD] = [Filter1].[CUST_STS_CD1]
I am getting my SQL query results as expected (about 300 or so records). However, my lambda is bringing all records (over 100k). It is complete ignoring my "where" clause in the statement.
Could you please point me in the right direction on this query?
I appreciate your time and consideration.
Well, since there is more than one way of skinning a cat, I ended up creating, executing a Stored Procedure that solved the problem and returned only the data that I needed. Thank you for all your responses though. I really learned something new here. I appreciate!
I have a problem with a dynamic parameter in Power Query. There's the code:
let
Parametro = Excel.CurrentWorkbook(){[Name="Parametro"]}[Content],
InicioExec_Valor = Parametro{0}[Valor],
FimExec_Valor = Parametro{1}[Valor],
Fonte = Sql.Database("DATABASE", "TABLE", [Query="select#(lf)#(lf)o.cd_controle, exe.nm_pessoa AS Executante, o.numero AS OM, #(lf)CONVERT(nvarchar(10), o.dt_abertura, 103) AS Abertura,#(lf)o.medidor Horimetro_OM,#(lf)p.nm_pessoa AS Cliente, #(lf)e.nm_equipto AS Equipamento, pat.nr_patrimonio AS Patrimonio, #(lf)CONVERT(nvarchar(10), o.dt_autoriz_execucao, 103) AS Inicio_Exec, #(lf)CONVERT(nvarchar(10), o.dt_encos_oficina, 103) AS Fim_Exec, Z.nm_apelido AS Unidade, #(lf)CASE WHEN fl_preventiva = 'C' THEN 'Corretiva' #(lf)WHEN fl_preventiva = 'P' then 'PREVENTIVA'#(lf)WHEN fl_preventiva = 'R' then 'INSPEÇÃO RESUMIDA'#(lf)WHEN fl_preventiva = 'V' then 'INSPEÇÃO PREVENTIVA'#(lf)WHEN fl_preventiva = 'E' then 'ENTREGA TÉCNICA'#(lf)else 'Indefinido' end AS 'Corret_Preven'#(lf),CONVERT(nvarchar(10), fl_remessa.dt_saida, 103) AS DataSaida#(lf),fl_rem_equ.vl_medidor Horimetro_Remessa#(lf),CONVERT(nvarchar(10), o.dt_abertura, 103) AS Abertura#(lf),o.medidor Horimetro_OM#(lf)#(lf)from orcos o#(lf)inner join controle c on (c.cd_controle = o.cd_controle)#(lf)inner join wcore_oid oid on (oid.cd_oid = c.cd_oid)#(lf)left outer join empresa AS Z ON Z.cd_empresa = o.cd_empresa #(lf)left outer join equipto e on (e.cd_equipto = o.cd_equipto)#(lf)left outer join pessoa f on (f.cd_pessoa = o.cd_pessoa_tec)#(lf)left outer join pessoa p on (p.cd_pessoa = o.cd_pessoa)#(lf)left outer join pessoa exe on (exe.cd_pessoa = o.cd_pessoa_exe)#(lf)left outer join patrimon pat on (pat.cd_patrimonio = o.cd_patrimonio)#(lf)left outer join est_almox x on x.cd_almox = pat.cd_almox#(lf)left outer join empresa emp on emp.cd_empresa = o.cd_empresa_origem #(lf)#(lf)left outer JOIN dbo.fich_loc ON (fich_loc.cd_controle= o.cd_controle_loc)#(lf)left outer JOIN dbo.fl_remessa fl_remessa ON (fl_remessa.cd_controle = dbo.fich_loc.cd_controle)#(lf)#(lf)INNER JOIN(select max(fl_remessa.cd_flremessa)cd_flremessa, A.cd_controle #(lf)#(tab)#(tab)#(tab)#(tab)#(tab) from dbo.fl_remessa#(lf)#(tab)#(tab)#(tab)#(tab)#(tab) inner join dbsislocsalvador..fich_loc on (fl_remessa.cd_controle = dbo.fich_loc.cd_controle)#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) inner join dbsislocsalvador..orcos a on (fich_loc.cd_controle= A.cd_controle_loc#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) and fl_remessa.dt_saida<=a.dt_abertura) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) inner join controle c on (c.cd_controle = A.cd_controle)#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) left outer join patrimon pat on (pat.cd_patrimonio = a.cd_patrimonio)#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) LEFT OUTER JOIN dbo.fl_rem_equ AS fl_rem_equ ON pat.cd_patrimonio = fl_rem_equ.cd_patrimonio AND fl_remessa.cd_flremessa = fl_rem_equ.cd_flremessa#(lf) #(tab)#(tab) left outer JOIN dbo.loc_flremequ_xplano AS loc_flremequ_xplano ON loc_flremequ_xplano.cd_flremequ = fl_rem_equ.cd_flremequ #(lf) #(tab)#(tab)#(tab) left outer JOIN dbo.equipto AS equipto ON fl_rem_equ.cd_equipto = equipto.cd_equipto#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) where #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) ((NOT EXISTS(select top 1 * from config_tag_xoid)) OR (c.cd_oid not in (select txo.cd_oid from #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) (select * from ( select cd_tag, nm_tag , (30) as fl_acesso from config_tag t where t.fl_ativo in ('S') ) tags #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) WHERE (fl_acesso = 10) ) tags inner join config_tag_xoid txo on tags.cd_tag = txo.cd_tag where 3=3 /*filter_tag_clause*/ #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) group by txo.cd_oid))) and ( ( (a.cd_controle_loc is not null and a.cd_fldevolucao is null) ) ) and#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) (a.cd_empresa IN (24,45,5,46,20,29,43,15,48,10,1,22,8,34,49,9,47,52,7)) and ( ( a.cd_local is null or a.cd_local in (0,1,2,3,5,6,7,8,9) ) ) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) AND (a.cd_controle_loc IS NOT NULL) AND (a.cd_fldevolucao IS NULL) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) and (a.fl_preventiva in ('C', 'P', 'R', 'V', 'E')) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) and (equipto.cd_grupo in (1478, 1480, 1481, 1482, 1483, 1484, 1485, 1486, 1487, 1488, 1491, 1492, 1548, 1549))#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) group by a.cd_controle) AS fl_remessa_max #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) ON fl_remessa.cd_flremessa = fl_remessa_max.cd_flremessa#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) and fl_remessa_max.cd_controle = o.cd_controle #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)LEFT OUTER JOIN dbo.fl_rem_equ AS fl_rem_equ ON pat.cd_patrimonio = fl_rem_equ.cd_patrimonio AND fl_remessa.cd_flremessa = fl_rem_equ.cd_flremessa#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)left outer JOIN dbo.equipto AS equipto ON fl_rem_equ.cd_equipto = equipto.cd_equipto#(lf) #(tab)#(tab)#(tab)#(tab)#(tab) #(lf)WHERE ((NOT EXISTS(select top 1 * from config_tag_xoid)) OR (c.cd_oid not in (select txo.cd_oid from #(lf)(select * from ( select cd_tag, nm_tag , (30) as fl_acesso from config_tag t where t.fl_ativo in ('S') ) tags #(lf)WHERE (fl_acesso = 10) ) tags inner join config_tag_xoid txo on tags.cd_tag = txo.cd_tag where 3=3 /*filter_tag_clause*/ #(lf)group by txo.cd_oid))) and ( ( (o.cd_controle_loc is not null and o.cd_fldevolucao is null) ) ) and#(lf)(o.cd_empresa IN (24,45,5,46,20,29,43,15,48,10,1,22,8,34,49,9,47,52,7)) and ( ( o.cd_local is null or o.cd_local in (0,1,2,3,5,6,7,8,9) ) ) #(lf)AND (o.cd_controle_loc IS NOT NULL) AND (o.cd_fldevolucao IS NULL) #(lf)and (o.fl_preventiva in ('C', 'P', 'R', 'V', 'E')) #(lf)and (equipto.cd_grupo in (1478, 1480, 1481, 1482, 1483, 1484, 1485, 1486, 1487, 1488, 1491, 1492, 1548, 1549))"]),
#"Tipo Alterado" = Table.TransformColumnTypes(Fonte,{{"Abertura", type date}, {"Inicio_Exec", type date}, {"Fim_Exec", type date}, {"DataSaida", type date}}),
#"Colunas Removidas" = Table.RemoveColumns(#"Tipo Alterado",{"cd_controle", "Horimetro_OM2", "Abertura2"}),
#"Filtro Datas" = Table.SelectRows(#"Colunas Removidas", each [Fim_Exec] >= InicioExec_Valor and [Fim_Exec] <= FimExec_Valor)
in
#"Filtro Datas"
And occur that error:
Expression.Error: Não conseguimos aplicar o operador < aos tipos
Number e Date. Detalhes:
Operator=<
Left=42795
Right=01/06/2007
How can I solve that?
Note: MY PARAMETER (01/03/2017) ARE FORMATED AS TEXT.
Although I don't read Spanish, looks like it says you cannot compare number and date with < operator.
It is good practice, by the way, to first convert parameters to the proper type:
InicioExec_Valor = Date.From(Parametro{0}[Valor]),
FimExec_Valor = Date.From(Parametro{1}[Valor]),`
Try this.
If it won't work, determine step that generates error by clicking them one-by-one.
There is more I wonder about. Why do you convert Fim_Exec to nvarchar and then to date?
1. #(lf)CONVERT(nvarchar(10), o.dt_encos_oficina, 103) AS Fim_Exec
2. {"Fim_Exec", type date},
Why don't use Fim_Exec = CAST(o.dt_encos_oficina as date)? (or datetime, depending on SQL Server version) in the query?
Same applies to other columns.
Next, it is best practice not to use native queries unless absolutely required.
And, yes, this is largest and most complex query I've seen up to date. :)
I am trying to figure out how to do a right join in vb.net I have tried several different approaches but neither works.
Dim query = From I In db.scll_label Where I.scll_transactiondate >= fromDate And I.scll_transactiondate <= toDate
Join p In db.pt_mstr.Where(Function(pt) pt.pt_domain = "mueller") On I.scll_part Equals p.pt_part
Join c In db.sclws_cfg.Where(Function(wk) wk.sclws_domain = "mueller") On I.scll_wsid Equals c.sclws_id
Select New ShiftAdjustedModel With
{.Label = I,
.TransactionDate = I.scll_transactiondate,
.PartLength = p.pt_length,
.PartNetWeight = p.pt_net_wt,
.PartDescription = p.pt_desc1,
.PartTolHigh = p.pt_tol_high,
.PartType = p.pt_part_type,
.PartUM = p.pt_um,
.ProjCode = c.sclws_proj_code,
.Site = c.sclws_site}
output sql
SELECT
[Extent1].[scll_ticket] AS [scll_ticket],
[Extent1].[scll_domain] AS [scll_domain],
[Extent1].[scll_site] AS [scll_site],
[Extent1].[scll_part] AS [scll_part],
[Extent1].[scll_qty] AS [scll_qty],
[Extent1].[scll_weight] AS [scll_weight],
[Extent1].[scll_transactiondate] AS [scll_transactiondate],
[Extent1].[scll_transactiontime] AS [scll_transactiontime],
[Extent1].[scll_shift] AS [scll_shift],
[Extent1].[scll_pcs_bundle] AS [scll_pcs_bundle],
[Extent1].[scll_bundle_lift] AS [scll_bundle_lift],
[Extent1].[scll_cust] AS [scll_cust],
[Extent1].[scll_wsid] AS [scll_wsid],
[Extent1].[scll_userid] AS [scll_userid],
[Extent1].[scll_remarks] AS [scll_remarks],
[Extent1].[scll_calc_weight] AS [scll_calc_weight],
[Extent1].[scll_total_feet] AS [scll_total_feet],
[Extent1].[scll_tolerance] AS [scll_tolerance],
[Extent1].[scll_drawlite_factor] AS [scll_drawlite_factor],
[Extent1].[scll_total_tare] AS [scll_total_tare],
[Extent1].[scll_tare_detail] AS [scll_tare_detail],
[Extent1].[scll_out_of_tolerance] AS [scll_out_of_tolerance],
[Extent1].[scll_tolerance_low] AS [scll_tolerance_low],
[Extent1].[scll_tolerance_high] AS [scll_tolerance_high],
[Extent1].[scll_std_weight] AS [scll_std_weight],
[Extent2].[pt_length] AS [pt_length],
[Extent2].[pt_net_wt] AS [pt_net_wt],
[Extent2].[pt_desc1] AS [pt_desc1],
[Extent2].[pt_tol_high] AS [pt_tol_high],
[Extent2].[pt_part_type] AS [pt_part_type],
[Extent2].[pt_um] AS [pt_um],
[Extent3].[sclws_proj_code] AS [sclws_proj_code],
[Extent3].[sclws_site] AS [sclws_site]
FROM [dbo].[scll_label] AS [Extent1]
INNER JOIN [dbo].[pt_mstr] AS [Extent2] ON [Extent1].[scll_part] = [Extent2].[pt_part]
INNER JOIN [dbo].[sclws_cfg] AS [Extent3] ON [Extent1].[scll_wsid] = [Extent3].[sclws_id]
WHERE ([Extent1].[scll_transactiondate] >= #p__linq__0) AND ([Extent1].[scll_transactiondate] <= #p__linq__1) AND ('mueller' = [Extent2].[pt_domain]) AND ('mueller' = [Extent3].[sclws_domain])
if i use this query
Dim query = From I In db.scll_label Where I.scll_transactiondate >= fromDate And I.scll_transactiondate <= toDate
Group Join p In db.pt_mstr.Where(Function(pt) pt.pt_domain = "mueller") On I.scll_part Equals p.pt_part Into parts = Group
Group Join c In db.sclws_cfg.Where(Function(wk) wk.sclws_domain = "mueller") On I.scll_wsid Equals c.sclws_id Into workstations = Group
From p In parts.DefaultIfEmpty From c In workstations.DefaultIfEmpty
Select New ShiftAdjustedModel With
{.Label = I,
.TransactionDate = I.scll_transactiondate,
.PartLength = p.pt_length,
.PartNetWeight = p.pt_net_wt,
.PartDescription = p.pt_desc1,
.PartTolHigh = p.pt_tol_high,
.PartType = p.pt_part_type,
.PartUM = p.pt_um,
.ProjCode = c.sclws_proj_code,
.Site = c.sclws_site}
i get this output
SELECT
[Extent1].[scll_ticket] AS [scll_ticket],
[Extent1].[scll_domain] AS [scll_domain],
[Extent1].[scll_site] AS [scll_site],
[Extent1].[scll_part] AS [scll_part],
[Extent1].[scll_qty] AS [scll_qty],
[Extent1].[scll_weight] AS [scll_weight],
[Extent1].[scll_transactiondate] AS [scll_transactiondate],
[Extent1].[scll_transactiontime] AS [scll_transactiontime],
[Extent1].[scll_shift] AS [scll_shift],
[Extent1].[scll_pcs_bundle] AS [scll_pcs_bundle],
[Extent1].[scll_bundle_lift] AS [scll_bundle_lift],
[Extent1].[scll_cust] AS [scll_cust],
[Extent1].[scll_wsid] AS [scll_wsid],
[Extent1].[scll_userid] AS [scll_userid],
[Extent1].[scll_remarks] AS [scll_remarks],
[Extent1].[scll_calc_weight] AS [scll_calc_weight],
[Extent1].[scll_total_feet] AS [scll_total_feet],
[Extent1].[scll_tolerance] AS [scll_tolerance],
[Extent1].[scll_drawlite_factor] AS [scll_drawlite_factor],
[Extent1].[scll_total_tare] AS [scll_total_tare],
[Extent1].[scll_tare_detail] AS [scll_tare_detail],
[Extent1].[scll_out_of_tolerance] AS [scll_out_of_tolerance],
[Extent1].[scll_tolerance_low] AS [scll_tolerance_low],
[Extent1].[scll_tolerance_high] AS [scll_tolerance_high],
[Extent1].[scll_std_weight] AS [scll_std_weight],
[Extent2].[pt_length] AS [pt_length],
[Extent2].[pt_net_wt] AS [pt_net_wt],
[Extent2].[pt_desc1] AS [pt_desc1],
[Extent2].[pt_tol_high] AS [pt_tol_high],
[Extent2].[pt_part_type] AS [pt_part_type],
[Extent2].[pt_um] AS [pt_um],
[Extent3].[sclws_proj_code] AS [sclws_proj_code],
[Extent3].[sclws_site] AS [sclws_site]
FROM [dbo].[scll_label] AS [Extent1]
LEFT OUTER JOIN [dbo].[pt_mstr] AS [Extent2] ON ('mueller' = [Extent2].[pt_domain]) AND ([Extent1].[scll_part] = [Extent2].[pt_part])
LEFT OUTER JOIN [dbo].[sclws_cfg] AS [Extent3] ON ('mueller' = [Extent3].[sclws_domain]) AND ([Extent1].[scll_wsid] = [Extent3].[sclws_id])
WHERE ([Extent1].[scll_transactiondate] >= #p__linq__0) AND ([Extent1].[scll_transactiondate] <= #p__linq__1)
I simply want a right outer join on the two tables it translated into a left outer join. Even if I start with the tables for the right join as a lot of posts have suggested I get weird sql using cross joins and unions instead of right join.