How to display data from 4 different tables - Query Designer - sql

I need to build my query using SQL Server Management Studio Query Designer
I need to display a concatenated string of tbl_B.Name + tbl_C.Name + tbl_D.Name, if Id in tbl_E/tbl_User is equal to UserId in tbl_A
This is the Table structure https://imgur.com/PZagLPq, https://imgur.com/moE4dcf
tbl_A UserId = Id in tbl_E/tbl_User
tbl_B_Id = Id in tbl_B
tbl_C_Id = Id in tbl_C
tbl_D_Id = Id in tbl_D
I tried the below code but doesn't display Name field from tbl_B, tbl_C and tbl_D
SELECT tbl_B.Name AS Expr1, tbl_C.Name AS Expr2, tbl_D.Name AS Expr3, tbl_A.UserId, [tbl_E].Id AS Expr5
FROM tbl_B
INNER JOIN tbl_D ON tbl_B.Id = tbl_D.Id
INNER JOIN tbl_A ON tbl_B.Id = tbl_A.Id
INNER JOIN tbl_C ON tbl_B.tbl_CId = tbl_C.Id AND tbl_D.Id = tbl_C.tbl_DId
INNER JOIN [tbl_E] ON tbl_B.Id = [tbl_E].Id AND tbl_A.UserId = [tbl_E].Id
WHERE (tbl_A.UserId = [tbl_E].Id)

Below query will give you the concatenated string of names for every userId, which has entry in all the tables.
SELECT
"tbl_A"."UserId",
CONCAT("tbl_B"."Name", '-', "tbl_C"."Name", '-', "tbl_D"."Name"),
"tbl_B"."Name" AS Expr1,
"tbl_C"."Name" AS Expr2,
"tbl_D"."Name" AS Expr3
FROM "tbl_E"
INNER JOIN "tbl_A" ON "tbl_A"."UserId" = "tbl_E"."id"
INNER JOIN "tbl_B" ON "tbl_B"."id" = "tbl_A"."tbl_B_id"
INNER JOIN "tbl_C" ON "tbl_C"."id" = "tbl_B"."tbl_C_id"
INNER JOIN "tbl_D" ON "tbl_D"."id" = "tbl_C"."tbl_D_id"

Related

Access Subquery On mulitple conditions

