I have two tables. In one there is a column with the price of the product and I need to combine it with the column nazwa_kategorii in the second, and write out the sum of all services.
Nazwa usługi Cena usługi ID Kategorii
Abonament za Internet 40.00 1
Bezpieczny Internet 9.90 3
Telewizja Pakiet pełny 80.00 2
GigaNagrywarka 15.00 2
Rabat za Internet -20.00 1
ID Kategorii Nazwa Kategorii Kolejność sortowania
1 Internet 1
2 Telewizja 3
3 Usługi Dodatkowe 2
At the end it should give this result:
Kategoria podsumowania Kwota
Internet 20.00
Usługi Dodatkowe 9.90
Telewizja 95.00
I can't figure out how to complete the query so that it writes me all the columns, not just one. Here is an example:
select Nazwa_kategorii,sum(Cena_uslugi ) as kwota
from (Kategorii left join Uslugi on Kategorii.id_kategorii = Uslugi.id_kategorii)
where (Uslugi.id_kategorii =1 );
Try this:
select nazwa_kategorii, sum(cena_uslugi) kwota
from kategorie k
join uslugi u on k.id_kat = u.id_kat
group by nazwa_kategorii
nazwa_kategorii
kwota
Telewizja
95.00
Usługi dodatkowe
9.90
Internet
20.00
https://dbfiddle.uk/?rdbms=postgres_14&fiddle=35d64442a58ed811cbbffddcf60245f2
Related
I would like to know how will I be able to Get all the orders that were purchased along with group of certain items. I have a master items table where it contains around 90 Based Items and I have My order table that contains all the receipts. so the output show shows all the orders that were purchased along with my based items in my Item Table.
Master Item Table:
ItemCode
-------
10001
10002
10003
10004
Order Table:
SiteID BusinessDate ItemName Units Sold Units Sale ItemCode OrderNo
----------------------------------------------------------------------------
1 06/08/2018 Apple 1 5 10001 122-1
1 06/08/2018 Coffee 1 16 10002 122-1
1 06/08/2018 Ice Tea 2 7 10044 122-1
1 06/08/2018 Beans 9 18 10004 122-1
4 06/08/2018 Donuts 7 17 10066 122-7
1 06/08/2018 Bread 1 7 10003 122-4
1 06/08/2018 Beans 4 8 10004 122-4
2 06/08/2018 Apple 2 5 10001 122-2
2 06/08/2018 Coffee 1 6 10002 122-2
3 06/08/2018 Bread 3 5 10003 122-3
3 06/08/2018 Beans 7 17 10004 122-3
3 06/08/2018 Ice Tea 7 17 10044 122-5
4 06/08/2018 Ice Coffee 7 17 10050 122-6
Result:
SiteID BusinessDate ItemName Units Sold Units Sale ItemCode OrderNo
----------------------------------------------------------------------------
1 06/08/2018 Apple 1 5 10001 122-1
1 06/08/2018 Coffee 1 16 10002 122-1
1 06/08/2018 Ice Tea 2 7 10044 122-1
1 06/08/2018 Beans 9 18 10004 122-1
1 06/08/2018 Bread 1 7 10003 122-4
1 06/08/2018 Beans 4 8 10004 122-4
2 06/08/2018 Apple 2 5 10001 122-2
2 06/08/2018 Coffee 1 6 10002 122-2
3 06/08/2018 Bread 3 5 10003 122-3
3 06/08/2018 Beans 7 17 10004 122-3
the query output has to return to me all the transaction that are purchased along with my Item master table values only, for example the output query didn't return the Order No: 122-6 as the Item Code not present in my master item code while Order No:122-1 is shown because one or two of my ItemCode are present in the order & Master Item table
You can try to write a subquery, which OrderNo and SiteID is matched.
then do join to [Order] table to get your expect result.
SELECT o.*
FROM [Order] o JOIN
(
SELECT DISTINCT OrderNo,SiteID
FROM MasterItem m
JOIN [Order] o on o.ItemCode = m.ItemCode
) t1 on t1.OrderNo = o.OrderNo and t1.SiteID = o.SiteID
sqlfiddle
Something like this? (substitute your own table names)
select * from [Order]
where [OrderNo] in (
select distinct [OrderNo]
from [Order] o
inner join [MasterItem] m on o.[ItemCode] = m.[ItemCode]
)
The subquery gets a list of orders that have the desired items in it, then this is used to select all rows from the Orders table. In other words, this will return all Order rows (any item) where there is an item in the order that is in the list of items in the MasterItem table.
Use an inner join
SELECT * FROM Order o
JOIN Item i ON i.itemCode = o.itemCode
Use inner join:
select * from ordertable o
inner join mastertable m on o.itemcode=m.itemcode
I wold use exists
select * from [Order] o
where exists(select 1 from
(SELECT DISTINCT OrderNo,SiteID
FROM MasterItem m
JOIN [Order] o on o.ItemCode = m.ItemCode
) t
where t.OrderNo=o.OrderNo and t.SiteID=o.SiteID
)
How do I use the view I created to get different data outputs. I have created the view VJobCustomerLaborCost. This view gives each job for each customer with total of labor cost for each job. I need to get each customer with total labor cost for each customer. I have given the View and sample outputs.
GO
CREATE VIEW VJobCustomerLaborCost
AS
SELECT DISTINCT
TJ.intJobID
,TJ.strJobDescription
,TC.intCustomerID
,TC.strLastName + ', ' + TC.strFirstName AS strCustomerName
,SUM(TJE.intHoursWorked * TE.monHourlyRate) AS monTotalLaborCost
FROM
TJobs AS TJ
,TJobCustomers AS TJC
,TCustomers AS TC
,TJobEmployees AS TJE
,TEmployees AS TE
WHERE
TJ.intJobID = TJC.intJobID
AND TJC.intCustomerID = TC.intCustomerID
AND TJ.intCustomerID = TJC.intCustomerID
AND TJE.intJobID = TJ.intJobID
GROUP BY
TJ.intJobID
,TJ.strJobDescription
,TC.intCustomerID
,TC.strLastName
,TC.strFirstName
GO
This is the output from my view. This gives me all customers and each jobs total labor cost
intJobID strJobDescription intCustomerID strCustomerName monTotalLaborCost
----------- ------------------------- ------------------- ----------------- ---------------------
1 Kitchen Remodel 1 Belcher, Bob 8740.00
8 Basement Remodel 1 Belcher, Bob 13300.00
9 Bathroom Remodel 1 Belcher, Bob 12065.00
10 Roof Replacement 1 Belcher, Bob 3325.00
11 Living Room Remodel 1 Belcher, Bob 0.00
3 Bedroom Remodel 3 Parker, Peter 3800.00
6 Roof Replacement 3 Parker, Peter 0.00
7 Basement Remodel 3 Parker, Peter 1710.00
4 Bedroom Remodel 4 Solo, Hans 2850.00
5 Basement Remodel 2 Stark, Tony 0.00
What I need is to use the View created to get total labor cost for each customer. So output would be
intCustomerID strCustomerName monTotalLaborCost
------------------- ----------------- ---------------------
1 Belcher, Bob 37,430
3 Parker, Peter 5,510
4 Solo, Hans 2,850
2 Stark, Tony 0.00
How do I use the above view to get this output?
It looks like you need just to group and sum your data:
select
intCustomerID,
strCustomerName,
sum(monTotalLaborCost) as monTotalLaborCost
from VJobCustomerLaborCost
group by intCustomerID, strCustomerName
I have three tables that contains data as below:
Users
Id Name Other_Columns
---------------------------
1 John Blah
2 Ricky Blah
3 Stella Blah
4 Bob Blah
Saldo
Id User_id Saldo
--------------------
1 3 0.00
2 1 9.00
3 2 0.15
4 4 3.50
Payments
Id User_id Amount Paid_date
------------------------------------------
1 2 10.00 2014-09-01 08:10
2 2 25.00 2014-09-01 09:00
3 3 100.00 2014-05-10 12:47
4 1 20.50 2014-02-23 15:30
How to get result like this:
Id Name Saldo Last Payment
------------------------------------------
1 John 9.00 23.02.2014 20.50
2 Ricky 0.15 01.09.2014 25.00
3 Stella 0.00 0000-00-00 0.00
4 Bob 3.50 10.05.2014 100.00
Thank you.
select u.id, u.name, s.saldo, p.last_paid_date, p2.amount
from users u
join saldo s
on u.id = s.user_id
join (select user_id, max(paid_date) as last_paid_date
from payments
group by user_id) p
on u.id = p.user_id
join payments p2
on p.last_paid_date = p2.paid_date
and p.user_id = p2.user_id
This answer assumes:
(1) On table SALDO, there is one row per USER_ID
(2) On table PAYMENTS, there can be multiple rows per USER_ID
(I'm pretty confident about #2 being true, I don't know about #1, as you didn't say and your sample data doesn't indicate one way or the other)
I would like to get report for drink purchased in whole month but price of the drink can change any time in month and I would like to get report for a month with price change
I have two tables
SELECT [ID]
,[DrinkID]
,[UserID]
,[qty]
,[DateTaken]
FROM [Snacks].[dbo].[DrinkHistory]
SELECT [ID]
,[DrinkID]
,[UserID]
,[qty]
,[DateTaken]
FROM [Snacks].[dbo].[DrinkHistory]
[DrinkHistory]:
ID DrinkID UserID qty DateTaken
----------------------------------------------------------------------
1 1 1 1 2014-05-10
2 1 1 2 2014-05-15
3 2 1 1 2014-06-01
4 2 1 4 2014-06-01
5 1 1 3 2014-05-20
6 1 1 4 2014-05-30
[DrinkPricesEffect]:
PriceID DrinkID DrinkPrice PriceEffectiveDate IsCurrent
-----------------------------------------------------------------------------------
1 1 10.00 2014-05-01 1
2 1 20.00 2014-05-20 1
3 2 9.00 2014-06-01 1
4 2 8.00 2014-01-01 1
5 1 30.00 2014-05-25 1
6 1 40.00 2014-05-28 1
I would like to have result as under date taken between 2014-05-1 to 2014-05-31
DrinkId Qty Price DateTaken PriceEffectiveDate
-----------------------------------------------------------------------
1 1 10 2014-05-10 2014-05-01
1 2 10 2014-05-15 2014-05-01
1 3 20 2014-05-20 2014-05-20
1 4 40 2014-05-30 2014-05-28
Is there any who can give me some idea or write query for me?
If your drink price can change any time in a month you could additionaly save the price for each purchase. I would add a column [PricePaid] to the table [DrinkHistory].
When adding a record to [DrinkHistory], the price for the drink at the moment is known, but later it might change so you save the current price to the history...
Then for your result you could just display the Whole [DrinkHistory]
SELECT * FROM DrinkHistory;
This should work:
Select
DH.DrinkId,
DH.Qty,
DPE.DrinkPrice AS Price,
DH.DateTaken,
DPE.PriceEffectiveDate
FROM DrinkHistory DH
JOIN DrinkPricesEffect DPE ON DPE.PriceID =
(
Select Top 1 PriceID FROM
(
Select PriceID,RANK() OVER(ORDER BY PriceEffectiveDate DESC ) AS rnk
FROM DrinkPricesEffect
WHERE DH.DrinkId = DrinkId AND
DH.DateTaken >= PriceEffectiveDate
)SubQ WHERE rnk = 1
)
WHERE DH.DateTaken Between '2014-05-01' AND '2014-05-30'
Here you can find the SQL Fiddle link: http://sqlfiddle.com/#!6/5f8fb/26/0
This question already has an answer here:
sql server calculate value according to pecentage and add and group them according to uid
(1 answer)
Closed 9 years ago.
I have this table.
orderid processid uid user processdesc companyname ordervalue perwet orgid
1 1 16 Damayanti Enquiry Synechron 33000 10.00 NULL
1 2 4 Meghana Requirement Synechron 33000 10.00 NULL
1 3 5 Yogini Proposal FollowUp Synechron 33000 10.00 NULL
1 4 5 Yogini Order Synechron 33000 20.00 NULL
1 5 4 Meghana Vendor Management Synechron 33000 10.00 NULL
1 6 1 anaghantech Collection Synechron 33000 20.00 NULL
2 1 4 Meghana Enquiry test 100000 10.00 NULL
2 2 4 Meghana Requirement test 100000 10.00 NULL
2 3 5 Yogini Proposal FollowUp test 100000 10.00 NULL
2 4 4 Meghana Order test 100000 20.00 NULL
2 5 4 Meghana Vendor Management test 100000 10.00 NULL
2 6 1 anaghantech Collection test 100000 6.67 NULL
2 6 4 Meghana Collection test 100000 6.67 NULL
2 6 16 Damayanti Collection test 100000 6.67 NULL
5 1 16 Damayanti Enquiry FASTER HYDRAULICS PVT. LTD. 700000 10.00 NULL
5 2 16 Damayanti Requirement FASTER HYDRAULICS PVT. LTD. 700000 10.00 NULL
5 3 16 Damayanti Proposal FollowUp FASTER HYDRAULICS PVT. LTD. 700000 10.00 NULL
5 4 16 Damayanti Order FASTER HYDRAULICS PVT. LTD. 700000 20.00 NULL
I want to add one more column to this table
like perwet/100 * ordervalue
using the query given below
SELECT a.orderid,
b.processid,
b.uid,
d.username,
c.processdesc,
a.companyname,
b.ordervalue,
b.perwet,
b.orgid
FROM ordermaster a,
temp_calpoints1 b,
process c,
userinfo d
WHERE a.orderid = b.orderid
AND c.processid = b.processid
AND d.uid = b.uid
ORDER BY b.orderid,
b.processid,
b.uid;
You can use VIEW instead of adding a new column to your table:
CREATE VIEW dbo.MyView
AS
SELECT a.orderid,
b.processid,
b.uid,
d.username,
c.processdesc,
a.companyname,
b.ordervalue,
b.perwet,
b.orgid,
MyField = b.perwet / 100 * b.ordervalue
FROM ordermaster a,
temp_calpoints1 b,
process c,
userinfo d
WHERE a.orderid = b.orderid
AND c.processid = b.processid
AND d.uid = b.uid