Recursive CTEs to pull BOM (Bill of Material) - sql-server-2008

I need some assistance or pointer on CTEs.
I am trying to extract Bill of Material and I have used CTEs query. The query works good and it pulls all the data. My struggle is there are lot of parts where the parts has new version on different levels and I want to grab new versions only. Currently my query grabs everything. I have a version column.
I tried few different things like trying to utilize max function within CTEs but I got an error saying group by, having cannot be part of recursive ctes.
Also, I tried using subquery but I didnt get the right result.
WITH BOM (
Parent
,Child
,Qty
,Childrev
,LEVEL
,sort
)
AS (
SELECT Parent
,cast(RTRIM(Child) AS NVARCHAR(max))
,Qty
,Childrev
,0 AS LEVEL
,cast(RTRIM(Child) AS NVARCHAR(max))
FROM Bomtable
UNION ALL
SELECT BOM.Parent
,cast(RTRIM(Bomtable.Child) AS NVARCHAR(max))
,Bomtable.Qty
,BOM.Childrev
,LEVEL + 1
,CAST(BOM.Sort + '..... ' + RTRIM(Bomtable.Child) AS NVARCHAR(max))
FROM BOM
INNER JOIN Bomtable ON Bomtable.Parent = BOM.Child
WHERE BOM.Parent = main product
ORDER BY SORT
)

I know I do not fully understand your data model. However, try replacing your BOM and BomTable tables with a derived table like this which will give you one row for each Child record with the greatest Childrev value without using a GROUP BY.
SELECT *
FROM (
SELECT *
, ROW_NUMBER() OVER (PARTITION BY Child ORDER BY Childrev DESC) AS ROW_NBR
FROM BOM
) AS x
WHERE x.ROW_NBR = 1;
Here is the documentation for the the OVER Clause.
Noel

Related

What is the proper MySQL way to take data from 4 rows, 1 column, and separate into 9 columns?

