Replacing info in Access - sql

I have a table within Access that looks similar to this:
ProductID - ClientID - ProductName - Description
118 - 5 - Full Search - xxxx
35 - 2 - Update - xxxx
105 - 7 - O & E - xxxx
I want to be able to call the ProductID and have it return the ProductName field.
ProductID = Forms!Orders.Product.Value()
Any help?

You can use DLookUp:
DlookUp("ProductName","Products","ProductID=" & SomeNumericID)
There are other ways, but you need to say where you are coming from and going to.

Related

SQL query to get different columns from a json string using a pattern matching

I have a json string - {"exteriorCheck":{"criteria":[{"code":"EXTERIOR_BODY","title":"Exterior - XYZ","value":5},{"code":"EXTERIOR_RIMS","title":"Exterior - ABC","value":4}],"images":[{"code":"EXTERIOR_PICTURES","keys":["share-tasks-b1c757e3-0cb6-41ea-a298-f3430aafb36c/0"]}],"comment":"i.o "},"interiorCheck":{"criteria":[{"code":"INTERIOR_SEATS","title":"Interior - Seats","value":5}
I want to create a column whenever there is "title" like for "title":"Exterior - XYZ"- the column would be Exterior - XYZ and the values would be taken from "value":5 , so 5 will be my output. Since there multiple such cases in the string- it is difficult to use substr with position. I have tried -
select
case when "json" like '%Exterior - XYZ%' then substr("JSON",89,1)
else null end as "Exterior - XYZ". But for the entire json its difficult to get the position.
Desired output:
Exterior - XYZ | Exterior - ABC | Interior - Seats
5 | 4 | 5
How to proceed using AWS Athena (considering multiple string functions wont work at athena)

SSRS Row Values To Column

I'm creating reports using Reporting Services. I want to show some values stored in the in columns. For example let's say I have a table as;
Target | Type | Value
- - - - - - - - - - -
Store A |Type I | 4
Store A |Type II | 5
Store A |Type III | 16
Store B |Type I | 10
Store B |Type II | 25
I want to list these values as;
Target | Type I | Type II | Type III
- - - - - - - - - - - - - - - - - - -
Store A |4|5|16
Store B |10|10|NULL(or 0)
Here is how I manage the situation right now, I use join as many as I need, so I can show these values in colums. However, when the data is too big it causes too many problems as expected. I wonder if there is an easier way to solve that problem?
You can use PIVOT in your data extraction SQL query like so
select
[Target],[Type I],[Type II],[Type III]
from
(
select * from yourTbl
) src
PIVOT
(
Max([Value]) for [Type] in ([Type I],[Type II],[Type III])
)p
Either group columns in the tablix https://www.youtube.com/watch?v=zM5DRsnH3E0 or perform the grouping in sql server using pivot. https://learn.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot You may need a dynamic pivot if the columns are not static.

SQL Server - Update field in all table records, using a field from the same table and values from other table

I have this scenario:
Table Territory
ID (int) - CODE (varchar) - NAME (varchar)
Data:
1 - GB - UNITED KINGDOM
2 - GB - ISLE OF MAN
3 - GB - NORTHERN IRELAND
4 - PT - PORTUGAL
5 - DE - GERMANY
6 - DE - HELGOLAND ISLAND
Table Rules:
ID (int) - TERRITORY_CODES (varchar) - TERRITORY_IDS (varchar)
1 - 'GB,PT' - NULL
2 - 'DE,PT' - NULL
I know the second table should not be like this, but I have no option to change it.
I want to fill the column TERRITORY_IDS with the IDs from the table TERRITORY separated by comma. For example:
Table Rules
ID (int) - TERRITORY_CODES (varchar) - TERRITORY_IDS (varchar)
1 - 'GB,PT' - '1,4'
2 - 'DE,PT' - '5,4'
There are several IDs for each territory code, but I want only one ID for each territory table, it could be the first one, doesn't matter.
What you are looking to do is a Bad Idea. It is a good thing that you recognize this is a bad Idea. But for those reading this question and do not understand why it is bad, this violates the First normal form (1NF) principle. Which is all columns should be atomic, meaning that they hold 1 and only 1 value.
Lets get to the nuts and bolts on how to do this Coalesce to the rescue.
Since I do not know why 'gb,pt' and 'de,pt' are grouped that way I didnt wrap this in a Cursor to go through the whole table. But you can easily wrap this in a cursor and do the entire table contents for you.
DECLARE #TERRITORY_Ids varchar(100)
SELECT #TERRITORY_Ids = COALESCE(#TERRITORY_Ids+ ', ', '') +
Id
FROM table_terrytory
WHERE code in ('gb','pt')
INSERT INTO table_rules
SELECT 'gb,pt',#TERRITORY_Ids

SQL Query to compare multiple rows

I'm new here, and new to SQL. I have searched, but I can't seem to find an answer to my question. Maybe you gurus can help. I have a table that has customer ID numbers and their status (number) among other things. For instance, a few lines would be like this:
Status - ACCTnum - CustName - City - State
95 - A330 - Billy Burger - Cleveland - Oh
11 - A330/Q - Billy Burger Store#2 - Cleveland - Oh
15 - B250 - Spanky - Columbus - Oh
15 - B250/Z - Spanky#2 - Springfield - OH
15 - B250/Y - Spanky#3 - Miami - FL
We see here, there is a main account number, and a sub account number, but they occupy the same field. Account A330 is billy burger, and his second store is A330/Q. The status column is for their salesman number. If the number is 95, it is a dead account. The problem is, for our purposes, the status of a main account cannot be dead if a sub account is in good standing. So what I need is a query that can basically select any records that meet the criteria: "If ACCTnum is status 95, and has sub accounts that are not status 95"
Ideally if I ran the query on the table above it should return the first two records, since A330 is status 95 and A330/Q is not. It should ignore the other records.
I have tried the INTERSECT command with no success (I assume because it only works for two different tables?). I am a total SQL n00b, be gentle ;)
select distinct c1.*
from Customers c1
inner join Customers c2 ON c2.ACCTnum LIKE c1.ACCTnum + '%'
where c1.ACCTnum.status = 95 and c2.ACCTnum <> 95

SQL Distinct Query for Two Tables

Table1:
id - name - address
-----------------------------
1 - Jim - Some Street
2 - Adam - Some Street
3 - ABC - Some Street
Table2:
id - job - finished_by
---------------------------
1 - ABC - 2
2 - EFD - 3
3 - XYZ - 2
4 - BVC - 1
In the above two tables Table1.id and Table2.finished_by are supposed to be linked.
For, eg in table 2, job ABC was finished by Adam.
My objective is to select DISTINCT records from Table 2.
and the result should output all the job completed by each of the persons.
I have this query so far:
SELECT *
FROM table2
LEFT JOIN table1 ON table2.finished_by = table1.id
LIMIT 0 , 30
This joins the Tables side by side, but how do i edit the query to make it display only distinct records, so that the output is:
id - job - id - name
----------------------------
1 - ABC - 2 - Adam
2 - EFD - 3 - ABC
4 - BVC - 1 - Jim
Update:
So, i've did some googling and made some changes to my query:
SELECT DISTINCT finished_by FROM table2
LEFT JOIN table1 ON table2.finished_by = table1.id
LIMIT 0 , 30
But, it seems that only first line of the query is executed since, i dont see the LEFT JOIN table.
May be this query needs a bit more finishing??
More Updates:
So, from some very distinguished members of StacKOverflow it has been brought to my notice that my logic is totally wrong.. So, i'll try to explain what i am trying to achieve in simple words and not program/code. May be that way i can be fetch a quick solution.
So, there's my Company: CompanyA
people like Jim, Adam etc work for CompanyA.. But, CompanyA sends Jim, Adam etc.. to work for another Company.. Say Company1
Jim, Adam etc can be sent to work for multiple such companies. Say Jim is sent to work for Company1 twice and Adam was sent to work for Company1 thrice.
Table 2 maintains records of how many time a person went to work for Company1 in the following format:
Table2: (Ref: Company1)
id - job - finished_by - Date
------------------------------------
1 - ABC - 2 - 10 Oct
2 - EFD - 3 - 11 Oct
3 - XYZ - 2 - 12 Oct
4 - BVC - 1 - 13 Oct
Now, my objective is simple, The reports need to be generated as follows for Company1:
List the persons we sent to Company1 (in Alphabetic Order)
This list should include No. of times the person went (and Dates)
Should also Include the job he did there while he was working for Company1
For, eg an Ideal Output/Report would be:
Name of Employee - Job Description - Dates
ABC - EFD - 11 Oct
Adam - ABC, XYZ - 10 Oct, 12 Oct
Jim - BVC - 13 Oct
I can do all the basic reporting, But i judt dont know how to Convert the numbers that are sitting into Table2 in finished_by coloumn into their respective names from table1
I hope i'm clear with my question now.
Thanks, Everyone!!
I really appreciate your time and effort
Based on your latest update, it sounds like you want a comma-separated list of the "job" names and dates. MySQL's GROUP_CONCAT function accomplishes that. So perhaps something like this:
SELECT table1.*, GROUP_CONCAT(table2.job), GROUP_CONCAT(table2.date)
FROM table1
INNER JOIN table2 ON (t1.id = t2.finished_by)
GROUP BY t1.id
This will give you a list of all employees who did work, along with comma-separated lists of where they did work and when.
Keep in mind that there's no order to the values in each GROUP_CONCAT list. So you can't be sure, for example, that the first job listed corresponds to the first date listed. But if you wanted to keep that connection intact you'd want each job in a separate row anyway.
SELECT DISTINCT *
FROM table2
LEFT JOIN table1 ON table2.finished_by = table1.id
LIMIT 0 , 30
does this work?
It sounds as though you want all Table1 details, together with a count of all jobs they have finished from Table2. If so, try this:
select t1.id,
max(t1.name) name,
max(t1.address) address,
count(t2.id) finished_jobs
from Table1 t1 left outer join Table2 t2 on t1.id = t2.finished_by
group by t1.id;