SQL query for an access database needed - ms-access

Hey guys, first off all sorry, i can't login using my yahoo provider.
anyways I have this problem. Let me explain it to you, and then I'll show you a picture.
I have a access db table. It has 'report id', 'recpient id', and 'recipient name' and 'report req'. What the table "means" is that do the user using that report still require it or can we decommission it.
Here is how the data looks like (blocked out company userids and usernames):
*check the link below, I cant post pictures cuz yahoo open id provider isnt working.
So basically I need to have 3 select queries:
1) Select all the reports where for each report, ALL the users have said no to 'reportreq'. In plain English, i want a listing of all the reports that we have to decommission because no user wants it.
2) Select all the reports where the report is required, and the batchprintcopy is more then 0. This way we can see which report needs to be printed and save paper instead of printing all the reports.
3)A listing of all the reports where the reportreq field is empty. I think i can figure this one out myself.
This is using Access/VBA and the data will be exported to an excel spreadsheet. I just a simple query if it exists, OR an alogorithm to do it quickly. I just tried making a "matrix" and it took about 2 hours to populate.
https://docs.google.com/uc?id=0B2EMqbpeBpQkMTIyMzA5ZjMtMGQ3Zi00NzRmLWEyMDAtODcxYWM0ZTFmMDFk&hl=en_US

I suggest:
SELECT DISTINCT o.reportid, o.ReportReq
FROM All_Reports AS o
WHERE o.reportid Not In (SELECT reportid FROM All_Reports
WHERE reportreq <>"N" Or reportreq Is Null)
There is a problem with this query in that I note that the sample document has a value for batchprintcopies where reportreq is null, so here are three possibilities:
1 Exclude reports where reportreq is null:
SELECT reportid, SUM(batchprintcopies) FROM All_Reports
WHERE reportreq <>"N"
GROUP BY reportid
HAVING Sum(batchprintcopies)>0
2 Group By reportreq to allow for further descisions:
SELECT reportid, reportreq, Sum(batchprintcopies) AS SumOfCopies
FROM All_Reports
GROUP BY reportid, reporteeq
HAVING Sum(batchprintcopies)>0
3 Include reports where reportreq is null:
SELECT reportid, SUM(batchprintcopies) FROM All_Reports
WHERE reportreq <>"N" Or reportreq Is Null
GROUP BY reportid
HAVING Sum(batchprintcopies)>0
It is unlikely, but not impossible that a field (column) contains a zero-length string. I reckon they should be avoided.
SELECT reportid FROM All_Reports
WHERE reportreq IS NULL OR reportreq = "";

1) This query works by taking each report ID and looking for a row where someone has not marked it as "not required" (with the assumption that 'n', and 'N' are the only ways to indicate that). If it finds any rows for that report ID that are still required.
SELECT DISTINCT report_id FROM table_name AS outer
WHERE NOT EXISTS
(SELECT report_id FROM table_name
WHERE report_req NOT IN ("n","N")
AND report_id=outer.report_id);
2) This query just adds up the values of batchprintcopy on a per-report_id basis (where the report is required, same assumption as above).
SELECT report_id, SUM(batchprintcopy) FROM table_name
WHERE report_req NOT IN ("n","N")
AND batchprintcopy > 0
GROUP BY report_id;
3)
SELECT report_id FROM table_name
WHERE report_req IS NULL OR report_req = "";

Related

SQL query to get agreement list

