SQL Access - Transpose Multiple results into one row - sql

I am really struggling with the following problem as I am fairly new to SQL.
Problem: The projtask table has multiple tasks for 1 project. I need to transpose results so that I show all the statuses (e.g. task 150, 130, 110, 70 status) for every task on a single result row against 1 project.
At the moment I am coming back with multiple result rows against 1 project due to the number of tasks associated with that project. I hope this makes sense. If not please probe. Thanks, all the help would be appreciated :)
Ultimately I want the result to look like:
Project X - Task 10 - Status C - Task 130 - Status A - Task 150 - Status C
Project Y - Task 10 - Status A - Task 130 - Status C - Task 150 - Status A
Project Z - Task 10 - Status C - Task 130 - Status C - Task 150 - Status C
SELECT IIf(dbo_projtask.[task-num]=150 And dbo_projtask.stat='C','Released') AS 150_status, dbo_projtask.[proj-num],
IIf(dbo_projtask.[task-num]=130 And dbo_projtask.stat='A','Active') AS 130_status
FROM dbo_projtask
GROUP BY IIf(dbo_projtask.[task-num]=150 And dbo_projtask.stat='C','Released'), dbo_projtask.[proj-num],
IIf(dbo_projtask.[task-num]=130 And dbo_projtask.stat='A','Active');**

Not sure if i understand correctly, but trying to create columns for all tasks under a project doesn't sound like a scalable solution. Why not create an resultset with ProjectID, TaskID and StatusID and do any processing/modifications clietside? Relational databases tend to not like ragged/dynamic columns all that much. If you are absolutely set on the proposed structure you'd need to build a dynamic query that uses a pivot construction of sorts, but have my doubts whether it will work if you have a flexible number of tasks per project.

Related

Scheduling of jobs through SQL Server stored procedure

