How to separate content from my query into 3 distinct columns - sql-server

I have got the following query, which puts the data that I need (text in the 245, 260 and 300 tags of my table called "bib") into a single column (called "Title"). Instead I would like to have this data separated into three distinct columns, one for each tag. Any suggestions would be very welcome
SELECT DISTINCT isbnEX_inverted.isbn, ISNULL(bib.text, CONVERT(varchar(255), bib_longtext.longtext)) AS Title, top_circ_summary.ranking, bib.tag, bib.bib#
FROM bib INNER JOIN
item ON bib.bib# = item.bib# INNER JOIN
isbnEX_inverted ON bib.bib# = isbnEX_inverted.bib# INNER JOIN
top_circ_summary ON item.bib# = top_circ_summary.bib# LEFT OUTER JOIN
bib_longtext ON bib.bib# = bib_longtext.bib# AND bib.tag = bib_longtext.tag
WHERE (isbnEX_inverted.isbn LIKE '%978__________%') AND (top_circ_summary.collection_group = 'jfic') AND (bib.tag in ('245', '520', '300'))
order by top_circ_summary.ranking
Here's a sample of the (tab separated) output
isbn Title ranking tag bib# 9780143307334 a217 p. :bill. ;c21
cm. 1 300 962366 9780143307334 aDiary of a wimpy kid :bthe third
wheel /cby Jeff Kinney. 1 245 962366 9780143307334 aTrying to find
a partner for the Valentine's Day dance, Greg finds solace in the fact
that his best friend Rowley also doesn't have a date, but an
unexpected twist might turn his night around. 1 520 962366

Related

Query Optimisation left outer join 7 tables - SQL Server

