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
Related
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
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 4 years ago.
Improve this question
I have below database table,In here i need group some data and get sum and avg of some data.
In here i need to calculate Item marketers monthly sales(Each Month)
As a Example :
I need to get a Patric Newton's Sales in Each months.
And also i need to calculate AvarageFactor of each month Each employee.
AvgFactor = SUM(Daily Sales)/ SUM(Value Factor) * 100( Each Month )
Database Table
I have tried it like below,
SELECT ItemMarketerName,DailySales,ValueFactor,[Month],[Year]
FROM [SR_Hotel].[dbo].[Table_1]
WHERE ItemMarketerName IS NOT NULL
Group by ItemMarketerName,DailySales,ValueFactor,[Month],[Year]
Order by ItemMarketerName
You should not include your aggregated columns in your GROUP BY.
Try:
SELECT ItemMarketerName, [Month], [Year], SUM(DailySales), SUM(DailySales) / SUM(ValueFactor) * 100
FROM [SR_Hotel].[dbo].[Table_1]
WHERE ItemMarketerName IS NOT NULL
GROUP BY ItemMarketerName, [Month], [Year]
ORDER BY ItemMarketerName
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
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
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
How to select needed values without negative Total values according to this query?
Second row should not be returned as the Total value is less than 0.
SELECT DealerCode, PaymentType, Total
FROM DEL_Purchases
WHERE DealerCode = 'A0686P'
select DealerCode, PaymentType, Total from DEL_Purchases
where DealerCode='A0686P' and Total > 0