How to show all departments where in March 2014 there were sales of goods with a price of less than 400 [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Please find below Tables.
How to show all departments where in March 2014 there were sales of goods with a price of less than 400?
CREATE TABLE dep
(
id NUMBER (10) NOT NULL,
city VARCHAR2 (100) NOT NULL,
name VARCHAR2 (100) NOT NULL,
CONSTRAINT dep_pk PRIMARY KEY (id)
);

I guess you need below query -
SELECT D.name, SUM(cost)
FROM sales S
JOIN dep D ON S.dep_id = D.id
WHERE EXTRACT(YEAR FROM time) = 2014
AND EXTRACT(MONTH FROM time) = 3
GROUP BY D.name
HAVING SUM(cost) < 400

Related

i make sql like this but second select not count rows correctly, and rows duplicated [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
select * from (
select distinct nationality,gender,regplacetype,count(gender) as total from population
where date_part('year',age(to_date(dateofbirth,'DDMMYYYY'))) BETWEEN 16 and 67 and gender = 'Man'
group by nationality,gender,regplacetype
) as man,
(
select distinct nationality,gender,regplacetype,count(nationality) as total from population
where date_part('year',age(to_date(dateofbirth,'DDMMYYYY'))) BETWEEN 16 and 57 and gender = 'female'
group by xnationality,gender,regplacetype) as female
with man as (
select distinct nationality,gender,regplacetype,count(gender) as total from population
where date_part('year',age(to_date(dateofbirth,'DDMMYYYY'))) BETWEEN 16 and 67 and gender = 'Man'
group by nationality,gender,regplacetype
) select * from man,
union
select distinct nationality,gender,regplacetype,count(nationality) as total from population
where date_part('year',age(to_date(dateofbirth,'DDMMYYYY'))) BETWEEN 16 and 57 and gender = 'female'
group by xnationality,gender,regplacetype

I need the right query for the following [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Here is the "Customers" table
ID Name Country
1 A UK
2 B CN
3 C IN
4 D GB
5 E CN
6 F GB
7 H UK
8 I IN
Need a query to show number of customers for each country. GB and UK should be considered as single country.
Use a CASE expression:
SELECT (CASE WHEN country = 'GB' THEN 'UK' ELSE country END) as country,
COUNT(*), Country
FROM Customer
GROUP BY (CASE WHEN country = 'GB' THEN 'UK' ELSE country END);

SQL sub query. Dont know how to start [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
The table is called PEOPLE and has the columns: id, fname, lname, state, jobtitle, salary, cat
I want to display the 50 states, the number of people in the state with a Cat value of Y, the number of people in the state with a Cat value of N and the total number of people in the state. The four column headings should be State, Yes, No and Total.
I know that we are supposed to use sub queries but not sure how to begin.
SELECT State
, SUM(CASE WHEN Cat = 'Y' THEN 1 ELSE 0 END) as Yes
, SUM(CASE WHEN Cat = 'N' THEN 1 ELSE 0 END) No
, COUNT(*) as Total
FROM PEOPLE
GROUP BY STATE
Here:
SELECT upper(p.state) State,
(SELECT count(*) from PEOPLE p2 where p2.state = p.state and p2.cat = 'Y') Yes,
(SELECT count(*) from PEOPLE p3 where p3.state = p.state and p3.cat = 'N') No,
count(*) Total
FROM PEOPLE p
GROUP BY upper(state)
If you have any doubt about it, fell free to ask.

Totalprice column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to create a totalprice column in a table which gets its answer from multiplying 2 values from 2 different tables?
e.g
orderquantity
order_id | product_code | quantity
ordertotalprice
order_id | customer_id | totalprice
products
product_Code | product_name | product_desc | productcost
I want the totalprice column to be = productcost*quantity
To query the totals per order:
SELECT orderquantity.order_id,
SUM(products.productcost * orderquantity.quantity) AS total
FROM orderquantity
INNER JOIN products ON products.product_code = orderquantity.product_code
GROUP BY orderquantity.order_id
Wrap this query in an UPDATE statement to update column totalprice in table ordertotalprice:
UPDATE ordertotalprice
SET totalprice = grouped.total
FROM (
SELECT orderquantity.order_id,
SUM(products.productcost * orderquantity.quantity) AS total
FROM orderquantity
INNER JOIN products ON products.product_code = orderquantity.product_code
GROUP BY orderquantity.order_id
) AS grouped
WHERE ordertotalprice.order_id = grouped.order_id
I am assuming here table ordertotalprice already contains the necessary records. If that is not the case, then you will need an INSERT statement instead of an UPDATE. But then I wonder where to get the customer_id from.

Multiple Foreign Keys from a column to a table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is my tables description :
Ticket
Id int pk
ShowTicketStateID_FK int
TicketStateID_FK int
TicketState
Id int
Name int
Actually, the TicketState has two foreign keys to Tickettable.
Here is some rows from Ticket table :
1 1 1
2 2 1
3 1 2
...
And here is rows for TicketState
1 'sold'
2 'reserved'
Now i want this Result set :
1 sold sold
2 reserved sold
3 sold reserved
How should I select rows from ticket table with both states name?
SELECT T.ID, S1.Name AS ShowState, S2.Name AS TicketState
FROM Ticket AS T
JOIN TicketState AS S1 ON T1.ShowTicketStateID = S1.ID
JOIN TicketState AS S2 ON T1.TicketStateID = S2.ID
The key concept is using the two different table aliases for the TicketState table in a single query. The table has to be scanned twice (speaking loosely), once for the ShowTicketStateID lookup and once for the TicketStateID lookup.