Need help for faster execution and optimisation for the below.
Find the execution plan in the 2 images attached..
Data in all tables are as follows:
company - 25 rows
warranty_period - 878 rows
company_claimtype - 216 rows
claim_type - 25 rows
customer - 15980 rows
auto_handle - 25 rows
auto_handle_condition - 31 rows
PART_REQUIRED_RETURN - 4521 rows
The following query took almost 12 mins to execute and it contains only left outer join of all the above mentioned tables:
select
company0_.ID as ID1_9_0_,
warrantype1_.ID as ID1_36_1_,
partsrequi2_.ID as ID1_20_2_,
claimtype4_.ID as ID1_8_3_,
autohandle5_.ID as ID1_4_4_,
autohandle6_.ID as ID1_5_5_,
customer7_.ID as ID1_14_6_,
company0_.CREATED as CREATED2_9_0_,
company0_.UPDATED as UPDATED3_9_0_,
company0_.AUTO_REJECT_USER as AUTO_REJ4_9_0_,
company0_.CLAIM_CREDIT_ORDER_HISTORY as CLAIM_CR5_9_0_,
company0_.CLAIM_ORDER_HISTORY as CLAIM_OR6_9_0_,
company0_.COMPANY_CODE as COMPANY_7_9_0_,
company0_.COMPANY_NAME as COMPANY_8_9_0_,
company0_.CONNECTED_TO_ERP as CONNECTE9_9_0_,
company0_.DEFAULT_CUSTOMER_ID as DEFAULT16_9_0_,
company0_.DATE_FORMAT as DATE_FO10_9_0_,
company0_.DISPLAY_WORK_LOCATION as DISPLAY11_9_0_,
company0_.DISTANCE_UNIT as DISTANC12_9_0_,
company0_.ENABLE_SHIPPING_LABEL as ENABLE_13_9_0_,
company0_.HANDLE_CREDIT_INVOICES as HANDLE_14_9_0_,
company0_.RETURNS_PARTS_DAYS as RETURNS15_9_0_,
warrantype1_.CREATED as CREATED2_36_1_,
warrantype1_.UPDATED as UPDATED3_36_1_,
warrantype1_.COMPANY_ID as COMPANY_8_36_1_,
warrantype1_.PERIOD_CONSUMER as PERIOD_C4_36_1_,
warrantype1_.PERIOD_PROFESSIONAL as PERIOD_P5_36_1_,
warrantype1_.PRODUCT_GROUP as PRODUCT_6_36_1_,
warrantype1_.PRODUCT_NO as PRODUCT_7_36_1_,
warrantype1_.COMPANY_ID as COMPANY_8_36_0__,
warrantype1_.ID as ID1_36_0__,
partsrequi2_.CREATED as CREATED2_20_2_,
partsrequi2_.UPDATED as UPDATED3_20_2_,
partsrequi2_.COMPANY_ID as COMPANY_5_20_2_,
partsrequi2_.PRODUCT_NO as PRODUCT_4_20_2_,
partsrequi2_.COMPANY_ID as COMPANY_5_20_1__,
partsrequi2_.ID as ID1_20_1__,
claimtype4_.CREATED as CREATED2_8_3_,
claimtype4_.UPDATED as UPDATED3_8_3_,
claimtype4_.CATEGORY as CATEGORY4_8_3_,
claimtype4_.CLAIM_TYPE as CLAIM_TY5_8_3_,
claimtype4_.SORT_ORDER as SORT_ORD6_8_3_,
claimtype4_.TEXT_KEY as TEXT_KEY7_8_3_,
claimtypes3_.COMPANY_ID as COMPANY_2_10_2__,
claimtypes3_.CLAIMTYPE_ID as CLAIMTYP1_10_2__,
autohandle5_.CREATED as CREATED2_4_4_,
autohandle5_.UPDATED as UPDATED3_4_4_,
autohandle5_.[ACTION] as ACTION4_4_4_,
autohandle5_.[ACTIVE] as ACTIVE5_4_4_,
autohandle5_.[ASSIGN] as ASSIGN6_4_4_,
autohandle5_.[COMPANY_ID] as COMPANY_8_4_4_,
autohandle5_.[NAME] as NAME7_4_4_,
autohandle5_.[COMPANY_ID] as COMPANY_8_4_3__,
autohandle5_.ID as ID1_4_3__,
autohandle6_.CREATED as CREATED2_5_5_,
autohandle6_.UPDATED as UPDATED3_5_5_,
autohandle6_.AUTO_HANDLE_ID as AUTO_HAN7_5_5_,
autohandle6_.[OPERATOR] as OPERATOR4_5_5_,
autohandle6_.[TYPE] as TYPE5_5_5_,
autohandle6_.[VALUE] as VALUE6_5_5_,
autohandle6_.AUTO_HANDLE_ID as AUTO_HAN7_5_4__,
autohandle6_.ID as ID1_5_4__,
customer7_.CREATED as CREATED2_14_6_,
customer7_.UPDATED as UPDATED3_14_6_,
customer7_.COMPANY_ADDRESS as COMPANY_4_14_6_,
customer7_.STREET as STREET5_14_6_,
customer7_.ZIPCODE as ZIPCODE6_14_6_,
customer7_.COMPANY_ID as COMPANY_9_14_6_,
customer7_.CUSTOMER_NO as CUSTOMER7_14_6_,
customer7_.NAME as NAME8_14_6_
from
COMPANY company0_
left outer join
WARRANTY_PERIOD warrantype1_
on company0_.ID=warrantype1_.COMPANY_ID
left outer join
PART_REQUIRED_RETURN partsrequi2_
on company0_.ID=partsrequi2_.COMPANY_ID
left outer join
COMPANY_CLAIMTYPE claimtypes3_
on company0_.ID=claimtypes3_.COMPANY_ID
left outer join
CLAIM_TYPE claimtype4_
on claimtypes3_.CLAIMTYPE_ID=claimtype4_.ID
left outer join
AUTO_HANDLE autohandle5_
on company0_.ID=autohandle5_.[COMPANY_ID]
left outer join
AUTO_HANDLE_CONDITION autohandle6_
on autohandle5_.ID=autohandle6_.AUTO_HANDLE_ID
left outer join
CUSTOMER customer7_
on company0_.DEFAULT_CUSTOMER_ID=customer7_.ID
What I find particularly strange is that you get about 585k records from PART_REQUIRED_RETURN and then tag those along but when the server adds the AUTO_HANDLE data things suddenly multiply to 2.1M records. Are you sure your join takes into account the entire PK? As your query looks right now I would assume that ID is the PK of AUTO_HANDLE, however the numbers seem to contradict this. Something similar happens when adding the AUTO_HANDLE_CONDITION data.
In the end you are reading 3.3M rows, or about 4.5Gb of data. I don't think you can expect to bring this down to seconds.

Using Multiple "Layered" SQL Server queries with parameters in Access

