SQL stored procedure with 3 tables - sql

I have the following tables linked here: tables.
What I have is here: procedure.
I need to list all projects and tasks belonging to a given customer (customer number given as a parameter). At the beginning of the report show the customer name followed by the first project, displaying the project and name. For each project display beneath it the task number (in ascending order), description, hours and start date. Follow the last task with the customer’s total number of tasks and project charges then do the same for each subsequent project for the customer. At the end of the report, give the total number of hours for that all the tasks, as well as how many projects and tasks were reported.
I'm not really sure how procedures work so any help is appreciated. Thank you!

The nextproject cursor has no relationship defined between TASK and either of the other tables used in the cursor, so every row in TASK is being joined to all the valid combinations of PROJECT and CUSTOMER. This is known as a "Cartesian join" and is probably not what you had in mind. To correct this you need modify the cursor slightly:
cursor nextproject is
select p.pjno, c.custname, t.taskno, t.descrip, t.hrs, t.start_date
from task T, project P, customer C
where C.custno = P.custno and
p.custno = custnum
AND t.pjno = p.pjno; -- ADDED
This may or may not fix all your problems, but it should move you along with this assignment.
SQLFiddle here for those who care to take a stab at it.
Share and enjoy.

Related

SQL select statement for generating form letters

I currently have a database in MS Access that I need to update the way we generate form letters.
We use a query I made 10 years ago to generate certificate of attendance letters.
SELECT doctors.address, seminar_title, seminar_date, first_name, last_name
FROM doctors, seminars, registrations
WHERE seminars.seminar_id=[Seminar] AND registrations.seminar_id=seminars.seminar_id AND
registrations.dr_id=doctors.dr_id
ORDER BY doctors.last_name;
The result is a set of letters for whatever seminar ID you enter. Each letter representing an individual registration by a doctor.
The issue now is that we have an additional table (Licenses), and I need each letter to display 1 to 5 licenses that a doctor might have.
The fields in the Licenses table are: license_ID, doctor_id, license_type, state, and license_number.
I can display one license, but getting a varying amount of license numbers and states to display, each matched on a letter to its licensee, has been beyond what I know how to do so far.
Any input is appreciated.
You'll want to create a subreport listing all of the licenses for the doctor the letter is addressed to. First you will need a table linking doctors to the licenses they have (e.g. "doctor_licenses"), with the fields dr_id and license_id. Add doctors.dr_id to your query above so it is part of the recordsource for your main report. Then create a second report to display the licenses for each doctor. The recordsource for your second report will be something like
SELECT dr_id,
license_type
FROM doctor_licenses
INNER JOIN licenses
ON doctor_licenses.license_id = licenses.license_id
Add the second report to your main report as a subreport. Link the subreport to your the main report by setting its "Link Master Fields" and "Link Child Fields" properties both to dr_id. More info on subreports and how to create and use them here:
https://support.microsoft.com/en-us/office/create-and-use-subreports-816f987a-4615-4058-8f20-ce817093bb33
EDIT because I can't comment on your original post, you'll still need to use a subreport to list doctor licenses as there is a one-to-many relationship between doctor and licenses (one doctor can have one or more licenses). If you're trying to add the list of licenses as a comma delimited list or something similar to your report you'll have to utilize a more complicated approach. Please look at the subreport solution and if that doesn't do what you need please be more specific about your requirements.

Multiple aggregates via join in Teradata

I have two tables
Email Contact History
Place of Service
that share a primarymembercustomerid. The Email Contact History has three fields:
Campaigncode
Primarymembercustomerid
maildate
and the Place of Service table has three fields
primarymembercustomerid
servicedate
serviceshortDesc
primarymembercustomerids are selected for E-mail campaigns, then if they walk into one of our branch offices and receive services, they show up in the Place of Service table. I want to count the number of primarymembercustomer ids that are mailed, and right next to it I want to have a count of primarymembercustomerids that showed up to a branch office.
What I have so far:
select
ch.campaigncode,
pos.serviceshortdesc,
count(ch.primarymembercustomerid),
count(pos.primarymembercustomerid)
from mktprodvm.cdmv_prmmbr_contacthist_email ch
right outer join mktprodvm.cdmv_pos pos on ch.primarymembercustomerid = pos.primarymembercustomerid
where ch.campaigncode = 'EDT_ALLACMO'
and pos.servicedate between '2017-02-01' and '2017-02-28'
group by 1,2
What I'm ending up with is a count of primarymembercustomerids that walk into a branch for that time period, but I'm not getting the total count of primarymembercustomerids that were E-mailed. I thought that by doing a right outer join I would get the total number of primarymembercustomerids that were mailed, but it's not working for me. I feel like I need to do some kind of subquery or correlated subquery, but I've ready about how to use them and I don't think that's right. I've never used them before and to be quite honest I'm not that great of a SQL coder either. Thanks for any help!
Because my low reputation on this site I can't comment, so I'm writing this as an answer.
I think that you are using the wrong type of join (or writing the tables in the wrong order). If you don't want to lose rows of your main table, the Email Contact History, you have to do a LEFT JOIN not a RIGHT JOIN.
Also, I don't know if it's possible, but I'm guessing that a primarymembercustomerid can have more than one service and since you are selecting the serviceshortdesc, a single Email might count in different rows of your anwerset and the total won't be accurate. According to what you said you want, I don't see a reason for including the service description in the SELECT.

