I'm trying to update a report (report builder) on SQL Server, which is based upon a stored procedure.
The stored procedure as is - queries 2 tables via INNER JOIN but was only designed to display data from 1 table.
I'm trying to get it now to also display some data from the 2nd table.
While it executes fine, its not displaying any data from the 2nd table.
Code:
SELECT n.ID, COMPANY, TITLE, EMAIL, FULL_NAME, chapter, n.member_type, ng.INDUSTRY_CLASS
from name n
inner join name_general ng on n.id = ng.id
WHERE MEMBER_TYPE in ('A','B','C','D', 'E','F','G')
and n.STATUS = 'A'
and N.EMAIL <> ''
and ng.no_mail = 0
and (
( charindex ( chapter,'WA') > 0 ))
This was originally written by past people & it works as wanted, and the only thing I've added is the ng.INDUSTRY_CLASS within the select statement. But when this is executed, no data displays for INDUSTRY_CLASS
*Obviously this is a partial extract of the stored procedure.
*Running on Server 2016 / SQL Server 2016
Sorry, ignore this question.
I realized that INDUSTRY_CLASS is on the Company Record & not an Individual's record.
Its a badly designed DB, in this same name table we have rows of people & rows of companies.
Obviously therefore both people & companies have the same fields.
The INDUSTRY_CLASS field is null for people, as that data is with a Company.
So both People and Companies have n.id in the name table & they also have n.co_id.
On a record for a person the n.co_id is just the n.id of the company from the same table.
This was the design of the CRM Manaufacturer... all im doing is querying the CRM db.
Related
This is on a Postgres server using SQL. I have a supply_chain table in which I need to pull the warehouse_id from, and an order table in which I need to pull the total returns. Located on the same server.
I need to combine them on the delivery zipcode from the order table and the zipcode on the supply_chain table. I am unsure the best way to join this in SQL.
SELECT deliveryzipcode, COUNT(OrderReturned) AS Total_returned
FROM transactions_log
WHERE OrderReturned= 'Yes'
GROUP BY deliveryzipcode;
This query will successfully return the number of returns based on zipcode. So basically I need to pull those warehouse_id's and count them.
Apologize in advance for not wording this question well.
You can try this :
SELECT sc.warehouse_id, sc.zipcode, tl.Total_returned
FROM logistics_supply_chain_network AS sc
INNER JOIN
(
SELECT deliveryzipcode, COUNT(OrderReturned) AS Total_returned
FROM transactions_log
WHERE OrderReturned= 'Yes'
GROUP BY deliveryzipcode
) AS tl
ON tl.deliveryzipcode = sc.zipcode ;
I have two separate tables in my access database, which both use a third table as the reference for one particular field on each table. The data is entered onto the different tables by separate forms. Then I have several queries that then reference those particular fields that count and show unique values. Those queries show the actual values, then I created an sql query that does the same thing, only it shows the reference ID instead of the value in the actual field.
table ODI----------table CDN----------reference table
id RHA---------id CHA----------------id HA
1 blank----------1 radio---------------1 internet
2 internet-------2 tv------------------2 radio
3 referral-------3 radio---------------3 referral
4 tv-------------4 blank---------------4 repeat customer
5 blank----------5 internet------------5 tv
6 internet-------6 referral------------6 employee
7 referral-------7 referral------------7 social media
this is the code I am trying to make work.
SELECT m.[Marketing Results], Count(*) AS [Count]
FROM (SELECT RHA as [Marketing Results] FROM ODI
UNION ALL
SELECT CHA as [Marketing Results] FROM CDN) AS m
GROUP BY m.[Marketing Results]
HAVING (((m.[Marketing Results]) Is Not Null))
ORDER BY Count(*) DESC;
and what my desired result is,
Marketing Results--Counts
referral------------------4
internet------------------3
radio---------------------2
tv------------------------2
Lookup fields with alias don't show what is actually stored in table. ID is stored, not descriptive alias. Lookup alias will carry into regular queries but Union query only pulls actual stored values. At some point need to include reference table in query by joining on key fields in order to retrieve descriptive alias. Options:
in each UNION query SELECT line, join tables
join UNION query to reference table
join aggregate query to reference table
Most experienced developers will not build lookups in table because of confusion they cause. Also, they are not portable to other database platforms. http://access.mvps.org/Access/lookupfields.htm
I have 3 tables that I wish to UPDATE data against (lets call them PROCESS, DIARY and HISTORY)
The 3 tables all have an ID column and the subset of data I wish to update is retrieved from a SELECT statement against the PROCESS table
SELECT ID FROM PROCESS WHERE STATUS = 1 AND COMPANY = 'XYZ'
Using T-SQL, I was planning to do 3 UPDATE statements (with the PROCESS table being last as it is the reference list) like so
UPDATE HISTORY ... WHERE ID IN (SELECT ID FROM PROCESS WHERE STATUS = 1 AND COMPANY = 'XYZ')
UPDATE DIARY ... WHERE ID IN (SELECT ID FROM PROCESS WHERE STATUS = 1 AND COMPANY = 'XYZ'
)
UPDATE PROCESS ... WHERE STATUS = 1 AND COMPANY = 'XYZ'
My question is: is this the most efficient way to do this within T-SQL - or should I be creating some sort of CTE to reference only once? (The number of documents/performance are not a problem, I'm just trying to find out if as an ex OO developer coming to SQL, I'm slipping into bad habits or missing a trick somewhere
I don't think you will be able to use CTE as CTE can be referenced only once. Updating 3 tables requires 3 separate queries to be run.
If obtaining the ID's in your inner query is expensive, you may consider running the query to get them only once and storing the results in a temporary table or table variable. This way you will be able to reference that temporary table or table variable in all update statements.
If the inner query is inexpensive to run, I would leave it as is to not complicate things unnecessarily.
--Query1
SELECT TransactionDetails.Transactions.TransactionId
, TransactionDetails.Transactions.CustomerId
, TransactionDetails.Transactions.TransactionType
, TransactionDetails.Transactions.DateEntered
, TransactionDetails.Transactions.RelatedProductId
, CustomerDetails.CustomerProducts.CustomerFinancialProductId
, CustomerDetails.CustomerProducts.CustomerId AS 'CustomerProducts--CustomerID'
, CustomerDetails.CustomerProducts.FinancialProductId
, CustomerDetails.CustomerProducts.AmountToCollect
FROM TransactionDetails.Transactions
INNER JOIN CustomerDetails.Customers ON TransactionDetails.Transactions.CustomerId = CustomerDetails.Customers.CustomerID
INNER JOIN CustomerDetails.CustomerProducts ON CustomerDetails.Customers.CustomerID = CustomerDetails.CustomerProducts.CustomerId
WHERE TransactionId = 2
ORDER BY CustomerFinancialProductId
--Query2
SELECT TransactionDetails.Transactions.TransactionId
, TransactionDetails.Transactions.CustomerId
, TransactionDetails.Transactions.TransactionType
, TransactionDetails.Transactions.DateEntered
, TransactionDetails.Transactions.RelatedProductId
, CustomerDetails.CustomerProducts.CustomerFinancialProductId
, CustomerDetails.CustomerProducts.CustomerId AS 'CustomerProducts--CustomerID'
, CustomerDetails.CustomerProducts.FinancialProductId
, CustomerDetails.CustomerProducts.AmountToCollect
FROM TransactionDetails.Transactions
INNER JOIN CustomerDetails.FinancialProducts ON TransactionDetails.Transactions.RelatedProductId = CustomerDetails.FinancialProducts.ProductId
INNER JOIN CustomerDetails.CustomerProducts ON CustomerDetails.FinancialProducts.ProductId = CustomerDetails.CustomerProducts.FinancialProductId
WHERE TransactionId = 2
ORDER BY CustomerFinancialProductId
Here are two queries that i have been given to compare. it asks which one navigates better ir should be the correct one used?
I know that the first query returns only 1 row where as the second returns 11 rows.
Also Query 1 navigates through Transactions to Customers where it's using ProductID as it's column in each. the 2nd query goes from Transactions to FinancialProducts where the join is on RelatedProductID in the transactions table and productID in the FinancialProducts table
Both queries then end in the same table with the same columns chosen in that table.
Lastly the FinancialProductID is 22 in the first query and in the 2nd query all 11 rows have a financialProductID of 22
ACTUAL QUESTION: WHICH ONE IS THE CORRECT FORM OF NAVIGATING TO THE TABLE
The question that you have asked is a business question about the meaning of the data.
It looks like both versions of the query are properly joining the tables together. You understand your business domain, whereas the rest of us do not.
That said, based on the names of the tables, the first version "makes sense" to me. A transaction has a customer and customers have products. So, if you want all the products for a customer on a transaction, then the first would seem to do that.
I don't know what role FinancialProducts plays in this database. It is quite possible that the second version "makes more sense" for this application. If you are using this database, you should learn what these tables are.
I am working with the Maximo Asset Management System (version 7.1.1.6). I am trying to display a result set on the start center that contains a Do Not Call list of specific people. However, when using a query for the result set (a query that was saved in the People section that contains the appropriate "where" clause such as "department='ABC'"), I cannot select the phone number or the email address as a column to display. I believe this is due to the fact that the "Primary Phone" and "Primary Email" fields in the person table are not really there. They are virtual fields that are connected in the People application to the Phone and Email tables and joined on the personid column. If I run the following query in the database, I get the result set that I want:
select * from dbo.person as p
left outer join dbo.phone as ph on p.personid=ph.personid and ph.isprimary=1
left outer join dbo.email as e on p.personid=e.personid and e.isprimary=1
Unfortunately for the result sets, you don't have access to the "FROM" clause you can only edit the "WHERE" clause.
Anyone have any idea how to solve this other than adding 2 new columns to the person table for primary phone and primary email? I don't want to HAVE to do it, but I can if I must.
How about this for a where clause
... where (select count(*) from dbo.phone ph where :personid = ph.personid and ph.isprimary=1) > 0 and (select count(*) from dbo.email e where :personid = e.personid and e.isprimary=1) > 0
I can also think of a solution creating a relationships in the database configuration application, but the above query is more straight forward.