HIve subquery in where clause - hive

i am new to hive please help me, i have a table name prac and i need to
find the products which give more profit(price-cost) than product Mouse. I have tried the below query but it is not working.
select product_name,price-cost
from prac
where price-cost > (select price-cost from prac where product_name='mouse')
and my data is
id,product_name,product_type,price,cost,date
100,maker,stationary,25,22,2008-01-15
101,mouse,computer,450,350,2009-04-16
102,white board,stationary,450,375,2010-06-25
103,sony viao,computer,35000,42000,2010-09-21

I got the Answer
select a.*,a.price-a.cost profit
from prac a
join (select price-cost p from prac where p_name='Maker') b
where a.price-a.cost>b.p;

Related

SQL using MAX(column) command

Can you help me answer these queries using max and min commands.
I've just started SQL and I am attempting to:
Find the lowest price of any CD
Find the number of CDs costing 11.99
Find the title of the most expensive rock CD
I'm using the min and max commands in my query but i cannot seem to get the correct queries. Below is a link to the tables that I'm referring to.
The code I have so far is:
1)SELECT cdPrice FROM `CD` order BY MIN(cdPrice)
2)
3)SELECT cdTitle FROM `CD` where cdGenre = 'Rock' AND order BY MAX(cdPrice)
Tables that I'm referring to:
#1
The lowest price of any CD is just a basic minimum select.
SELECT MIN(Price)
FROM CD
#2
We need to use a basic COUNT and where clause to get the count for that specific price.
SELECT COUNT(*)
FROM CD
WHERE Price = '11.99'
#3
With #3 we need to find the highest price. We can do this in multiple ways, the way that I would do it is with a subquery and where clause just off my head.
SELECT Title, Price
FROM CD
WHERE Price =
(
SELECT MAX(Price) FROM CD
)
1. select min(cdPrice) from CD;
2. select cdID from CD where Price=11.99;
3. select a.cdTitle, a.Price, a.cdId
from CD a
inner join (select cdId, MAX(Price) as max_p
from CD
group by Genre
having Genre = 'Rock'
) b a.cdId = b.cdId and a.Price = b.max_p;
It gets a bit complicated to get the title of the most expensive Rock CD. Here are my answers. Hope it helps.
SELECT MIN(column_name) FROM table_name
Try do homework by your self, its fast way to understand SQL.

Error 1214 when using GROUP BY in my view

I've got a problem with an view on my database. All other views are working correctly, but when I'm trying to add a GROUP BY statement behind my query,
then I'll get the next Error:
1214 - The used table type doesn't support FULLTEXT indexes
I've set all my tables to ISAM, and i noticed the whole problem is in the GROUP BY statement.
My query looks like this:
CREATE VIEW `id_winkels`
AS
SELECT
`w`.`w_id` AS `w_id`,
`w`.`k_id` AS `k_id`,
`w`.`w_naam` AS `w_naam`,
`w`.`w_logo` AS `w_logo`,
`w`.`w_homepage` AS `w_homepage`,
`w`.`w_straat` AS `w_straat`,
`w`.`w_huisnummer` AS `w_huisnummer`,
`w`.`w_postcode` AS `w_postcode`,
`w`.`w_woonplaats` AS `w_woonplaats`,
`w`.`w_land` AS `w_land`,
`w`.`w_actief` AS `w_actief`,
`w`.`w_datum` AS `w_datum`,
COUNT(`p`.`p_id`) AS `totaal`
FROM `Winkels` `w`
LEFT JOIN `Producten` `p`
ON `w`.`w_id` = `p`.`w_id`
GROUP BY `w`.`w_naam`
Is there another option to count the amount of products by my shop without useing a group by?
When I'm running the query without the group by statement, it will return my total amount of product in one row.
As you only select fields from Winkels, you should get the product count in a subquery rather than joining the tables:
CREATE VIEW id_winkels AS
SELECT
w.w_id AS w_id,
w.k_id AS k_id,
w.w_naam AS w_naam,
w.w_logo AS w_logo,
w.w_homepage AS w_homepage,
w.w_straat AS w_straat,
w.w_huisnummer AS w_huisnummer,
w.w_postcode AS w_postcode,
w.w_woonplaats AS w_woonplaats,
w.w_land AS w_land,
w.w_actief AS w_actief,
w.w_datum AS w_datum,
(
select count(*)
from Producten p
where p.w_id = w.w_id
) AS totaal
FROM Winkels w;

SQL Error - not a group by expression

