SQL query not showing results - sql-server

I want to do some visualization on Tableau with AdventureWorks 2014 data but my data is not showing on Tableau. I ran the query on SQL server-it ran successfully but did not show results. I need Product Cost, Product ID and Sales reason on Tableau. Kindly help.
SQL extract
Select * from
[Production].[ProductCostHistory] as pc
inner join [Sales].[SalesOrderDetail] as sod
on pc.ProductID=sod.ProductID
inner join [Sales].[SalesReason] as sr
on pc.ModifiedDate=sr.ModifiedDate

Your query is wrong... specifically the join on SalesReason. You are going to want to look at the SalesOrderHeaderSalesReason table, and the SalesOrderHeader table at a minimum. Start with the Sales Schema and go from there.
Here's a good diagram of the Data Model for the Sales Schema (part of it). There are others out there easily found on google.

Related

SQL query to join "GeneralJournalAccountEntry" to table "CustInvoiceTrans" to get column value "MainAccount"

We have a Dynamics AX 2012 environment backed by SQL Server. We load invoice line data from table "CustInvoiceTrans" into an EDW fact table. We are wanting to find what the "MainAccount" column value is for each invoice line as it relates to table "GeneralJournalAccountEntry". This would need to be done through a SQL query.
I have attempted an Internet search on the topic, but found nothing definitive. I have also searched through the AX SQL views hoping to find a solution there, but did not come across anything. It is possible that I could be missing something though.
SQL Pseudo Example:
SELECT CIT.InvoiceID
,CIT.InvoiceDate
,CIT.SalesPrice
,GJAE.MainAccount
FROM CustInvoiceTrans CIT
LEFT JOIN {..Some table(s)} ST
ON ST.{SomeColumn} = CIT.{SomeColumn}
LEFT JOIN GeneralJournalAccountEntry GJAE
ON GJAE.{SomeColumn} = ST.{SomeColumn}
If this is possible, it would assume that one invoice line relates to one general ledger account entry line to return one MainAccount. I am uncertain if an invoice line could be split among multiple general ledger account entry lines.
It sounds to me like you are worried that you might end up with duplicates when you join the GeneralJournalAccountEntry table? My example shows how you can ensure that the GeneralJournalAccountEntry table has a 1:1 relationship with the CustInvoiceTrans table. I've assumed:
That there is a primary key for your accounts and customers (AccountId and CustomerId)
That there is some table that maps customer's to accounts
SELECT
cit.InvoiceID
, cit.InvoiceDate
, cit.SalesPrice
, t.MainAccount
FROM (
SELECT
gjae.AccountId
, gjae.MainAccount
FROM GeneralJournalAccountEntry gjae
GROUP BY
gjae.AccountId
, gjae.MainAccount
) t
JOIN [SomeTable] st
ON st.AccountId = t.AccountId
JOIN CustInvoiceTrans cit
ON cit.CustomerId = st.CustomerId

Is there a way to identify if a column is not been used in a SQL Server Database?

I have a big database and a lot tables and I would like to identify what columns are not been called by any store procedure or any query, or not in use.
I'm not sure if this is what you're looking for, but in your DB under views there's a folder for System Views - three of which are the following. Looking in all_objects look for the table name, then use the object_id of that table to select from the other queries. There may be other meta-data here that is appropriate for your need.
SELECT *
FROM sys.all_objects
SELECT *
FROM sys.all_columns
WHERE object_id = 981578535
SELECT *
FROM sys.all_views
WHERE object_id = 981578535
After an investigation I have found a new feature of SQL Server called Query Store where you can find in disk the executions with other information. If you have a SQL Server 2016 instance you can find it in the properties of the data base. There you can change the amount of days to capture and then you can in theory try to find the columns in question. The idea of this technology is to give you more options for performance tuning.
You can find the information with this query :
SELECT TOP 10 qt.query_sql_text, q.query_id,
qt.query_text_id, p.plan_id, rs.last_execution_time
FROM sys.query_store_query_text AS qt
JOIN sys.query_store_query AS q
ON qt.query_text_id = q.query_text_id
JOIN sys.query_store_plan AS p
ON q.query_id = p.query_id
JOIN sys.query_store_runtime_stats AS rs
ON p.plan_id = rs.plan_id
where qt.query_sql_text LIKE '%ColumnToFind%'
ORDER BY rs.last_execution_time;
Credits: Query Store

how to pull the details together from SQL instance?