I've studied and tried days worth of SQL queries to find "something" that will work. I have a table, apj32_facileforms_subrecords, that uses 7 columns. All the data I want to display is in 1 column - "value". The "record" displays the number of the entry. The "title" is what I would like to appear in the header row, but that's not as important as "value" to display in 1 row based upon "record" number.
I've tried a lot of CONCAT and various Pivot queries, but nothing seems to do more than "get close" to what I'd like as the end result.
Here's a screen shot of the table:
The output "should" be linear, so that 1 row contains 9 columns:
Project; Zipcode; First Name; Last Name; Address; City; Phone; E-mail; Trade (in that order). And the values in the 9 columns come from "value" as they relate to the "record" number.
I know there are LOT of examples that are similar, but nothing I've found covers taking all the values from "value" and CONCAT to 1 row.
This works to get all the data I want - SELECT record,value FROM apj32_facileforms_subrecords WHERE (record IN (record,value)) ORDER BY record
But the values are still in multiple rows. I can play with that query to get just the values, but I'm still at a loss to get them into 1 row. I'll keep playing with that query to see if I can figure it out before one of the experts here shows me how simple it is to do that.
Any help would be appreciated.
Using SQL to flatten an EAV model representation into a relational representation can be somewhat convoluted, and not very efficient.
Two commonly used approaches are conditional aggregation and correlated subqueries in the SELECT list. Both approaches call out for careful indexing for suitable performance with large sets.
correlated subqueries example
Here's an example of the correlated subquery approach, to get one value of the "zipcode" attribute for some records
SELECT r.id
, ( SELECT v1.value
FROM `apj32_facileforms_subrecords` v1
WHERE v1.record = r.id
AND v1.name = 'zipcode'
ORDER BY v1.value LIMIT 0,1
) AS `Zipcode`
FROM ( SELECT 1 AS id ) r
Extending that, we repeat the correlated subquery, changing the attribute identifier ('firstname' in place of 'zipcode'. looks like we we could also reference it by element, e.g. v2.element = 2
SELECT r.id
, ( SELECT v1.value
FROM `apj32_facileforms_subrecords` v1
WHERE v1.record = r.id
AND v1.name = 'zipcode'
ORDER BY v1.value LIMIT 0,1
) AS `Zipcode`
, ( SELECT v2.value
FROM `apj32_facileforms_subrecords` v2
WHERE v2.record = r.id
AND v2.name = 'firstname'
ORDER BY v2.value LIMIT 0,1
) AS `First Name`
, ( SELECT v3.value
FROM `apj32_facileforms_subrecords` v3
WHERE v3.record = r.id
AND v3.name = 'lastname'
ORDER BY v3.value LIMIT 0,1
) AS `Last Name`
FROM ( SELECT 1 AS id UNION ALL SELECT 2 ) r
returns something like
id Zipcode First Name Last Name
-- ------- ---------- ---------
1 98228 David Bacon
2 98228 David Bacon
conditional aggregation approach example
We can use GROUP BY to collapse multiple rows into one row per entity, and use conditional tests in expressions to "pick out" attribute values with aggregate functions.
SELECT r.id
, MIN(IF(v.name = 'zipcode' ,v.value,NULL)) AS `Zip Code`
, MIN(IF(v.name = 'firstname' ,v.value,NULL)) AS `First Name`
, MIN(IF(v.name = 'lastname' ,v.value,NULL)) AS `Last Name`
FROM ( SELECT 1 AS id UNION ALL SELECT 2 ) r
LEFT
JOIN `apj32_facileforms_subrecords` v
ON v.record = r.id
GROUP
BY r.id
For more portable syntax, we can replace MySQL IF() function with more ANSI standard CASE expression, e.g.
, MIN(CASE v.name WHEN 'zipcode' THEN v.value END) AS `Zip Code`
Note that MySQL does not support SQL Server PIVOT syntax, or Oracle MODEL syntax, or Postgres CROSSTAB or FILTER syntax.
To extend either of these approaches to be dynamic, to return a resultset with a variable number of columns, and variety of column names ... that is not possible in the context of a single SQL statement. We could separately execute SQL statements to retrieve information, that would allow us to dynamically construct a SQL statement of a form show above, with an explicit set of columns to be returned.
The approaches outline above return a more traditional relational model, (individual columns each with a value).
non-relational munge of attributes and values into a single string
If we have some special delimiters, we could munge together a representation of the data using GROUP_CONCAT function
As a rudimentary example:
SELECT r.id
, GROUP_CONCAT(v.title,'=',v.value ORDER BY v.name) AS vals
FROM ( SELECT 1 AS id ) r
LEFT
JOIN `apj32_facileforms_subrecords` v
ON v.record = r.id
AND v.name in ('zipcode','firstname','lastname')
GROUP
BY r.id
To return two columns, something like
id vals
-- ---------------------------------------------------
1 First Name=David,Last Name=Bacon,Zip Code=98228
We need to be aware that the return from GROUP_CONCAT is limited to group_concat_max_len bytes. And here we have just squeezed the balloon, moving the problem to some later processing, to parse the resulting string. If we have any equal signs or commas that appear in the values, it's going to make a mess of parsing the result string. So we will have to properly escape any delimiters that appear in the data, so that GROUP_CONCAT expression is going to get more involved.

mysql multiple similar table query[seems not duplicate]

I have a sales software that use MYSQL database and i want to make a web extension using PHP. the software creating a new table everyday for every days transaction with same column but name like sales_data_Yearmonthday.
e.g
sales_data_20190122
sales_data_20190123
sales_data_20190124
sales_data_20190125
sales_data_20190126
sales_data_20190127
sales_data_20190128
So my question is what will be the best way to query these table if i want to get sales report for last 7 days?
UNION ALL is one option to join all table but are there any other option to do that for getting best performance as 356 table will be created every year and each table may have contain over 5000 records.
its may not the best database design but i cannot change it.
Given the specified constraints (as unfortunate as that situation is)...
using UNION ALL is the most appropriate solution to satisfying the specification.
If we are wanting the "past seven days", then we (our code) needs to figure out which tables will be required (vs the tables that would be required e.g. "so far this month") and dynamically construct SQL text.
We first write the query against one table, get that tested.
SELECT t.fee
, t.fi
, t.fo
, t.fum
FROM sales_data_20190128 t
WHERE t.foo = ?
Then we just repeat that query for each table that might included rows we are interested in, excluding tables we know for sure will not contain rows we want, and combine the queries with UNION ALL set operator.
If we need the whole set ordered, then wrap each SELECT in parens, and finish with an ORDER BY clause. e.g.
( SELECT t.fee, t.fi, t.fo, t.fum
FROM sales_data_20190124 t
WHERE t.foo = ?
)
UNION ALL
( SELECT t.fee, t.fi, t.fo, t.fum
FROM sales_data_20190125
WHERE t.foo = ?
)
UNION ALL
( SELECT t.fee, t.fi, t.fo, t.fum
FROM sales_data_20190126 t
WHERE t.foo = ?
)
UNION ALL
( SELECT t.fee, t.fi, t.fo, t.fum
FROM sales_data_20190127 t
WHERE t.foo = ?
)
UNION ALL
( SELECT t.fee, t.fi, t.fo, t.fum
FROM sales_data_20190128 t
WHERE t.foo = ?
)
ORDER BY 1,2
Do NOT try to simplify the code by creating view that concatenates all of the tables together, and query against that. Don't do that.

The query gives single row query returns more than one row

I'm trying to show staff_code, staff_name and dept_name for those who have taken one book.
Here's my query:
SELECT SM.STAFF_CODE,SM.STAFF_NAME,DM.DEPT_NAME,BT.BOOK_CODE
FROM STAFF_MASTER SM,DEPARTMENT_MASTER DM,BOOK_TRANSACTIONS BT
WHERE SM.DEPT_CODE =DM.DEPT_CODE
AND SM.STAFF_CODE = (
SELECT STAFF_CODE
FROM BOOK_TRANSACTIONS
HAVING COUNT(*) > 1
GROUP BY STAFF_CODE)
It gives the error:
single-row subquery returns more than one row.
How to solve this?
Change = to IN:
WHERE SM.STAFF_CODE IN (SELECT ...)
Because the select returns multiple values, using equals won't work, but IN returns true if any of the values in a list match. The list can be a hard-coded CSV list, or a select with one column like your query is.
That will fix the error, but you also need to remove BOOK_TRANSACTIONS from the table list and remove BOOK_CODE from the select list.
After making these changes, your query would look like this:
SELECT SM.STAFF_CODE,SM.STAFF_NAME,DM.DEPT_NAME
FROM STAFF_MASTER SM,DEPARTMENT_MASTER DM
WHERE SM.DEPT_CODE =DM.DEPT_CODE
AND SM.STAFF_CODE IN (
SELECT STAFF_CODE
FROM BOOK_TRANSACTIONS
HAVING COUNT(*) > 1
GROUP BY STAFF_CODE)
I recommend learning the modern (now over 25 year old) JOIN syntax.

remove duplicate rows in SQL Server Query

how could we remove or not displaying duplicate row with some conditional clause in sqlserver query, the case look like this one,
code decs
-------------------------
G-006 New
G-006 Re-Registration
how can we display just G-006 with Re-Registration Desc, i have tried with this query but no luck either
with x as (
select new_registration_no,category,rn = row_number()
over(PARTITION BY new_registration_no order by new_registration_no)
from equipment_registrations
)
select * from x
By using the same field in the PARTITION BY and ORDER BY clause, the rn field will always equal 1.
Assuming that new_registration_no = code and category = decs, you could change the ORDER BY field to be ORDER BY category DESC to get that result. However, that's a pretty arbitrary ORDER BY - you're just basing it on a random text value. I'm also not 100% sure how well the ROW_NUMBER() function works in a CTE.
A better solution might be something like:
SELECT *
FROM
(
SELECT
New_Registration_No,
Category,
ROW_NUMBER() OVER
(
PARTITION BY New_Registration_No
ORDER BY
CASE
WHEN Category = 'Re-Registration' THEN 1
WHEN Category = 'New' THEN 2
ELSE 3
END ASC ) rn
FROM Equipment_Registrations
) s
WHERE rn = 1
You can set the order in the CASE statement to be whatever you want - I'm afraid that without more information, that's the best solution I can offer you. If you have a known list of values that might appear in that field, it should be easy; if not, it will be a little harder to configure, but that will be based on business rules that you did not include in your original post.

Producing multiple maximum and minimum values with SQL Query

I'm becoming frustrated with a curious limitation of SQL - its apparent inability to relate one record to another outside of aggregate functions. My problem is summarized thusly.
I have a table, already sorted. I need to find its maximum values (note the plural!) and minimum values. No, I am not looking for a single maximum or single minimum. More specifically I'm trying to generate a list of the local peaks of a numeric sequence. A rough description of an algorithm to generate this is:
WHILE NOT END_OF_TABLE
IF RECORD != FIRST_RECORD AND RECORD != LAST_RECORD THEN
IF ((RECORD(Field)<RECORD_PREVIOUS(Field) AND RECORD(Field)<RECORD_NEXT(Field)) OR
RECORD(Field)>RECORD_PREVIOUS(Field) AND RECORD(Field)>RECORD_NEXT(Field)) THEN
ADD_RESULT RECORD
END IF
END IF
END WHILE
See the Problem? I need to do a query that a given record must compare against the previous and next records' values. Can this even be accomplished in standard SQL?
Your frustration is shared by many; while SQL is great for working with general sets, it's terribly deficient when trying to work with issues specific to ordered sets (whether it's physically ordered in the table or there is an implicit or explicit logical order is irrelevant). There are some things that can help (for example, the rank() and row_number() functions), but the solutions can differ across RDBMS's.
If you can be specific about which platform you're working with, I or someone else can provide a more detailed answer.
You have to self-join twice and generate a rownumber without gaps:
In T-SQL:
WITH ordered AS (
SELECT ROW_NUMBER() OVER (ORDER BY your_sort_order) AS RowNumber
,* -- other columns here
)
SELECT *
FROM ordered
LEFT JOIN ordered AS prev
ON prev.RowNumber = ordered.RowNumber - 1
LEFT JOIN ordered AS next
ON next.RowNumber = ordered.RowNumber + 1
WHERE -- here you put in your local min/local max and end-point handling logic - end points will have NULL in next/prev
Yes. You need a self join - but without a database schema, it's hard to be specific about the solution.
Specifically, I'm wondering about the "ordering" thing you mention - but I'm going to assume there's an "ID" field we can use for this.
(Oh, and I'm using old-school join syntax, coz I'm a dinosaur).
select *
from myTable main,
myTable previous,
myTable next
where previous.id = main.id - 1
and next.id = main.id + 1
and previous.record > main.record
and next.record < main.record
(I think I've interpreted your requirement correctly in the greater/less than clauses, but adjust to taste).
SELECT
current.RowID,
current.Value,
CASE WHEN
(
(current.Value < COALESCE(previous.Value, current.Value + 1))
AND
(current.Value < COALESCE(subsequent.Value, current.Value + 1))
)
THEN
'Minima'
ELSE
'Maxima'
END
FROM
myTable current
LEFT JOIN
myTable previous
ON previous.RowID = (SELECT MAX(RowID) FROM myTable WHERE RowID < current.ROWID)
LEFT JOIN
myTable subsequent
ON subsequent.RowID = (SELECT MIN(RowID) FROM myTable WHERE RowID > current.ROWID)
WHERE
(
(current.Value < COALESCE(previous.Value, current.Value + 1))
AND
(current.Value < COALESCE(subsequent.Value, current.Value + 1))
)
OR
(
(current.Value > COALESCE(previous.Value, current.Value - 1))
AND
(current.Value > COALESCE(subsequent.Value, current.Value - 1))
)
Note: The < and > logic is copied from you, but does not cater for local maxima/minima that are equal across one or more consecutive records.
Note: I've created a fictional RowID to join the records in order, all the is important is that the joins get the "previous" and "subsequent" records.
Note: The LEFT JOINs and COALESCE statements cause the first and last values to always be counted as a maxima or minima.