FullText Search priory - sql

i want to use full text search for create simple search engin.
for example when user search "Hamed Khatami",search engine fetch rows that have both word then
fetch rows that have one of them.
i created bottom query
select * from dbo.DownloadCenterFileLanguage
where CONTAINS(*,N' "*PC*" and "*Game*" ')
UNION
select * row from dbo.DownloadCenterFileLanguage
where CONTAINS(*,N' "*PC*" or "*Game*" ')
but it has a problem , it order result base on P.K.
Appreciate to help me.
Regards

after a day , i understand that my approach is wrong.
look at these codes
select * from dbo.DownloadCenterFileLanguage
inner join
CONTAINSTABLE (dbo.DownloadCenterFileLanguage,
*, 'ISABOUT ( "hamed" , "khatami" ,"*hamed*", "*khatami*"')') AS KEY_TBL
ON DownloadCenterFileLanguage.Id = KEY_TBL.[KEY]
with this approach everything work correctly
Best Regards

Related

Is there a better way instead of using multiple UNION ALL queries in SQL?

I want to run the same select query but change only the romecode field which is a string and limit each result by 30. Finally, I concatenate all the results into one usingUNION ALL.
This is the full list of codes which means that I need to repeat the same select + UNION ALL many times:
('G1603', 'E1205', 'D1101', 'N1202', 'M1501', 'G1402', 'I1401',
'M1607', 'J1102', 'C1201', 'M1801', 'I1203', 'I1604', 'M1705',
'H2102', 'M1203', 'K2503', 'E1103', 'N1103', 'M1805', 'H1204',
'M1602', 'D1106', 'M1707', 'C1501', 'M1701', 'G1101', 'J1302',
'C1103', 'E1401', 'J1201', 'H1301', 'C1301')
And how I am doing now:
(
SELECT
appellationlibelle,
romelibelle,
romecode,
descriptioncleaned,
description
FROM
`scrappers-293910.vigilant_memory_raw.indeed`
WHERE romecode = 'G1603' LIMIT 30)
UNION ALL
(
SELECT
appellationlibelle,
romelibelle,
romecode,
descriptioncleaned,
description
FROM
`scrappers-293910.vigilant_memory_raw.indeed`
WHERE romecode = 'E1205' LIMIT 30)
UNION ALL
(
SELECT
appellationlibelle,
romelibelle,
romecode,
descriptioncleaned,
description
FROM
`scrappers-293910.vigilant_memory_raw.indeed`
WHERE romecode = 'D1101' LIMIT 30)
I repeat this select 33 times.
I tried to find a similar solution but I couldn't find any. If it is a duplicated question just kindly drop the link please :D
Combine into a single query, use Row_number() to give each row a number resetting on each Romecode. Then bring back all rows where the row number is 30 or below.
You just need to clarify how you choose which 30 rows to bring back. Your original query doesn't specify so you'll need to figure that out to plug into the row_number order by.
select
*
from
(
SELECT
appellationlibelle,
romelibelle,
romecode,
descriptioncleaned,
description,
row_number() over (partition by romecode order by datecreation) as rn
FROM
scrappers-293910.vigilant_memory_raw.indeed
WHERE romecode in
('G1603', 'E1205', 'D1101', 'N1202', 'M1501', 'G1402', 'I1401',
'M1607', 'J1102', 'C1201', 'M1801', 'I1203', 'I1604', 'M1705',
'H2102', 'M1203', 'K2503', 'E1103', 'N1103', 'M1805', 'H1204',
'M1602', 'D1106', 'M1707', 'C1501', 'M1701', 'G1101', 'J1302',
'C1103', 'E1401', 'J1201', 'H1301', 'C1301')
) thedata where thedata.rn <= 30
Maybe I've misunderstood your question, but this seems easy to do with an IN (...) condition.
SELECT
appellationlibelle,
romelibelle,
romecode,
descriptioncleaned,
description
FROM
`scrappers-293910.vigilant_memory_raw.indeed`
WHERE romecode in ('G1603', 'E1205', 'D1101', 'N1202', 'M1501', 'G1402', 'I1401',
'M1607', 'J1102', 'C1201', 'M1801', 'I1203', 'I1604', 'M1705',
'H2102', 'M1203', 'K2503', 'E1103', 'N1103', 'M1805', 'H1204',
'M1602', 'D1106', 'M1707', 'C1501', 'M1701', 'G1101', 'J1302',
'C1103', 'E1401', 'J1201', 'H1301', 'C1301')
-- LIMIT 30 <-- don't know if you still want this?