I have some analytical data for different cases. Each case is associated with one or more photos. Each photo is analyzed by two users.
The stored data looks like
What I want is to have SQL query to generate agreement result as shown below
So, for case 17116 there is agreement on photo 175062 from user id 26 and 27. Similar case is with photo id 176031 from user id 24 and 29.
Can somebody help me out to achieve this.
Thanks for sharing your valuable time.
Here is sample data to test with
Case Id,Photo Id,FeatureCheck,Result,CheckedBy
17116,173442,severity,none,24
17116,173442,severity,low,25
17116,175062,severity,none,26
17116,175062,severity,none,27
17116,175427,severity,medium,24
17116,175427,severity,high,28
17116,175748,severity,low,22
17116,175748,severity,none,30
17116,176031,severity,low,24
17116,176031,severity,low,29
17277,175309,severity,none,24
17277,175309,severity,none,25
17277,175649,severity,none,24
17277,175649,severity,none,25
You can try below query:
select PhotoId,
max(FeatureCheck),
max(Result),
max(CheckedBy),
min(CheckedBy)
from MyTable
group by PhotoId
having count(distinct FeatureCheck) = 1
and count(distinct Result) = 1
SELECT caseid, photo_id , feature_check, agreedupon,
group_concat(checkedby SEPARATOR ',') as listusers
FROM table1
GROUP BY case_id, photo_id
Asssuming the possibility of more than 2 users checked the data. Then grouping them is more dynamic.

Using union in codeigniter query builder, and filter in a virtual mysql column

In my project I am using datatables plugin with serverside processing. It works fine untill i do a search or order(sort) operation because it needs active record to do that.
My scenario is, i have an account table, revenue table and payment table, and I want to view all the data of revenue and payment table, thats why I need a union. my query is like below---
SELECT 'Income' as source, fld_type, fld_amount, ta.fld_account as account, fld_date, tbl_revenue.fld_description as fld_traninfo, tbl_revenue.fld_created as created
FROM tbl_revenue JOIN tbl_accounts as ta on tbl_revenue.fld_account_id = ta.fld_id
UNION
SELECT 'Expense' as source, fld_type, fld_amount, tae.fld_account, fld_date, tbl_payment.fld_description as fld_traninfo, tbl_payment.fld_created as created
FROM tbl_payment JOIN tbl_accounts as tae on tbl_payment.fld_account_id = tae.fld_id
Is there any way to use query builder in this query?
And second question, you can see I created a virtual column named 'source', i want to filter this column using where clause with append this query like below
WHERE source like "%a%" limit(10,0)
But this returns that I don't have any column name 'source', how can I filter this column?
Any help is appreciated.
there is a way to do that but its a bit hacky because codeigniter's querybuilder adds an auto SELECT statement to the query if you didn't specify it by yourself
In order to get what you want, you've to split your select statements in 2 queries and add the where clause to this query
Something like that should work:
$strQuery1 = $this->db
->select('income as source, fld_type, fld_amount, ta.fld_account as account, fld_date, tbl_revenue.fld_description as fld_traninfo, tbl_revenue.fld_created as created')
->from('tbl_revenue')
->join('tbl_accounts as ta', 'tbl_revenue.fld_account_id = ta.fld_id')
->get_compiled_select();
$strQuery2 = $this->db
->select('Expense as source, fld_type, fld_amount, ta.fld_account as account, fld_date, tbl_revenue.fld_description as fld_traninfo, tbl_revenue.fld_created as created')
->from('tbl_payment')
->join('tbl_accounts as ta', 'tbl_revenue.fld_account_id = ta.fld_id')
->get_compiled_select();
$strWhere = substr($this->db->like('source', 'a', 'both')->get_compiled_select(), 8);
$query = $this->db->query($strQuery1.' UNION '.$strQuery2.$strWhere);

Select AS is not working properly with jdbc

I am using JTable to display the result of a query. Table does not show as XYZ for all columns but it shows XYZ as a header for the fields not existing in database(manipulated fields).
Don't know much of database internals.Please forgive if it's too basic.
rs1 = st1.executeQuery("SELECT product.`id` as `Product ID`,product.`serialnumber` as `Serial Number`, product.`dop` as `Date Of Purchase` FROM product where product.`dop` between '"+from+"' and '"+to+"'");
reportTable.setModel(buildTableModel(rs1));
same query on query browser Output:
Product ID Serial Number Date Of Purchase
1 123244mf43m 08/08/2013
My Output With JDBC is:
id serialnumber dop //table header
1 123244mf43m 08/08/2013
There is a configuration setting described here:
http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html
useOldAliasMetadataBehavior
which, if set to true (the default in 5.0.x) will only return aliases (if any) for ResultSetMetaData.getColumnName() or ResultSetMetaData.getTableName() rather than the original column/table name.
Not sure if this is applicable to you, but could be the cause.