I have tried to find answers to this question on the net but am not really getting anywhere.
I tried to do screen shots but don't have enough rep. Anyway.
Here is my current query structure.
Tables:
tblInventoryItems(intSerial, intItem, intStyle) -This is the list of current items that are on inventory(built or unbuilt)
tblLocations(intLocationID, chrLocationName, intLotType)
tblItemStyles(intItemStyleID, intBasic Style)
tblItemList(intItemID, intType, intSubtype, intStyle)
tblCommissions(intBuildEvent, intItemType, intItemSubtype)
tblBuildEvents(intSerial, intBuildEventType, dtEventDate)
First Set of Queries
qryCompletedBarns:
SELECT dbo.tblInventoryItems.intSerial,dbo.tblBuildEvents.dtEventDate
FROM dbo.tblItemList
INNER JOIN dbo.tblItemStyles ON
dbo.tblItemList.intStyle = dbo.tblItemStyles.intBasicStyle
INNER JOIN dbo.tblInventoryItems ON
dbo.tblItemList.intItemID = dbo.tblInventoryItems.intItem AND
dbo.tblItemStyles.intItemStyleID = dbo.tblInventoryItems.intStyle
INNER JOIN dbo.tblCommissions ON
dbo.tblItemList.intSubType = dbo.tblCommissions.intItemSubtype
AND dbo.tblItemList.intType = dbo.tblCommissions.intItemType
INNER JOIN dbo.tblBuildEvents ON
dbo.tblBuildEvents.intBuildEventType = dbo.tblCommissions.intBuildEvent AND
dbo.tblInventoryItems.intSerial = dbo.tblBuildEvents.intSerial
WHERE (dbo.tblCommissions.ynCompletesBarn = 1)
So First we select all items that have a build event that qualifies them as completed. An item can have multiple build events but there is only one that qualifies it as completed
qryCompletedBarnDateFilter:
SELECT qryCompletedBarns.intSerial, qryCompletedBarns.dtEventDate
FROM qryCompletedBarns
WHERE (((qryCompletedBarns.dtEventDate)<=InventoryDate()));
Then we jump to Access and filter those results by a UDF that pulls the criteria for the build date of the item(Saved report definitions are stored in a table).
Now, we have a list of all Inventory items that meet the first criteria, which is that they must be built on or before the Inventory Date that the user selects.
Second set of queries:
qryInventoryLogDateFilter:
SELECT tblHaulLogs.*, tblHaulLogs.dtmHaulDate
FROM tblHaulLogs
WHERE (((tblHaulLogs.dtmHaulDate)<=InventoryDate()));
Get all the Haullogs (or you could call them inventory transactions) that are equal to or older than the InventoryDate criteria.
qryLastLogForBarn:
SELECT t1.intSerial, t1.intHaulType, t1.intDestinationSource, t1.intDestination
FROM qryInventoryLogDateFilter AS t1
LEFT JOIN qryInventoryLogDateFilter AS t2
ON (t1.intLogID < t2.intLogID) AND (t1.intSerial = t2.intSerial)
WHERE t2.intLogID IS NULL;
Get the last inventory transaction for each Inventory Item. This query is actually very fast when executed on the server natively.
qryInventoryCurrentCustomer:
SELECT tblSales.intSerial, tblCustomers.chrFirstName + ' ' + tblCustomers.chrLastName
AS chrCustomerName, tblSales.dtSaleDate
FROM tblSales INNER JOIN tblCustomers ON
tblSales.intCustomer = tblCustomers.intCustID
LEFT OUTER JOIN tblHaulLogs ON
tblSales.intSaleID = tblHaulLogs.intOrigon
WHERE (tblHaulLogs.intHaulType IS NULL) AND (tblSales.bolSaleCancelled = 0)
OR (tblHaulLogs.intHaulType <> 3) AND
(tblSales.intOrderType <4) AND (tblSales.bolSaleCancelled = 0)
GROUP BY tblSales.intSerial, tblCustomers.chrFirstName + ' ' + tblCustomers.chrLastName,
tblSales.dtSaleDate, tblSales.intOrderType;
This query does not actually return an "Inventory Item" but rather checks the sales table and returns a customer name if the inventory item was sold.
Now we finally join the two "sections" and the "Current Customer Query":
SELECT qryCompletedBarnDateFilter.intSerial, qryLastLogForBarn.intHaulType,
IIf(IsNull([intDestination]),7,[intDestination]) AS Location,
qryInventoryCurrentCustomer.chrCustomerName,
qryInventoryCurrentCustomer.dtSaleDate, qryCompletedBarnDateFilter.dtEventDate
FROM (qryCompletedBarnDateFilter
LEFT JOIN qryLastLogForBarn
ON qryCompletedBarnDateFilter.intSerial = qryLastLogForBarn.intSerial)
LEFT JOIN qryInventoryCurrentCustomer
ON qryCompletedBarnDateFilter.intSerial = qryInventoryCurrentCustomer.intSerial
WHERE (((qryLastLogForBarn.intDestinationSource)=1
Or (qryLastLogForBarn.intDestinationSource) Is Null));
Then we join to the "Locations" table to get the name of the inventory item's location, and also filter out a certain type of location.
qryRetailBarnsOnInventory:
SELECT qryBarnsOnInventory.intSerial, qryBarnsOnInventory.Location,
tblLocations.chrLocationName, qryBarnsOnInventory.chrCustomerName,
qryBarnsOnInventory.dtEventDate, qryBarnsOnInventory.intHaulType
FROM tblLocations
INNER JOIN qryBarnsOnInventory
ON tblLocations.intLocationID = qryBarnsOnInventory.Location
WHERE (((tblLocations.intLotType)=1));
The Final Query:
SELECT tblItemStyles.chrItemStyle, tblInventoryItems.intSerial,
tblItemSizeList.intItemSize, tblItemPrices.ccyPrice,
tblInventoryItems.txtOptions, qryRetailBarnsOnInventory.chrCustomerName,
qryRetailBarnsOnInventory.dtEventDate, qryRetailBarnsOnInventory.intHaulType,
qryRetailBarnsOnInventory.chrLocationName, tblItemList.intType,
tblItemList.intSubType, tblItemList.intStyle, tblItemSizes.intItemSizeID,
tblItemStyles.intItemStyleID, tblItemPrices.intPriceSheet,
tblInventoryItems.bolinTransit
FROM tblItemSizeList INNER JOIN (tblItemSizes INNER JOIN
(qryRetailBarnsOnInventory INNER JOIN (((tblItemList INNER JOIN
tblItemPrices ON tblItemList.intItemID = tblItemPrices.intItemID)
INNER JOIN tblItemStyles ON tblItemList.intStyle = tblItemStyles.intBasicStyle)
INNER JOIN tblInventoryItems
ON (tblItemList.intItemID = tblInventoryItems.intItem)
AND (tblItemStyles.intItemStyleID = tblInventoryItems.intStyle))
ON qryRetailBarnsOnInventory.intSerial = tblInventoryItems.intSerial)
ON tblItemSizes.intItemSizeID = tblItemList.intSize)
ON tblItemSizeList.intItemSizeID = tblItemSizes.intSize
WHERE (((tblItemList.intType) Like ItemType())
AND ((tblItemList.intSubType) Like ItemSubType())
AND ((tblItemList.intStyle) Like BasicStyle())
AND ((tblItemSizes.intItemSizeID) Like ItemSize())
AND ((tblItemStyles.intItemStyleID) Like SubStyle())
AND ((tblItemPrices.intPriceSheet)=1)
AND ((tblInventoryItems.bolinTransit)=False)
AND ((IIf(IsNull([intHaulType]),2,3)) Like NewUsed())
AND ((IIf(IsNull([chrCustomerName]),2,3)) Like IsSold())
AND ((qryRetailBarnsOnInventory.Location) Like LotID()));
Summary:
In plain English, here is what I want to happen:
Get all Inventory Items from the inventory items table that were completed on or before the date criteria, then get the last inventory transaction on or before the date criteria for each of those items. And then filter by additional criteria that the user provides
I have the correct results but it is SLOW! Like 10-15 seconds. And we do a tremendous amount of queries every day.
What my idea was is to put this all on SQL Server in sprocs or views. If I do this all with views on SQL server and type manual criteria in, it's blazing fast. The problem I have is that every query requires a parameter. Not just the last one. I can pass parameters for the last query all right with a passthrough query but how do I pass the date criteria parameters for the first query?
EDIT:
Would it be possible to get the date criteria from another table? The criteria is stored in another table in the DB. If I could perform some kind of lookup to get that value and use it as the criteria that would make life really simple.

