Grouping Column Headers in MS Access - ms-access

Ok so I'm aware of grouping rows in Ms Access Reports or Crosstab Queries but can you group column headers?
I have a query with the following fields
DaysWork___Source___Opening___CashIN___CashOUT
1/10/18____Cashier1___1500______800______200
1/10/18____Cashier2___130_______100______900
2/10/18_____Cashier1__1500______500______250
2/10/18_____Cashier2__1300______150_______25
Can I get the query to be displayed like this in a report or another query
DaysWork___________Cashier1_______________________Cashier2
___________Opening___CashIN___CashOUT___Opening___CashIN___CashOUT
1/10/18______1500_______800______200_______1300_____100______900
2/10/18______1500_______500______250_______1300_____150_______25

Unfortunately, SQL has no required feature.
If cashier names are fixed and known number of cashiers, you can use a query like this:
SELECT DaysWork
,Max(IIf(Source <> 'Cashier1', NULL, Opening)) AS Opening_1
,Max(IIf(Source <> 'Cashier1', NULL, CashIN)) AS CashIN_1
,Max(IIf(Source <> 'Cashier1', NULL, CashOUT)) AS CashOUT_1
,Max(IIf(Source <> 'Cashier2', NULL, Opening)) AS Opening_2
,Max(IIf(Source <> 'Cashier2', NULL, CashIN)) AS CashIN_2
,Max(IIf(Source <> 'Cashier2', NULL, CashOUT)) AS CashOUT_2
FROM MyTable
GROUP BY DaysWork
Also you can create a query and use it instead of MyTable, where each cashier has a corresponding number and use those numbers instaed of Source column in report query. In this case you don't need to know cashier names, but number of different cashiers should be limited
SELECT DaysWork
,Max(IIf(CashierNo <> 1, NULL, Opening)) AS Opening_1
,Max(IIf(CashierNo <> 1, NULL, CashIN)) AS CashIN_1
,Max(IIf(CashierNo <> 1, NULL, CashOUT)) AS CashOUT_1
,Max(IIf(CashierNo <> 2, NULL, Opening)) AS Opening_2
,Max(IIf(CashierNo <> 2, NULL, CashIN)) AS CashIN_2
,Max(IIf(CashierNo <> 2, NULL, CashOUT)) AS CashOUT_2
FROM qry_OnMyTable
GROUP BY DaysWork

Related

Transferring data from one table to another, some relational columns

Table 1 - this table is completely populated from an XLSX file...
Tables 2 and 3 - contain 1-1 references for a couple of the columns in the final table.
Table 3 - the on I am trying to get populated with first three tables....
As you can see, the tables are sensible, there is no einsteinic equations or conversions going on. Here is the code that I have already tried, unsuccessfully:
INSERT INTO att_oem_orders SELECT NULL, ost.om_or_po, (SELECT j.job_id FROM jobs j WHERE j.project_number = project_no), NULL, (SELECT ao.id FROM att_oem WHERE ao.item_no = item_no), ost.po_number, (SELECT ol.id FROM order_lsc WHERE STATUS = ol.line_status_code), ost.ordered_date, ost.shipment_date, NULL, NULL, ost.item_qty, NULL, NULL, NULL, NULL, ost.shipping_to, ost.tracking_number, ost.carrier) FROM oem_temp_sync WHERE ost.item_qty > 0
Try limiting the number of possible values to put into a field using FIRST() like so:
INSERT INTO att_oem_orders SELECT
NULL,
ost.om_or_po,
(SELECT FIRST(j.job_id) FROM jobs j WHERE j.project_number = project_no),
NULL,
(SELECT FIRST(ao.id) FROM att_oem WHERE ao.item_no = item_no),
ost.po_number,
(SELECT FIRST(ol.id) FROM order_lsc WHERE STATUS = ol.line_status_code),
ost.ordered_date,
ost.shipment_date,
NULL,
NULL,
ost.item_qty,
NULL,
NULL,
NULL,
NULL,
ost.shipping_to,
ost.tracking_number,
ost.carrier
FROM oem_temp_sync WHERE ost.item_qty > 0
You also appear to have an extra ) just before FROM
With a little bit of help from everyone, I've found a few errors and fixed a few sub-queries. Here is the final version:
INSERT INTO att_oem_orders
SELECT
NULL,
ost.om_or_po,
(SELECT
FIRST(j.id)
FROM
jobs j
WHERE j.project_number = ost.project_no),
NULL,
(SELECT
FIRST(ao.id)
FROM
att_oem ao
WHERE ao.item_no = ost.item_no),
ost.po_number,
(SELECT
FIRST(ol.id)
FROM
att_oem_lsc ol
WHERE ol.status = ost.line_status_code),
ost.ordered_date,
ost.shipment_date,
NULL,
NULL,
ost.item_qty,
NULL,
NULL,
NULL,
NULL,
ost.ship_to,
ost.tracking_no,
ost.carrier
FROM
oem_sync_temp ost
WHERE ost.item_qty > 0