I have to write a stored procedures for scheduling the Azure pipelines (Jobs).
Frequency ----Number of times batch needs to run in a day
Timing column will have entry for batch start time
Table A will have static entries for batches. Frequency denotes in a day how many times job will run and timing column will have the batch run time separated by comma(,)
Batch_ID Batch_Name Frequency Timing
-----------------------------------------------
1 ABC 2 7:00,13:00
Table B will have listing of jobs corresponding to one particular batch.This table will be static and have one time entry like table B.
Table B
Batch_ID JOB_ID JOB_NM
--------------------------------
1 1 Job_1
1 2 Job_1
Table C will contain the dependencies of the jobs in a batch
Table C
Batch_ID JOB_ID DEPENDENY_JOB_ID
----------------------------------------
1 1
1 2 1
When Batch executes, table D will be populated with batch start time.
Table D
Batch_ID Batch_Name Status start_Time end_time
-------------------------------------------------------
1 abc Start 7:00
As soon as Table E is populated,table D will populated with Job details.Job 2 will start only when job 1 finishes.
Table E
Batch_ID Batch_Name JOB_ID JOB_NM Start_Time End_Time
----------------------------------------------------------------------
1 abc 1 Job_1 7:00
1 abc 2 Job_2 7:15
When Job 2 completes then we will update the Table D end time column.
Once first run is completed, we need to check frequency column of table A and run the job again (if it's more than 1) and do the entire exercise again.
In case our 1st batch didn't complete before the start time of batch 2 then we have to hold the 2nd batch until batch 1 is completed.
Could anyone help me how to start this?
As #Gordon Linoff said, you are lacking a question on your "question".
If I can give an opinion on this, I dont think its a good design idea to split your logic between data factory and stored procedures in a database. Be mindful that in the future, the user mantaining the pipelines may not have access to the database and will not be able to understand half of it. Even if YOU are the one mantaining this, 2 years from now chances are you are going to forget what you did and following the line between 2 resources may take you more time than it should. It will also make troubleshooting harder.
It really depends on the scenario you are working on, but to sum it up: try to have everything logic related in one place.
Hope this helped!

operation must use an updateable query error Access [duplicate]

This question already has an answer here:
Why is my query not updateable?
(1 answer)
Closed 5 years ago.
I have a master table tblBudget which contains entries like
ProjID Type Budget Active
101 ROM 100 No
101 PLE 110 No
101 DLE 120 Yes
102 ROM 200 No
102 PLE 210 Yes
Every month I get an excel which i import and store into a temp table tblMonthlyBudget that contains entries like
ProjID Type Budget Active
101 EAC 100 Yes
102 DLE 110 Yes
I wrote an update query that tries to update all the Active entries in tblBudget to No so that new records which are the most active could then be inserted. My query is
UPDATE tblBudget
INNER JOIN tblMonthlyBudget
ON tblBudget.ProjectId = tblMonthlyBudget.ProjectID
SET tblBudget.Active = false
However I get the error
operation must use an updateable query
even though the query seems to show correctly in the datasheet and design view. I get it only while executing the query . I tried searching for the error and have tried all sorts of combinations without success. Any alternative approach is welcome.
I suspect this is because the temporary table contains more than one records.
I have tried to replicate your problem using the data and table structures, you provided. But the UPDATE works as I would expect. It will still work if there are repeated entries in tblMonthlyBudget - it just updates an excessive number of times. Please read mcve and alter your question with data and table structure that actually produces your error. Otherwise we have almost zero hope of helping you.

in theory: would this be possible: SQL query for efficient use of build parts

I have a couple of tables, which I have simplified to the extreme for the purpose of this example.
Table 1, 'Units'
ID UnitName PartNumber
1 UnitX 1
2 UnitX 1
3 UnitX 2
4 UnitX 3
5 UnitX 3
6 UnitY 1
7 UnitY 2
8 UnitY 3
Table 2, 'Parts'
ID PartName Quantity
1 Screw 2
2 Wire 1
3 Ducttape 1
I would like to query on these tables which of these units would be Possible to build, AND if so, which one could be built first ideally to make efficient use of these parts.
Now the question is: can this be done using SQL, or is a background application required/more efficient?
So in this example, it is easy, because only one unit (unit Y) can be built.. But I guess you get the idea. (I'm not looking for a shake and bake answer, just your thoughts on this.)
Thanks
As you present it, it is efficient to use sql. As you described PartNumber column of table Units is a foreign key on ID column of Parts table, so a simple outer join or selecting units that the PartNumber are "NOT IN" the Parts table would give you the units that can not be build.
However if your db schema consists of many non normalised tables, or is very complex without indexes, other "bad" things etc
it could be examined whether specific application code is faster. But i really doubt it for the particular case, the query seems trivial.

Query in Access (adding missing values)

I've got two tables, Guides and Availability. Guides is a list of guides, and Availability is a list of dates for which information is known about a particular guide. For instance 03/03/14 - Guide 3 - Available or 05/03/14 - Guide 1 - Busy. If a guide's availability on a particular day isn't know yet, there isn't an entry in Availability at all for that day.
I want to write a query that will return a list of the statuses of all the guides during the next three days. If the status isn't known it will say Unknown.
I'm sure in SQL this task is definitely doable, but I am constrained by having to write a Web App in Access 2013. This means I must use the query editor in Access. Can anyone with expertise offer some guidance? If anything is unclear in the question, do say so.
Example query output:
03/03/2013 - Guide 1 - Busy
03/03/2013 - Guide 2 - Unknown
03/03/2013 - Guide 3 - Unknown
04/03/2013 - Guide 1 - Available
04/03/2013 - Guide 2 - Available
04/03/2013 - Guide 3 - Unknown
05/03/2013 - Guide 1 - Unknown
05/03/2013 - Guide 2 - Unknown
05/03/2013 - Guide 3 - Unknown
Table: Guides
Guide 1
Guide 2
Guide 3
Table: Availability
03/03/2013 - Guide 1 - Busy
04/03/2013 - Guide 1 - Available
04/03/2013 - Guide 2 - Available
P.S.: Someone has suggested I might need to make a third table listing all the dates for the next 10 years. This wouldn't be a problem if it helps with a solution
If you are limited to Access, you won't be able to use recursive CTE's (google it to find out about them, if interested).
therefore, you would have to create a table with all dates. Provided the table is called AllDates, and the date field is Dte, the query will look like this (you may need to filter by date range to limit the number of returned rows, otherwise you will see the records for all 10 years):
SELECT Availability.Dte, Availability.Guide, Availability.Status
FROM Availability INNER JOIN Guides ON Availability.Guide = Guides.Guide
UNION ALL
SELECT AllDates.Dte, Guides.Guide, 'Unknown' AS Status
FROM AllDates, Guides
WHERE NOT EXISTS (SELECT 1 FROM Availability WHERE Availability.Dte = AllDates.Dte And Availability.Guide = Guides.Guide)
ORDER BY 1, 2;
For the web-version of Access you will need to use 2 queries. The first one (DatesGuides) will list all guides for all dates, and its syntax is as simple as:
SELECT Guides.Guide, AllDates.Dte
FROM AllDates, Guides;
The second query will be LEFT JOINED with the first one. Its syntax is:
SELECT DatesGuides.Dte, DatesGuides.Guide, Nz([Status],'Unknown') AS Expr1
FROM Availability RIGHT JOIN DatesGuides ON Availability.Dte = DatesGuides.Dte AND Availability.Guide = DatesGuides.Guide;

SQL (Access): Multiple values per "ID" - Store as TRUE unless any FALSE

Good afternoon!
I have recently come across an issue that I am hoping can be solved with your help. Our system is [sadly] ran on Access (2007). I have decent experience with SQL and elect to use this method for most of queries rather than the Design View. However, this is the issue I have come across recently:
A table (with its own primary key of course) contains the ParticipantID and Records. This table may contain multiple records per person due to having events at various locations. With this information we track whether or not each record is within our system already due to the location falling under our large "umbrella" (Internal). To make it look simple, it looks something like this, ignoring the primary key as we only care about the participant ID.
ParticipantID Internal
1 -1
1 -1
2 0
3 -1
3 -1
3 0
4 -1
4 0
I want to be able to say if ANY of the records of a participant are not Internal (eg. =0), then in this query's results, store it as 0.
Hence, the results table would look something like:
ParticipantID Internal
1 -1
2 0
3 0
4 0
Does this make sense? Thank you in advance!
You can use Max:
SELECT internal.ParticipantID, Max(internal.Internal) AS MaxOfInternal
FROM internal
GROUP BY internal.ParticipantID;
I built the above using the query design window.
If the values Internal can be only 0 and -1 the following may help
Select ParticipantID,max(internal) from thetable
Group by ParticipantID