With this SQL Query, I am attempting to list overdue book by patron. In addition, I want to group and order the books by patron. I understand you have to use some kind of aggregate function when doing a GROUP BY function. Even so, I am getting a "not a group by expression" error. Any help is appreciated.
Edit: updated code
What if I want to get the total fees per person and book? The bottom code is still resulting in the same error.
SELECT PATRON.LAST_NAME, PATRON.FIRST_NAME, PATRON.PHONE, BOOK.BOOK_TITLE, CHECKOUT.DUE_DATE, SUM((SYSDATE - CHECKOUT.DUE_DATE) * 1.00) AS FEE_BALANCE
FROM CHECKOUT
JOIN PATRON
ON CHECKOUT.PATRON_ID=PATRON.PATRON_ID
JOIN COPY
ON CHECKOUT.COPY_ID=COPY.COPY_ID
JOIN BOOK
ON COPY.BOOK_ID=BOOK.BOOK_ID
WHERE CHECKOUT.RETURN_DATE IS NULL
AND CHECKOUT.DUE_DATE > SYSDATE
GROUP BY PATRON.PATRON_ID
HAVING SUM((SYSDATE - CHECKOUT.DUE_DATE) > 0
ORDER BY PATRON.LAST_NAME, PATRON.FIRST_NAME;
You need to add these 3 columns to the GROUP BY clause...
PATRON.PHONE, BOOK.BOOK_TITLE, CHECKOUT.DUE_DATE
So it would be...
SELECT
PATRON.LAST_NAME,
PATRON.FIRST_NAME,
PATRON.PHONE,
BOOK.BOOK_TITLE,
CHECKOUT.DUE_DATE,
SUM((SYSDATE - CHECKOUT.DUE_DATE) * 1.00) AS BALANCE
FROM CHECKOUT
JOIN PATRON
ON CHECKOUT.PATRON_ID=PATRON.PATRON_ID
JOIN COPY
ON CHECKOUT.COPY_ID=COPY.COPY_ID
JOIN BOOK
ON COPY.BOOK_ID=BOOK.BOOK_ID
WHERE CHECKOUT.RETURN_DATE IS NULL
AND CHECKOUT.DUE_DATE > SYSDATE
GROUP BY PATRON.LAST_NAME, PATRON.FIRST_NAME, PATRON.PHONE, BOOK.BOOK_TITLE, CHECKOUT.DUE_DATE
ORDER BY PATRON.LAST_NAME, PATRON.FIRST_NAME
Bottom line is, if you have a GROUP BY clause, you can only select columns that are part of the GROUP BY unless you are using some kind of scalar value function on them, like COUNT, SUM, MAX, MIN, etc.
Otherwise if you aren't grouping or using a scalar-value function, multiple rows could have different values for that column, so what would the query results be?

How can I make these two queries a single query with an inner join?

I have these two queries that I need to make into a single query. Keyfield1 and TPOLNO should be the join fields. How would I go about making this a single query?
SELECT TPOLNO, SUM(TTSAMT) AS SUM FROM PFPOSTR410 WHERE
((TTRNYY=2012 AND TTRNMM=3 AND TTRNDD>=27) OR (TTRNYY=2012 AND TTRNMM>3) OR
(TTRNYY=2013 AND TTRNMM<=2) OR (TTRNYY=2013 AND TTRNMM=3 AND TTRNDD<=27))
GROUP BY TPOLNO HAVING SUM(TTSAMT)>=5000 ORDER BY TPOLNO ASC
SELECT KEYFIELD1, KEYFROBJ FROM CMRELATN WHERE RELROLETC=8
Thanks in advance for any direction!
Josh
If i understand your questions, you need to do a simple inner join of the 2 tables:
SELECT TPOLNO, SUM(TTSAMT) AS SUM, KEYFIELD1, KEYFROBJ
FROM PFPOSTR410, CMRELATN
WHERE
((TTRNYY=2012 AND TTRNMM=3 AND TTRNDD>=27) OR (TTRNYY=2012 AND TTRNMM>3) OR
(TTRNYY=2013 AND TTRNMM<=2) OR (TTRNYY=2013 AND TTRNMM=3 AND TTRNDD<=27))
AND KEYFIELD1=TPOLNO
AND RELROLETC=8
GROUP BY TPOLNO, KEYFIELD1, KEYFROBJ
HAVING SUM(TTSAMT)>=5000
ORDER BY TPOLNO ASC

Sales report by state in magento in 1.7.0.2

There was a solution provided for 1.4 MySQL query for "Sales Report by Country and State/Province" unfortunately the table structure has changed. Anyone know a query to order sales by state in 1.7.0.2? I would be most appreciative.
Here is what i came up with:
SELECT
sales_flat_order_address.region
, count(*)
FROM
`sales_flat_order` AS `main_table` INNER JOIN `sales_flat_order_address`
ON main_table.entity_id = sales_flat_order_address.parent_id
AND sales_flat_order_address.address_type="shipping"
GROUP BY
sales_flat_order_address.region