Need assistance with t-sql query max date + distinct

Just like in the title I need some help with t-SQL query to deliver a report. What I need to do is to pull data from client table and shipment table. Next the records must exclude those clients which have not done any shipments starting from 100 and must include the day of the last order made by the client.
OK lets clear what is the goal of this query.
I do not know if it is good idea but I have pasted an image from excel.
Anyway, as can you see at the moment I am pulling data that includes all of those shipments lately made but I am looking to find out how to exclude those clients whose booked more shipments and they starting from '100'.
And this is my query
SELECT j.ClientName,
j.ContactName,
j.PhoneMumber,
j.Email,
js.OrderNumb,
js.SentDate
FROM Client j
outer apply (
SELECT top 1 *
FROM Shipment js
WHERE js.ClientNum= j.ClientNUm
ORDER BY
js.SentDate DESC
) js
where j.ClientBur= 'HB'
Can you help me out to get on the right track and find solution?
The question is not very clear, but I'm guessing you are probably looking for condition Not like '100%'
Something like this
select * -- whatever
from Client j
where j.ClientBur= 'HB'
and j.OrderNum not like '100%'
You can add one more OUTER APPLY to get TOP 1 row with OrderNumb started with 100 and then exclude them in WHERE statement:
SELECT j.ClientName,
j.ContactName,
j.PhoneMumber,
j.Email,
js.OrderNumb,
js.SentDate
FROM Client j
outer apply (
SELECT top 1 *
FROM Shipment js
WHERE js.ClientNum= j.ClientNUm
ORDER BY
js.SentDate DESC
) js
outer apply (
SELECT top 1 *
FROM Shipment js
WHERE js.ClientNum= j.ClientNUm
AND LEFT(js.OrderNumb,3) = '100'
ORDER BY js.SentDate DESC
) js100
WHERE j.ClientBur= 'HB'
AND js100.OrderNumb IS NULL

Code works fine till COALESCE

