Optimize Images retrieval from database - sql

I have two tables in a database which are "Setup" and "Items". "Setup" contain an image column with a data type of varbinary(max) and about 21 rows of records and "Items" also contains 12000 records. "Setup" table joins with "Items" table. The problem is when I run my join query with Image in it, it takes about 2 minutes but if I take the image column out of the query it takes 0 seconds. And if I maintain the image column but change the data type of image column to varbinary(1000) it takes 0 seconds. I want to use varbinary(max) as a data type for image column and execute the query in 0 seconds.
This is my query:
SELECT PS.[Item Image], P.[ItemId], P.SetupId, PS.Consumable, PS.Category,
PS.[Description], PS.Brand, PS.[Item Name], P.[Serial #], P.[Vendor Name],
P.[Date Received], [Location], S.[State] AS [Item State], Location
FROM PHOTOMATICS AS P
INNER JOIN PHOTOMATICSETUP AS PS
ON PS.[SetupId] = P.SetupId
LEFT JOIN PHOTOMATICSTATE AS S
ON S.PhotomaticId = P.ItemId ORDER BY P.ItemId

Related

Optimizing Access Query with Multiple sub-queries

I have a table of data that contains baseline project information, called ADMIN_WORK. I have a separate table that contains various attributes related to each project listed in ADMIN_WORK called WR_ATTRIBUTE. The structure of WR_ATTRIBUTE is such that each project has about 300 attributes (300 rows in the table) each with a unique name associated with the project number. I need to pull approximately 15 of these attributes into a final table.
I built a query with a number of sub-queries, but it takes multiple hours to run. The code below represents pulling a single attribute (called Circuit) from WR_ATTRIBUTE for all projects.
SELECT ADMIN_WORK.WR_NO AS [WR Number],
ADMIN_WORK.PREMISE_ID AS [Premise ID],
AttributeCircuit.Circuit
FROM ADMIN_WORK INNER JOIN
(SELECT ADMIN_WORK.WR_NO AS [WR Number], WR_ATTRIBUTE.ATTRIBUTE_VALUE AS Circuit
FROM ADMIN_WORK INNER JOIN
WR_ATTRIBUTE ON ADMIN_WORK.WR_NO = WR_ATTRIBUTE.WR_NO
WHERE (((WR_ATTRIBUTE.WR_ATTRIBUTE_CODE)="Circuit") AND ((ADMIN_WORK.WR_TYPE_CODE)="ACNEM"))
) AS AttributeCircuit
ON ADMIN_WORK.WR_NO = [AttributeCircuit].[WR Number]);
My final query has about 14 more of these subqueries similarly implemented, each with a different WR_ATTRIBUTE_CODE.
My final table is generated accurately, but it is extremely slow. I am wondering if there is a better way of structuring my query.
You probably just want conditional aggregation:
SELECT w.WR_NO AS [WR Number],
w.PREMISE_ID AS [Premise ID],
a.Circuit
FROM ADMIN_WORK as INNER JOIN
(SELECT a.WR_NO AS [WR Number],
MAX(IIF(a.WR_ATTRIBUTE_CODE)="Circuit" a.ATTRIBUTE_VALUE, NULL) AS Circuit ,
. . .
FROM WR_ATTRIBUTE as a
GROUP BY WR_NO
) as a
ON w.WR_NO = a.WR_NO ;

Option group on access form as query parameter