This SQL query needs to be done in ACCESS.
I am trying to do a subquery on the total sales, but I want to link the sale to the province AND to product. The below query will work with one or the other: (po.product_name = allp.all_products) AND (p.province = allp.all_province); -- but it will no take both.
I will be including every month into this query, once I can figure out the subquery on with two criteria.
Select
p.province as [Province],
po.product_name as [Product],
all_price
FROM
(purchase_order po
INNER JOIN person p
on p.person_id = po.person_id)
left join
(
select
po1.product_name AS [all_products],
sum(pp1.price) AS [all_price],
p1.province AS [all_province]
from (purchase_order po1
INNER JOIN product pp1
on po1.product_name = pp1.product_name)
INNER JOIN person p1
on po1.person_id = p1.person_id
group by po1.product_name, pp1.price, p1.province
)
as allp
on (po.product_name = allp.all_products) AND (p.province = allp.all_province);
Make the first select sql into a table by giving it an alias and join table 1 to table 2. I don't have your table structure or data to test it but I think this will lead you down the right path:
select table1.*, table2.*
from
(Select
p.province as [Province],
po.product_name as [Product]
--removed this ,all_price
FROM
(purchase_order po
INNER JOIN person p
on p.person_id = po.person_id) table1
left join
(
select
po1.product_name AS [all_products],
sum(pp1.price) AS [all_price],
p1.province AS [all_province]
from (purchase_order po1
INNER JOIN product pp1
on po1.product_name = pp1.product_name)
INNER JOIN person p1
on po1.person_id = p1.person_id
group by po1.product_name, pp1.price, p1.province --check your group by, I dont think you want pp1.price here if you want to aggregate
) as table2 --changed from allp
on (table1.product = table2.all_products) AND (table1.province = table2.all_province);

why selecting particular columns from same table slows down query performance significantly?

I have SELECT statement that querying columns from tblQuotes. Why if I am selecting columns a.ProducerCompositeCommission and a.CompanyCompositeCommission, then query spinning forever.
Execution plans with and without those columns are IDENTICAL!
If I commented them out - then it brings result for 1 second.
SELECT
a.stateid risk_state1,
--those columns slows down performance
a.ProducerCompositeCommission,
a.CompanyCompositeCommission,
GETDATE() runDate
FROM
tblQuotes a
INNER JOIN
lstlines l ON a.LineGUID = l.LineGUID
INNER JOIN
tblSubmissionGroup tsg ON tsg.SubmissionGroupGUID = a.SubmissionGroupGuid
INNER JOIN
tblUsers u ON u.UserGuid = tsg.UnderwriterUserGuid
INNER JOIN
tblUsers u2 ON u2.UserGuid = a.UnderwriterUserGuid
LEFT OUTER JOIN
tblFin_Invoices tfi ON tfi.QuoteID = a.QuoteID AND tfi.failed <> 1
INNER JOIN
lstPolicyTypes lpt ON lpt.policytypeid = a.policytypeid
INNER JOIN
tblproducercontacts prodC ON prodC.producercontactguid = a.producercontactguid
INNER JOIN
tblProducerLocations pl ON pl.producerlocationguid = prodc.producerlocationguid
INNER JOIN
tblproducers prod ON prod.ProducerGUID = pl.ProducerGUID
LEFT OUTER JOIN
Catalytic_tbl_Model_Analysis aia ON aia.ImsControl = a.controlno
AND aia.analysisid = (SELECT TOP 1 tma2.analysisid
FROM Catalytic_tbl_Model_Analysis tma2
WHERE tma2.imscontrol = a.controlno)
LEFT OUTER JOIN
Catalytic_tbl_RDR_Analysis rdr ON rdr.ImsControl = a.controlno
AND rdr.analysisid = (SELECT TOP 1 tma2.analysisid
FROM Catalytic_tbl_RDR_Analysis tma2
WHERE tma2.imscontrol = a.controlno)
LEFT OUTER JOIN
tblProducerContacts mnged ON mnged.producercontactguid = ProdC.ManagedBy
LEFT OUTER JOIN
lstQuoteStatusReasons r1 ON r1.id = a.QuoteStatusReasonID
WHERE
l.LineName = 'EARTHQUAKE'
AND CAST(a.EffectiveDate AS DATE) >= CAST('2017-01-01' AS DATE)
AND CAST(a.EffectiveDate AS DATE) <= CAST('2017-12-31' AS DATE)
ORDER BY
a.effectiveDate
The execution plan can be found here:
https://www.brentozar.com/pastetheplan/?id=rJawDkTx-
I ran sp_help and this is what I see:
What exactly wrong with those columns?
I dont use them in a JOIN or anything. Why such bahaviour?
Table Size:
Indexes on table tblQuotes

How to use group by only for some columns in sql Query?

The following query returns 550 records, which I am then grouping by some columns in the controller via linq. However, how can I achieve the "group by" logic in the SQL query itself? Additionally, post-grouping, I need to show only 150 results to the user.
Current SQL query:
SELECT DISTINCT
l.Id AS LoadId
, l.LoadTrackingNumber AS LoadDisplayId
, planningType.Text AS PlanningType
, loadStatus.Id AS StatusId
, loadWorkRequest.Id AS LoadRequestId
, loadStatus.Text AS Status
, routeIds.RouteIdentifier AS RouteName
, planRequest.Id AS PlanId
, originPartyRole.Id AS OriginId
, originParty.Id AS OriginPartyId
, originParty.LegalName AS Origin
, destinationPartyRole.Id AS DestinationId
, destinationParty.Id AS DestinationPartyId
, destinationParty.LegalName AS Destination
, COALESCE(firstSegmentLocation.Window_Start, originLocation.Window_Start) AS StartDate
, COALESCE(firstSegmentLocation.Window_Start, originLocation.Window_Start) AS BeginDate
, destLocation.Window_Finish AS EndDate
AS Number
FROM Domain.Loads (NOLOCK) AS l
INNER JOIN dbo.Lists (NOLOCK) AS loadStatus ON l.LoadStatusId = loadStatus.Id
INNER JOIN Domain.Routes (NOLOCK) AS routeIds ON routeIds.Id = l.RouteId
INNER JOIN Domain.BaseRequests (NOLOCK) AS loadWorkRequest ON loadWorkRequest.LoadId = l.Id
INNER JOIN Domain.BaseRequests (NOLOCK) AS planRequest ON planRequest.Id = loadWorkRequest.ParentWorkRequestId
INNER JOIN Domain.Schedules AS planSchedule ON planSchedule.Id = planRequest.ScheduleId
INNER JOIN Domain.Segments (NOLOCK) os on os.RouteId = routeIds.Id AND os.[Order] = 0
INNER JOIN Domain.LocationDetails (NOLOCK) AS originLocation ON originLocation.Id = os.DestinationId
INNER JOIN dbo.EntityRoles (NOLOCK) AS originPartyRole ON originPartyRole.Id = originLocation.DockRoleId
INNER JOIN dbo.Entities (NOLOCK) AS originParty ON originParty.Id = originPartyRole.PartyId
INNER JOIN Domain.LocationDetails (NOLOCK) AS destLocation ON destLocation.Id = routeIds.DestinationFacilityLocationId
INNER JOIN dbo.EntityRoles (NOLOCK) AS destinationPartyRole ON destinationPartyRole.Id = destLocation.DockRoleId
INNER JOIN dbo.Entities (NOLOCK) AS destinationParty ON destinationParty.Id = destinationPartyRole.PartyId
INNER JOIN dbo.TransportationModes (NOLOCK) lictm on lictm.Id = l.LoadInstanceCarrierModeId
INNER JOIN dbo.EntityRoles (NOLOCK) AS carrierPartyRole ON lictm.CarrierId = carrierPartyRole.Id
INNER JOIN dbo.Entities (NOLOCK) AS carrier ON carrierPartyRole.PartyId = carrier.Id
INNER JOIN dbo.EntityRoles (NOLOCK) AS respPartyRole ON l.ResponsiblePartyId = respPartyRole.Id
INNER JOIN dbo.Entities (NOLOCK) AS respParty ON respPartyRole.PartyId = respParty.Id
INNER JOIN Domain.LoadOrders (NOLOCK) lo ON lo.LoadInstanceId = l.Id
INNER JOIN Domain.Orders (NOLOCK) AS o ON lo.OrderInstanceId = o.Id
INNER JOIN Domain.BaseRequests (NOLOCK) AS loadRequest ON loadRequest.LoadId = l.Id
--Load Start Date
LEFT JOIN Domain.Segments (NOLOCK) AS segment ON segment.RouteId = l.RouteId AND segment.[Order] = 0
LEFT JOIN Domain.LocationDetails (NOLOCK) AS firstSegmentLocation ON firstSegmentLocation.Id = segment.DestinationId
LEFT JOIN dbo.Lists (NOLOCK) AS planningType ON l.PlanningTypeId = planningType.Id
LEFT JOIN dbo.EntityRoles (NOLOCK) AS billToRole ON o.BillToId = billToRole.Id
LEFT JOIN dbo.Entities (NOLOCK) AS billTo ON billToRole.PartyId = billTo.Id
WHERE o.CustomerId in (34236) AND originLocation.Window_Start >= '07/19/2015 00:00:00' AND originLocation.Window_Start < '07/25/2015 23:59:59' AND l.IsHistoricalLoad = 0
AND loadStatus.Id in (285, 286,289,611,290)
AND loadWorkRequest.ParentWorkRequestId IS NOT NULL
AND routeIds.RouteIdentifier IS NOT NULL
AND (planSchedule.EndDate IS NULL OR (planSchedule.EndDate is not null and CAST(CONVERT(varchar(10), planSchedule.EndDate,101) as datetime) > CAST(CONVERT(varchar(10),GETDATE(),101) as datetime))) ORDER BY l.Id DESC
linq:
//Get custom grouped data
var loadRequest = (from lq in returnList
let loadDisplayId = lq.LoadDisplayId
let origin = lq.OriginId //get this origin for route
let destination = lq.DestinationId // get this destination for route
group lq by new
{
RouteId = lq.RouteName,
PlanId = lq.PlanId,
Origin = lq.OriginId,
Destination = lq.DestinationId
}
into grp
select new
{
RouteId = grp.Key.RouteId,
PlanId = grp.Key.PlanId,
Origin = grp.Key.Origin,
Destination = grp.Key.Destination,
Loads = (from l in grp select l)
}).OrderBy(x => x.Origin).ToList();
I'm guessing you want to Group By column 1 but include columns 2 and 3 in your Select. Using a Group By you cannot do this. However, you can do this using a T-SQL Windowing function using the OVER() operator. Since you don't say how you want to aggregate, I cannot provide an example. But look at T-SQL Windowing functions. This article might help you get started.
One important thing you need to understand about GROUP BY is that you must assume that there are multiple values in every column outside of the GROUP BY list. In your case, you must assume that for each value of Column1 there would be multiple values of Column2 and Column3, all considered as a single group.
If you want your query to process any of these columns, you must specify what to do about these multiple values.
Here are some choices you have:
Pick the smallest or the largest value for a column in a group - use MIN(...) or MAX(...) aggregator for that
Count non-NULL items in a group - use COUNT(...)
Produce an average of non-NULL values in a group - use AVG(...)
For example, if you would like to find the smallest Column2 and an average of Column3 for each value of Column1, your query would look like this:
select
Column1, MIN(Column2), AVG(Column3)
from
TableName
group by
Column1

Problem with join query in SQL Server

Below is the query which works but with some errors:
SELECT dbo.Booking.Cost,
dbo.Booking.StatusID,
dbo.Account.FirstName,
dbo.Attendee.HelmetsPurchased AS ProductsPurchased,
dbo.Attendee.GaragesPurchased,
dbo.Attendee.SecondDriver AS [Driver Name]
FROM dbo.Booking
JOIN dbo.Attendee ON dbo.Booking.EventID = dbo.Attendee.EventID
JOIN dbo.Account ON dbo.Booking.UserID = dbo.Account.UserID
WHERE (dbo.Booking.EventID = 15)
Output:
The issue with the generated table is the column product purchased and driver name is populated with text for every row which in this case it should be populated for only the rows which has Drivername and product purchased. There is only one row in the attendee table which has drivername, product purchase fields populated for username mark, while all the other rows has null value for both the drivername and products.
Instead of an INNER JOIN on the table Attendee you should do a LEFT JOIN. This will include rows where these two columns are null as well.
SELECT b.Cost,
b.StatusID,
ac.FirstName,
at.HelmetsPurchased AS ProductsPurchased,
at.GaragesPurchased,
at.SecondDriver AS [Driver Name]
FROM dbo.Booking b
LEFT JOIN dbo.Attendee at ON b.EventID = at.EventID
LEFT JOIN dbo.Account ac ON b.UserID = ac.UserID
WHERE b.EventID = 15
If you don't need driver name, the following could help:
SELECT
B.EventId,
B.Cost,
B.StatusID,
A.FirstName,
SUM(AT.HelmetsPurchased) AS ProductsPurchased,
SUM(AT.GaragesPurchased) AS GaragesPurchased
FROM
dbo.Booking B
INNER JOIN
dbo.Account A ON B.UserID = A.UserID
LEFT OUTER JOIN
dbo.Attendee AT ON B.EventID = AT.EventID
WHERE (dbo.Booking.EventID = 15)
GROUP BY B.EventId, B.Cost, B.StatusId, A.FirstName

SQL stored procedure Join question

currently the stored proc returns one record based on a employee_number that is passed as a parameter. If the surpervisor_id is NULL, there is no record returned. If I want to return a record with all the other fields, and have a NULL in the supervisor_id field, how would I configure that?
SELECT DISTINCT
tblPeople.PERSON_ID, tblPeople.FIRST_NAME, tblPeople.LAST_NAME,
tblPeople.EMPLOYEE_NUMBER, tblPeople.EMAIL_ADDRESS,
tblAddress.ADDRESS_LINE1, tblAddress.ADDRESS_LINE2, tblAddress.ADDRESS_LINE3,
tblAddress.TOWN_OR_CITY, tblAddress.STATE, tblAddress.COUNTRY,
tblAddress.POSTAL_CODE, tblPeople.PHONE_NUMBER,
tblPeople.EFFECTIVE_START_DATE, tblPeople.EFFECTIVE_END_DATE,
tblLocation.LOC_CODE,
pp2.FIRST_NAME AS Expr1, pp2.LAST_NAME AS Expr2,
pp2.EMPLOYEE_NUMBER AS Expr3, tblAssignment.SUPERVISOR_ID,
tblAssignment.ATTRIBUTE2, tblAssignment.ATTRIBUTE3,
tblPeople.INSTANCE_ID, tblAssignment.LOC_ID
FROM
tblPeople AS tblPeople
RIGHT OUTER JOIN
tblAddress AS tblAddress ON tblPeople.PERSON_ID = tblAddress.PERSON_ID
AND tblPeople.INSTANCE_ID = tblAddress.INSTANCE_ID
INNER JOIN
tblAssignments AS tblAssignment ON tblPeople.PERSON_ID = tblAssignment.PERSON_ID
AND tblPeople.INSTANCE_ID = tblAssignment.INSTANCE_ID
INNER JOIN
Locations AS Location ON Location.LOC_ID = tblAssignment.LOC_ID
AND Location.INSTANCE_ID = tblAssignment.INSTANCE_ID
INNER JOIN
tblPeople AS pp2 ON pp2.PERSON_ID = tblAssignment.SUPERVISOR_ID
AND pp2.INSTANCE_ID = tblAssignment.INSTANCE_ID
WHERE
tblPeople.EMPLOYEE_NUMBER = '3298'
AND tblPeople.INSTANCE_ID = 1
AND tblAssignment.Assignment_Status = 'Current'
Most likely, you just need to change this JOIN
INNER JOIN
tblPeople AS pp2 ON pp2.PERSON_ID = tblAssignment.SUPERVISOR_ID
AND pp2.INSTANCE_ID = tblAssignment.INSTANCE_ID
to an LEFT OUTER JOIN
LEFT OUTER JOIN
tblPeople AS pp2 ON pp2.PERSON_ID = tblAssignment.SUPERVISOR_ID
AND pp2.INSTANCE_ID = tblAssignment.INSTANCE_ID
If that SUPERVISOR_ID IS NULL, then the columns from the pp2 table will be NULL, but you should still get data for all other columns back.