I have two tables in MS Access 2013.
Table of SERVICE:
(ID is the primary key)
ID PART_ID SERVICE_DATE REMARK WITHDRAWN
1 A0001 01/04/2014
2 A0002 01/04/2014
3 A0003 01/04/2014 SCRAP
4 A0004 01/04/2014
5 A0001 01/05/2014
6 A0002 01/05/2014
Table of WITHDRAW:
(ID is the primary key)
ID PART_ID DRAW_DATE
1 A0001 02/04/2014
2 A0002 02/04/2014
3 A0001 02/05/2014
I'd like to put a "NO" into the field of WITHDRAWN in Table of SERVICE to indicate the part has not been withdraw yet, if:
Part is first time in Table of SERVICE and never appear in Table of WITHDRAW.
Part appears few times in Table of SERVICE and Table of WITHDRAW, already serviced this time, not withdraw yet.
Part is not under category of "SCRAP".
Targetted Result:
Table of SERVICE
ID PART_ID SERVICE_DATE REMARK WITHDRAWN
1 A0001 01/04/2014
2 A0002 01/04/2014
3 A0003 01/04/2014 SCRAP
4 A0004 01/04/2014 NO
5 A0001 01/05/2014
6 A0002 01/05/2014 NO
But I just know how to write the code for the first condition:
UPDATE SERVICE LEFT JOIN WITHDRAW ON SERVICE.PART_ID = WITHDRAW.PART_ID SET SERVICE.WITHDRAWN = "NO" WHERE WITHDRAW.PART_ID Is Null;
Can somebody amend my code so that I can achieve the targetted result? Thanks!
I think what you are looking for is the following query:
UPDATE service
LEFT JOIN withdraw
ON service.part_id = withdraw.part_id
AND service.service_date <= withdraw.draw_date
SET service.withdrawn = "no"
WHERE withdraw.part_id IS NULL
AND Nz(service.remark, "") <> "scrap";
Your rules where a bit hard to understand, but I think I got it. I threw rule 1 and 2 together as follows:
1.Part is first time in Table of SERVICE and never appear in Table of WITHDRAW.
2.Part appears few times in Table of SERVICE and Table of WITHDRAW, already serviced this time, not withdraw yet.
==> The service_date of the part must be greater than the last withdraw_date for every part.
Related
I have control dashboard where multiple tables are listed with query
And in dashboard I can switch it to one table from ALLData to User1Table... and vice versa.
When there is only one table chosed I can easily manipulate data. However, I am struggling with updating rows when ALLData(all tables) are listed in dashboard. I can update it checking each table. I was wondering is there any better way to update it.
Tables have no DR. All tables have same column names.
//ALLData
SELECT * FROM users1
UNION ALL
SELECT * FROM users2...
user1
id name tel status
1 Bob 911 1
user2
id name tel status
3 Anna 11 0
3 Jack 12 1
//ALLData in dashboard
id name tel status
1 Bob 911 1
3 Anna 11 0
3 Jack 12 1
I can use id and status as PK
I'm trying to join a few tables in MySQL. Our setup is a little unique so I try to explain as good as I can.
I have a table 'INVENTORY' that represents the current items on stock.
These items are stored in a table 'COMPONENT'
Components are being used in installations.
Every user can have multiple installations and the same component can be used in multiple installation as well.
To uniquely map a component to an installation, it can be assigned to a PRODUCT. a product as has a 1-1 relationship with an installation. A component is not directly related to an installation
To finally assign a product to a specific installation a mapping table COMPOMENT_PRODUCT is used.
Example:
A component is like a part, lets say a screw. This screw is used in a computer. The very same screw can be used on multiple computers. But each computer can only be used on one specific installation.
TABLE COMPOMENT_PRODUCT
COMPOMENT_ID PRODUCT_ID
1 1
1 2
2 1
2 2
So we have the components C1 and C2 relevant for two installations.
TABLE INVENTORY
COMPOMENT_ID INSTALLATION_ID ON_STOCK
1 1 5
1 2 2
What I want to achieve
Now, I want to retrieve the inventory state for all components. But, not every component has an inventory record. In these cases, the ON_STOCK value from the inventory shall be NULL
That means, for this example I'd expect the following results
COMPOMENT_ID PRODUCT_ID ON_STOCK
1 1 5
1 2 2
2 1 NULL
2 2 NULL
But executing this query:
SELECT DISTINCT
COMPONENT_PRODUCT.COMPONENT_ID,
COMPONENT_PRODUCT.PRODUCT_ID,
INVENTORY.ON_STOCK
FROM INVENTORY
RIGHT JOIN COMPONENT_PRODUCT ON COMPONENT_PRODUCT.COMPONENT_ID =
INVENTORY.COMPONENT_ID
returns the following resultset:
COMPONENT_ID PRODUCT_ID ON_STOCK
1 1 5
1 2 5
1 1 2
1 2 2
2 1 (null)
2 2 (null)
Now, my next thought was, "of course, this is how joins behave, okay I need to group the results". But the way SQL works, the aggregation is not entirely predictable. SO when I
GROUP BY COMPONENT_PRODUCT.COMPONENT_ID,COMPONENT_PRODUCT.PRODUCT_ID
I get this result:
COMPONENT_ID PRODUCT_ID ON_STOCK
1 1 5
1 2 5
2 1 (null)
2 2 (null)
I have prepared a Fiddle here: http://sqlfiddle.com/#!9/71ca87
What am I forgetting here? Thanks in advance for any pointers.
Try this query -
SELECT DISTINCT
COMPONENT_PRODUCT.COMPONENT_ID,
COMPONENT_PRODUCT.PRODUCT_ID,
INVENTORY.ON_STOCK
FROM INVENTORY
RIGHT JOIN COMPONENT_PRODUCT ON COMPONENT_PRODUCT.COMPONENT_ID =
INVENTORY.COMPONENT_ID
AND COMPONENT_PRODUCT.PRODUCT_ID = INVENTORY.INSTALLATION_ID
id user_id apt_id name value datetime
1 1 1 bp 109 ....
2 1 1 sugar 180 ....
3 2 2 bp 170 ....
I am trying to create the table in this approach because, the patient column is not the standard one, sometimes patient will be store the bp and sugar, sometime only bp.
Am i right in creating the design. If right, how to get the records of single patient.
Thanks,
If am not wrong, userid is your patientid in your scenario, in that case, use the below query to get the single patient record,
select * from Patienttable where user_id = '1'
Here you will get the single patient record. i.e., for user_id = 1
Output:
id user_id apt_id name value datetime
1 1 1 bp 109 ....
2 1 1 sugar 180 ....
Note: You can change as you want instead of 1
Others may disagree, but I wouldn't do it this way, unless you had several changing symptoms that you collect at different appointments. If it's a small collection (some of which are not collected), I would just add them as columns to the appointment table, and leave the sugar column as NULL when it's not collected.
user_id apt_id bp sugar datetime
1 1 109 180 ....
2 1 170 ....
The model you're proposing is a variant of Entity-Attribute-Value design, which has some strengths and some weaknesses. Aaron Bertrand had a good writeup of when an EAV design is useful, and what the costs are for that design. Based on the scenario you described, I don't think it's the best fit.
I have been working on a employee work management project and I am a little stuck. I have 3 tables:
1: employees
empid, empFirst empLast
1 jon smith
2 mark road
3 jane hall
2: holiday
id employee id datestart dateend
1 2 2015-08-07 2015-08-12
2 3 2015-07-4 2015-07-11
3 2 2015-07-20 2015-07-24
3: Task Assigned
id taskid assignedTo(userid) startTask endTask
1 1 1 2015-07-10 2015-07-14
2 2 2 2015-07-29 2015-07-29
3 2 3 2015-07-18 2015-07-30
4 3 2 2015-08-30 2015-09-03
5 4 2 2015-09-10 2015-09-03
I'm not sure how to go about querying the tables to see who is available for a task in a date range (multiple user assigned to the same task). I have a query which I would here:
so if you take the holiday table out if the equation and just run the query below
SELECT employees.empId, employees.empFirst, employees.empLast
FROM employees
LEFT JOIN taskassigned
ON employees.empId = taskassigned.assignedTo
WHERE taskassigned.assignedTo IS NULL or
not (taskassigned.startTask BETWEEN '2015-07-29 14:30:00' AND '2015-07-29 18:30:00'
or taskassigned.endTask BETWEEN '2015-07-29 14:30:00' AND '2015-07-29 18:30:00')
the result I get is:
empId empFirst empLast
1 jon smith (he is available)
2 mark road
2 mark road
As you can see Mark is not available on this date (in the task table).
I would like the query the holiday table first to see if they are on holiday then the task table to see if they already have a task on the date range then the result to show me how is available for the task.
I can't test this at the moment, but try:
SELECT employees.empId, employees.empFirst, employees.empLast
FROM employees
LEFT JOIN taskassigned
ON employees.empId = taskassigned.assignedTo
LEFT JOIN holiday
ON employees.empId = holiday.employeeId
WHERE (
taskassigned.assignedTo IS NULL
OR (
'2015-07-29 14:30:00' NOT BETWEEN taskassigned.startTask AND taskassigned.endTask
AND '2015-07-29 18:30:00' NOT BETWEEN taskassigned.startTask AND taskassigned.endTask
)
)
AND (
holiday.employeeId IS NULL
OR (
'2015-07-29 14:30:00' NOT BETWEEN holiday.dateStart AND holiday.dateEnd
AND '2015-07-29 18:30:00' NOT BETWEEN holiday.dateStart AND holiday.dateEnd
)
)
This would check to see if the specified start date doesn't fall inbetween the assigned task's start or end date, and if the specified end date doesn't fall inbetween the assigned task's start or end date, and then do the same for holidays.
Hi I don't have the right tools to test right now but here is what you can try to do:
when using date comparison:
try to convert/cast to DATE (make sure time is not included) to make sure the result is correct.
as far as I know when using between the start and end date are also included (maybe in some RDMS feature)
Also for including holiday, what you can do is like this (either):
first join with holiday table first then with the result join again with the task assigned table.
or
first join with task assigned table then with the result join again with the holiday table
Sorry for no code included, as I have no time to setup.
This is the first question i post in stackoverflow. Hope that you guys can help me resolve this problem. I have been stuck for 2 days.
I have 6 tables. It's all below:
students:
id name lastname
1 John Snow
2 Sansa Stark
3 T-Bag Bagwell
student_course:
id student_id course_id course_start course_end
1 1 1 2015-06-19 2015-08-20
2 2 3 2015-07-09 2015-09-15
3 3 1 2015-05-15 2015-08-22
payment:
payment_id student_id course_id
1 1 1
2 2 3
3 3 1
payment_initial:
payment_id payment_due
1 2015-06-12
3 2015-05-08
payment_installment:
payment_id payment_due int_payment_due
2 2015-07-02 2015-07-15
passport_visa:
student_id passport_expiry_date visa_expiry_date
1 2015-09-10 2015-10-12
2 2015-09-12 2015-09-15
3 2015-10-11 2015-9-28
And the result i want is: result will be sort by date combined form 3 tables which have "Date" field. "Date" field after sorting include only date after present.
How can I make query string that bring me the result like this:
student_id(1) course_id(1) course_start(2015-06-19)
student_id(2) course_id(3) payment_id(2) payment_due(2015-07-02)
student_id(2) course_id(3) course_start(2015-07-09)
student_id(2) course_id(3) payment_id(2) int_payment_due(2015-07-15)
student_id(1) course_id(1) course_end(2015-08-20)
student_id(3) course_id(1) course_end(2015-08-22)
student_id(3) passport_expiry_date(2015-09-12)
.....
I want to add Name and Lastname at the result but it show too long. So I just write like that.
Last result i want to get is the date field (sorted), and which event of date will happen (course start, course end, payment due...)
Sorry if my English grammar is bad. Please help me. Thank you all.
I just collect all result after insert data (which has date field) into one table, and sort them in that table. That makes more insert query but easy to sort.
So I do it on that way.