I am working in Access 2016 and I have a from that the user can select the training type the want to run the report on. All trainings has an option value of 1, Bloodborne has an option value of 2 and so on. I want to take the value of my form and pass it into a query so it will display all employees that took that training.
My form is View February Training and contains a option group within a frame that is called FrameAllorCurrent. My query contains the training field which I am able to filter with the following Like "68" Were 68 is the training ID from the training table.
I know I have to do something in the training field of the query along the lines of
[forms]![View February Training].[FrameAllOrCurrent].Value = 2
but how do I make it so when it equals 2 it makes the training field "68" and returns all my bloodborne trainings?
SQL Query:
SELECT CompletedTrainings.RecordID
,CompletedTrainings.Employee
,CompletedTrainings.Training
,CompletedTrainings.CompletedDate
,CompletedTrainings.ExpiredDate
,EmployeeInformation.Employee
,EmployeeInformation.Active
,Trainings.TrainingID
,Trainings.[Training Name]
FROM Trainings
INNER JOIN (
EmployeeInformation INNER JOIN CompletedTrainings
ON EmployeeInformation.ID = CompletedTrainings.Employee
) ON Trainings.TrainingID = CompletedTrainings.Training
WHERE (((EmployeeInformation.Active) LIKE "-1"))
ORDER BY Trainings.[Training Name]
There are several solutions to this. But one way to do it would be:
SELECT CompletedTrainings.RecordID
,CompletedTrainings.Employee
,CompletedTrainings.Training
,CompletedTrainings.CompletedDate
,CompletedTrainings.ExpiredDate
,EmployeeInformation.Employee
,EmployeeInformation.Active
,Trainings.TrainingID
,Trainings.[Training Name]
FROM Trainings
INNER JOIN (
EmployeeInformation INNER JOIN CompletedTrainings
ON EmployeeInformation.ID = CompletedTrainings.Employee
) ON Trainings.TrainingID = CompletedTrainings.Training
WHERE EmployeeInformation.Active LIKE "-1"
AND [Training Name] =
SWITCH (
[Forms]![View February Training].[FrameAllOrCurrent] = 1, "Whatever 1 should be",
[Forms]![View February Training].[FrameAllOrCurrent] = 2, "68",
[Forms]![View February Training].[FrameAllOrCurrent] = 3, "If there's a 3... etc."
)
ORDER BY Trainings.[Training Name]
NOTE: I am uncertain what your related field is, so its listed as [Training Name], this also assumes the column is in TEXT format, based on your quotations earlier. If the data type is numeric, just remove the quotes.

TSQL Show greatest Parcel# from row

