Simple SQL Query- Exchanging IntegerID With Name From Other Table.
Apr 17, 2008
Hi Guys,
I have the following query:
SELECT DecisionSessionID, RuleID, Operator1, Criteria1, AttributeForCriteria1, Operator2, Criteria2, AttributeForCriteria2, Operator3, Criteria3,
AttributeForCriteria3, Result
FROM myTable
WHERE (SessionID = @SessionID)
Now I want that instead of the complicated Criteria1, Crieria 2 and Criteria 3 values the users sees the names which
are defined in myCriteria. I though the query might then look like this
SELECT
DecisionSessionID,
RuleID,
Operator1,
Criteria1 (SELECT CriteriaName FROM myCriteria WHERE Criteria1 = CriteriaID),
AttributeForCriteria1,
Operator2,
Criteria2 (SELECT CriteriaName FROM myCriteria WHERE Criteria2 = CriteriaID).
AttributeForCriteria2,
Operator3,
Criteria3 (SELECT CriteriaName FROM myCriteria WHERE Criteria3 = CriteriaID),
AttributeForCriteria3,
Result
FROM dbo.aspnet_FuzzyRules
WHERE (DecisionSessionID = @DecisionSessionID)
I am not very familiar with the sql syntax and as you can imagine the above psyeudo version is not really working.
Do you know how the correct syntax for such a nested query would look like?
I have a database with three tablestbl_listings - listings of houses on for saletbl_intersted - table which tracks if a user is interested in thelisting, it has two columns mls(the key for tbl_listings) and user(userlogin)tbl_review - table which trackes if a user has reviewed the listing.Like tbl_interested it has two columns (the key for tbl_listings) anduser(user login)How can I create a query on tbl_listings for reocords reviewed by oneuser?I am trying to create a query for listings that are revied by useruserid. I am using the query below. It works fine unless there is arecord in tbl_interested for a differnt user.In reality I am calling this query from the web. On the website I havean intersted dropdown with the choices All, interested, not interested.The website also has a reviewed dropdown with all, reviewed and notreviewed.I am using the query below as a starting point. my query works finewith one user, but if a user2 enters a record in tbl_intersted itthrows off the left join for user1. How can I fix this?SELECT COUNT(B.reviewed) AS review_count,Count(B.mls) as mls_count,A.mls,FROM mls.tbl_listings ALEFT OUTER JOIN mls.tbl_review B ON A.mls = B.mlsLEFT OUTER JOIN mls.tbl_interested D ON A.mls = D.mlswhere (B.reviewed = 'userid') and ((D.interested is null) or(D.interested = 'userid'))----My query works fine if there is one user, however once user2 reviews arecord from tbl_listing user1
...but apparently not me, I'm very new with this T-SQL stuff and am seeking the advice of the seasoned pros at this forum.
Description on my SQL-5 Environment:
Table I Sales: Prod_ID, Prod_DT, Sales_DT, Buyer_Name, Buyer_State
Table II Repairs: Prod_ID, Prod_DT, Sales_DT, Repair_DT
These 2 tables are joined by the common key Prod_ID & also and share the product's production & sales dates. What I would like to do is produce a rate summary similar to description below.
Important to remember that not all products experience repairs, so the basis for Sales_CNT needs to be the Sales Table, even thou Prod_DT also appears in Repairs Table.
It's simple enough for novice like me to produce 2 tables independently and then merge back those resulting tables into the single table output described above. But my question is how do I write a single SQL "SELECT" request that will produce the results into just a single table.
I am trying to write a simple search page that will search all the fields in a database to find all records that match a user input string. The string could happen anywhere in any of the fields. I have a dataset and can write a query but am unsure what the format is for this simple task. I figured it would look like this:
SELECT Table.* FROM Table WHERE * = @USERINPUT But thats not working. Can someone help.? Thanks..
If i want to exchange message between two different instances (or) databases in two different servers, then I have to enable the transport protocol which wil be disabled by default in service broker.I read in some article that some registry settings has to be changed.So can someone suggest on the same.
"Connection attempt failed with error: '10061(No connection could be made because the target machine actively refused it.)'." Im getting this error in the initiating database.The message that i sent still exists in thr Transmission queue fo initiating database.
It will be better if someone sends a sample or the steps to follow to communicate between databases in two different servers.So that i can cross check with mine.
In my database/MY SERVER (SQL7/Win2K), I run a simple query with a Table/10000 rows (without cluster index): SELECT * FROM TABLE it take over 30s. Why it's slow? How can I check for reason? How to configure my server to improve performance? Thanks in advance. TH ---------------------------------- SP_CONFIGURE's RESULT in MY SERVER ----------------------------------
Table 'spt_values'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0. name minimum maximum config_value run_value ----------------------------------- ----------- ----------- ------------ ----------- affinity mask 0 2147483647 0 0 allow updates 0 1 1 1 cost threshold for parallelism 0 32767 5 5 cursor threshold -1 2147483647 -1 -1 default language 0 9999 0 0 default sortorder id 0 255 52 52 extended memory size (MB) 0 2147483647 0 0 fill factor (%) 0 100 0 0 index create memory (KB) 704 1600000 0 0 language in cache 3 100 3 3 language neutral full-text 0 1 0 0 lightweight pooling 0 1 0 0 locks 5000 2147483647 0 0 max async IO 1 255 32 32 max degree of parallelism 0 32 0 0 max server memory (MB) 4 2147483647 2147483647 2147483647 max text repl size (B) 0 2147483647 65536 65536 max worker threads 10 1024 255 255 media retention 0 365 0 0 min memory per query (KB) 512 2147483647 1024 1024 min server memory (MB) 0 2147483647 0 0 nested triggers 0 1 1 1 network packet size (B) 512 65535 4096 4096 open objects 0 2147483647 0 0 priority boost 0 1 1 1 query governor cost limit 0 2147483647 0 0 query wait (s) -1 2147483647 -1 -1 recovery interval (min) 0 32767 0 0 remote access 0 1 1 1 remote login timeout (s) 0 2147483647 5 5 remote proc trans 0 1 0 0 remote query timeout (s) 0 2147483647 0 0 resource timeout (s) 5 2147483647 10 10 scan for startup procs 0 1 0 0 set working set size 0 1 0 0 show advanced options 0 1 1 1 spin counter 1 2147483647 10000 10000 time slice (ms) 50 1000 100 100 two digit year cutoff 1753 9999 2049 2049 Unicode comparison style 0 2147483647 196609 196609 Unicode locale id 0 2147483647 1033 1033 user connections 0 32767 0 0 user options 0 4095 0 0
I am trying to make a single display page for an author's books.the books page only displays books of a type "type" (novels, non-fiction, etc)I would like to make it so that it can also show all books if "type" isn't selected. I THOUGHT the string would look like this: <asp:SqlDataSource ID="SqlDSBooks" runat="server" ConnectionString="<%$ ConnectionStrings:csK2Reader %>" SelectCommand="SELECT * FROM [Books] ( If @Type <> "" then WHERE ([Type] = @Type)) ORDER BY [SortDate] DESC"> But it doesn't seem to want to work. I get a "server tag is not well formed" error.
I have a table called "member" that has two fields (amongst others) called "firstname" and "city"I want to return a list that sorts people who live in the same city, but does not return people who do not have a city in common.For example if this is my data:Karen - ParisMike - RomeTim - Dallas
Jim - ParisChris - DallasJohn - ManhattanSarah - OrlandoDavid - DallasThe query would return this:Dallas - ChrisDallas - DavidDallas - TimParis - JimParis - KarenI have this SQL so far: Select city, firstname From member Order By city, firstnamebut it includes all the cities including those that only have 1 resident.Can someone tell me how to do this?Thanks a lot,Chris
hi,i have three fields in the database tablep1 p2 rank10% 20% 3 21% 40% 2now i am passing the input as 15%...this 15% is related to 3rd rank.. so i need the o/p rank as 3..if the p1 and p2 datatypes are varchar..then how we can we write the query for that one tgo get the rank as 3.Ramesh
Hi All Can anyone tell me what this simple SQL query would be: Find all the words "black", "dvd" and "player" from any of the fields "Product", "Brand" or "Description".
I am trying to select players from a database based on the userid not having any user's name in it with an order by ASC. I basically want all the players that haven't been taken. Here is what I came up with and it gives me empty.
strSQLA="SELECT UserID, playerName from Allplayers WHERE UserID = '' ORDER BY playername ASC"
I hope it's considered appropriate to post a SQL query question here; it's not I'll gladly post this question elsewhere. That having been said, I've run into a simple but quite tricky SQL query and was wondering if the community might be able to help. Here's what's going on: I have a "Documents" table. Let's call it DI have a "Benefits" table. Let's call it B.There is a many-to-many relationship between D and B so I have a third table to store the relationships. Let's call that table BD. When I create a document it creates a row in D. Let's call that D1, D2...DN. When I create a benefit it creates a row in B. Let's call that B1, B2...BN When I associate a document to a paritcular benefit, let's say that if D1 and B2 are associated, that I created row B2-D1 in my BD table. Now here's what my challenge is. Suppose I have these entries in my tables: D: D1, D2, D3B: B1, B2, B3 Table BD:B1-D1B2-D1B2-D2 When the user goes to add documents to benefit B1, I want to show the user only documents that are not yet added to B1. In this case, I want to show the user D2 and D3 but not D1 since that one's already added. How do I write a SQL query to do this? Here's what I've been using, but it shows too many results (e.g. included D1, D2, D3) SELECT D.*, BD.*FROM Documents AS d LEFT JOIN rel_BenefitsDocuments AS bd ON d.doc_id = bd.doc_idWHERE (benefit_id <> @benefit_id OR benefit_id IS NULL) Any thoughts?
I want to write a sql query for an asp page which will display only unique rows from the specified column along with the number of count for each unique row.
Example:
Table that I want to query
Last Name || First Name Gates || Bill Boyce || Mike Gates || Bill Gates || Phil
Results I want:
Last Name || First Name || Count Gates || Bill || 2 Boyce || Mike || 1 Gates || Phil || 1
I am new to SQL and this question may be most easiest to many of you. Here is what I need.
I have two identical tables (exactly the same in structure) having a compound primary key with a combination of 3 columns. Can someone give me the most efficient query that fetches all the rows from table1 that are not in table2.
from sales_contact sc , invoice i , invoice_line_item il , sales_region_special_section srss , sales_region sr , issue_date idd
where sc.sales_contact_id = i.sales_contact and i.invoice_id = il.invoice and srss.sales_region = sr.sales_region_id and il.issue_date = idd.issue_date_id and srss.invoice_line_item = il.invoice_line_item_id
order by sc.sales_contact_id
================
heres some of the result set I want only the records with the greatest id for each unique sales contact...
AF85F32E-8E34-4C40-9468-00148A34E903, 41, N AF85F32E-8E34-4C40-9468-00148A34E903, 42, N 5D26328A-192B-4E4E-9B34-010C4E077CF8, 77, N 5D26328A-192B-4E4E-9B34-010C4E077CF8, 70, N 5D26328A-192B-4E4E-9B34-010C4E077CF8, 34, BC B44C914E-6001-40CE-8AB6-0126BD572D45, 25, NW B44C914E-6001-40CE-8AB6-0126BD572D45, 26, NW B44C914E-6001-40CE-8AB6-0126BD572D45, 24, NW B44C914E-6001-40CE-8AB6-0126BD572D45, 28, HC B44C914E-6001-40CE-8AB6-0126BD572D45 , 28, NW
this would be an ideal result set using the data above...note there are situtations where there are n* for a greatest issue id.
AF85F32E-8E34-4C40-9468-00148A34E903, 42, N 5D26328A-192B-4E4E-9B34-010C4E077CF8, 77, N B44C914E-6001-40CE-8AB6-0126BD572D45, 28, HC B44C914E-6001-40CE-8AB6-0126BD572D45, 28, NW
This should be easy, I'm just having a brainfart at the moment, can't remember how to do this:
Say I have a bunch of records in a table, with an ID field as Key. I want to return the count of times each ID shows up, so for the following example data:
This should be obvious but for some reason I can't see it.
When this is done I'm going to join in another table to get a name based on the ID with the highest count. I believe it's faster to do a TOP 1 here rather than after the join, but I want to verify that while I'm at it also.
I am a complete newbie to SQL. I have a simple query to make in SQL, but I'm not being able to construct the statement properly. I've already tried googling this info, but with little luck. If someone could help me here, I'd be very thankful.
The problem:
I have a table with a list of questions. All questions have a serial number. The query has to return whether a particular question is the last one in the table or not (according to serial no.)
Logically, this is the query -
Code:
select islast = 0 if count(*) from question where serialno > $serial != 0 else select islast = 1;
The '$serial' part will get replaced dynamically before query execution through another program.
Any idea how to code this in valid SQL? I would like to make it a simple statement and not a stored procedure. Thanks.
Hey.. hope someone can help, i desperatly need some assistance. I have a few query issues but first things first.. one is that i have a table of messages and users having an online conversation, so fields are msg id, subject, topic, message, user, reply to and time it was sent. Im trying to do a query that will tell me which user sent the most messages, ive tried using count and stuff like that but not really getting anywhere. It will basically have to count each time a user has spoken and then give me the name of the user that has sent the most messages, ive been playing around with stuff like SELECT Count(*) AS Expr1 FROM Table1 WHERE User='andro8472' OR User='bumies';
That will count the times those users have spoken but cant get further
any help would be greatly appreciated really stuck at the mo..
I'm having a bad day as I just cannot get a query to work and its doing my head in and it should be a simple query.
I'll try and explain what I need.
Have a simple query with one join, this is it so far:
SELECT DISTINCT Replace(StaffName,'''''',' ') AS StaffName,OutReason,OutStartDate ,OutEndDate,OutStartTime,OutEndTime,OutID FROM vwOutOfOffice LEFT OUTER JOIN vwStaffList ON OutUser=StaffID WHERE OutComplete=0
What I'm having trouble with adding to the WHERE is as follows, I need a list of Staff who are out of the office based on the various criteria:
Example Current date=29/02/2008 Current time=14:00
Start date---End Date----Start Time--End Time--Should Appear 27/02/2008--28/02/2008--09:00------17:00-----No (because of date) 28/02/2008--03/03/2008--08:00------17:00-----Yes 28/02/2008--29/02/2008--17:00------17:00-----Yes 29/02/2008--29/02/2008--12:00------14:00-----Yes 29/02/2008--29/02/2008--14:30------17:00-----No (because of time) 02/03/2008--02/03/2008--08:00------10:00-----No (because of date)
There may be others I've missed but as long as the dates combined with the times match correctly I need the records to appear.
Please help before I end up putting my head through the monitor.
ID Number NameJoin DateSex 101 Jason01/02/1996M 117 Robert06/25/1999M 113 Doug08/12/1999M 161 Marian07/30/1998F
Please let me know the SQL queries for the following questions for the above table:
1)Write a Select statement that selects the persons whose Type is "M" and who have joined in the last four years and place the result in an array. Today's date is 12/31/1999.
2) Write a Select statement that selects the persons who do not have unique ID Number and place the results in an array.
3) Write code to sort the array by Name + Join Date. Assume that the array has already been created and is sorted in the same manner as the table above. The Join Date is of type smalldatetime. The Name field should be sorted case insensitively.
How do I retrieve all rows from a middle_name column that have a character length of <= 2. This table is populated with middle names and middle initials. I want to isolate the middle initials only.
SELECT ('Section : ' + F.Section + ' ' + (SELECT S.SectionName FROM SFM_Section AS S WHERE S.Company = F.Company AND S.Department = F.Department AND S.Section = F.Section ) ), F.Variety, F.Category, isnull(SUM(F.AreaCutCurrent), 0) AS AreaCutCurrent, isnull(SUM(F.TonnageCurrent), 0) AS TonnageCurrent, isnull(SUM(F.YieldCurrent), 0) AS YieldCurrent FROM SFM_Field AS F WHERE F.CropYear = 2007 and ActiveFlag = 1 and Section = 04 GROUP BY F.Company, F.Department, F.Section, F.Variety, F.Category ORDER BY F.Section, F.Variety, F.Category
that returns me the following results:
Section:04 Region Haute|M0052/78|R07|1.84 |137.64 |74.804 Section:04 Region Haute|M0695/69|R01|37.71|2817.65|434.009 Section:04 Region Haute|M0695/69|R02|35.08|2282.33|523.234 Section:04 Region Haute|M0695/69|R05|21.2 |1563.01|147.579 Section:04 Region Haute|M0695/69|R09|6.52 |484.39 |74.293 Section:04 Region Haute|M0695/69|R11|0 |0 |0 Section:04 Region Haute|M1397/86|R04|8.35 |793.61 |190.141 Section:04 Region Haute|M1400/86|GS |18.03|2093.91|116.135
What should I do if I don't want the records that the sum is equal to 0 to appear in my results? (Like the 3rd row from the end)
You probably seen this Q before but I have no idea what to search and and therefor any direction would be appreciated.
I have three columns table, two are int and one is datetime (for sorting). I would like to query all values witch are unique within the two integers, however, in some cases data looks like this: int1 = 1 int2 = 2 and another record like: int1 = 2 int2 = 1
this pair should be considered as a duplicate and second record should not come with the results set.
Hi AllI know that I am supposed to submit a schema of my table, but it is a verybasic one so I'll crack straight on with my query.In essence I have a 3 column table called STOCKTRANS, which consists ofSTOCKCODE, TRANSACTIONDATE and TRANSACTIONTYPE.Each time a user orders a stock item I insert the stock code and the datethey ordered it into this table, egSTOCKCODE TRANSACTIONDATE TRANSACTIONTYPEIVP 1-4-04 1STP 31-6-04 8KGC 8-7-04 6IVP 11-8-04 8etc etcetcWhat I want to create is a simple query that informs what the last orderdate was for a stock code. As you can see from the above example, I mayhave hundreds of the same stock code with different dates so I thought thatthe following query would work:select STOCKCODE, MAX(TRANSACTIONDATE) as 'LastOrderDate'from STOCKTRANSwhere STOCKCODE = 'IVP' and TRANSACTIONTYPE in (1,8)But it doesn't.NOTE: the reason for the transaction types of 1 and 8 is because these arethe types I want. The other types are used for credits, returns, etc.First of all the above, brings me back loads of rows when all I want is:IVP 11-8-04 << this being the last date of order for this stock item.Just as a test I removed the STOCKCODE out of the select and found that Idid get my one liner, but for some reason I was only getting the last datefor transaction types 1 only - not the last date for a combination of 1 and8 transactions.Can you help??Many thanksRgds Robbie