how to get sum of this column in sql server - c#

i'm use below query for fetch my data with an ordernumber...
SELECT Details.Quantity,
Details.OrderNumber,
Orders.OrderDate,
Products.ProductName,
Products.UnitPrice ,
Details.Quantity*Products.UnitPrice as qprice
FROM Details
INNER JOIN Orders
ON Details.OrderNumber = Orders.OrderNumber
INNER JOIN Products
ON Details.ProductID = Products.ProductID
where Orders.OrderNumber='14195'
this query give me below result
now i want give sum of qprice column..
how can i do this??

Like this?
SELECT SUM(Details.Quantity * Products.UnitPrice) AS Amount
FROM Details
INNER JOIN Orders ON
Details.OrderNumber = Orders.OrderNumber
INNER JOIN Products ON
Details.ProductID = Products.ProductID
WHERE Orders.OrderNumber = '14195'
If you need to have TotalAmount as an extra column to what you already have then:
SELECT
Details.Quantity,
Details.OrderNumber,
Orders.OrderDate,
Products.ProductName,
Products.UnitPrice,
Details.Quantity*Products.UnitPrice as qprice,
SUM(Details.Quantity*Products.UnitPrice) OVER() TotalAmount
FROM Details
INNER JOIN Orders ON
Details.OrderNumber = Orders.OrderNumber
INNER JOIN Products ON
Details.ProductID = Products.ProductID
WHERE Orders.OrderNumber = '14195'

Related

update query with multiple inner joins

I want to update the table with multiple inner join query but when I write the query it through an error incorrect syntax near inner keyword
Update inventory_detail INNER JOIN inventory
ON inventory_detail.inventory_id = Inventory.Inventory_id
INNER JOIN Ingredients
ON Inventory.Inventory_id=Ingredients.invenotry_id
SET inventory_detail.Quantity=inventory_detail.Quantity-1
WHERE inventory_detail.loc_id =1 AND Ingredients.item_id=27 ;
ERD diagram
Change your query to:
UPDATE inventory_detail
SET Quantity = Quantity - 1
FROM inventory_detail
INNER JOIN inventory
ON inventory_detail.inventory_id = Inventory.Inventory_id
INNER JOIN Ingredients
ON Inventory.Inventory_id = Ingredients.invenotry_id
WHERE inventory_detail.loc_id = 1
AND Ingredients.item_id = 27;

Get value to display from another sql table

I have 2 tables as attached image:
In order to save space, i only store the ID.
In the gridview, i want to show the name based on the ID.
I tried on inner join but only 1 parameter can be inner join.
Any direction can i go? Please help.
Here is the code i tried:
SelectCommand="SELECT * FROM [ProjectInfo] inner join [Employee] ON Employee.ID = ProjectInfo.ProjectInfo_Leader AND Employee.ID = ProjectInfo.ProjectInfo_CoLeader AND Employee.ID = ProjectInfo.ProjectInfo_Helper"
SelectCommand="SELECT * FROM [ProjectInfo] inner join [Employee] ON Employee.ID = ProjectInfo.ProjectInfo_Leader OR Employee.ID = ProjectInfo.ProjectInfo_CoLeader OR Employee.ID = ProjectInfo.ProjectInfo_Helper"
I am not familiar with SQL, thus i can only try and error...
Thanks.
It is obvious solution.
select l.name leader,c.name [co-lider],h.name helper
from role r
left join people l on r.lider=l.id
left join people c on r.[co-lider]=c.id
left join people h on r.helper=h.id
Note: it is better to use left join in case some roles are not filled.

left join with 3 tables mysql

I have 3 tables in my Database
and i want to retrieve all products with their quantity from bill_Details tables.
This is the query:
SELECT p.prod_Id,p.prod_Name,COALESCE(sum(b.de_Quantity+b.de_Bonus),0)
,p.prod_Cost,p.prod_ExpDate,p.prod_BonusInfo,p.prod_Note
FROM (products p
LEFT JOIN bill_Details b
ON p.prod_Id=b.prod_Id)
LEFT JOIN bills a
ON b.bill_Id = a.bill_Id and a.cus_Sup=1 and a.archived=0
GROUP BY p.prod_Id, p.prod_Name,p.prod_Cost,p.prod_ExpDate,p.prod_BonusInfo,
p.prod_Note
ORDER BY p.prod_Name asc
The issue is that this query retrieve the same quantity when a.cus_Sup=1 or a.cus_Sup=0 !
Knowing that when a.cus_Sup=0 quantity should be 0 , and when a.cus_Sup=1 it's 29.5 for a specific product.
This is the data:
Below Query works for your case.
SELECT p.prod_id,
p.prod_name,
COALESCE(Sum(b.de_quantity + b.de_bonus), 0),
p.prod_cost,
p.prod_expdate,
p.prod_bonusinfo,
p.prod_note
FROM products p
LEFT JOIN (SELECT bd.*
FROM bill_details bd
JOIN bills a
ON bd.bill_id = a.bill_id
WHERE a.cus_sup = 1
AND a.archived = 0) b
ON p.prod_id = b.prod_id
GROUP BY p.prod_id,
p.prod_name,
p.prod_cost,
p.prod_expdate,
p.prod_bonusinfo,
p.prod_note
ORDER BY p.prod_name ASC