ibm 1 sql creating - need to add 2 tables

I have following view which is working but not sure how to add 2 tables to join.
This table is adres1 and it will join on the IDENT# and IDSFX# to table
prodta.adres1 called adent# and adsfx#, there I need a col. ads15.
then i also need to get the ship to, row in this adres1. this we get first from the order table, prodta. oeord1 in col. odgrc#. This grc# is 11 pos and is combined 8 and 3 of the ent and suf. these 2 represent the ship to record and looking in same table adres1 (we do have many logical views on them if it's easier, like adres15) we can get col. ADSTTC for the ship to state.
Not sure if can included these 2 new parts to the current view created code below. Please ask if something not clear, it's an old system and somewhat developed convoluted.
CREATE VIEW Prolib.SHPWEIGHTP AS SELECT
T01.IDORD#,
T01.IDDOCD,
T01.IDPRT#,
t01.idsfx#,
T01.IDSHP#,
T01.IDNTU$,
T01.IDENT#,
(T01.IDNTU$ * T01.IDSHP#) AS LINTOT,
T02.IAPTWT,
T02.IARCC3,
T02.IAPRLC,
T03.PHVIAC,
T03.PHORD#,
PHSFX#,
T01.IDORDT,
T01.IDHCD3
FROM PRODTA.OEINDLID T01
INNER JOIN PRODTA.ICPRTMIA T02 ON T01.IDPRT# = T02.IAPRT#
INNER JOIN
(SELECT DISTINCT
PHORD#,
PHSFX#,
PHVIAC,
PHWGHT
FROM proccdta.pshippf) AS T03 ON t01.idord# = T03.phord#
WHERE T01.IDHCD3 IN ('MDL','TRP')
I'm not exactly clear on what you're asking, and it looks like some of the column-names are missing from your description, but this should get you pretty close:
CREATE VIEW Prolib.SHPWEIGHTP AS
SELECT T01.IDORD#,
T01.IDDOCD,
T01.IDPRT#,
t01.idsfx#,
T01.IDSHP#,
T01.IDNTU$,
T01.IDENT#,
( T01.IDNTU$ * T01.IDSHP# ) AS LINTOT ,
T02.IAPTWT,
T02.IARCC3,
T02.IAPRLC,
T03.PHVIAC,
T03.PHORD#,PHSFX#,
T01.IDORDT,
T01.IDHCD3,
t04.ads15
FROM PRODTA.OEINDLID T01
INNER JOIN PRODTA.ICPRTMIA T02
ON T01.IDPRT# = T02.IAPRT#
INNER JOIN (SELECT DISTINCT
PHORD#,
PHSFX#,
PHVIAC,
PHWGHT
FROM proccdta.pshippf) AS T03
ON t01.idord# = T03.phord#
JOIN prodta.adres1 as t04
on t04.adent# = t01.adent#
and t04.adsfx# = t01.adsfx#
JOIN prodta.oeord1 t05
on t05.odgrc# = T01.IDENT# || T01.SUFFIX
WHERE T01.IDHCD3 IN ('MDL','TRP')
Let me know if you need more details.
HTH !

SQL Server LEFT JOIN

This query has been keeping me busy for the last couple of days. I tried to rewrite it with different ideas but I keep having the same problem. To simplify the problem I put part of my query in a view, this view returns 23 records. Using a left join I would like to add fields coming from the table tblDatPositionsCalc to these 23 records. As you can see I have an additional condition on the tblDatPositionsCalc in order to only consider the most recent records. With this condition it would return 21 records. The join should be on two fields together colAccount and colId.
I simply want the query to return the 23 records from the view and where possible have the information from tblDatPositionsCalc. There is actually only 2 records in the view without corresponding id and account in tblDatPositionsCalc, that means out of the 23 records only 2 will have missing values in the fields coming from the table tblDatPositionsCalc.
The problem with my query is that it only returns the 21 records from tblDatPositionsCalc. I don't understand why. I tried to move the condition on date in just after the JOIN condition but that did not help.
SELECT TOP (100) PERCENT
dbo.vwCurrPos.Account,
dbo.vwCurrPos.Id,
dbo.vwCurrPos.TickerBB,
dbo.vwCurrPos.colEquityCode,
dbo.vwCurrPos.colType,
dbo.vwCurrPos.colCcy,
dbo.vwCurrPos.colRegion,
dbo.vwCurrPos.colExchange,
dbo.vwCurrPos.[Instr Type],
dbo.vwCurrPos.colMinLastDay,
dbo.vwCurrPos.colTimeShift,
dbo.vwCurrPos.Strike,
dbo.vwCurrPos.colMultiplier,
dbo.vwCurrPos.colBetaVol,
dbo.vwCurrPos.colBetaEq,
dbo.vwCurrPos.colBetaFloor,
dbo.vwCurrPos.colBetaCurv,
dbo.vwCurrPos.colUndlVol,
dbo.vwCurrPos.colUndlEq,
dbo.vwCurrPos.colUndlFut,
tblDatPositionsCalc_1.colLots,
dbo.vwCurrPos.[Open Positions],
dbo.vwCurrPos.colListMatShift,
dbo.vwCurrPos.colStartTime,
tblDatPositionsCalc_1.colPrice,
tblDatPositionsCalc_1.colMktPrice,
dbo.vwCurrPos.colProduct,
dbo.vwCurrPos.colCalendar,
CAST(dbo.vwCurrPos.colExpiry AS DATETIME) AS colExpiry,
dbo.vwCurrPos.colEndTime,
CAST(tblDatPositionsCalc_1.colDate AS datetime) AS colDate,
dbo.vwCurrPos.colFund,
dbo.vwCurrPos.colExchangeTT,
dbo.vwCurrPos.colUserTag
FROM dbo.vwCurrPos
LEFT OUTER JOIN dbo.tblDatPositionsCalc AS tblDatPositionsCalc_1
ON tblDatPositionsCalc_1.colId = dbo.vwCurrPos.Id
AND tblDatPositionsCalc_1.colAccount = dbo.vwCurrPos.Account
WHERE (tblDatPositionsCalc_1.colDate =
(SELECT MAX(colDate) AS Expr1 FROM dbo.tblDatPositionsCalc))
ORDER BY
dbo.vwCurrPos.Account,
dbo.vwCurrPos.Id,
dbo.vwCurrPos.colEquityCode,
dbo.vwCurrPos.colRegion
Any idea what might cause the problem?
(Option 1) DrCopyPaste is right so your from clause would look like:
...
FROM dbo.vwCurrPos
LEFT OUTER JOIN dbo.tblDatPositionsCalc AS tblDatPositionsCalc_1
ON tblDatPositionsCalc_1.colId = dbo.vwCurrPos.Id
AND tblDatPositionsCalc_1.colAccount = dbo.vwCurrPos.Account
and (tblDatPositionsCalc_1.colDate =
(SELECT MAX(colDate) AS Expr1 FROM dbo.tblDatPositionsCalc))
...
reason: the where clause restriction of left joined to column = some expression with fail to return for "null = something" so the row will be removed.
(Option 2) As oppose to pushing code in to additional views where it is harder to maintain you can nest sql select statements;
select
X.x1,X.x2,
Y.*
from X
left join
(select Z.z1 as y1, Z.z2 as y2, Z.z3 as y3
from Z
where Z.z1 = (select max(Z.z1) from Z)
) as Y
on x.x1 = Y.y1 and X.x2 = Y.y2
The advantage here is you check each nested sub query a move out quickly. Although if you still building up more logic check out common table expressions (CTE's) http://msdn.microsoft.com/en-us/library/ms175972.aspx

Grouping results to get unique rows after multiple joins

disclaimer : I don't have full control over the db schema don't judge the data structure or the naming conventions :)
I am doing this large query with multiple joins :
SELECT TOP 30
iss.iss_lKey as IssueId,
iss.iss_sName as IssueName,
con.con_lKey as ContainerId,
con.con_sName as ContainerName,
sto.sto_lKey as StoryId,
sto.sto_sName as StoryName,
sto.sto_Guid as StoryGuid,
sto.sto_sByline as Byline,
sto.sto_created_dWhen as StoryCreatedDate,
sto.sto_deadline_dWhen as StoryDeadline,
sto.sto_lType as StoryType,
sto.sto_sct_lKey as StoryCategory,
sto.sto_created_use_lKey as CreatedBy,
sfv.sfv_tValue as FieldValue,
sf.sfe_lKey as StoryFieldId,
sf.sfe_sCaption as StoryFieldCaption,
sre.sre_lIndex as RevisionIndex
FROM tStory30 sto
JOIN tContainer30 con ON sto.sto_con_lKey = con.con_lKey
JOIN tIssue30 iss ON con.con_iss_lKey = iss.iss_lKey
LEFT OUTER JOIN tStoryRevision30 sre ON sre.sre_sto_lKey = sto.sto_lKey
LEFT OUTER JOIN tStoryField30 sf ON sre.sre_lKey = sf.sfe_sre_lKey
LEFT OUTER JOIN tStoryFieldValue30 sfv ON sfv.sfv_sfe_lKey= sf.sfe_lKey
WHERE sre.sre_lIndex = 0
AND (sto.sto_sName LIKE '%' + #0 + '%'
OR sfv.sfv_tValue LIKE '%' + #0 + '%')";
What I need is really only one row by StoryId, that includes the FieldValue that matched if there was any. I am currently grouping in the code to produce the output, but that prevents me from paging the results.
from r in items
group r by new { r.StoryId, r.ContainerId, r.IssueId }
into storyGroup
select {
storyGroup.Key.StoryId,
storyGroup.Key.ContainerId,
storyGroup.Key.IssueId,
Hits = storyGroup.ToList()
}
Is there any way to achieve this kind of grouping in sql, so that I could then page the result properly (using ROW_NUMBER() OVER)?
Also, I am aware that this is bad practice and should use FullText search. it is planned to setup a solr instance, or use the fulltext options in sqlserver. This is a first attempt to get a smthg going.
EDIT
trying to explain verbally what I try to achieve :
For the context, our app is a cms for magazine editor/publisher.
for a given magazine they have many Issues
each issue has many Container (sort of logical article group)
in each container you have several stories
a story van have 0 or many revisions
the fields of a story are stored by revision (many field per revision)
and a field has a field value.
I need to retrieve the stories that have a given text in the name or in a field value of the first revision (that's the where revisionIndex = 0).
but I also need to retrieve associated data for each story. (issueId, name, containerId and name, and so one..)
the difficult one is probably to retrieve one of the fieldvalue that matched the search. I don't need all of them, just one...
hope this helps!
EDIT Sample data searching for "test". I simplified the columns to make it easier to understand.
Row | IssueId | IssueName | ContainerId | StoryId | FieldValue
1 | 11 IssueName A 394 868 Test Marsupilami bla bla youpi
2 | 40 IssueName B 6 631 story save test
3 | 40 IssueName B 6 666 test story
4 | 4 IssueName c 30 846 test abs
5 | 4 IssueName c 30 846 absc test
6 | 4 IssueName c 30 846 hello test
I am able to get the row number in sqlserver on my query, but here, as you see, I get amultiple times the same story. In this case, I could have simple the following result:
Row | IssueId | IssueName | ContainerId | StoryId | FieldValue
1 | 11 IssueName A 394 868 Test Marsupilami bla bla youpi
2 | 40 IssueName B 6 631 story save test
3 | 4 IssueName c 30 846 test abs
if a story would have test in the story name, then I am ok with a null value in the column FieldValue which field value is selected doesn't matter much.
This is a digression but are you aware that you have converted a left join to an inner join?
LEFT OUTER JOIN tStoryRevision30 sre ON sre.sre_sto_lKey = sto.sto_lKey
LEFT OUTER JOIN tStoryField30 sf ON sre.sre_lKey = sf.sfe_sre_lKey
LEFT OUTER JOIN tStoryFieldValue30 sfv ON sfv.sfv_sfe_lKey= sf.sfe_lKey
WHERE sre.sre_lIndex = 0
try this instead
LEFT OUTER JOIN tStoryRevision30 sre ON sre.sre_sto_lKey = sto.sto_lKey
AND sre.sre_lIndex = 0
LEFT OUTER JOIN tStoryField30 sf ON sre.sre_lKey = sf.sfe_sre_lKey
LEFT OUTER JOIN tStoryFieldValue30 sfv ON sfv.sfv_sfe_lKey= sf.sfe_lKey
(I would have done this in a comment but it is easier to see the code change here.

Resources