I want to left join the 2 tables below (h and he) to the main table but so I only have one field [mpxn] rather than 2 [h.mpxn & he.mpxn]
Is there away to do this? there shouldn't be any instances where there is a result in both h and he tables.
SELECT distinct [Interaction ID]
,[BP Number]
,he.mpxn
FROM [DOMCustomers].[Bart].[OpenComplaints] as c
Left join DOMCustomers.Bart.MeterHealth as h on c.[BP Number]=h.[Business Partner]
Left join DOMCustomers.Bart.ENAMeterHealth as he on c.[BP Number]=he.[Business Partner]
Not sure if you are asking for the 2 column values to be concatenated together, then joined to the main table - or
if you are trying to combine the results of the 2 tables (h, he) into one result in one column, then join that to the table?
Anyway, here is some examples of how to achieve what I think you may be asking:
CREATE TABLE h
(
mpxn VARCHAR(100)
,[Business Partner] VARCHAR(100)
);
CREATE TABLE he
(
mpxn VARCHAR(100)
,[Business Partner] VARCHAR(100)
);
CREATE TABLE OpenComplaints
(
[Interaction ID] VARCHAR(100)
,[BP Number] VARCHAR(100)
);
INSERT INTO h VALUES ('h',1);
INSERT INTO he VALUES ('he',1);
INSERT INTO OpenComplaints VALUES ('1',1);
Now for some queries.
This one does a union, then joins to the main table:
;WITH a AS (
SELECT
h.mpxn
,h.[Business Partner]
FROM h
UNION ALL
SELECT
he.mpxn
,he.[Business Partner]
FROM he
)
SELECT
c.[Interaction ID]
,c.[BP Number]
,h.mpxn
FROM OpenComplaints c
left join a AS h on c.[BP Number]=h.[Business Partner]
[
This one concatenates the columns together, then joins:
;WITH b AS (
SELECT
h.mpxn+' '+he.mpxn AS mpxn
,h.[Business Partner]
FROM h
JOIN he ON h.[Business Partner] =he.[Business Partner] )
SELECT
c.[Interaction ID]
,c.[BP Number]
,h.mpxn
FROM OpenComplaints c
left join b AS h on c.[BP Number]=h.[Business Partner]
Here is a SQL Fiddle example
You could UNION the two tables together prior to the LEFT JOIN
with mpxn_cte([BP Number], mpxn) as (
select [BP Number], mpxn from DOMCustomers.Bart.MeterHealth
union
select [BP Number], mpxn from DOMCustomers.Bart.ENAMeterHealth)
select distinct c.[Interaction ID]
,c.[BP Number]
m.mpxn
from M [DOMCustomers].[Bart].[OpenComplaints] as c
left jion mpxn_cte m on c.[BP Number]=m.[BP Number];
With the assumption that there are never overlapping entries in MeterHealth and ENAMeterHealth this is probably the query result expected:
SELECT
opencomplaint.[Interaction ID],
opencomplaint.[BP Number],
Coalesce( health.mxpn, enahealth.mpxn, 0) AS mxpn
FROM
[DOMCustomers].[Bart].[OpenComplaints] AS opencomplaint
LEFT OUTER JOIN DOMCustomers.Bart.MeterHealth AS health
ON opencomplaint.[BP Number] = health.[Business Partner]
LEFT OUTER JOIN DOMCustomers.Bart.ENAMeterHealth AS enahealth
ON opencomplaint.[BP Number] = enahealth.[Business Partner]
GROUP BY
opencomplaint.[Interaction ID],
opencomplaint.[BP Number],
Coalesce( health.mxpn, enahealth.mpxn, 0) AS mxpn
;
Couple of points:
Using column names with spaces is counterproductive for SQL code reading
GROUP BY is always more secure than DISTINCT
Using one or two letter aliases might work up to three tables, after that and with more complicated transformations reading code becomes an exercise in "I do not like my colleagues at all"
Use semicolons to terminate SQL statements - again this makes code easier to read (caveat emptor: some tools just can not handle standard ANSI SQL after 20 years!)
Keep joins at same level on same level of indentation
The Coalesce works best with a default fallback value that instinctively tells the user that no value from the outer joins came back - 0 in this case
Coalesce introduces a first come, first serve transformation rule - best comment your business logic above the use of the function why it was written like it is
Related
I have a problem with my query. I have a simple example here that illustrates the code I have.
SELECT distinct ID
FROM Table
WHERE IteamNumber in (132,434,675) AND Year(DateCreated) = 2019
AND ID NOT IN (
SELECT Distinct ID FROM Table
WHERE IteamNumber in (132,434,675) AND DateCreated < '2019-01-01')
As you can see, I'm retrieving unique data id's that has been created in 2019 and not earlier.
The select statements works fine, but once I use the NOT IN statement, the query could easily go 1 minute plus.
My other question could this be related to the computer/server performance that is running the SQL Server for Microsoft Business Central? Because the same query worked perfectly after all even with the (NOT IN) statement, but that was in Microsoft dynamics C5 SQL Server.
So my question is there something wrong with my query or is it mainly a server issue?
UPDATE: here is a real example: this takes 25 seconds to retrieve 500 rows
Select count(distinct b.No_),'2014'
from [Line] c
inner join [Header] a
on a.CollectionNo = c.CollectionNo
Inner join [Customer] b
on b.No_ = a.CustomerNo
where c.No_ in('2101','2102','2103','2104','2105')
and year(Enrollmentdate)= 2014
and(a.Resignationdate < '1754-01-01 00:00:00.000' OR a.Resignationdate >= '2014-12-31')
and NOT EXISTS(Select distinct x.No_
from [Line] c
inner join [Header] a
on a.CollectionNo = c.CollectionNo
Inner join [Customer] x
on x.No_ = a.CustomerNo
where x.No_ = b.No_ and
c.No_ in('2101','2102','2103','2104','2105')
and Enrollmentdate < '2014-01-01'
and(a.Resignationdate < '1754-01-01 00:00:00.000' OR a.Resignationdate > '2014-12-31'))
If I understand correctly you can write the query as a GROUP BY query with a HAVING clause:
SELECT ID
FROM t
WHERE IteamNumber in (132, 434, 675)
GROUP BY ID
HAVING MIN(DateCreated) >= '20190101' -- no row earlier than 2019
AND MIN(DateCreated) < '20200101' -- at least one row less than 2020
This will remove rows for which an earlier record exists. You can further improve the performance by creating a covering index:
CREATE INDEX IX_t_0001 ON t (ID) INCLUDE (IteamNumber, DateCreated)
I usually prefer JOINs than INs, you can get the same result but the engine tends be able to optimize it better.
You join your main query (T1) with what was the IN subquery (T2), and you filter that T2.ID is null, ensuring that you haven't found any record matching those conditions.
SELECT distinct T1.ID
FROM Table T1
LEFT JOIN Table T2 on T2.ID = T1.ID AND
T2.IteamNumber in (132,434,675) AND T2.DateCreated < '2019-01-01'
WHERE T1.IteamNumber in (132,434,675) AND Year(T1.DateCreated) = 2019 AND
T2.ID is null
UPDATE: Here is the proposal updated with your real query. Since your subquery has inner joins, I have created a CTE so you can left join that subquery. The functioning is the same, you left join your main query with the subquery and you return only the rows with no matching records found on the subquery.
with previous as (
Select x.No_
from [Line] c
inner join [Header] a on a.CollectionNo = c.CollectionNo
inner join [Customer] x on x.No_ = a.CustomerNo
where c.No_ in ('2101','2102','2103','2104','2105')
and Enrollmentdate < '2014-01-01'
and (a.Resignationdate < '1754-01-01 00:00:00.000' OR a.Resignationdate > '2014-12-31'))
)
Select count(distinct b.No_),'2014'
from [Line] c
inner join [Header] a on a.CollectionNo = c.CollectionNo
inner join [Customer] b on b.No_ = a.CustomerNo
left join previous p on p.No_ = b.No_
where c.No_ in ('2101','2102','2103','2104','2105')
and year(Enrollmentdate)= 2014
and (a.Resignationdate < '1754-01-01 00:00:00.000' OR a.Resignationdate >= '2014-12-31')
and p.No_ is null
Issue is because of your IN statement, it is preferred in my opinion to avoid any IN statement rather then this, create join with subquery and filter out your data using where clause.
In case of IN statement each record of your table mapped with all the records of subquery, which definitely slows down your process.
If it is mandatory to use IN clause then use it with index. Create proper index of your respected columns, which improve your performance.
Instead of IN you may use EXISTS to increase the performance of your query.
Example of EXISTS is :
SELECT distinct ID
FROM Table AS T
WHERE IteamNumber in (132,434,675) AND Year(DateCreated) = 2019
AND NOT EXISTS (
SELECT Distinct ID FROM Table AS T2
WHERE T1.ID=T2.ID
AND IteamNumber in (132,434,675) AND DateCreated < '2019-01-01' )
I am using a complex query. I need to returns me always a row even it doesnt find anything.
SELECT
a.InventoryItemID,
a.Name,
a.RetailPrice,
b.MainGroupItemCode,
b.MainGroupItemID,
c.VatValue,
a.Code,
a.Weight,
b.MainGroupItemName,
a.RetailPrice2,
a.FreePrice,
case when isnull(e.IsActive,0)=1 and isnull(d.price,0)!=0 then d.Price else RetailPrice End as CustomPrice
from InventoryMaster a
join InventoryMainGroupItems b on a.MainGroupItemID=b.MainGroupItemID
join VatCodes c on b.VatCodeID=c.VatCodeID
join InventoryPrices d on d.InventoryItemID=a.InventoryItemID
join InventoryCatalog e on e.CatalogID=d.CatalogID
where a.InventoryItemID=2 and ISNULL(e.catalogID,1)=3
The problem is in last line ISNULL(e.catalogID,1)=3. In my table it doesn't exist CatalogID with number 3.
So it doesnt returns me anything, but there is CatalogID with number 1. I have set that if is null to return me 1, Unfortunately i dont get any row back from my query. How can i fix this ?
My question has been solved i just want to add one more join table with one wheere condition isnide
SELECT *
from
(
SELECT t1.ID,
t1.Name,
COALESCE(t2.price,t1.Price) AS price ,
Row_number() OVER(partition BY t1.ID ORDER BY t1.ID) rn
FROM InventoryMaster t1
LEFT JOIN inventoryprices t2
ON t1.ID=t2.ID
LEFT join InventoryCatalog t3
ON t3.ID=t2.ID and t3.ID=2
where t1.ID=2
) t
WHERE t.rn=1
it returns me always the retailprice from First Table Inventory
Add 3 cols near beginning of the select
d.InventoryItemID,
d.CatalogID,
e.CatalogID
Then remove And ISNULL from the Where, and run to see what you get.
It may be that
join InventoryCatalog e
needs
ON d.CatalogID=ISNULL(e.catalogID,1)
I have the following code:
IF (OBJECT_ID('tempdb..#Data') IS NOT NULL)
BEGIN
DROP TABLE #Data
END
SELECT
t.Name, x.Time, x.Date, x.Total,
xo.DrvCommTotal, x.Name2, x.Street, x.Zip,
r.Route1
INTO
#Data
FROM
table1 xo WITH(NOLOCK)
LEFT JOIN
Table2 t WITH(NOLOCK) ON t.ID = x.ID
LEFT JOIN
Route1 r ON r.RouteID = x.RouteID
WHERE
x.Client = 1
AND x.Date = '9/13/2018'
GROUP BY
t.Name, x.Time, x.Date, x.Total, xo.DrvCommTotal, x.Name2,
x.Street, x.Zip, r.Route1
ORDER BY
Route1
SELECT DISTINCT
F.*, F2.NumOrders
FROM
#Data F
LEFT JOIN
(SELECT
Route1, COUNT(*) NumOrders
FROM
#Data
GROUP BY
Route1) F2 ON F2.Route1 = F.Route1
LEFT OUTER JOIN
(SELECT
Street + ',' + Zip Stops, Time, RouteN1
FROM
#Data
GROUP BY
RouteNo1, street, Zip) F3 ON F3.Route1 = F.Route1
WHERE
F.Route1 IS NOT NULL
ORDER BY
F.Route1
and it provides me with a list of routes and stops. The column NumOrders lets me know how many orders are on each route. I need the stops to become individual columns I will label Stop1, Stop2, etc. so that each route is only one row and all the information is contained on the row for one route.
I'm currently using the temp table because the data is so large. I can play with my SELECT statement without having to re-run the entire code.
How do I move the stops for each route into columns?
Hum.. Not quite sure I understand the question but it sounds that you want to pivot the data so that the routes break into columns. If so, I would use a sql Pivot. Here is an example from the documentation:
USE AdventureWorks2014;
GO
SELECT VendorID, [250] AS Emp1, [251] AS Emp2, [256] AS Emp3, [257] AS Emp4, [260] AS Emp5
FROM
(SELECT PurchaseOrderID, EmployeeID, VendorID
FROM Purchasing.PurchaseOrderHeader) p
PIVOT
(
COUNT (PurchaseOrderID)
FOR EmployeeID IN
( [250], [251], [256], [257], [260] )
) AS pvt
ORDER BY pvt.VendorID;
Also, here is the link to how to use pivot: https://learn.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-2017
Since you already have all the data in your temp table, you could pivot that on the way out.
I have the following code to insert data from a table in another database, but how can I insert into a primary key column by incrementing the last record in ID column value by 1? In my case [WORK ORDER #] is a primary key it doesn't allow null.
[WORK ORDER #] is nvarchar(10)
INSERT INTO DB1.dbo.WORKORDERS ([WORK ORDER #], [CUSTOMER], [SO DATE], [SO NUMBER])
SELECT *
FROM OPENQUERY([DB29],
'SELECT DISTINCT
NULL, --need to set auto increment value here
Customers.Customer_Bill_Name,
JrnlHdr.TransactionDate,
JrnlHdr.Reference)
FROM Customers
INNER JOIN JrnlHdr ON Customers.CustomerRecordNumber = JrnlHdr.CustVendId
WHERE JrnlHdr.JrnlKey_Journal = 11
AND JrnlHdr.TransactionDate = CURDATE()
-------------------// i tried as follows-----
--> You only do this one time...not with each query
create sequence dbo.WorkOrderSequence
as int
start with 43236
--> I took out the part that failed (you got option 1 and 3 kinda
--> mashed together)
insert DB1.dbo.WORKORDERS
([WORK ORDER #],[CUSTOMER],[SO DATE],[SO NUMBER],[ASSY PN-S],[CUSTOMER PN],[SHIP VIA],[PROMISED DATE],[COMMENTS],[PO #],[WO Notes])
select
convert(varchar(10), next value for DB1.dbo.WorkOrderSequence ),
x.Customer_Bill_Name,
x.TransactionDate,
x.Reference,
x.ItemID,
x.PartNumber,
x.WhichShipVia,
x.ShipByDate,
x.Comment2,
x.CustomerInvoiceNo,
x.SalesDescription
from
openquery
([DB29],
'select distinct
Customers.Customer_Bill_Name,
JrnlHdr.TransactionDate,
JrnlHdr.Reference,
LineItem.ItemID ,
LineItem.PartNumber,
Customers.WhichShipVia,
JrnlHdr.ShipByDate,
JrnlHdr.Comment2,
JrnlHdr.CustomerInvoiceNo,
LineItem.SalesDescription
FROM Customers
INNER JOIN JrnlHdr
ON Customers.CustomerRecordNumber = JrnlHdr.CustVendId
LEFT OUTER JOIN Address
ON Customers.CustomerRecordNumber = Address.CustomerRecordNumber
INNER JOIN JrnlRow
ON JrnlHdr.PostOrder = JrnlRow.PostOrder
INNER JOIN LineItem
ON JrnlRow.ItemRecordNumber = LineItem.ItemRecordNumber
WHERE JrnlHdr.JrnlKey_Journal = 11 AND JrnlHdr.TransactionDate = CURDATE()
AND JrnlHdr.PostOrder = JrnlRow.PostOrder
AND JrnlHdr.CustVendId = Customers.CustomerRecordNumber
AND JrnlRow.ItemRecordNumber = LineItem.ItemRecordNumber
AND JrnlHdr.POSOisClosed = 0'
) as x
Option 1
If you're on at least SQL Server 2012 (you didn't mention a specific version), you have a general sequence number generator that you can use. I like it a lot for this kind of scenario. In the DB1 database, you'd add your sequence like this:
create sequence dbo.WorkOrderSequence
as int
start with 5002230 --> pick a starting number greater
--> than any existing [WorkOrder #]
Then, you can just get the next number(s) in your insert-for-select statement:
insert DB1.dbo.WORKORDERS
([WORK ORDER #], [CUSTOMER], [SO DATE], [SO NUMBER])
select
convert(varchar(10), next value for DB1.dbo.WorkOrderSequence ),
x.Customer_Bill_Name,
x.TransactionDate,
x.Reference
from
openquery
([DB29],
'select distinct
Customers.Customer_Bill_Name,
JrnlHdr.TransactionDate,
JrnlHdr.Reference
from
Customers
inner join
JrnlHdr on
Customers.CustomerRecordNumber = JrnlHdr.CustVendId
where
JrnlHdr.JrnlKey_Journal = 11
and
JrnlHdr.TransactionDate = CURDATE()'
) as x
The sequence is a standalone auto-incrementing number. Every time you use the next value for dbo.WorkOrderSequence, it auto-increments. This way, you don't have to modify any table definitions.
Option 2
Alternatively, you could alter the DB1.dbo.WORKORDERS table so that the default value to use the expression...
alter table dbo.WORKORDERS
alter column [Work Order #] nvarchar(10) not null
default( convert( nvarchar(10), next value for dbo.WorkOrderSequence ) )
If you do this, then you can completely omit inserting the [Work Order #] altogether and let the default do the magic.
Option 3
If you're not on 2012, but on at least 2008, you can still get there...but it's a little trickier because you have to get the current starting [Work Order #]:
insert DB1.dbo.WORKORDERS
([WORK ORDER #], [CUSTOMER], [SO DATE], [SO NUMBER])
select
convert(varchar(10), x.RowNum + y.MaxOrderNum ),
x.Customer_Bill_Name,
x.TransactionDate,
x.Reference
from
openquery
([DB29],
'select distinct
row_number() over( order by JrnlHdr.TransactionDate ) as RowNum,
Customers.Customer_Bill_Name,
JrnlHdr.TransactionDate,
JrnlHdr.Reference
from
Customers
inner join
JrnlHdr on
Customers.CustomerRecordNumber = JrnlHdr.CustVendId
where
JrnlHdr.JrnlKey_Journal = 11
and
JrnlHdr.TransactionDate = CURDATE()'
) as x
cross join
(
select
convert( int, max( [Work Order #] ) ) as MaxOrderNum
from
Db1.dbo.WORKORDERS
) as y
Option 4
If you're on something earlier than 2008...you'll probably want a stored procedure to do the insert in two steps: inserting the work orders into a temporary table (one with an auto-increment [Work Order #] starting at the current table's max( [Work Order #] ) + 1 )...and then step 2 would insert the temp table into WORKORDERS with a convert.
I've dabbled a little with SQL but not to create tables as much. I have used postgreSQL along with knex to do so and I believe the solution that should fit for your needs as well is using the value unique. I apologize if that's not correct since I am junior to coding, but hopefully looking at this will help or looking at the SQL docs :) difference between primary key and unique key
The suggested answer, in this post, works great for two columns.
I have about 50 different date columns, where I need to be able to report on the most recent interaction, regardless of table.
In this case, I am bringing the columns in to a view, since they are coming from different tables in two different databases...
CREATE VIEW vMyView
AS
SELECT
comp_name AS Customer
, Comp_UpdatedDate AS Last_Change
, CmLi_UpdatedDate AS Last_Communication
, Case_UpdatedDate AS Last_Case
, AdLi_UpdatedDate AS Address_Change
FROM Company
LEFT JOIN Comm_Link on Comp_CompanyId = CmLi_Comm_CompanyId
LEFT JOIN Cases ON Comp_CompanyId = Case_PrimaryCompanyId
LEFT JOIN Address_Link on Comp_CompanyId = AdLi_CompanyID
...
My question is, how I would easily account for the many possibilities of one column being greater than the others?
Using only the two first columns, as per the example above, works great. But considering that one row could have column 3 as the highest value, another row could have column 14 etc...
SELECT Customer, MAX(CASE WHEN (Last_Change IS NULL OR Last_Communication> Last_Change)
THEN Last_Communication ELSE Last_Change
END) AS MaxDate
FROM vMyView
GROUP BY Customer
So, how can I easily grab the highest value for each row in any of the 50(ish) columns?
I am using SQL Server 2008 R2, but I also need this to work in versions 2012 and 2014.
Any help would be greatly appreciated.
EDIT:
I just discovered that the second database is storing the dates in NUMERIC fields, rather than DATETIME. (Stupid! I know!)
So I get the error:
The type of column "ARCUS" conflicts with the type of other columns specified in the UNPIVOT list.
I tried to resolve this with a CAST to make it DATETIME, but that only resulted in more errors.
;WITH X AS
(
SELECT Customer
,Value [Date]
,ColumnName [Entity]
,BusinessEmail
,ROW_NUMBER() OVER (PARTITION BY Customer ORDER BY Value DESC) rn
FROM (
SELECT comp_name AS Customer
, Pers_EmailAddress AS BusinessEmail
, Comp_UpdatedDate AS Company
, CmLi_UpdatedDate AS Communication
, Case_UpdatedDate AS [Case]
, AdLi_UpdatedDate AS [Address]
, PLink_UpdatedDate AS Phone
, ELink_UpdatedDate AS Email
, Pers_UpdatedDate AS Person
, oppo_updateddate as Opportunity
, samdat.dbo.ARCUS.AUDTDATE AS ARCUS
FROM vCompanyPE
LEFT JOIN Comm_Link on Comp_CompanyId = CmLi_Comm_CompanyId
LEFT JOIN Cases ON Comp_CompanyId = Case_PrimaryCompanyId
LEFT JOIN Address_Link on Comp_CompanyId = AdLi_CompanyID
LEFT JOIN PhoneLink on Comp_CompanyId = PLink_RecordID
LEFT JOIN EmailLink on Comp_CompanyId = ELink_RecordID
LEFT JOIN vPersonPE on Comp_CompanyId = Pers_CompanyId
LEFT JOIN Opportunity on Comp_CompanyId = Oppo_PrimaryCompanyId
LEFT JOIN Orders on Oppo_OpportunityId = Orde_opportunityid
LEFT JOIN SAMDAT.DBO.ARCUS on IDCUST = Comp_IdCust
COLLATE Latin1_General_CI_AS
WHERE Comp_IdCust IS NOT NULL
AND Comp_deleted IS NULL
) t
UNPIVOT (Value FOR ColumnName IN
(
Company
,Communication
,[Case]
,[Address]
,Phone
,Email
,Person
,Opportunity
,ARCUS
)
)up
)
SELECT Customer
, BusinessEmail
,[Date]
,[Entity]
FROM X
WHERE rn = 1 AND [DATE] >= DATEADD(year,-2,GETDATE()) and BusinessEmail is not null
You could use CROSS APPLY to manually pivot your fields, then use MAX()
SELECT
vMyView.*,
greatest.val
FROM
vMyView
CROSS APPLY
(
SELECT
MAX(val) AS val
FROM
(
SELECT vMyView.field01 AS val
UNION ALL SELECT vMyView.field02 AS val
...
UNION ALL SELECT vMyView.field50 AS val
)
AS manual_pivot
)
AS greatest
The inner most query will pivot each field in to a new row, then the MAX() re-aggregate them back in to a single row. (Also skipping NULLs, so you don't need to explicitly cater for them.)
;WITH X AS
(
SELECT Customer
,Value [Date]
,ColumnName [CommunicationType]
,ROW_NUMBER() OVER (PARTITION BY Customer ORDER BY Value DESC) rn
FROM (
SELECT comp_name AS Customer
, Comp_UpdatedDate AS Last_Change
, CmLi_UpdatedDate AS Last_Communication
, Case_UpdatedDate AS Last_Case
, AdLi_UpdatedDate AS Address_Change
FROM Company
LEFT JOIN Comm_Link on Comp_CompanyId = CmLi_Comm_CompanyId
LEFT JOIN Cases ON Comp_CompanyId = Case_PrimaryCompanyId
LEFT JOIN Address_Link on Comp_CompanyId = AdLi_CompanyID
) t
UNPIVOT (Value FOR ColumnName IN (Last_Change,Last_Communication,
Last_Case,Address_Change))up
)
SELECT Customer
,[Date]
,[CommunicationType]
FROM X
WHERE rn = 1