HI this code is working fine until the last statement there is more to it but was wondering if we can learn what is incorrect on this.
this is on the ibm i (as400)
'SQL0199 Keyword Select Not Selected. Valid Tokens: For Use Skip Wait With Fetch Order Union Except Optimize' can you explain this issue to me?
SELECT COUNT(*)
FROM DLIB.ORDHEADR,DLIB.TRANCODE,DLIB.TRA11
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND OHORDT IN('RTR','INT','SAM')
AND OHREQD = replace(char(current date, iso), '-', '')
AND OHHLDC = ' '
AND ( ( TSTATS IN('AEP','SPJ')
AND OHORD# NOT in (SELECT a.TSORD#
FROM DLIB.TRANCODE a
WHERE a.TSTATS IN('EEP','SPC')
)
)
OR TSTATS IN('EEP','SPC')
AND OHORD# IN (SELECT DISTINCT(C.TSORD#)
FROM DLIB.TRANCODE C
JOIN (SELECT DISTINCT (B.TSORD#), MAX(B.TSUTIM) AS C_TSUTIM,
MAX(B.TSUDAT) AS C_TSUDAT
FROM DLIB.TRANCODE B
WHERE B.TSTATS IN ('EEP','SPC','ECM','ECT',
'ECA','CEL','BOC','COM',
'COO','REV','MCO','CPA',
'ECV','ECC','EPT','EPM',
'CAT','CAC','CAM','CAS',
'MAC','004','006','600',
'MEP','EPC','CPK')
GROUP BY B.TSORD#
) q1
ON C.TSORD# = q1.TSORD#
AND C.TSUDAT = q1.C_TSUDAT
AND C.TSUTIM = q1.C_TSUTIM
WHERE C.TSORD# NOT IN (SELECT F.TSORD#
FROM DLIB.TRANCODE F
WHERE F.TSTATS IN ('SPJ','REL','EAS','REV',
'STP','SPT','PPC','SPM',
'BPA','BPB','BPC','BPD','BPE',
'BPF','BPG','BPH','BPI','BPJ',
'BPK','BPL','BPM','BPN','CBM',
'BPO','BPP','BAT','BCM',
'BAM','WAT','WAM','LBL','012',
'006','600','004','SCP','CBA',
'CBB','CBC','CBD','CBE',
'CBF','CBG','CBH','CBI','CBJ',
'CBK','CBL','CBM','CBN','CBO',
'CBP','CBQ','CBR','CBS',
'CBT','CBU','CBV','CBW',
'CBX','CBY','CBZ','CB1',
'CB2','CB3','CB4','CB5')
)
AND C.TSTATS IN('EEP','SPC')
)
)
-- till here it's fine.
SELECT COALESCE(SUM(OdQty#),0)
You need to use GROUP BY to SUM.
SELECT COALESCE(SUM(Goals),0) AS TeamGoals
FROM Players
GROUP BY TeamId
After formatting your code so that we can see better where the various parts of the statement begins and ends, we can see what matches up with what.
Everything up to "till here it's fine" is one SQL SELECT statement. You need a semicolon to begin your next query, which starts with SELECT COALESCE(), but is incomplete since there is no FROM clause. Once you've put the terminator on the first statement it should run.
The second query is another question. You didn't show us the rest of the code. As TeKapa says, you need a GROUP BY clause anytime you use an aggregate function. But this is only required, if you are also including a non-aggregate column in the results.
SELECT TeamID, COALESCE(SUM(Goals),0) AS TeamGoals
FROM Players
GROUP BY TeamId
That will give you each TeamID in Players, and the total Goals for each team. You would probably also include ORDER BY TeamID
But if you simply want the combined total of all Players, it is completely valid to say
SELECT SUM(Goals) AS TotalGoals
FROM Players
Taking a step back, it seems like your query has gotten so complex, that even you may be having difficulty managing it. Hopefully others wont be asked to maintain something like this.
If such code is going into production, I recommend finding ways to modularize portions of the complexity, such as with views, or common table expressions. It may also be a good idea to store those lists of values in a table, rather than hardcoding them.

Access data source using SQL to show most recent entry per site

First of all I am a complete beginner to SQL and have been thrown in at the deep end a bit ! I'm learning as I go along and each mistake I make or question I ask will hopefully help me develop... please be kind :)
I have a working query that extracts electricty meter readings and other information. I am after finding the most recent reading for each site. This is the query at the moment :
PARAMETERS [Site Group] Text ( 255 );
SELECT
Lookup.Lookup_Name AS [Group],
Contacts.Name AS Site,
Points.Number AS MPAN,
Max(DataElectricity.Date) AS MaxDate,
DataElectricity.M1_Present,
DataElectricity.M2_Present,
DataElectricity.M3_Present,
DataElectricity.M4_Present,
DataElectricity.M5_Present,
DataElectricity.M6_Present,
DataElectricity.M7_Present,
DataElectricity.M8_Present,
DataElectricity.Direct
FROM
DataElectricity INNER JOIN (Lookup INNER JOIN (Points INNER JOIN Contacts ON Points.Contacts_Id = Contacts.Id) ON Lookup.Lookup_Id = Contacts.Group_1) ON DataElectricity.Point_Id = Points.Id
WHERE
((DataElectricity.Direct)='D')
GROUP BY
Lookup.Lookup_Name, Contacts.Name, Points.Number, DataElectricity.M1_Present, DataElectricity.M2_Present, DataElectricity.M3_Present, DataElectricity.M4_Present, DataElectricity.M5_Present, DataElectricity.M6_Present, DataElectricity.M7_Present, DataElectricity.M8_Present, DataElectricity.Direct
ORDER BY
Lookup.Lookup_Name, Contacts.Name, Max(DataElectricity.Date) DESC;
However this returns all the readings for a site rather than just the most recent... I'm sure this is simple but I can't figure it out.
Any advice or guidence is gratefully received :)
Can't you just use top 1 to get only the first result?
SELECT top 1 ...
I have evolved the code a bit further using caspian's suggestion of SELECT top 1... but am struggling to refine it further and produce the result I need.
PARAMETERS [Site Group] Text ( 255 );
SELECT
Lookup.Lookup_Name,
Contacts.Name AS Site,
Points.Number AS MPAN,
DataElectricity.M1_Present,
DataElectricity.M2_Present,
DataElectricity.M3_Present,
DataElectricity.M4_Present,
DataElectricity.M5_Present,
DataElectricity.M6_Present,
DataElectricity.M7_Present,
DataElectricity.M8_Present,
DataElectricity.Direct
FROM
(
SELECT TOP 1 DataElectricity.Date AS MaxDate,
DataElectricity.M1_Present,
DataElectricity.M2_Present,
DataElectricity.M3_Present,
DataElectricity.M4_Present,
DataElectricity.M5_Present,
DataElectricity.M6_Present,
DataElectricity.M7_Present,
DataElectricity.M8_Present,
DataElectricity.Point_id
FROM
DataElectricity
ORDER BY MaxDate DESC
)
DataElectricity INNER JOIN (Lookup INNER JOIN (Points INNER JOIN Contacts ON Points.Contacts_Id = Contacts.Id) ON Lookup.Lookup_Id = Contacts.Group_1) ON DataElectricity.Point_Id = Points.Id
WHERE
((Lookup.Lookup_Name)=Lookup_Name)
ORDER BY
Lookup.Lookup_Name, Contacts.Name, MaxDate DESC;
I do have a Google Drive file showing a small example of the data tables and desired result with hopfully a clear guide as to how the tables connect.
https://docs.google.com/file/d/0BybrcUCD29TxWVRsV1VtTm1Bems/edit?usp=sharing
The actual data contains hundreds of Site Groups each with potentially hundreds of sites.
I would like my end users to be able to select the Site Group name from the Lookup.Lookup_Name list and for it to return all the relevant sites and readings.
.... I really hope that makes sense !

Why do I have to use DISTINCT for this to work?

here's my problem: I have an SQL query that makes 4 calls to a lookup table to return their values from a list of combinations in another table. I finally got this working, and for some reason, when I run the query without DISTINCT, I get a ton of data back, so I'm guessing that I'm either missing something or not doing this correctly. It would be really great if this would not only work, but also return the list alphabetically by the first colour name.
I'm putting my SQL here I hope I've explained this well enough:
SELECT DISTINCT
colour1.ColourID AS colour1_ColourID,
colour1.ColourName AS colour1_ColourName,
colour1.ColourHex AS colour1_ColourHex,
colour1.ManufacturerColourID AS colour1_ManufacturerColourID,
colour2.ColourID AS colour2_ColourID,
colour2.ColourName AS colour2_ColourName,
colour2.ColourHex AS colour2_ColourHex,
colour2.QEColourID2 AS colour2_QEColourID2,
colour3.ColourID AS colour3_ColourID,
colour3.ColourName AS colour3_ColourName,
colour3.ColourHex AS colour3_ColourHex,
colour3.QEColourID3 AS colour3_QEColourID3,
colour4.ColourID AS colour4_ColourID,
colour4.ColourName AS colour4_ColourName,
colour4.ColourHex AS colour4_ColourHex,
colour4.QEColourID4 AS colour4_QEColourID4,
Combinations.ID,
Combinations.ManufacturerColourID AS Combinations_ManufacturerColourID,
Combinations.QEColourID2 AS Combinations_QEColourID2,
Combinations.QEColourID3 AS Combinations_QEColourID3,
Combinations.QEColourID4 AS Combinations_QEColourID4,
Combinations.ColourSupplierID,
ColourSuppliers.ColourSupplier
FROM
ColourSuppliers INNER JOIN
(
colour4 INNER JOIN
(
colour3 INNER JOIN
(
colour2 INNER JOIN
(
colour1 INNER JOIN Combinations ON
colour1.ColourID=Combinations.ManufacturerColourID
) ON colour2.ColourID=Combinations.QEColourID2
) ON colour3.ColourID=Combinations.QEColourID3
) ON colour4.ColourID=Combinations.QEColourID4
) ON ColourSuppliers.ColourSupplierID=Combinations.ColourSupplierID
WHERE Combinations.ColourSupplierID = ?
Thanks
Steph
It looks as though you've probably got multiple records for each set of four colour combinations in the Combinations table - posting the structure of the table might help us to work it out.
Adding the clause order by colour1.ColourName to the end of the query should sort it alphabetically by the first colour name.
My guess (and it is a guess because your SQL query is very wide!) is that you're getting the cartesian product.