where clause and case statement giving empty records in select statement

I have a table called Tb_patientBeds.
Now I want to retrieve the records set as occupied, unoccupied or all based on the status column in this table.
Here are my other columns:
patientBedID int IDENTITY(1,1) NOT NULL,
patientBedType [varchar](20) NULL,
BedCharge [varchar](20) NULL,
status [varchar](20) NULL,
I wrote the query like
select * from Tb_patientBeds where [status]= case
when [status]= '0'
then 'occupied'
when [status]='1'
then 'unoccupied' else 'All'
end
The query is not returning records, it's showing empty records.
Could anybody help me in this regard?
Try this:
SELECT
CASE
WHEN [status] = 1 THEN
'unoccupied'
WHEN [status] = 0 THEN
'occupied'
ELSE
'All'
END,
*
FROM Tb_patientBeds

Stored procedure to fetch asset details based on input parameters

I am writing a stored procedure to fetch details based on CATEGORY ID passed into it, if the CATEGORY ID passed to it is not null, i am fetching based on CATEGORY ID , if the CATEGORY ID is null i am fetching all the details whose CATEGORY ID is except 3, can anyone help me in resolving this problem.
My procedure is like:
CREATE procedure [dbo].[SearchAssetdetails]
(
#assetCategory as int = null,
#assetType as int = null,
#assetDescription as nvarchar(200) = null,
#purchaseDate as datetime = null,
#validUpto as datetime = null)
as
begin
select ad.CategoryID,ad.AssetTypeId,ad.AssetDetailId,ad.AssetDescription,ad.Cost,ad.IsOwn,ad.LastModifiedby,ad.LastModifiedDatetime,ad.Location,ad.NoofLicences,ad.PurchaseDate,ad.SerialNumber,ad.ValidUpto,ad.VendorId,ad.Version,ad.WarrantyExpirationDate
from AssetDetails ad where
(ad.AssetDescription like (case when #assetDescription is not null then '%'+#assetDescription+'%' else ad.AssetDescription end)
and ad.CategoryID=(case when #assetCategory is not null then #assetCategory else ad.CategoryID end)
and ad.AssetTypeId=(case when #AssetType is not null then #AssetType else ad.AssetTypeId end)
and ad.PurchaseDate=(case when #purchaseDate is not null then #purchaseDate else ad.PurchaseDate end)
and ad.ValidUpto=(case when #validUpto is not null then #validUpto else ad.ValidUpto end)
) end;
Due to the AND condition in where clause, your inputs to other parameters may be mismatching for input category id as null but replaced with 3.
First, select records with other parameters and see if any of them fetches a record with category id 3.
I suggest you show us records that have category id as 3, to give us a better picture on data.
Else there is no error I believe.

Join on an optional null value

I'm having trouble with the following query:
SELECT
job.name,
job_element.label,
job_element_role_hours.role,
job_element_role_hours.hours_budgeted,
latest_rate.hourly_rate,
( job_element_role_hours.hours_budgeted * latest_rate.hourly_rate ) AS line_cost
FROM
job_element
INNER JOIN job ON job_element.job = job.id
INNER JOIN job_element_role_hours ON job_element_role_hours.element = job_element.id
LEFT JOIN(
SELECT
rate.*
FROM
rate
LEFT JOIN rate AS newest ON (
rate.role = newest.role
AND COALESCE(rate.client_company, 1) = COALESCE(newest.client_company, 1)
AND COALESCE(rate.client_group, 1) = COALESCE(newest.client_group, 1)
AND COALESCE(rate.client_contact, 1) = COALESCE(newest.client_contact, 1)
AND newest.date_from > rate.date_from
)
WHERE newest.id IS NULL
) AS latest_rate ON (
latest_rate.role = job_element_role_hours.role
AND (
COALESCE(latest_rate.client_company, 1) = COALESCE(job.client_company, 1)
OR latest_rate.client_company IS NULL
)
AND (
COALESCE(latest_rate.client_group, 1) = COALESCE(job.client_group, 1)
OR latest_rate.client_group IS NULL
)
AND (
COALESCE(latest_rate.client_contact, 1) = COALESCE(job.client_contact, 1)
OR latest_rate.client_contact IS NULL
)
)
WHERE job.id = 4
So... this query fetches a list of Elements for a Job. Each Element is broken down by "Role Hours" - an Element called "Build an HTML email", for example, might have two hours of Designer, and two hours of Developer - and then the calculates a cost based on the Hourly Rate for each Role.
The issue is where I join the latest_rate subquery back to the main query. Here's my rates table:
'CREATE TABLE `rate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`client_company` int(11) DEFAULT NULL,
`client_group` int(11) DEFAULT NULL,
`client_contact` int(11) DEFAULT NULL,
`role` int(11) DEFAULT NULL,
`date_from` datetime DEFAULT NULL,
`hourly_rate` decimal(18,2) DEFAULT NULL,
`last_edited_by` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
)
There might be several rates for a role: a "global" rate where the role is set but the client_company, client_group and client_contact fields are NULL; a company-specific rate where the role and client_company are set but everything else is NULL, and a client-specific rate where the role, client_company and client_contact is set but the client_group is NULL.
I want to get the one rate for the role that "most closely" matches the client_company, client_group and client_contact set for the Job. So if there's a Rate record with the role and client_company set (and the client_company matches the one for the Job), I want that one. Otherwise, I'll fall back to the one with NULL client_company.
My attempt at joining is clearly wrong:
AND (
COALESCE(latest_rate.client_company, 1) = COALESCE(job.client_company, 1)
OR latest_rate.client_company IS NULL
)
It'll return all the records that either match the client_company, or have a NULL client_company.
How can I get it to just get the one with the most matching fields?
Thanks :)
What you could try is have one query that basically gets all matching rates against one parameter that you absolutely want to match - seems to be role in your case.
Then you can do soemthing like:
MAX(
CASE WHEN <conditions 1> THEN <calculation 1> END,
CASE WHEN <conditions 2> THEN <calculation 2> END,
...
)
This would give you the maximum rate possible, i.e, the rate applicable if you had the most matching fields.

SQL Server 2008 Merge Soft Delete Error

I'm trying to perform a soft delete on a row in my target table using the SQL server 2008 MERGE command.
I think this should fall under the "when not matched by source" section, since the source is missing the row and the target still has it. All I want to do is set the IsActive bit to false, but I'm getting an error.
"Attempting to set a non-NULL-able column's value to NULL."
What am I missing?
The Users table is:
[ID] [nvarchar](50) NOT NULL,
[FirstName] [nvarchar](200) NULL,
[LastName] [nvarchar](200) NULL,
[EmailAddress] [nvarchar](200) NULL,
[IsActive] [bit] NOT NULL
The Merge statement is:
merge into Users
using TempUserTable lu
on Users.ID = TempUserTable.ID
when matched then
update set
ID = lu.ID,
FirstName = lu.FirstName,
LastName = lu.LastName,
EMailAddress = lu.EmailAddress,
IsActive = lu.Status
when not matched then
insert (ID, FirstName, LastName, EmailAddress, IsActive)
values (lu.ID, lu.FirstName, lu.LastName, lu.EmailAddress, lu.Status)
when not matched by source then
update set IsActive = 0;
You can get this to work exactly as you want but for me I needed to add a condition in the NOT MATCHED line.
So try something like...
WHEN NOT MATCHED BY SOURCE
AND TARGET.[IsActive] = 1
AND TARGET.[DeletedOn] IS NULL
THEN UPDATE
SET
TARGET.[IsActive] = 0,
TARGET.[DeletedOn] = SYSDATETIMEOFFSET()
It appears that your temp table TempUserTable either has a NULL in the IsActive column or the ID column.