Currently I'm trying to learn on pivot table, here is my table diagram.
I want to generate data row in branch name and column with month with sum total in sales.
SELECT *
FROM
(SELECT
BRANCH.NAME, SALES.TOTAL, TIME.MONTH
FROM
SALES
INNER JOIN
BRANCH ON SALES.BRANCH_ID = BRANCH.BRANCH_ID
INNER JOIN
TIME ON SALES.TIME_ID = TIME.TIME_ID
) AS TABLE1
PIVOT (
SUM(SALES.TOTAL) FOR TIME.MONTH IN ([APR],[MAY],[JUN])
) PIVOTTABLE
it shows an error:
The column prefix 'SALES' does not match with a table name or alias name used in the query.
Is it my table structure got problem or just my query are wrong?
Remove Sales and Time prefix or use TABLE1 instead:
PIVOT (
SUM(TOTAL) FOR MONTH IN ([APR],[MAY],[JUN])
) PIVOTTABLE
Try this:
SELECT * FROM
(
SELECT BRANCH.NAME,SALES.TOTAL,TIME.MONTH
FROM SALES
INNER JOIN BRANCH
ON SALES.BRANCH_ID=BRANCH.BRANCH_ID
INNER JOIN TIME
ON SALES.TIME_ID=TIME.TIME_ID
)AS TABLE1
PIVOT (
SUM(TABLE1.TOTAL) FOR TABLE1.MONTH IN ([APR],[MAY],[JUN])
) PIVOTTABLE
Related
I basically want to to a cross join with my store-product master list with the calendar table that has all possible dates. However, i want to filter for a year (365 days) before making the join with the master list.
I am trying the following query -
select * from ( select a.store_id,a.product_id from mez_2018_store_product_lst) a cross join
(select b.day_id,cast(to_date(from_unixtime(unix_timestamp(b.day_date, 'yyyy-MM-dd'))) as b.date from calendar where day_id>=20170101 and day_id<=20180101 ) b
And I keep getting EOF error.
Can you guys help ?
Try with below query:
hive> select * from
(select store_id,
product_id from mez_2018_store_product_lst) a
cross join
(select day_id,
to_date(from_unixtime(unix_timestamp(day_date, 'yyyy-MM-dd')))dt from calendar
where day_id>=20170101 and day_id<=20180101 ) b;
I have 2 tables ItemMaster and ItemDetail, I am creating a report to display columns from both tables.
I want to display First 3 columns from "ItemMaster" table and then Max(FinishDate) and Max(Amount) from "ItemDetail" table.
I tried to generate results as below written query but not working.
Please help
Thanks
SELECT IM.ItemCode,
IM.Customer,
IM.FinYear,
DET.FinishDate,
DET.Amount
FROM ItemMaster AS IM
INNER JOIN ( SELECT MAX(FinishDate) AS FinishDate, MAX(Amount) AS Amount
FROM ItemDetail ID
WHERE IM.ItemCode = ID.ItemCode) AS DET
ON IM.ItemCode = DET.ItemCode
You need to modify the query of the derived table a little bit so as to include a GROUP BY clause:
SELECT IM.ItemCode,
IM.Customer,
IM.FinYear,
DET.FinishDate,
DET.Amount
FROM ItemMaster AS IM
INNER JOIN ( SELECT ID.ItemCode,
MAX(FinishDate) AS FinishDate, MAX(Amount) AS Amount
FROM ItemDetail ID
GROUP BY ID.ItemCode) AS DET
ON IM.ItemCode = DET.ItemCode
Now you can do a proper JOIN on the ItemCode field.
Try this:
SELECT IM.ItemCode ,
IM.Customer ,
IM.FinYear ,
DET.FinishDate ,
DET.Amount
FROM ItemMaster AS IM
CROSS APPLY ( SELECT MAX(FinishDate) AS FinishDate ,
MAX(Amount) AS Amount
FROM ItemDetail ID
WHERE ID.ItemCode = IM.ItemCode
) AS DET
There is no sub-select needed, imho. Just join both tables and aggregate as you would do on a single table:
select im.ItemCode, im.Customer, im.FinYear,
MAX(id.FinishDate), MAX(id.Amount)
from ItemMaster im
join ItemDetail id
on im.ItemCode = id.ItemCode
group by im.ItemCode, im.Customer, im.FinYear
WITH Members as (
select Members, ReportingMonth from Members ) ,
Calls as (
select Received, Answered, Abandoned,ReportingMonth from Calls ) ,
Rate as (
select Rate from Rate ) ,
Complaints as (
select ComplaintsReceived, ComplaintsProcessed,ReportingMonth from Complaints )
select M.Members,
M.ReportMonth,
R.Rate,
C.Received,
C.Answered,
C.Abandoned,
CO.ComplaintsReceived,
CO.ComplaintsProcessed
from Members as M
left join Calls C ON M.ReportMonth = C.ReportMonth
left join Rate R ON M.ReportMonth = R.ReportMonth
LEFT JOIN Complaints CO ON M.ReportMonth = CO.ReportMonth
Hello friends, i have above query as a dataset to my monthly SSRS report. I have to group the report by ReportingMonth. I tried to group it by column grouping but it's not giving me the result i want. I want to group all the data in the select list by reporting month which is used in column group in my SSRS report. I see in select list it's coming from Members cte so the column group only populates the members data.
Is there any way to group all the fields in the select list by reporting month?
I have a table productHistory
productHistory (id_H , id_product , name , tsInsert);
I wanna get from the table productHistory the last product in the giving period (start, end):
tsInsert must be between the start and the end.
I can do like this:
select max(id_H)
from productHistory
where tsInsert>=:start and tsInsert <=:end
group by id_product;
then select all from productHistory where id_H in the previous selection.
This query is very heavy, is there any other solution using the right join for example?
I tried this solution:
SELECT * FROM productHistory x
INNER JOIN
(
SELECT MAX(id_H) as maxId
FROM productHistory
GROUP id_product
) y
ON x.id_H = y.maxId
and x.TSINSERT >=:start and x.TSINSERT <=:end
i have to update total_orders of customers column to be equal to the total number of all the orders placed by the customer(in cust_order)
here is what i have tried
update (select *
from atish_customer a
inner join
(
select cust_nbr,count(cust_nbr) as count_orders
from atish_cust_order
group by cust_nbr
)c
on c.cust_nbr=a.cust_nbr)
set tot_orders=count_orders;
But this is the error i get
ORA-01779: cannot modify a column which maps to a non key-preserved table
How about this:
UPDATE customer SET total_orders = (
SELECT COUNT(*) FROM cust_order
WHERE cust_order.cust_nbr = customer.cust_nbr
)
[I'm not sure where your atish_customer and atish_customer_order comes into play...they're not shown in your diagram]
Explanation: Basically the inner select just counts the number of orders from the cust_order table for each cust_nbr. By joining the outer customer.cust_nbr to the inner cust_order.cust_nbr, each [outer] row will be updated with the correct total. This is called a correlated subquery (see here for a short tutorial).