I'm currently trying to query a list of our assembly items, along with their corresponding components.
I've been trying this query a few different ways, here is where I'm currently at (closest I've gotten)
SELECT inv_mast.item_id AS [Item ID]
,inv_mast.item_desc AS [Item Description]
,IMC.item_id AS [Component Item ID]
FROM inv_mast
INNER JOIN assembly_line ON inv_mast.inv_mast_uid = assembly_line.inv_mast_uid
LEFT JOIN inv_mast IMC ON assembly_line.component_inv_mast_uid = inv_mast.inv_mast_uid
WHERE inv_mast.item_id = 'LFV-SV59Z-2ZGD-LOGO-L'
In the "Assembly_line" table there is a Component_inv_mast_uid which I need to use to query our Item ID's for the components (inv_mast joins to assembly_line on the inv_mast_uid)
It should export something like this:
LFV-SV59Z-2ZGD-LOGO-L (LFV-SV59Z-2ZGD-L)
LFV-SV59Z-2ZGD-LOGO-L (Print Fee)
I forgot to mention what is currently exporting.
When I run this query, it provides most information correct, however the "Component Item ID" column is returning Null, and I'm not sure why.
I've only had experience so far with joining basic tables, so subqueries are very new to me.
try to use
WHERE inv_mast.item_id like 'LFV-SV59Z-2ZGD-LOGO-L%'
instead of = 'LFV-SV59Z-2ZGD-LOGO-L'
Related
I'm trying to tie a description to group codes to use in another table. Unfortunately the data is from a very old database and didn't have very good requirements for fields when it was initially created. So now I have a number of fields for part number group codes that were blank. I'm trying to convert these null values to say "blank". I've tried this many different ways and cannot get the nz function to modify the data in any way.
In the provided code snippet I have tried using the nz function only after select, only after from, and in both places as shown.
SELECT [Part Numbers].Part, nz([Part Numbers].Group,"Blank"), [Group Codes].Description
FROM [Part Numbers]
INNER JOIN [Group Codes] ON nz([Part Numbers].[Group],"Blank") = [Group Codes].[Group Code];
That query doesn't return records where Group field is Null. It can't even be displayed in query design with that join.
Consider RIGHT JOIN:
SELECT [Part Numbers].Part, Nz([Part Numbers].Group,"Blank") AS Expr1, [Group Codes].Description
FROM [Group Codes] RIGHT JOIN [Part Numbers] ON [Group Codes].[Group Code] = [Part Numbers].Group;
I wanted to be able to grab related APVM.Name data from this query however I get the error:
"Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
When I add APVM.Name to the GROUP BY clause, the data starts to lose its accuracy. How can I get the APVM.Name data without this error? Thank you in advance!
SELECT
bSLHD.SL as [SL],
APVM.Vendor as [Vendor Number],
bSLHD.Job as [Job Number],
JCCM.Department,
APVM.Name
FROM
bSLHD
INNER JOIN SLCT ON
bSLHD.SL = SLCT.SL
AND
bSLHD.VendorGroup = SLCT.VendorGroup
AND
bSLHD.Vendor = SLCT.Vendor
AND
bSLHD.SLCo = SLCT.SLCo
INNER JOIN APVM ON bSLHD.Vendor = APVM.Vendor
INNER JOIN JCCM ON bSLHD.Job = JCCM.Contract AND bSLHD.JCCo = JCCM.JCCo
WHERE
JCCM.Department = '10'
GROUP BY
bSLHD.SL,
APVM.Vendor,
bSLHD.Job,
JCCM.Department
HAVING
SUM(CASE WHEN SLCT.CompCode = 'LI' THEN 1 ELSE 0 END) = 0
ORDER BY
bSLHD.SL
Try replacing APVM.Name to Max(APVM.Name) or any other aggregate if u wanna see distinct names under the ongoing hierarchy. This is just a work around meaning if this output suits your requirements in terms of name getting repeats won't serve any grouping aggregations just that it would be the result of grouped entities with max names (mainly distinct)
There were vendors with different names associated with two different companies. One being our production company and the other being a test company. Filtering those values out resolved. Thank you.
I have a table of data that contains baseline project information, called ADMIN_WORK. I have a separate table that contains various attributes related to each project listed in ADMIN_WORK called WR_ATTRIBUTE. The structure of WR_ATTRIBUTE is such that each project has about 300 attributes (300 rows in the table) each with a unique name associated with the project number. I need to pull approximately 15 of these attributes into a final table.
I built a query with a number of sub-queries, but it takes multiple hours to run. The code below represents pulling a single attribute (called Circuit) from WR_ATTRIBUTE for all projects.
SELECT ADMIN_WORK.WR_NO AS [WR Number],
ADMIN_WORK.PREMISE_ID AS [Premise ID],
AttributeCircuit.Circuit
FROM ADMIN_WORK INNER JOIN
(SELECT ADMIN_WORK.WR_NO AS [WR Number], WR_ATTRIBUTE.ATTRIBUTE_VALUE AS Circuit
FROM ADMIN_WORK INNER JOIN
WR_ATTRIBUTE ON ADMIN_WORK.WR_NO = WR_ATTRIBUTE.WR_NO
WHERE (((WR_ATTRIBUTE.WR_ATTRIBUTE_CODE)="Circuit") AND ((ADMIN_WORK.WR_TYPE_CODE)="ACNEM"))
) AS AttributeCircuit
ON ADMIN_WORK.WR_NO = [AttributeCircuit].[WR Number]);
My final query has about 14 more of these subqueries similarly implemented, each with a different WR_ATTRIBUTE_CODE.
My final table is generated accurately, but it is extremely slow. I am wondering if there is a better way of structuring my query.
You probably just want conditional aggregation:
SELECT w.WR_NO AS [WR Number],
w.PREMISE_ID AS [Premise ID],
a.Circuit
FROM ADMIN_WORK as INNER JOIN
(SELECT a.WR_NO AS [WR Number],
MAX(IIF(a.WR_ATTRIBUTE_CODE)="Circuit" a.ATTRIBUTE_VALUE, NULL) AS Circuit ,
. . .
FROM WR_ATTRIBUTE as a
GROUP BY WR_NO
) as a
ON w.WR_NO = a.WR_NO ;
I am trying to run a Left Join in the MS Access SQL. I am trying to Left Join my "OldPE" table to New "1 PE" table and update my column labeled "Line Num". There is no primary key in these tables so I am linking them through a series of conditions. Here is my code so far (excuse the poor formatting I am new and still learning SQL).
UPDATE [1 PE]
LEFT JOIN OldPE ON ([1 PE].SumRes = OldPE.SumRes)
AND ([1 PE].[Project Code] = OldPE.[Project Code])
AND ([1 PE].[DeptID] = OldPE.[DeptID])
AND ([1 PE].[Res Code] = OldPe.[Res Code])
AND ([1 PE].[Explain The Cost] LIKE OldPE.[Explain The Cost])
AND ([1 PE].Notes LIKE OldPE.Notes)
SET [1 PE].[Line Num] = [OldPE].[Line Num];
There are a lot of rows that have null or blank values in their "Explain The Cost" and "Notes" columns. I used a like statement because some of the notes that I want together vary slightly due to spelling mistakes and such. However now that I use the "like" it won't return the rows with a null value for these columns. The SQL code wont accept a WHERE EXISTS (I may also just be writing it wrong).
How do I get these null values to still be returned while using the Like command
Use ISNULL to join back to the original value if it is null. (This will include the null values in the results.)
AND ([1 PE].[Explain The Cost] LIKE ISNULL(OldPE.[Explain The Cost],[1 PE].[Explain The Cost]))
I'm a die hard Excel VBA fan and having to make the transition to a very basic SQL based piece of software. I have managed to adapt an existing query to my needs (still not sure how !) but I could do with some help to further tweek it :)
The query is:
PARAMETERS [Date From] DateTime, [Date To] DateTime, [Site Group] Text;
SELECT Contacts.Name, DataProfile.Date, DataProfile.TotalUnits
FROM (Lookup INNER JOIN Groups ON Lookup.Lookup_Id = Groups.Lookup_Id) INNER JOIN (Contacts INNER JOIN (Points INNER JOIN DataProfile ON Points.Id = DataProfile.Point_Id) ON Contacts.Id = Points.Contacts_Id) ON Groups.Link_Id = Contacts.Id
WHERE (((Lookup.Lookup_Name)=[Site Group]) AND ((DataProfile.Date) Between [Date From] And [Date To]) AND ((Points.Type)='Electricity') AND ((DataProfile.Type)=0))
AND Contacts.Group_1=[Client Group]
ORDER BY Contacts.Name, DataProfile.Date;
This produces a list of daily totals by site by day and looks like :
Site.......Date.........Value
Site 1....01/01/13...444.7
Site 1....02/01/13...861.5
Site 1....03/01/13...850.0
etc...
Is it possible to write the query to output as :
Site.....01/01/13....02/01/13....03/01/13
Site1....444.7.......861.5.........850.0
Site2....111.1.......222.2.........333.3
etc...
It would make my life so much easier to have one row per site rather than so many. At the moment I am coding excel to adapt the data - but it's long and not efficient. If I could get SQL to output in this format I would be like some kind of office god !!!
Any help/advise/guidence is welcome :)
What version of SQL are you running?
Can you use PIVOT?
http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx