So I'm using SAP Business One application and using B1 validation configuration to give error messages. The main goal of this is where it compares addresses from the BP master data address and the address from the sales order/delivery order address.
So here's the code for the first query which is for the open sales orders only:
SELECT dbo.ORDR.DocNum, dbo.ORDR.DocStatus, dbo.RDR12.StreetS, dbo.RDR12.BlockS, dbo.RDR12.CityS, dbo.RDR12.ZipCodeS, dbo.RDR12.StateS, dbo.RDR12.CountryS
FROM dbo.ORDR INNER JOIN
dbo.RDR12 ON dbo.ORDR.DocEntry = dbo.RDR12.DocEntry
WHERE (dbo.ORDR.DocStatus = 'o')
Heres the code for the second query from the business partner data. This contains all addresses and data
SELECT dbo.CRD1.Street, dbo.CRD1.Address, dbo.CRD1.Block, dbo.CRD1.ZipCode, dbo.CRD1.City, dbo.CRD1.Country, dbo.CRD1.State
FROM dbo.CRD1 INNER JOIN
dbo.OCRD ON dbo.CRD1.CardCode = dbo.OCRD.CardCode
So now I'm hoping to be able to create an SQL condition where it compares these two. Like for example (pseudo code):
if(street != street.s)
begin
if(zip != zip.s)
begin
if(country != country.s).....
begin
Select 'error' for browse
else
select 'passed' for browse
Overall I'm just trying to compare the 2 queries with ONLY open sales orders/delivery orders.
So I'm trying to get it to trigger the error message.
The problem being, I don't know how to pull the values from each one since there's tons of addresses to compare from and I can't just hard code it in.
For example the inputted data is 91234 for zipcode, and zipcode.s is 92134 which is obviously different and would give the error message.
Related
I've constructed two tables: one to list the services and another to link the service to each department.
Table 1 contains the columns [s id,s name]. [s_id,s_name].
-- The Services table, records all services for all departments. [1,Install Windows 10],[2,print Payslip].
Table 2 : services_assignments consist of columns [ss_id,ss_s_id_ss_d_id].
-- services_assignments matching the services with departments.
I need to return the service that "NOT" matched for the department example.
i tried to use JOIN with Where conditions in selection but not result as the following code.
function get_services_for_assign(){ // for assigmnets
$this->db->select('*');
$this->db->from('services');
if($this->uri->segment('2')) {
$this->db->join('services_assignments','services.sr_id = services_assignments.ss_s_id','left');
$this->db->where_not_in('services_assignments.ss_d_id',$this->uri->segment('2'));
}
//$this->db->where('sr_display','1');
$data=$this->db->get();
return $data->result();
}
On the other hand, I attempted to write it manually as seen below.
function get_services_for_assign(){ // for assigmnets
$dep=$this->uri->segment('2');
$query = "SELECT sr.sr_id FROM services AS sr WHERE sr.sr_id = (SELECT sa.ss_s_id FROM services_assignments AS sa WHERE sa.ss_d_id = 1)";
$this->db->query($query);
$data=$this->db->get();
return $data->result();
}
and I encountered the following error; I discovered numerous results that were similar to my problem, but I couldn't solve it.
A Database Error Occurred
Error Number: 1096
No tables used
SELECT *
Filename: C:/xampp/htdocs/townteam/system/database/DB_driver.php
Line Number: 691
I need your help to return the results services that were not used by the other department.
You have not stated which version of CodeIgniter you are using or what the expected behaviour is when no department id is passed.
This should work in both CI3 and CI4. If using CI4, you should probably use prepared statements. I could not find documentation on passing parameters into a multi-condition join with CI so I have written the query out in full.
$sql = <<<'SQL'
SELECT s.*
FROM services s
LEFT JOIN services_assignments sa
ON s.sr_id = sa.ss_s_id
AND sa.ss_d_id = ?
WHERE sa.ss_s_id IS NULL
AND s.sr_display = 1
SQL;
$this->db->query($sql, [$this->uri->segment('2')]);
pry(main)> Loan.joins(:statistics).where(state: <some states>).where.not(statistics: {state: <some other states>}).order(created_at: "desc").last.statistics.map(&:state)
2015-09-21 20:53:54,423|65310|DEBUG|development| - Loan Load (0.9ms) SELECT `loans`.* FROM `loans` INNER JOIN `statistics` ON `statistics`.`loan_id` = `loans`.`id` WHERE `loans`.`state` IN ('started', 'pending_declined') AND (`statistics`.`state` NOT IN ('prequalified', 'conditionally_approved', '4506t_results_uploaded', 'customer_forms_uploaded', 'ready_for_etran', 'etran_verified', 'forms_to_be_verified', 'forms_verified', 'credit_memo_entered', 'loandoc_generated', 'loandoc_completed', 'loandoc_customer_received_need_signatures', 'signatures_checked_and_uploaded', 'boarded')) ORDER BY `loans`.`created_at` ASC LIMIT 1
2015-09-21 20:53:54,426|65310|DEBUG|development| - Statistic Load (0.3ms) SELECT DISTINCT `statistics`.* FROM `statistics` WHERE `statistics`.`loan_id` = 97
=> ["started", "prequalified", "conditionally_approved", "customer_forms_uploaded", "ready_for_etran", "pending_declined"]
So, maybe I'm not understanding what's going on here... I'm asking SQL to find me some Loans where their Statistics do not contain certain values. In this example, I'm saying to leave out any loans with a Statistic of prequalified, but, as you can see from the print out, the Loan#statistics does have prequalified, along with several other states I'd like to leave out.
Can anyone shed some light on this? I've been fighting with it for hours, and my head is spinning at this point.
With that ActiveRecord query, you've:
First, found a set of loans
next, ordered by created_at
then, used last to find limit to 1 result, finding the oldest of the set
So, you have an instance of Loan.
Since you called #statistics on the method, I can infer that loan has_many :statistics, and you've found all statistics that holds a foreign key value that matches the instance of Loan that you found. Now you have set of statistics.
For the set of statistics, you've mapped them to the map attribute.
Since you've already joined the statistics try removing .last.statistics from your query. User map on the result set to its state. Also, consider using #includes or #select.
It because you use last.statistics. It means the result on loan object will be joined with statistics whereas you have created condition before.
Look at your last result query:
Statistic Load (0.3ms) SELECT DISTINCT `statistics`.* FROM `statistics` WHERE `statistics`.`loan_id` = 97
remove your last.statistics
Loan.joins(:statistics).where(state: <some states>).where.not(statistics: {state: <some other states>}).order(created_at: "desc").map(&:state)
or
if you want to add condition to determine some loans that you need in before map(&state)
Loan.joins(:statistics).where(state: <some states>).where.not(statistics: {state: <some other states>}).where("loans.id IN (97)").order(created_at: "desc")
You query returns product of Loan and Statistic so it still returns Loan records that have some Statistic that does not have state you specified.
If you only want Loan that has no Statistic on those states at all you probably want your SQL to be something along this line:
SELECT loans.*,
FROM loans
LEFT OUTER JOIN (
SELECT statistics.loan_id, COUNT(*) count
FROM quotes
WHERE statistics.state IN ('prequalified', 'conditionally_approved')
GROUP BY statistics.loan_id
) statistics
ON statistics.loan_id = loans.id
WHERE loans.state IN ('started', 'pending_declined')
AND statistics.count IS NULL;
My SQLfu is not what I'd be proud of so this might not be the most optimised query ever but it should get the result you expect.
You could convert that to ActiveRecord query interface but unfortunately subquery and LEFT JOIN are not really supported, at least not in the way that we going to use it will be something like this:
join_query = <<SQL
LEFT OUTER JOIN (
SELECT statistics.loan_id, COUNT(*) AS count
FROM statistics
WHERE statistics.state IN (<<state>>)
) statistics ON loans.id = statistics.loan_id
SQL
Loan
.joins(join_query)
.where(statistics: { count: null })
.where(state: <<somestate>>)
.order(created_at: :desc)
The <<SQL ... SQL is Heredoc by the way if you're not familiar with it.
I have a table request:
and a table requesthx:
A single Request can have many log updates by multiple techs. For example, tech1 can create a log for the initial phone contact and add notes in the log section. Then tech2 could take those notes and complete a portion of the job requirement. Tech 3 could also be working on the same job waiting for a scheduled appointment.
If tech3 is logged in, I'd like to display a count of every open ticket for tech3.
What is the best way to accomplish this?
I've completed several ugly queries that had queries running within foreach loops to add to a count variable, but this seems like the really long way to reach a simple count total.
I've tried...
SELECT requesthx.hxID, requesthx.requestID,
requesthx.datetime_gmt, requesthx.log, requesthx.techID, requesthx.status,
COUNT($requestTable.requestID) AS tickets, request.status, requesthx.techID
FROM requesthx
LEFT JOIN request
ON (requesthx.requestID = request.requestID)
WHERE (requesthx.status <> 'closed'
AND request.status = 'open'
AND requesthx.techID = '1')
GROUP BY requesthx.techID;
...on a query that had 5 open tickets with techID = 1. I only receive 1 as the count. Any ideas?
You are correct the database has a way of counting this kind of information.
Select count(*) as "TicketCount"
From requesthx
Where techid=3
And status = "open"
Group by techid
I assume my own value for status fields in both tables
SELECT
`requesthx`.`hxID`
, `requesthx`.`requestID`
, `requesthx`.`datetime_gmt`
, `requesthx`.`log`
, `requesthx`.`techID`
, `requesthx`.`status`
, COUNT(`request`.`requestID`) AS tickets
, `request`.`status`
, `requesthx`.`techID`
FROM
`requesthx`
LEFT JOIN `test`.`request`
ON (`requesthx`.`requestID` = `request`.`requestID`)
WHERE (`requesthx`.`status` ="logged"
AND `request`.`status` ="open"
AND `requesthx`.`techID` =3)
GROUP BY `requesthx`.`techID`;
I have a report in Access 2013 that prints an equipment log. There is a bunch of dates listed for each piece of equipment. I wanted to only print the newest date for each piece of equipment. I have searched the internet and this site with no luck. So any suggestions will be greatly appreciated.
My SQL statement is:
SELECT dbo_eq_location_transfer_d.equipment_id, dbo_equipment.description, dbo_eq_location_transfer_d.transaction_no, dbo_eq_location_transfer_d.job_no, dbo_jobs.description, dbo_eq_location_transfer_d.date_booked, dbo_eq_location_transfer_d.delivery_time, dbo_eq_location_transfer_d.line_no, dbo_eq_location_transfer_d.row_modified_by, dbo_eq_location_transfer_d.comment
FROM (dbo_eq_location_transfer_d INNER JOIN dbo_jobs ON dbo_eq_location_transfer_d.job_no = dbo_jobs.job_no) INNER JOIN dbo_equipment ON dbo_eq_location_transfer_d.equipment_no = dbo_equipment.equipment_no
ORDER BY dbo_eq_location_transfer_d.equipment_id, dbo_eq_location_transfer_d.transaction_no;
The date_booked field is the date field I am trying narrow down. I have a simple SQL query that works and I have been trying copy that into the about SQL but cannot seem to get it to mesh. It is:
SELECT [dbo_eq_location_transfer_d.equipment_no], Max(dbo_eq_location_transfer_d.date_booked) AS ["Newest Date"]
FROM dbo_eq_location_transfer_d
GROUP BY [dbo_eq_location_transfer_d.equipment_no];
In your query set the date fields criteria to:
>Now()-30
This will show any dates for the last 30 days just change 30 to the number of days you want to see.
Now that I understand your structure & data, here is what I did:
(1) Create the following query to select only the most recent 'date_booked' for each 'equipment_no'; save the query with name '23020071_A':
SELECT dbo_eq_location_transfer_d.equipment_no,
First(dbo_eq_location_transfer_d.transaction_no) AS FirstOftransaction_no,
First(dbo_eq_location_transfer_d.job_no) AS FirstOfjob_no,
First(dbo_eq_location_transfer_d.date_booked) AS FirstOfdate_booked
FROM (dbo_eq_location_transfer_d
INNER JOIN dbo_jobs ON dbo_eq_location_transfer_d.job_no = dbo_jobs.job_no)
INNER JOIN dbo_equipment ON dbo_eq_location_transfer_d.equipment_no = dbo_equipment.equipment_no
GROUP BY dbo_eq_location_transfer_d.equipment_no
ORDER BY First(dbo_eq_location_transfer_d.date_booked) DESC;
(2) I created the following query combining the new query with your existing query:
SELECT dbo_eq_location_transfer_d.equipment_id, dbo_equipment.description,
dbo_eq_location_transfer_d.transaction_no, dbo_eq_location_transfer_d.job_no,
dbo_jobs.description, dbo_eq_location_transfer_d.date_booked,
dbo_eq_location_transfer_d.delivery_time, dbo_eq_location_transfer_d.line_no,
dbo_eq_location_transfer_d.row_modified_by, dbo_eq_location_transfer_d.comment
FROM 23020071_A INNER JOIN ((dbo_eq_location_transfer_d
INNER JOIN dbo_jobs ON dbo_eq_location_transfer_d.job_no = dbo_jobs.job_no)
INNER JOIN dbo_equipment ON dbo_eq_location_transfer_d.equipment_no = dbo_equipment.equipment_no)
ON ([23020071_A].FirstOftransaction_no = dbo_eq_location_transfer_d.transaction_no)
AND ([23020071_A].equipment_no = dbo_eq_location_transfer_d.equipment_no)
AND ([23020071_A].FirstOfjob_no = dbo_eq_location_transfer_d.job_no)
ORDER BY dbo_eq_location_transfer_d.equipment_id, dbo_eq_location_transfer_d.transaction_no;
Now when I run the second query, it returns only the most recent row for that piece of equipment.
I am very frustrated from linq to sql when dealing with many to many relationship with the skip extension. It doesn't allow me to use joinned queries. Not sure it is the case for SQL server 2005 but I am currently using SQL Server 2000.
Now I consider to write a store procedure to fetch a table that is matched by two tables e.g. Album_Photo (Album->Album_Photo<-Photo) and Photo table and only want the Photos data so I match the Album's ID with Album_Photo and use that ID to match the photo. In the store procedure I am just fetch all the joinned data. After that in the linq to sql, I create a new Album object.
e.g.
var albums = (from r in result
where (modifier_id == r.ModifierID || user_id == r.UserID)
select new Album() {
Name = r.Name,
UserID = r.UserID,
ModifierID = r.ModifierID,
ID = r.ID,
DateCreated = r.DateCreated,
Description = r.Description,
Filename = r.Filename
}).AsQueryable();
I used the AsQueryable to get the result as a IQueryable rather than IEnumerable. Later I want to do something with the collection, it gives me this error:
System.InvalidOperationException: The query results cannot be enumerated more than once.
It sounds like you have a situation where the query has already executed by the time you are want to filter it later in your code.
Can you do something like...
var albums = (blah blah blah).AsQueryable().Where(filterClause) when you have enough info to process
what happens if you try albums.where(filter) later on in the code? Is this what you are trying?