I am looking to sort the details of number of DB's present in the SQL instance along with their Recovery model type and the size.?
ex: name , recovery_model_desc resides under sys.databases and size from sys.master_files. Also database_id is the shared column.
How to get the result in together?
JOIN the two tables together on the key field you have correctly identified:
SELECT db.*, mf.*
FROM sys.databases db
LEFT JOIN sys.master_files mf ON db.database_id = mf.database_id
I suspect that the LEFT JOIN could just be JOIN/INNER JOIN as I don't think there can be records in sys.databases without any corresponding records in sys.master_files, but I don't know for sure, so stuck with a LEFT JOIN for that reason.
If you need some basics around JOINs to get you started with understanding how this works, here are some resources:
MSDN Blog: Introduction to Joins
W3 Schools: SQL Joins
SQL Authority: Introduction to JOINs
Essential SQL: Introduction to Database Joins
I've also often found a visual explanation of SQL Joins to be a helpful reference at times.

SQL Joining journal tables to user tables

I have a MSSQL database with 3 tables: Journals, Customers, and UserAccounts.
I'm trying to query Journals for transactions per account manager. This table has a customer ID column that links to Customers.
The Customers table has a ACC_Manager column that links to UserAccounts via UserID.
Inside the UserAcounts table are first and last name columns.
So it would be
Select
Journal.amount,
Customer.name,
UserAccounts.first
From
Tables
where
Journal.ACC_manager = 'Matt'
I'm having issues joining the tables so I can query using UserAccounts.first. Could anybody help? Thanks
Try the following. I didn't get exact column names so don't just use this code without modifying it a bit to suit your specific needs:
SELECT
j.amount,
c.name,
u.first
FROM
Journals j
JOIN Customers c ON
c.customerID = j.customerID -- Exact column names?
JOIN UserAccounts u ON
u.UserID = c.ACC_Manager
WHERE
u.first = 'Matt'
You may also need to use LEFT JOIN as opposed to JOIN. Read up on JOINs to be sure.

Relationships between stored procs?

I'll start by saying hello! This forum has been a great help to me over the past few months, but have only now joined and asking my first question.
I'm working with the Northwind database in SQL Server 2008 r2 to build a vb.net application. I've been wrecking my head for a week trying to figure out how to make an order/invoice form. I can get the information I need to display using separate stored procs (GetCustInfo, GetOrderInfo, GetProductInfo, or something like that), but I'm having trouble figuring out how to display them on the form.
When I select all the info I need in one sp (as in the Invoice view which comes built in the db), I get 2155 rows, which is the number of items which have been ordered in the company history.
What I want to do is display this information, but navigate by OrderID (which would give me 830 rows, each with a certain number of products related to the OrderID). So I'm thinking I need different stored procs related which can be related in some way.
I'd really appreciate any help that can be given on this.
Many thanks in advance.
p.s. I have screenshots of the Northwind sample app which shipped/ships with Access, which is really what I'm trying to recreate in SQL Server. Unfortunately, no code!
MM
Yes you can achieve it by many ways and SP is one. Just create a SP to select that related products passing OrderId as a input parameter.
Some options (with contrived examples):
You can ALTER existing stored procedures to get what you want (not recommended if you want to use the existing procedures for other queries).
ALTER PROCEDURE usp_ExistingProcedure
AS
BEGIN
SELECT t1.Value
, t2.Value
-- Supose that this was the addition we made to an existing stored procedure
, t2.ValueTwo
FROM TableOne t1
INNER JOIN TableTwo t2 ON t1.ID = t2.ID
END
You can CREATE new stored procedures for your queries; in the above example, it would be a create procedure with a new name.
You may be able to create a VIEW to obtain what you need - this will operate a little differently.
CREATE VIEW uv_ApplicationView
AS
SELECT t1.Value
, t2.Value
, t2.ValueTwo
FROM TableOne t1
INNER JOIN TableTwo t2 ON t1.ID = t2.ID
You can pull the query directly from the VB application, though if you want to reuse it for something else, I wouldn't recommend this approach.
// A re-usable approach calling a stored procedure
SqlCommand myQuery = new SqlCommand("EXECUTE usp_myQuery", sqlConn);
// A query directly in the C# code:
string msQuery = "SELECT t1.Value, t2.Value, t2.ValueTwo FROM TableOne t1 INNER JOIN TableTwo t2 ON t1.ID = t2.ID"
// Later ...
SqlCommand myQuery = new SqlCommand(msQuery, sqlConn);

Resources