Query that Gets Transactions a user can take in SQLServer

I have a tblData and a tblUser.
I only want to display the transactions that the user can take.
The transactions are linked by a DisplayNum, if one of the Transactions in the DisplayNum does not match the TransType in the tblUser then the user cannot take any TransType in that DisplayNum.
The final version put together after some chatting:
SELECT DISTINCT q2.Id, q3.SubQ, q1.DisplayNum, q1.TransType, q1.TotalTransTime, q1.UserId
FROM (
SELECT D.DisplayNum, HighestTransTime.TransType, SUM(D.TransTime) AS TotalTransTime, U.UserId
FROM tblData D
INNER JOIN tblUser U ON D.TransType=U.TransType
INNER JOIN
(
SELECT DISTINCT innerQuery.DisplayNum, TransType
FROM tblData
INNER JOIN
(
SELECT DisplayNum, MAX(TransTime) AS TransTime FROM tblData GROUP BY DisplayNum
) innerQuery ON tblData.DisplayNum = innerQuery.DisplayNum AND tblData.TransTime = innerQuery.TransTime
) HighestTransTime ON D.DisplayNum=HighestTransTime.DisplayNum
WHERE U.UserId = 10
AND D.TransType IN (SELECT TransType FROM tblUser WHERE tblUser.UserId = U.UserId)
AND D.DisplayNum NOT IN (SELECT DisplayNum FROM tblData WHERE TransType NOT IN (SELECT TransType FROM tblUser WHERE tblUser.UserId = U.UserId))
GROUP BY D.DisplayNum, HighestTransTime.TransType, U.UserId
) q1
INNER JOIN (SELECT DisplayNum, MAX(ID) AS ID FROM tblData GROUP BY DisplayNum) q2 ON q1.DisplayNum = q2.DisplayNum
INNER JOIN (SELECT SubQ, ID FROM tblData) q3 ON q2.ID=q3.ID
ORDER BY q2.ID
As far I can understand you could use simple join on TransType column.
SELECT *
FROM dbo.tblData INNER JOIN dbo.tblUser ON
dbo.tblData .TransType = dbo.tblUser .TransType

Unexpected database output when using INNER JOIN

I have the following SQL query
SELECT
r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID,
ra.BEZEICHNUNG AS raumBEZEICHNUNG, ra.ID AS raumID
FROM
RAUM r
INNER JOIN RAZUORDNUNG rz ON rz.RAUM_ID = r.ID
INNER JOIN RAUMATTRIBUTE ra ON rz.RAUMATTRIBUTE_ID = ra.ID
WHERE
RAUMKLASSE_ID = ISNULL(#Raumklasse_ID, RAUMKLASSE_ID)
AND STADT_ID = ISNULL(#Stadt_ID, STADT_ID)
AND GEBAEUDE_ID = ISNULL(#Gebaeude_ID, GEBAEUDE_ID)
AND REGION_ID = ISNULL(#Region_ID, REGION_ID)
AND RAUMATTRIBUTE_ID = ISNULL(#Raumattribute_ID, RAUMATTRIBUTE_ID)
But I think that something is wrong with that.
For example:
If I put three in the RAUMKLASSE_ID textfield in the browser and invoke my method it returns only one room. But there are six rooms with that ID. The strange thing is, that if I remove the two INNER JOIN and the second line of my SELECT, like this:
SELECT
r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID
FROM
RAUM r
WHERE
RAUMKLASSE_ID = ISNULL(#Raumklasse_ID, RAUMKLASSE_ID)
AND STADT_ID = ISNULL(#Stadt_ID, STADT_ID)
AND GEBAEUDE_ID = ISNULL(#Gebaeude_ID, GEBAEUDE_ID)
AND REGION_ID = ISNULL(#Region_ID, REGION_ID)
AND RAUMATTRIBUTE_ID = ISNULL(#Raumattribute_ID, RAUMATTRIBUTE_ID)
it is returning the six rooms, which is correct. I don't know what the problem is with my query. Maybe someone can help me with that?
Thanks in advance
This is the expected behaviour, since:
FROM RAUM r
INNER JOIN RAZUORDNUNG rz ON rz.RAUM_ID = r.ID
INNER JOIN RAUMATTRIBUTE ra ON rz.RAUMATTRIBUTE_ID = ra.ID
Will get you only the rooms that are found in the tables RAUM, RAZUORDNUNG and RAUMATTRIBUTE tables, removing these INNER JOINs will get you all the rooms from the RAUM table that satisfy your condition, check these pages for more details about JOINs:
A visual explanation for JOINs.
Wikipedia article about JOINs
INNER JOIN won't return RAUM entries that have no corresponding RAZUORDNUNG or RAUMATTRIBUTE. You may need a LEFT JOIN instead; in this case, raumBEZEICHNUNG and raumID may be null in the returned set.

Categories