MS Access Query - Use a parameter value as one of the selected records

I am writing a MS Access Query with Parameters and wondered if it were possible to include one of the parameters as the returned selected records:
PARAMETERS [#SubmissionID] IEEEDouble, [#StartDate] DATETIME, [#EndDate] DATETIME;
INSERT INTO tblHUD_client_profile
(SubmissionID, ClientID)
SELECT [#SubmissionID] as SubmissionID, DISTINCT(ClientID)
FROM tblClientEducation
WHERE (BeginDate BETWEEN [#StartDate] AND [#EndDate]
OR EndDate BETWEEN [#StartDate] AND [#EndDate])
AND NOT EXISTS(
Select ClientID
from tblHUD_client_profile
WHERE SubmissionID = [#SubmissionID]
AND ClientID = tblClientEducation.ClientID
);
The "Select [#SubmissionID] as SubmissionID" always gives me a syntax error.
I apologize if there's a question with this solution already out there. I looked around for this but with so many basic questions about MS Access Queries and Parameters, I couldn't find what I was looking for.
Leave out # for MS Access:
PARAMETERS SubmissionID Integer;
SELECT [SubmissionID] as SubmissionID, DISTINCT(ClientID)
FROM tblClientEducation
After reviewing the original code, it seems to work as expected.

How do I copy multiple values from within the same table to other values in that same table in SQL?

First of all I'm rather new to SQL and so even though I believe a similar question was asked in this thread ( SQL Query - Copy Values in Same Table ) I literally can't understand it well enough to utilize the information. For that I apologize.
Now, I have a table that looks something like this:
company id | parameter name | parameter title
P | Parameter One | First Parameter
P | Parameter Two | Second Parameter
P | Parameter Three| Third Parameter
W | Parameter One | NULL
W | Parameter Two | NULL
Except that my table obviously has quite a lot of rows. I already went through filling in all the parameter titles where the company id was 'P' and would like to avoid manually doing the same for those with company id 'W'. My question is what SQL statement (this is in Microsoft SQL Server 2008) can I use to copy the values in the column "parameter title" where the company id is 'P' to the values in the same column where the company id is 'W' and both parameter names match up (W has less parameters than P)?
Using the previously linked thread I was able to come up with the following, but it spits out an error and I know it's not done correctly:
UPDATE COMP_PARAMETER_COPY
SET PARAM_TITLE=(SELECT PARAM_TITLE FROM COMP_PARAMETER_COPY P
WHERE P.COMP_ID = 'P' AND P.PARAM_TITLE=PARAM_TITLE)
WHERE COMP_ID='W'
(I'm playing around with a copy of the table instead of the actual table)
The error I get is "Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated."
Thank you for your help and advice,
-Asaf
You need to ensure that your subquery is only returning one result. Right now that error message is telling you that you're getting more than one record returned.
UPDATE W
SET PARAM_TITLE = (
SELECT PARAM_TITLE FROM COMP_PARAMETER_COPY P
WHERE P.COMP_ID = 'P' AND P.PARAM_NAME = W.PARAM_NAME
)
FROM COMP_PARAMETER_COPY W
WHERE W.COMP_ID = 'W'
Try giving the above SQL a whirl. This could still give you more than one result, but without knowing what your table looks like and what the data constraints are it's hard to give you something guaranteed to work.
Try adding the DISTINCT keyword to your query:
UPDATE COMP_PARAMETER_COPY
SET PARAM_TITLE=(SELECT DISTINCT PARAM_TITLE FROM COMP_PARAMETER_COPY P
WHERE P.COMP_ID = 'P' AND P.PARAM_TITLE=PARAM_TITLE)
WHERE COMP_ID='W'