SQL IF [complex condition] THEN OUTPUT NEW TABLE [closed] - sql

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 7 years ago.
Improve this question
I'm not sure how to approach this problem.
I need to output a table containing just one row with "yes" IF some complex condition is met. How would I got about doing this?
The condition is something like this: "if the age difference between the oldest and youngest professors between 2004 and 2008 is at most 10 years"...
I'm using PostgreSQL.
Thanks.

Based on your description, something like this should work.
SELECT
CASE WHEN
(DATE_PART('year', MAX(Birthdate)) - DATE_PART('year', MIN(Birthdate))) * 12 +
(DATE_PART('month', MAX(Birthdate)) - DATE_PART('month', MIN(Birthdate)))
<= 120 THEN 'Yes'
ELSE 'NO'
END
FROM Professors
WHERE DateEmployed BETWEEN '2004-01-01' AND '2008-12-31';
SQL Fiddle example

Related

Can you Help me to write optimize sql query? [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 1 year ago.
Improve this question
My query too much time to execute for fetching data.
SELECT `app`.*, `pat`.`title`, `email_address`, `bad_debtor`, `county`, `date_of_birth`, `dentist_id`, `doctor_id`, `ethnicity`, `first_name`, `middle_name`, `last_name`, `gender`, `pat`.`mobile_phone`, `pat`.`prevent_appointment_booking`, `pat`.`use_email`, `pat`.`use_sms`, `pat`.`recall_method`, `pat`.`status`, `pat`.`pat_id`, `pat`.`active` FROM `patients` `pat` LEFT JOIN `appoiment` `app` ON `pat`.`id` = `app`.`patient_id` WHERE (date(app.start_time) > date(NOW()) - INTERVAL 7 DAY) and `app`.`state` IN ('Completed') and `app`.`patient_id` is not NULL GROUP BY `pat`.`id`
This part
WHERE (date(app.start_time) > date(NOW()) - INTERVAL 7 DAY) and `app`.`state` IN ('Completed') and `app`.`patient_id` is not NULL
the problem is in where clause which takes too much time.
help me to optimize query.
replace app. * with the correct column you need

SQL - Get number of Product in an order and total value [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 4 years ago.
Improve this question
Current Database:
tbl_Order:
ID.........UserName........date
1..........john............march 30 2017
2..........mike............march 31 2017
tbl_Products_In_Order
Order_ID.........Qty_Purchased........UnitPrice........TotalPrice
1...............2.....................1.00.............2.00
1...............3.....................2.00.............6.00
2...............1.....................8.00.............8.00
i need the output to be:
Order_ID...UserName...date............Number_Of_Products.........Total_Qty_All
1..........John.......march 30 2017...2..........................5
2..........mike.......march 31 2017...1..........................1
Can someone help me create the SQL query?
Here is a PostgreSQL solution (but using just basic stuff, so should be same or very similar in most SQL dialects):
SELECT
Order_ID,
UserName,
date,
count(Order_ID) AS Number_Of_Products,
sum(Qty_Purchased) AS Total_Qty_All
FROM
tbl_Products_In_Order AS pio
INNER JOIN tbl_Order AS o ON pio.Order_ID = o.ID
GROUP BY Order_ID, UserName, date
ORDER BY Order_ID

update sql query in new 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 7 years ago.
Improve this question
I have a column in which i have 'goswara no. 21/1980' i need only 21 and year from date column like this col gr=21/year(date) through sql2005 query
http://sqlfiddle.com/#!6/bf991/6
something like this?
select
right(columnname,4) as year ,
SUBSTRING ( columnname ,len(columnname) - 6 ,2 ) as result
from test

How to display two expression result in different column in oracle sql [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 8 years ago.
Improve this question
I have a table ACCOUNT(ACCOUNT_NUMBER,BRANCH_NAME,BALANCE)
Now I need to find in Oracle SQL : all accounts with balances over R.100000 receive 6 percent interest whereas all others receive 5 percent.
I need to display the balance with 6% and balance with 5% in two different column.
What is the SQL query for it?
Maybe this is what you want:
select
account_number,
case
when balance > 100000 then balance*1.06
end AS With6PercentInterest,
case
when balance <= 100000 then balance*1.05
end AS With5PercentInterest
from ACCOUNT;
Sample SQL Fiddle

postgres count customers with 2 or more orders [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
I need to get monthly numbers of customers who made 2 or more orders. I'm not that good with postgresql so any help would be appreciated.
Table name is shop_orders
Relevant columns are 'email' - customer, 'status_code' should have value 'CONFIRMED', date - timestamp.
Thank you.
Try this:
select EXTRACT(month FROM datecolumn ) as month,email
from shop_orders
where status_code = 'CONFIRMED'
group by EXTRACT(month FROM datecolumn ),email
having count(1) >2