I have a table consisting of customer purchase records having following columns
CUSTOMER TRANSDATE SALENUM STORE TRANSTYP PRICE
-------- --------- --------- ----- --------- ------
1 12-FEB-2013 777 O CASH 7.99
1 12-FEB-2013 777 O CASH 6.99
1 12-FEB-2013 778 O CARD 9.17
2 23-APR-2013 987 D CASH 7.65
1 15-MAY-2013 1098 T CARD 2.35
I want to aggregate over salenum i,e for each salenum, i want the total price, as well as the store, transtyp, transdate,customer values as these are same for a particular salenum
However if i use
select customer,transdate,salenum,store,transtyp,sum(price) from table1 group by salenum
Its obviously saying not a valid group by value. How to get the desired result?
SAMPLE RESULT:
CUSTOMER TRANSDATE SALENUM STORE TRANSTYP PRICE
-------- --------- --------- ----- --------- ------
1 12-FEB-2013 777 O CASH 15.98
1 12-FEB-2013 778 O CARD 9.17
All non-aggregated columns should be in the group by:
select customer,transdate,salenum,store,transtyp,sum(price)
from table1
group by customer,transdate,salenum,store,transtyp
SELECT CUSTOMER,TRANSDATE,SALENUM,STORE,TRANSTYP,SUM(PRICE)
FROM TABLE1
GROUP BY CUSTOMER,TRANSDATE,SALENUM,STORE,TRANSTYP
ORDER BY CUSTOMER
I using the order by clause because it will be in order and also in sequence.
Related
This is the query written by me:
select resort.resortid,
resort.resortname,
(count(booking.bookingid)) as "TOTALBOOKING",
booking.totalcharge as "TOTALAMOUNT"
from resort
join booking on booking.resortid=resort.resortid
group by resort.resortid,resort.resortname,booking.totalcharge
order by resort.resortid;
and output:
RESORTID RESORTNAME TOTALBOOKING TOTALAMOUNT
---------- ------------------------------ ------------ -----------
1001 TREE OF LIFE RESORT 1 30000
1001 TREE OF LIFE RESORT 1 48000
1002 PALETTE RESORTS 1 30000
1003 GRAND GRT 1 32000
1004 GREEN COCONUT 1 30000
Expected Output:
RESORTID RESORTNAME TOTALBOOKING TOTALAMOUNT
---------- ------------------------------ ------------ -----------
1001 TREE OF LIFE RESORT 2 78000
1002 PALETTE RESORTS 1 30000
1003 GRAND GRT 1 32000
1004 GREEN COCONUT 1 30000
How to add the count of totalbookings having same resortid ?
You can use the windowed version of count().
SELECT resort.resortid,
resort.resortname,
count(booking.bookingid) AS "TOTALBOOKING",
booking.totalcharge AS "TOTALAMOUNT",
count(booking.bookingid) OVER (PARTITION BY resort.resortid) AS "totalbookings having same resortid"
FROM resort
INNER JOIN booking
ON booking.resortid = resort.resortid
GROUP BY resort.resortid,
resort.resortname,
booking.totalcharge
ORDER BY resort.resortid;
I think that you want to sum() the totalcharge per resortid and resortname: so you need just the two latter columns in the group by clause (not totalcharge).
select
r.resortid,
r.resortname,
count(*) totalbooking ,
sum(b.totalcharge) as totalamount
from resort r
inner join booking b on b.resortid = r.resortid
group by r.resortid, r.resortname
order by r.resortid;
Note that table aliases make the query easier to write and read.
This gives you one row per resort, with the total number of bookings and the sum of totalcharge.
I got this selection:
select EN.NAME_COMP, EN.CODE_CODE_COMP, sum(IT.AMOUNT) as TOTAL_AMOUNT
from COMPANY EN, SALARY IT
where IT.NO_ACC = EN.NO_ACC
group by EN.NAME_COMP, EN.CODE_CODE_COMP, IT.AMOUNT
;
sum(IT.AMOUNT) must totalize all amount from NAME_COMP, but instead of this, it display every NAME_COMP separately and with separately amount, it not make his SUM
NAME_COMP CODE_CODE_COMP TOTAL_AMOUNT
------------------------------ -------------------- --------------
COMP 1 23 1500.5
COMP 1 23 500.5
COMP 4 12 2100.75
I have a table
SELECT PRD_CODE,PRD_MAKE,PRD_NAME FROM PRD_MST
SELECT PDI_DISC_QTY,PDI_DISC_PRICE FROM PRD_DISC_INF
select customerid,Productid,quantity from ShoppingCartItem
Records
PRD_MST
PRD_CODE PRD_MAKE PRD_NAME
----------- ---------------- ----------
4 mobile moto e
5 cycle hero
PRD_DISC_INF
PDI_DISC_QTY PDI_DISC_PRICE
----------------- ------------------
2.00 2.10
1.00 2.31
ShoppingCartItem
customerid Productid quantity
----------- ----------- -----------
1 5 5
The Problem is that
if a customer buy 5 mobile and price of single unit is 200 then how do i calculate discount amount base on PDI_DISC_QTY in table 'PRD_DISC_INF'
Need help i am unable to get the logic
Try this DEMO
select PDI_DISC_QTY *
(cast(round(((select quantity from ShoppingCartItem)/PDI_DISC_QTY),0) as int))
from PRD_DISC_INF ;
1) vendor table
--------------------------------------------
VENDid VENDname
--- -----
1 ABC
2 XYZ
3 WXY
2)purchase table
---------------------------------------------
VENDid Purchasedate
------ ------------
1 12-01-2012
1 10-11-2013
2 22-02-2014
2 11-04-2014
3 10-05-2014
3 11-06-2014
1 14-06-2014
output(list all rows of vendor table and only max(purchasedate) from purchase table)
---------------------------------------------
VENDid VENDname PurchaseDate
------- -------- -------------
1 ABC 14-06-2014
2 XYZ 11-04-2014
3 WXY 11-06-2014
i got some queries like to solve previous problem-
SELECT v.VendID, VendName, Max(PurchaseDate)
FROM vendor v
INNER JOIN purchase p
ON v.VendID = p.VendID
Group By v.VendID, VendName
select VENDid, VENDname,
(select top 1 purchaseDate from purchase p
where p.VENDid=v.VENDid order by purchaseDate desc) as 'Purchase date'
from Vendor v
Que. If i will add some more column in purchase table like -
2)purchase table
------------------------------------------
VENDid Purchasedate amount_paid
------ ------------ ------------
1 12-01-2012 10000
1 10-11-2013 20000
2 22-02-2014 15000
2 11-04-2014 30000
3 10-05-2014 80000
3 11-06-2014 17000
1 14-06-2014 28000
and i want amount_paid along with previous output like-
---------------------------------------------
VENDid VENDname PurchaseDate amount_paid
------- -------- ------------- -------------
1 ABC 14-06-2014 28000
2 XYZ 11-04-2014 30000
3 WXY 11-06-2014 17000
then what will be query..
You appear to be using SQL Server. If so, you can use cross apply:
select v.VENDid, v.VENDname, p.PurchaseDate, p.Amount_Paid
from Vendor v cross apply
(select top 1 p.*
from purchase p
where p.VENDid = v.VENDid
order by p.purchaseDate desc
) p ;
RateName Rate EffectiveDate
-------- ---- -------------
RateOne 400 2011-01-05
RateOne 410 2011-06-31
RateTwo 147 2010-09-21
RateThree 68 2011-07-14
RateTwo 100 2011-10-30
If I have data such as the above, how can I run a query such that I'll have these as results:
RateName Rate EffectiveDate
-------- ---- -------------
RateOne 410 2011-06-31
RateThree 68 2011-07-14
RateTwo 100 2011-10-30
Basically, I just need the latest rates for each distinct rate name.
You can try this:
SELECT A.*
FROM YourTable A
INNER JOIN ( SELECT RateName, MAX(EffectiveDate) AS MaxDate
FROM YourTable
GROUP BY RateName) B
ON A.RateName = B.RateName AND A.EffectiveDate = B.MaxDate