SQL Server How to Build Query?

I'm kind new in this forum but I'm stuck in a problem and I need our help.
I Have one table with several lines where each line represent one project, then in another table I'll have many tasks that need to be done in each project, each task would have a percentage of at what level is, and the result of this two tables is one table where I'll have the process Id and also the percentage of accomplished with the average of the last entries of every tasks values.
I can't figured out the query that needs to be done to have the result that I want.
Can anyone help me? You can see follow the link bellow to see tables and the result that I want.
Table iamges
I didnt understand the colors of rows you used, but with your description, i think this is the query you are looking for:
select P.id_Proceso, P.SubProceso, avg(R.estado)
from Processos P
join Registros R
on P.ID = R.Id_processo
group by P.id_Proceso, P.SubProceso

List a column value that has a certain number or great, referencing two tables

I have two tables named Project and Task. Check below for a portion out of the tables:
PROJECT
ProjectNumber ProjectTitle
TASK
ProjectNumber TaskNumber Hours
I need to list all project which have at least 2 tasks, displaying the project number, how many tasks for that project, and the total number of hours worked on tasks for that project.
I'm not sure how to do this because there are multiple tasks for one project. Any information is appreciated. Thank you!
If the table is needed, I'm not sure how I can add it because I can't link websites. Also, this is in sqlplus.
I think this should work:
select
projectnumber,
count(*) as count_of_tasks
sum(hours) as sum_hours
from
task
group by
projectnumber
having
count(*)>1

Recurring Orders

Hi everyone I'm working on a school project, and for my project I chose to create an ecommerce system that can process recurring orders. This is for my final project, I'll be graduating in May with an associates in computer science.
Keep in mind this is no where a final solution and it's basically a jumping off point for this database design.
A little background on the business processes.
- Customer will order a product, and will specify during checkout whether it is a one time order or a weekly/monthly order.
- Customer will specify a location in which to pick up their order (this location is specific only to the order)
- If the value of the order > 25.00 then it is accepted otherwise it is rejected.
- This will populate the orders_test and order_products_test tables respectively
Person on the back end will have a report generated for deliveries for the day based on these two tables.
They will be able to print it off and it will generate a list of what items go to what location.
Based on the following criteria.
date_of_next_scheduled_delivery = current date
remaining_deliveries > 0
Once they are satisfied with the delivery list they will press "Process Deliveries" button.
This will adjust the order_products_test table as follows
Subtract 1 from remaining_deliveries
Insert current date into date_of_last_delivery_processed
Based on delivery_frequency (i.e. once, weekly, monthly) it will change the date_of_next_scheduled_delivery
status values in the order_products_test table can either be active, hold, or canceled, expired
I just would like some opinions if I am approaching this correctly or if I should scratch this approach and start over again.
A few thoughts, though not necessarily complete (there's a lot to your question, but hopefully these points help):
I don't think you need to keep track of remaining deliveries. You only have 2 options - a one time order, or a recurring order. In both cases, there's no sense in calculating remaining deliveries. It's never leveraged.
In terms of tracking the next delivery date, you can just keep track of the day of the order. If it's recurring -- monthly or weekly, regardless -- everything is calculable from that first date. Most DB systems (MySQL, SQL Server, Oracle, etc) support more than enough date computation flexibility so that you can calculate this on the fly, as opposed to maintaining such a known schedule.
If the delivery location is only specific to the order, I see no use in creating a separate table for it -- it's functionally dependent on the order, you should keep it in the same table as the order. For most e-commerce systems, this is not the case because they tend to associate a list of delivery locations with accounts, which they prompt you about when you order more than once (e.g., Amazon).
Given the above, I bet you can just get away with 2 of your 4 tables above -- Account and Order. But again, if delivery locations are associated with Accounts, I would indeed break that out. (but your question above doesn't suggest that)
Do not name your tables with a "_test" suffix -- it's confusing.