I have the follwoing query embedded in a stored procedure
select u.UserName, s.Name, count(i.id) as NumberAccounts, sum(i.total) as CCTotal
from invoice i
inner join visit v on v.id = i.id
inner join branch b on b.id = v.branchid
inner join practice p on p.id = b.practiceid
inner join visitscheme vs on vs.id = v.id
inner join [plan] pl on pl.id = vs.planid
inner join scheme s on s.id = pl.schemeid
inner join creditcontrol cc on cc.SchemeId = s.id
inner join [user] u on u.id = cc.userid
where p.APIKey = #pracId
and (u.id = #CCid OR #CCId = '999999')
and (s.id = #SchemeId or #SchemeId = 999999)
group by u.UserName, s.Name
order by u.username
When I run it instead of result being grouped by username and scheme I get the following
UserName Name NumberAccounts CCTotal
chanel BANKMED 9954 11882514.19
ciske BANKMED 9954 11882514.19
Estee BANKMED 9954 11882514.19
Feroza BANKMED 9954 11882514.19
No grouping applied, the same values in each. Anyone know where I am going wrong?
Thanks
just write like this....
select u.UserName, s.Name, count(i.id) as NumberAccounts, sum(i.total) as CCTotal
from invoice i
inner join visit v on v.id = i.id
inner join branch b on b.id = v.branchid
inner join practice p on p.id = b.practiceid and p.APIKey = #pracId
inner join visitscheme vs on vs.id = v.id
inner join [plan] pl on pl.id = vs.planid
inner join scheme s on s.id = pl.schemeid and (s.id = #SchemeId or #SchemeId = 999999)
inner join creditcontrol cc on cc.SchemeId = s.id
inner join [user] u on u.id = cc.userid and (u.id = #CCid OR #CCId = '999999')
group by u.UserName, s.Name
order by u.username
assuming that your joins are correct
Related
I have three views in my sql server:
CREATE VIEW [dbo].[v_CompanyLabelCreate]
AS
SELECT
c.Id AS Id,
c.[Name] AS CompanyName,
CASE WHEN r.ApprovedPatentCreativeApplication > 2 OR r.ApprovedCopyRightBookApplication + r.ApprovedCopyRightSoftwareApplication > 4 OR NumberInProduction + NumberNotInProduction > 10 THEN N'创造型'
ELSE NULL END AS Label
FROM Company c INNER JOIN ResearchInfo r ON c.Id = r.CompanyID
WHERE c.Submitted = 1
CREATE VIEW [dbo].[v_CompanyLabelHighPotential]
AS
SELECT
c.Id AS Id,
c.[Name] AS CompanyName,
CASE WHEN si1.Income >= 1000000 AND si2.Income <> 0 AND ((si1.Income -si2.Income)/si2.Income) >= 1 THEN N'潜力型'
WHEN n1.TotalProfit >= 500000 AND n2.TotalProfit <> 0 AND ((n1.TotalProfit - n2.TotalProfit)/n2.TotalProfit) >= 1 THEN N'潜力型'
WHEN nfa1.AssetsTotal >= 5000000 AND nfa2.AssetsTotal <> 0 AND ((nfa1.AssetsTotal - nfa2.AssetsTotal)/nfa2.AssetsTotal) >= 1 THEN N'潜力型'
ELSE NULL END AS Label
FROM Company c INNER JOIN SaleIncome si1 ON c.Id = si1.CompanyID AND si1.YearQuarterID = 2
INNER JOIN SaleIncome si2 ON c.Id = si2.CompanyID AND si2.YearQuarterID = 1
INNER JOIN NetProfit n1 ON c.Id = n1.CompanyID AND n1.YearQuarterID = 2
INNER JOIN NetProfit n2 ON c.Id = n2.CompanyID AND n2.YearQuarterID = 1
INNER JOIN NonFloatingAsset nfa1 ON c.Id = nfa1.CompanyID AND nfa1.YearQuarterID = 2
INNER JOIN NonFloatingAsset nfa2 ON c.Id = nfa2.CompanyID AND nfa2.YearQuarterID = 1
WHERE c.Submitted = 1
CREATE VIEW [dbo].[v_CompanyLabelHighTotalPolicies]
AS
SELECT
c.Id AS Id,
c.[Name] AS CompanyName,
CASE WHEN p.NumberOfPolicies >= 4 AND cp.PolicyID IS NOT NULL THEN N'政策大户'
ELSE NULL END AS Label
FROM Company c LEFT JOIN (SELECT CompanyID, COUNT(*) AS NumberOfPolicies FROM CompanyPolicy GROUP BY CompanyID) p ON c.Id = p.CompanyID
LEFT JOIN (SELECT CompanyID, PolicyID FROM CompanyPolicy WHERE PolicyID = 7) cp ON c.Id = cp.CompanyID
WHERE c.Submitted = 1
When I run a select query which inner joins these three views:
select *
from v_CompanyLabelCreate clc
INNER JOIN v_CompanyLabelHighPotential clhp ON clc.Id = clhp.Id
INNER JOIN v_CompanyLabelHighTotalPolicies chtp ON clc.Id = chtp.Id
It is fast and smooth. However, when I try to run a select query which inner join these three views with some other tables:
select *
FROM Company c
INNER JOIN BasicInfo b ON c.Id = b.CompanyID
INNER JOIN CompanyDimension cd ON c.Id = cd.CompanyID
INNER JOIN TotalProfit t ON c.Id = t.CompanyID AND t.YearQuarterID = 2
INNER JOIN SaleCost sc ON c.Id = sc.CompanyID AND sc.YearQuarterID = 2
INNER JOIN CompanyFinancialIndex cfi ON c.Id = cfi.CompanyID AND cfi.YearQuarterID = 6
INNER JOIN SaleIncome si1 ON c.Id = si1.CompanyID AND si1.YearQuarterID = 2
INNER JOIN SaleIncome si2 ON c.Id = si2.CompanyID AND si2.YearQuarterID = 1
INNER JOIN NetProfit n1 ON c.Id = n1.CompanyID AND n1.YearQuarterID = 2
INNER JOIN NetProfit n2 ON c.Id = n2.CompanyID AND n2.YearQuarterID = 1
INNER JOIN v_CompanyLabelCreate clc ON c.Id = clc.Id
INNER JOIN v_CompanyLabelHighPotential clhp ON clc.Id = clhp.Id
INNER JOIN v_CompanyLabelHighTotalPolicies chtp ON clc.Id = chtp.Id
WHERE c.Submitted = 1
It looks like it will take forever to get the result (Download the execution plan here: the execution plan). Now, if I just run the query without inner joining the three views like this:
select *
FROM Company c
INNER JOIN BasicInfo b ON c.Id = b.CompanyID
INNER JOIN CompanyDimension cd ON c.Id = cd.CompanyID
INNER JOIN TotalProfit t ON c.Id = t.CompanyID AND t.YearQuarterID = 2
INNER JOIN SaleCost sc ON c.Id = sc.CompanyID AND sc.YearQuarterID = 2
INNER JOIN CompanyFinancialIndex cfi ON c.Id = cfi.CompanyID AND cfi.YearQuarterID = 6
INNER JOIN SaleIncome si1 ON c.Id = si1.CompanyID AND si1.YearQuarterID = 2
INNER JOIN SaleIncome si2 ON c.Id = si2.CompanyID AND si2.YearQuarterID = 1
INNER JOIN NetProfit n1 ON c.Id = n1.CompanyID AND n1.YearQuarterID = 2
INNER JOIN NetProfit n2 ON c.Id = n2.CompanyID AND n2.YearQuarterID = 1
WHERE c.Submitted = 1
It is fast and smooth again.
About the tables used:
Company: A table which stores company Ids and company names. Of course c.Id is the PK and it will be referred to as Company ID later.
ResearchInfo, BasicInfo, CompanyDimension, CompanyFinancialIndex: CompanyID is foreign key, every company only has at most one row in this table.
SaleIncome, SaleCost, NetProfit, NonFloatingAsset, TotalProfit: the combination of CompanyID and YearQuarterID work as Primary Key, although every table has an identity field which is defined as the PK.
You also can have a missed index issue, but your query is a bit complex. You can try to reduce the complexity using cte table like (You need the field names be unique in CTEs):
WITH cte_tb1
AS (SELECT * -- UNIQUE NAMES FIELDS IS REQUIRED
FROM company c
INNER JOIN basicinfo b
ON c.id = b.companyid
INNER JOIN companydimension cd
ON c.id = cd.companyid
INNER JOIN totalprofit t
ON c.id = t.companyid
AND t.yearquarterid = 2
INNER JOIN salecost sc
ON c.id = sc.companyid
AND sc.yearquarterid = 2
INNER JOIN companyfinancialindex cfi
ON c.id = cfi.companyid
AND cfi.yearquarterid = 6
INNER JOIN saleincome si1
ON c.id = si1.companyid
AND si1.yearquarterid = 2
INNER JOIN saleincome si2
ON c.id = si2.companyid
AND si2.yearquarterid = 1
INNER JOIN netprofit n1
ON c.id = n1.companyid
AND n1.yearquarterid = 2
INNER JOIN netprofit n2
ON c.id = n2.companyid
AND n2.yearquarterid = 1
WHERE c.submitted = 1),
cte_tb2
AS (SELECT * -- UNIQUE NAMES FIELDS IS REQUIRED
FROM v_companylabelcreate clc
INNER JOIN v_companylabelhighpotential clhp
ON clc.id = clhp.id
INNER JOIN v_companylabelhightotalpolicies chtp
ON clc.id = chtp.id)
SELECT *
FROM cte_tb1 a
INNER JOIN cte_tb2 b
ON a.id = b.id
How to change this query to use joins (inner or left) but not subquery
SELECT * FROM External_Blk_Itm_Contracts ELC
WHERE
NOT EXISTS(SELECT
NULL
FROM
[BUDCONTRACTS] c
INNER JOIN
BUDTERMINALS t on t.TerminalID = c.TerminalID AND t.MBFTERMINALNAME = ELC.TerminalName AND c.CONTRACTNAME = ELC.ContractName
INNER JOIN
BudCustomers ct on ct.CustomerId = c.CustomerId AND ELC.CustomerName = ct.LegalName
)
AND ELC.ContractName = '00-000'
Untested (obviously):
SELECT distinct
ELC.*
FROM
External_Blk_Itm_Contracts ELC
LEFT JOIN
BUDCONTRACTS c on c.CONTRACTNAME = ELC.ContractName
LEFT JOIN
BUDTERMINALS t on t.TerminalID = c.TerminalID and t.MBFTERMINALNAME = ELC.TerminalName
LEFT JOIN
BudCustomers ct on ct.CustomerId = c.CustomerId and ELC.CustomerName = ct.LegalName
WHERE
c.CONTRACTNAME is NULL
and t.TerminalID is NULL
and ct.CustomerID is NULL
AND ELC.ContractName = '00-000'
My biggest question about this is why we're joining using name fields to ELC? Isn't there e.g. TerminalID, CustomerID and CotractID available on ELC?
I fixed this by
SELECT ELC.* FROM External_Blk_Itm_Contracts ELC
INNER JOIN BUDTERMINALS BT ON BT.MBFTERMINALNAME = ELC.TerminalName AND ELC.ContractName = '00-000'
INNER JOIN BudCustomers BCUST ON BCUST.LegalName = ELC.CustomerName
LEFT JOIN BUDCONTRACTS BCON ON BCON.CONTRACTNAME = ELC.ContractName AND BCON.TERMINALID = BT.TERMINALID AND BCON.CUSTOMERID = BCUST.CustomerID
WHERE BCON.CONTRACTID IS NULL
I know this question was asked many times but I was tring and trying without success
I have one to many relationship between two tables and some more inner-joins to get more data.
Here is my query:
SELECT
ShopOffer.OfferID,
ShopOffer.OfferMessage,
Shop.ID,
Shop.Name,
Shop.Phone,
[User].Name,
[User].UserID,
ShopOfferStatus.Name AS StatusName,
BlockedShopInUser.IsBlocked
FROM
ShopOffer
INNER JOIN ShopOfferStatus ON ShopOffer.ShopOfferStatusID = ShopOfferStatus.ShopOfferStatusID
INNER JOIN Shop ON ShopOffer.ShopID = Shop.ShopID
INNER JOIN UserRequest ON ShopOffer.UserRequestID = UserRequest.UserRequestID
INNER JOIN [User] ON UserRequest.UserID = [User].UserID
INNER JOIN BlockedShopInUser ON Shop.ShopID = BlockedShopInUser.ShopID AND [User].UserID = BlockedShopInUser.UserID
Each shop can create many offers. In that query I would like to get only the last offer for each shop.
Thanks.
Here is a way:
;WITH LastShopOffer AS
(
SELECT *,
RN = ROW_NUMBER() OVER(PARTITION BY ShopID ORDER BY OfferID DESC)
FROM ShopOffer
)
SELECT
SO.OfferID,
SO.OfferMessage,
S.ID,
S.Name,
S.Phone,
U.Name,
U.UserID,
SOS.Name AS StatusName,
B.IsBlocked
FROM ( SELECT *
FROM LastShopOffer
WHERE RN = 1) SO
INNER JOIN ShopOfferStatus SOS
ON SO.ShopOfferStatusID = SOS.ShopOfferStatusID
INNER JOIN Shop S
ON SO.ShopID = S.ShopID
INNER JOIN UserRequest UR
ON SO.UserRequestID = UR.UserRequestID
INNER JOIN [User] U
ON UR.UserID = U.UserID
INNER JOIN BlockedShopInUser B
ON S.ShopID = B.ShopID
AND U.UserID = B.UserID;
I think you have to start with Shop and then perform a CROSS APPLY on the TOP 1 record from ShopOffer:
SELECT
ShopOffer.OfferID,
ShopOffer.OfferMessage,
Shop.ID,
Shop.Name,
Shop.Phone,
[User].Name,
[User].UserID,
ShopOfferStatus.Name AS StatusName,
BlockedShopInUser.IsBlocked
FROM Shop
CROSS APPLY (
SELECT TOP 1 OfferID, OfferMessage, ShopOfferStatusID, UserRequestID
FROM ShopOffer AS s
WHERE s.ShopID = Shop.ShopID
ORDER BY s.OfferID DESC
) ShopOffer
INNER JOIN ShopOfferStatus ON ShopOffer.ShopOfferStatusID = ShopOfferStatus.ShopOfferStatusID
INNER JOIN UserRequest ON ShopOffer.UserRequestID = UserRequest.UserRequestID
INNER JOIN [User] ON UserRequest.UserID = [User].UserID
INNER JOIN BlockedShopInUser ON Shop.ShopID = BlockedShopInUser.ShopID AND [User].UserID = BlockedShopInUser.UserID
I have this sql database
And I want to show my data such as following:
Note:
Total sales = Sum(ProductsTbl.SalePrice *
OrderDetailsTbl.SoldQuantity)
Discounts = Sum(ProductsTbl.SalePrice * OrderDetailsTbl.SoldQuantity)
* DiscountRatesTbl.DiscountRate/100
Payed = SUM(isnull(CashMovementTbl.CashMovementValue,00.00)
So how can I do a query in SQL Server to show data like that.
Thanks for any help.
I tried this query but failed to got Discounts:
SELECT c.CustName as 'Customer' ,
Total Sales= ( SELECT SUM(p.SalePrice * d.SoldQuantity) FROM OrderDetailsTbl d
INNER JOIN OrdersTbl o ON d.orderID = o.orderID INNER JOIN ProductsTbl p ON p.ProductID = d.ProductID
WHERE o.CustomerID = c.CustomerID ) ,
Discounts= (Select isnull(sum(p.SalePrice * d.SoldQuantity)* dr.DiscountRate/100),00.00 FROM OrderDetailsTbl d
INNER JOIN OrdersTbl o ON d.orderID = o.orderID INNER JOIN ProductsTbl p ON p.ProductID =
d.ProductID inner join DiscountRatesTbl dr on dr.orderID = o.orderID WHERE o.CustomerID = c.CustomerID ) ,
Payed= ( SELECT SUM(isnull(cm.CashMovementValue,00.00)) FROM CashMovementTbl cm
INNER JOIN OrdersTbl o ON o.orderID = cm.orderID WHERE o.CustomerID = c.CustomerID)
FROM CustomersTbl c
where ( SELECT SUM(p.SalePrice * d.SoldQuantity) FROM OrderDetailsTbl d
INNER JOIN OrdersTbl o ON d.orderID = o.orderID INNER JOIN ProductsTbl p ON p.ProductID =
d.ProductID WHERE o.CustomerID = c.CustomerID ) is not null
order by c.CustName
This is not an optimal way for query writing...
But replace the part of your query for discount calculation with the following:
Discounts= (Select isnull(SUM(p.SalePrice * d.SoldQuantity* dr.DiscountRate/100),00.00) FROM OrderDetailsTbl d
INNER JOIN OrdersTbl o ON d.orderID = o.orderID INNER JOIN ProductsTbl p ON p.ProductID =
d.ProductID inner join DiscountRatesTbl dr on dr.orderID = o.orderID WHERE o.CustomerID = c.CustomerID ) ,
This will works.
Simply the SUM must include all multipliers and not simple multiplication of p.SalePrice and d.SoldQuantity
I have the following code I need to write subquery to return the same result.
SELECT Users.UserID,
Users.FirstName,
courses.CourseID,
Count(Exams.examID)NumExams into CurrentExams
FROM ClassCourses
INNER JOIN courses ON ClassCourses.CourseID = courses.CourseID
INNER JOIN Exams ON ClassCourses.ClassID = Exams.ClassID
AND ClassCourses.CourseID = Exams.CourseID
INNER JOIN UserExams ON Exams.ExamID = UserExams.ExamID
INNER JOIN Users ON UserExams.UserID = Users.UserID
group by Users.UserID, courses.CourseID, Users.FirstName
IF EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'AllExams')
drop table AllExams
SELECT courses.CourseID, count(ExamID) TotalExams into AllExams
FROM Exams
INNER JOIN courses ON Exams.CourseID = courses.CourseID
group by Courses.CourseID
SELECT CurrentExams.FirstName,
CurrentExams.CourseID,
AllExams.TotalExams,
AllExams.TotalExams-CurrentExams.NumExams AS Unlisted
FROM AllExams
inner JOIN CurrentExams on CurrentExams.CourseID=AllExams.CourseID
Here is a simple version of one query for what you need. Assuming you have sql server 2005 or later versions.
;with CurrentExams as
(
SELECT Users.UserID, Users.FirstName,courses.CourseID,Count(Exams.examID)NumExams
FROM
ClassCourses
INNER JOIN
courses ON ClassCourses.CourseID = courses.CourseID INNER JOIN
Exams ON ClassCourses.ClassID = Exams.ClassID AND ClassCourses.CourseID = Exams.CourseID INNER JOIN
UserExams ON Exams.ExamID = UserExams.ExamID INNER JOIN
Users ON UserExams.UserID = Users.UserID
group by Users.UserID,courses.CourseID, Users.FirstName
),
AllExams as
(
SELECT courses.CourseID,count(ExamID) TotalExams
FROM
Exams
INNER JOIN
courses ON Exams.CourseID = courses.CourseID
group by Courses.CourseID
)
SELECT CurrentExams.FirstName, CurrentExams.CourseID, AllExams.TotalExams,AllExams.TotalExams-CurrentExams.NumExams AS Unlisted
FROM
AllExams
inner JOIN CurrentExams on CurrentExams.CourseID=AllExams.CourseID