I have data that looks like this.
Select distinct
x.[PropertyBasics.PK Resnet Property ID], x.filname,
c.mv_30day_value '30 Day Value As Is',
c.mv_30day_repvalue '30 Day Value Repaired',
t.parcel 'Parcel#',x. *
from
Resnet_Reporting_ops.dbo.Ops_FullExportFLATV3 as X (NOLOCK)
left join
resnet_mysql.dbo.form_tax t (nolock) on x.[PropertyBasics.PK Resnet Property ID] = t.property_id
left join
resnet_mysql.dbo.form_fm c (nolock) on t.task_id = c.task_id
where
X.[PropertyBasics.Property Basics - ResID] = 217
and x.[PropertyBasics.PK Resnet Property ID] = 1153829
How do you get this data to only show 1 record for Parcel #?
select distinct tests the ENTIRE row to see if it is unique. Just the smallest little difference in any column will make a row "distinct". The following 2 values are different, so that will cause 2 rows
741722064100000
741-722-06-41-00000
and the following 2 value pairs are different, which cause 2 more rows:
500000 800000
435000 850000
in all that combines to make 4 rows.
So, it looks like we could strip the dash from data item 2 above, and that would make it equal data item 1.
replace(t.parcel,'-','') AS [Parcel#]
But is that always true? Could there be other differences in other rows not shown here?
How do we decide between the value pairs 3. or 4. ? MAX()won't work e.g.
MAX(c.mv_30day_value), MAX(c.mv_30day_repvalue)
would produce
500000 8500000
and that combination doesn't exist in the source data
The logic required to meet the expected result isn't well defined.
Try the following:
SELECT
x.[PropertyBasics.PK Resnet Property ID]
, x.filname
, MAX(c.mv_30day_value) "30 Day Value As Is"
, MAX(c.mv_30day_repvalue) "30 Day Value Repaired"
, replace(t.parcel,'-','') "Parcel#"
-- , x.* NO WAY !!
FROM Resnet_Reporting_ops.dbo.Ops_FullExportFLATV3 AS X
LEFT JOIN resnet_mysql.dbo.form_tax t ON x.[PropertyBasics.PK Resnet Property ID] = t.property_id
LEFT JOIN resnet_mysql.dbo.form_fm c ON t.task_id = c.task_id
WHERE X.[PropertyBasics.Property Basics - ResID] = 217
AND x.[PropertyBasics.PK Resnet Property ID] = 1153829
GROUP BY
x.[PropertyBasics.PK Resnet Property ID]
, x.filname
, replace(t.parcel,'-','')
;
Note. x.* isn't feasible with GROUP BY as you need to specify the columns will define each unique row. x.* is also counter productive with "select distinct" as for each additional column of output you increase the possibility of more rows. (i.e. more columns generally = more differences = more rows). Also as mentioned doubt MAX() produces a good result here.

Filling not existing data with 0 SQL / Access

Please see below explanation of my problem. I have 2 tables.
[TABLE1: LocationsOFproducts - Keep data about location of products and quantity][1]
[TABLE2: Datahistory - keep data about stock movement ( removing/adding product from/to stock )][2]
[QUERY1: SumDataHistoryQuery - Making Sum of Products moved from the same location][3]
QUERY2: TOTAL QTY - Counting how many products left in some location after stock movement
SQL Code:
SELECT LocationsOFproducts.[Bay no]
,LocationsOFproducts.[Product Code]
,LocationsOFproducts.LocationQTY
,SumDatahistoryQuery.SumOfQTY
,([LocationQTY]) + ([SumOfQTY]) AS TOTALQTY
FROM LocationsOFproducts
INNER JOIN SumDatahistoryQuery ON (LocationsOFproducts.[Product Code] = SumDatahistoryQuery.[Product Code])
AND (LocationsOFproducts.[Bay no] = SumDatahistoryQuery.[Bay no])
AND (LocationsOFproducts.[Product Code] = SumDatahistoryQuery.[Product Code])
GROUP BY LocationsOFproducts.[Bay no]
,LocationsOFproducts.[Product Code]
,LocationsOFproducts.LocationQTY
,SumDatahistoryQuery.SumOfQTY
,([LocationQTY]) + ([SumOfQTY])
ORDER BY LocationsOFproducts.[Product Code];
RESULT:
I expect a little bit different result.
My query should check is there any more value with Bay no,product code and QTY in Datahistory table, if not I want to fill that space with data from LocationOfproducts table and in the field SumOfQTY fill those
values with 0 to make a sum in TOTALQTY column.
Please see below what I need to get:
Please see query what I need to get - I maked that in Photoshop by cutting and pasting fields:
Can anyone know how to write the right query?

SQL query date column sorting issue

I am writing a SQL query which will return data of customer bills payments.
We have these payments in two different tables, one is "Bills" and other is "receipts". When my query data it shows that "receipts data" (from receipts table) in ascending order. I have two more date columns to show in result set which are "bill month" and "due date" (from bills table). When I try to apply order by on any of these mentioned columns it shows wrong data. Please help.
My query is attached below. thank you.
select distinct
bil.bill_id as Bill#, --(bill), this table has bill month and due date column
rec.receiptid as IDs, --(receipts),this table has receipt payment date,amount
sp.spaceno as spaceno,-- (space), this table has property names
lo.description as locaiton,-- (location),this table has details of property
one.name as owner_name,-- (owner), this table has owners information
bt.billtype as Particular,--(billtype),where bill types are saved
rec.rdate as Date,-- this column of receipt table showing wrong date data
dc.dr as opening,-- (DCnotes), this table has total payable amount column
rec.amount as dr,-- this column tells the paid amount
bil.amount as cr,-- this columen tell the payable amount
bil.balamount as balance,-- this column tells the net total of dr-cr columns
bil.billmonth as BillMonth,-- bill month column from bill table
bil.duedate as DueDate -- bill due date column from bill table
from
bills bil
inner join
Owners one on bil.ownerid = one.ownerid
inner join
Receipts rec on bil.spaceid = rec.propertyid
inner join
DCnotes dc on bil.spaceid = dc.property
inner join
BillTypes bt on bil.billtypeid = bt.billtypeid
inner join
Spaces sp on bil.spaceid = sp.spaceid
inner join
Locations lo on sp.locationid = lo.locationid
where
bil.siteid = '15'
This is the image of SQL query result:
This is the image of required data in report:
Please check attached images of required data and SQL query result