Complex DB Search Forms (Store Proc Vs. Complex Where)
Nov 12, 2003
I have web forms with about 10-15 optional search parameters (fields) for a give table. Each item (textbox) in the form is treated as an AND condition.
Right now I build complex WHERE clauses based on wheather data is present in a textbox and AND each one in the clause. Also, if a particular field is "match any word", i get a ANDed set of OR's. As you can imagine, the WHERE clause gets quite large.
I build clauses like this (i.e., 4 fields shown):
SELECT * from tableName WHERE (aaa like '%data') AND (bbb = 'data') AND (ccc like 'data%') AND ( (xxx like '%data') OR (yyy like '%data%') )
My question is, are stored procedures better for building such dynamic SQL clauses? I may have one field or all fifteen. I've written generic code for building the clauses, but I don't know much about stored procedures and am wondering if I'm making this more difficult on myself.
I could really use some suggestions on the best way to get the results of a pretty complex aspx form. There are a whole bunch of search criteria. Most of the criteria are ranges of numbers such as a starting number and ending number but other criteria need to search text fields for keywords. Most fields are optional which means that many values will not be submitted. Not that there is a right and wrong way to do it, but what typically works out better for these things, an aspx procedure that puts together a highly complicated select statement or writing a highly complicated stored procedure? Either way I see a ton of if statements, or am I making this a lot harder then it needs it needs to be?
Complex to me, anyways. I posted this quite a bit ago, although the question was different, and you guys pointed out what was wrong, which saved me lots of headaches and coffee, so hopefully you guys can point out what is wrong this time.
This PROC is used in a search engine type script I wrote that searches through a database of magazine articles. You can search with just a name or description, or a range of dates. You can also search for all articles posted after a date, or before a date. On the perl side, I pass it 4 parameters, $querry_string, the description or name, $datefrom and $dateto, which specifies a range of dates, and $slice, which is which slice of results to return, like 1-10.
The script *mostly* works, it works correctly with two dates or no dates are specified, however it will not work with only one date filled in. ( See querry2 in the proc ) Any ideas what is wrong? When I run the line in Querry Analyzer it works, but in the script, it finds 0 results no matter what.
CREATE PROC [dbo].[search_querry_results]( @datefrom datetime = NULL, @datetodatetime = NULL, @querry_string varchar(60), @slice int ) AS
SET @querry = 'SELECT TOP ' + CAST(@slice AS varchar(100)) + ' * FROM FREETEXTTABLE( Exponent, *, ''' + @querry_string + ''' ) as ct JOIN Exponent AS e ON ct.[KEY] = e.[Key] WHERE date > ''' + CAST( @datefrom AS varchar(30)) + ''' AND date < ''' + CAST(@dateto AS varchar(30)) + ''' ORDER BY Rank DESC'
SET @querry2 = 'SELECT TOP ' + CAST(@slice AS varchar(100)) + ' * FROM FREETEXTTABLE( Exponent, *, ''' + @querry_string + ''' ) as ct JOIN Exponent AS e ON ct.[KEY] = e.[Key] WHERE date < ''' + CAST(@dateto AS varchar(30)) + ''' ORDER BY Rank DESC'
SET @querry3 = 'SELECT TOP ' + CAST(@slice AS varchar(100)) + ' * FROM FREETEXTTABLE( Exponent, *, ''' + @querry_string + ''' ) as ct JOIN Exponent AS e ON ct.[KEY] = e.[Key] ORDER BY Rank DESC'
BEGIN IF ( @datefrom IS NOT NULL ) AND ( @dateto IS NOT NULL ) EXEC sp_executesql @querry
ELSE
IF ( @dateto IS NOT NULL ) AND ( @datefrom IS NULL) EXEC sp_executesql @querry2
ELSE
IF ( @datefrom IS NULL ) AND (@dateto IS NULL ) EXEC sp_executesql @querry3 END GO
There is the code. Let me know if you can come up with something, im all out of ideas. Thanks
How do you write a store procedure to meet the following conditions:
PROXIMITY SERACH PROCEDURE
COMPANY table
PK company_id int company_name varchar(100) premise varchar(10) street varchar(30) town varchar(100) postcode char(7) x_loc int y_loc int
COMPANY_KEYWORD table
PK, FK1 company_id int PK keyword varchar(100)
The data above shows an extract of a company listings database. The company table holds the name and address of the company as well as a physical location in terms of an Easting and Northing co-ordinate. Each company has one or more keywords or phrases associated with it in the company_keyword table. The full database holds 5 million companies with an average of 4 keywords each,
A stored procedure is required to provide proximity searching of the database. The procedure should return a list of companies that satisfy the keyword search expression and fall within the defined maximum distance of the specified location. Results should be limited to show the closest companies up to a maximum of @max_records.
Example Search Expressions @search_expression - Description TESCO AND CASHPOINT -Return all companies that have all of the keywords specified. TESCO OR SAINSBURY’’S - Return all records that have one or more of the keywords specified. TESCO EXPRESS AND CASHPOINT OR SAINSBURY’S LOCAL - Return all companies that have either both of the first two keywords or the third.
Distance can be calculated using the following formula:
Distance = sqrt( square(x1-x2) + square(y1-y2) )
THE QUESTION 1. Create a stored procedure to satisfy the requirement shown above 2.Comment on the database design and identify any indexes that may be appropriate.
How do you write a store procedure to meet the following conditions:
PROXIMITY SERACH PROCEDURE
COMPANY table
PK company_id int company_name varchar(100) premise varchar(10) street varchar(30) town varchar(100) postcode char(7) x_loc int y_loc int
COMPANY_KEYWORD table
PK, FK1 company_id int PK keyword varchar(100)
The data above shows an extract of a company listings database. The company table holds the name and address of the company as well as a physical location in terms of an Easting and Northing co-ordinate. Each company has one or more keywords or phrases associated with it in the company_keyword table. The full database holds 5 million companies with an average of 4 keywords each,
A stored procedure is required to provide proximity searching of the database. The procedure should return a list of companies that satisfy the keyword search expression and fall within the defined maximum distance of the specified location. Results should be limited to show the closest companies up to a maximum of @max_records.
Example Search Expressions @search_expression - Description TESCO AND CASHPOINT -Return all companies that have all of the keywords specified. TESCO OR SAINSBURY’’S - Return all records that have one or more of the keywords specified. TESCO EXPRESS AND CASHPOINT OR SAINSBURY’S LOCAL - Return all companies that have either both of the first two keywords or the third.
Distance can be calculated using the following formula:
Distance = sqrt( square(x1-x2) + square(y1-y2) )
THE QUESTION 1. Create a stored procedure to satisfy the requirement shown above 2.Comment on the database design and identify any indexes that may be appropriate.
I need a query to join 3 tables. Here is my setup: TABLEFields GroupsID, Name KeywordsID, GroupID, Keyword SearchID, Keyword
"Groups" and "Keywords" are linked one to many, with each group being assigned multiple keywords. "Search" holds a list of keywords that I want to search for. In particular, I need to find the groups that have ALL of the keywords in "Search" assigned to them (not just at least one). I've tried many different approaches, but cannot find a way to do this with a single query. All my attempts so far also return groups that only have a few of the keywords in "Search" assigned to them, but not ALL. Can anyone help? I am desperate to find a solution... Thanks!
I am currently faced with developing a search function (ASP.NET 1.1 based user interface) to a database (SQL Server 2000 ), and I am wondering how best to handle the logistics of it.
The search can take anywhere from one to 9 parameters. The search would be executed against three related tables.
The main problem, as I see it at this stage, is how to handle parameters that have not been selected, it's easy enough to do a search with all 9 parameters, but as soon as there is an unpredictable variable number of search criteria, I just don't know how best to handle this in a stored procedure for example.
We have some reports run quite slow because the queries are complicate and tables are large. So we create a materialized view in Oracle which store the result from the query and refresh it occationally. How to do this in MSSQL? I try the indexed view but seems it has lots of restrictions, our query has sub queries and cross database table joins so can't use the indexed view. Any other object or temp table can be used to cache the report data and be accessed globally by other procedures?
I need some help on how to structure a sql statement. I am creating a membership directory and I need the stored procedure to output the Last Name, First Name (and if married) Spouse First Name. Like this Flinstone, Fred & Wilma All members are in one directory linked by two fields. [Family ID] all the family members have the same family id and then there is a Family position id that shows if they are the Husband, Wife or Kids. I have no problem with this part select (LastName + ',' + FirstName) as Name, [Phone 1] as Phone, [Unit Name] as WD, [Street 1] as Street, SUBSTRING(City,1,3) as City, SUBSTRING(Postal,1,5) as Zipfrom Membership Where [HH Order]=1 Order By LastName ASC Could someone help me on how to display the " & Spouse FirstName " as part of the name field only if there is a spouse [HH Order]=2 for the current [Family ID]????
I need to get multiple values for each row in a database, then do a calculation and insert the calculation and the accountnumber related to the calculation the data, into a different column. I get an error trying it this way...there is no real identifier, it is jsut something that needs to get done per row...any ideas on how I can accomplish this? Declare @NetCommission decimal Declare @AccountNumber varchar(50) Set @NetCommission = (select (CommissionRebate * Quantity) from Account A Join Trades T on A.AccountNumber = T.AccountNumber) Set @AccountNumber = (select A.AccountNumber from cmsAccount A Join Trades T on A.AccountNumber = T.AccountNumber)
Hello, could someone help with this query in a stored proc.? SET @SQL = 'SET ''' + @avgwgt + ''' = ' '(SELECT AVG(AverageWeight) FROM CageFishHistory where CageID IN (' + @cagearray + ') and ItemDate =''' + CONVERT(varchar(23),@startdate) + ''')' EXEC @SQL I'm trying to get an average value across dynamically selected rows. (I'm using a list array to deliver the selection to the stored proc). I need to re-use the average value within the procedure,so it's not enough to output it as a column of the resultset - EG. 'Select AVG(AverageWeight) as AvgWgt' . If I take out the @avgwgt line it works fine, but otherwise I'm getting this error: "Incorrect syntax near '(SELECT AVG(AverageWeight) FROM CageFishHistory where CageID IN ('." It may be that I can access a column of the resultset in the rest of the procedure, and that would help avoid the use of pesky apostrophes, but I don't know how to do it.
I'm sure there is a way of cracking this, but I can't think of a good solution. Right now I am not happy with the solutions I come up with, one of which takes 4 minutes to run on SQL Server The scenario: User is presented with search page where one or more search terms can be entered/selected. There are no required parameters. It can be any or all of the possibilities presented. Below is a model of the search parameters presented. The user will either select to show more options under Profile ABC, or go down to Profile STU or Profile XYZ to show more options, or even select all Profiles and then select from Type 1 and either a. or. b. or. c. or ALL of the above. I cannot predict what a user will make part of the search query so I have to have a stored procedure ready which can handle any or all of the parameters a user may select. Am I biting off more than I can chew (it seems so)? Or is there an elegant way of handling the unknown combination of search parameters that a user might throw into my sql query? I'm running this under ASP 1.0 and SQL Server 2000.
[check to show the options below] Profile ABC [check to shore more options] Type 1
A. Contains fields for entering another data string and selecting from drop-down boxes B. ditto C. ditto D. ditto [check to show more options] Type 2
A. Contains fields for entering another data string and selecting from drop-down boxes B. ditto C. ditto D. ditto [check to show more options] Type 3
A. Contains fields for entering another data string and selecting from drop-down boxes
B. ditto C. ditto D. ditto [check to select more options] Type 4
A. Contains fields for entering another data string and selecting from drop-down boxes B. ditto C. ditto D. ditto [check to show more options] Profile XYZ (as above) [check to select more options] Profile STU (as above)
I'm working on a system that used to load control dynamically into a table structure based on "Row" and "Column" properties in the item object. The system is now being revamped, and instead of a table structure, it's being loaded into a list, which will be controled by css. The new relevant variables are "Sequence" and "Width." Since there are already thousands of existing items in the database, I have to write a script that can take a really good guess at legacy items' Row and Col, and input values for Sequence and Width. Since all items exist on "tabs," I can query for all items on a given tabID, Ordered By Row, Col -- that will give me a sequence. Width isn't literal, it has 6 presets: Whole, Half, Third, Quarter, Two Thirds, Three Quarters, represented in the table as 0,1,2,3,4,5 -- for our purposes, I'll assume that all items on a row are equal in width. We can determine width by figuring out the number of items within the same row, so if there is only one in the row, it's a Whole (0), if there are three in the row it'll be a Third (2), etc.
I'd like to create a query that gets all items by tab, assigns the appropriate sequence, and figures out how many items are in the row with a given item, to assign the correct width.... but I have no idea how to make t-sql do that. I don't mind multiple queries to get the whole process done, and it doesn't need to be efficient -- this is a one-off script to run to give legacy items values that we can work with. Where would I start?
HI. I have 3 tables 1- std with : stdID , programID. 2- Programs with :ProgramID , Cost 3 - Movements with : stdID , balance. the first table contain the stdID and ProgramID , some times the std hasn't programID that mean he hasn't programID. then we return null. if the std has programID there is to cases. the first one he have a movement on his balance then we get the biggest balance for the std. the second case he hasn't any moventen then we get his balance from Programs table by the ProgramID .
I need sql server function that return table like this stdID , Balance that means every std with his Balance. Regards.
This is too complex anyone know how to make it less complex. I am trying to get all the selected fields from contacts into a datagrid where the other fields contain a string in textbox1. This works SELECT [company], [contactname], [emailaddress], [secondemailaddress], [phonenumber], [webpage] FROM [contacts] WHERE (([AB] LIKE '%' + ? + '%') AND ([AL] LIKE '%' + ? + '%')) When i add all the rest of the fields it says its too complex. Please Help
SELECT [company], [contactname], [emailaddress], [secondemailaddress], [phonenumber], [webpage] FROM [contacts] WHERE (([AB] LIKE '%' + ? + '%') AND ([AL] LIKE '%' + ? + '%') AND ([B] LIKE '%' + ? + '%') AND ([BB] LIKE '%' + ? + '%') AND ([BD] LIKE '%' + ? + '%') AND ([BA] LIKE '%' + ? + '%') AND ([BH] LIKE '%' + ? + '%') AND ([BL] LIKE '%' + ? + '%') AND ([BN] LIKE '%' + ? + '%') AND ([BR] LIKE '%' + ? + '%') AND ([BS] LIKE '%' + ? + '%') AND ([BT] LIKE '%' + ? + '%') AND ([CA] LIKE '%' + ? + '%') AND ([CB] LIKE '%' + ? + '%') AND ([CF] LIKE '%' + ? + '%') AND ([CH] LIKE '%' + ? + '%') AND ([CM] LIKE '%' + ? + '%') AND ( LIKE '%' + ? + '%') AND ([CR] LIKE '%' + ? + '%') AND ([CT] LIKE '%' + ? + '%') AND ([CV] LIKE '%' + ? + '%') AND ([CW] LIKE '%' + ? + '%') AND ([DA] LIKE '%' + ? + '%') AND ([DD] LIKE '%' + ? + '%') AND ([DE] LIKE '%' + ? + '%') AND ([DG] LIKE '%' + ? + '%') AND ([DH] LIKE '%' + ? + '%') AND ([DL] LIKE '%' + ? + '%') AND ([DN] LIKE '%' + ? + '%') AND ([DT] LIKE '%' + ? + '%') AND ([DY] LIKE '%' + ? + '%') AND ([E] LIKE '%' + ? + '%') AND ([EC] LIKE '%' + ? + '%') AND ([EH] LIKE '%' + ? + '%') AND ([EN] LIKE '%' + ? + '%') AND ([EX] LIKE '%' + ? + '%') AND ([FK] LIKE '%' + ? + '%') AND ([FY] LIKE '%' + ? + '%') AND ([G] LIKE '%' + ? + '%') AND ([GL] LIKE '%' + ? + '%') AND ([GU] LIKE '%' + ? + '%') AND ([GY] LIKE '%' + ? + '%') AND ([HA] LIKE '%' + ? + '%') AND ([HD] LIKE '%' + ? + '%') AND ([HG] LIKE '%' + ? + '%') AND ([HP] LIKE '%' + ? + '%') AND ( LIKE '%' + ? + '%') AND ([HU] LIKE '%' + ? + '%') AND ([HX] LIKE '%' + ? + '%') AND ([IM] LIKE '%' + ? + '%') AND ([IP] LIKE '%' + ? + '%') AND ([IV] LIKE '%' + ? + '%') AND ([JE] LIKE '%' + ? + '%') AND ([KA] LIKE '%' + ? + '%') AND ([KT] LIKE '%' + ? + '%') AND ([KW] LIKE '%' + ? + '%') AND ([KY] LIKE '%' + ? + '%') AND ([L] LIKE '%' + ? + '%') AND ([LA] LIKE '%' + ? + '%') AND ([LD] LIKE '%' + ? + '%') AND ([LE] LIKE '%' + ? + '%') AND ([LL] LIKE '%' + ? + '%') AND ([LN] LIKE '%' + ? + '%') AND ([LS] LIKE '%' + ? + '%') AND ([LU] LIKE '%' + ? + '%') AND ([M] LIKE '%' + ? + '%') AND ([ME] LIKE '%' + ? + '%') AND ([MK] LIKE '%' + ? + '%') AND ([ML] LIKE '%' + ? + '%') AND ([N] LIKE '%' + ? + '%') AND ([NE] LIKE '%' + ? + '%') AND ([NG] LIKE '%' + ? + '%') AND ([NN] LIKE '%' + ? + '%') AND ([NP] LIKE '%' + ? + '%') AND ([NR] LIKE '%' + ? + '%') AND ([NW] LIKE '%' + ? + '%') AND ([OL] LIKE '%' + ? + '%') AND ([OX] LIKE '%' + ? + '%') AND ([PA] LIKE '%' + ? + '%') AND ([PE] LIKE '%' + ? + '%') AND ([PH] LIKE '%' + ? + '%') AND ([PL] LIKE '%' + ? + '%') AND ([PO] LIKE '%' + ? + '%') AND ([PR] LIKE '%' + ? + '%') AND ([RG] LIKE '%' + ? + '%') AND ([RH] LIKE '%' + ? + '%') AND ([RM] LIKE '%' + ? + '%') AND ([S] LIKE '%' + ? + '%') AND ([SA] LIKE '%' + ? + '%') AND ([SE] LIKE '%' + ? + '%') AND ([SG] LIKE '%' + ? + '%') AND ([SK] LIKE '%' + ? + '%') AND ([SL] LIKE '%' + ? + '%') AND ([SM] LIKE '%' + ? + '%') AND ([SN] LIKE '%' + ? + '%') AND ([SO] LIKE '%' + ? + '%') AND ([SP] LIKE '%' + ? + '%') AND ([SR] LIKE '%' + ? + '%') AND ([SS] LIKE '%' + ? + '%') AND ([ST] LIKE '%' + ? + '%') AND ([SW] LIKE '%' + ? + '%') AND ([SY] LIKE '%' + ? + '%') AND ([TA] LIKE '%' + ? + '%') AND ([TF] LIKE '%' + ? + '%') AND ([TN] LIKE '%' + ? + '%') AND ([TQ] LIKE '%' + ? + '%') AND ([TR] LIKE '%' + ? + '%') AND ([TS] LIKE '%' + ? + '%') AND ([TW] LIKE '%' + ? + '%') AND ([UB] LIKE '%' + ? + '%') AND ([W] LIKE '%' + ? + '%') AND ([WA] LIKE '%' + ? + '%') AND ([WC] LIKE '%' + ? + '%') AND ([WD] LIKE '%' + ? + '%') AND ([WN] LIKE '%' + ? + '%') AND ([WR] LIKE '%' + ? + '%') AND ([WS] LIKE '%' + ? + '%') AND ([WV] LIKE '%' + ? + '%') AND ([YO] LIKE '%' + ? + '%'))
hi how are you please help me in my problem which i can't make it. Now, i have a project in ASP.NET and SQL Server 2005. let's call the project an image gallery, in my project i have a table named "Category" in which all the categories are in this table. also while adding a new category a new table will be created automatically with the name of that category. now, what i want is that to build a query that reads the contents of the tables that the tables name are the names of the each record in the "Category" table. is that possible ? please if any one help can me in my problem.
I have 2 tables, say table1, and table2. There is a DocID (primary key) in table1. In table2, DocID is the foriegn key. There can be more than 1 DocID.
how do I join these two tables, such that I get all the otherID's for each DocID. ie., DocID OtherID 1 2 and 10 and 13 and 25 2 3
i am writing this query to display search results on a search page (with keyword search) and so, if I display the result in more than one row, then the user might think that there is more than document...whereas the case is that there is only one document with more than one other ID's.
is there any way I can do this? display...more than 1otherID in the same row for the same DociD? Currently, I am using a left outer join of table1 and table2.
I cant get "order by" to work in this sql query..I use this query: "SELECT DISTINCT TOP 12 name,total = COUNT(*) FROM products where kat = 'music' group by namn"and I want to add this some where to get 12 random records: "ORDER BY NewID()"I tried this: "SELECT DISTINCT TOP 12 name,total = COUNT(*) FROM products where kat = 'music' group by namn ORDER BY NewID()"" but get the error:"ORDER BY items must appear in the select list if SELECT DISTINCT is specified"I can´t figure out how I should write the query..Somebody have any ideas??/Radiwoi
table_a has patient_id, tran_id and other fields a,b,c table_b has patient_id,tran_id, key_id and other fileds d,e,f table_a patien_id + tran_id is unique table_b patient_id + tran_id is not unique, could be duplicated.
I have to create a query which will retrieve fields from table a a,b,c and fields d,e from table b where table a. patient_id + tran_id = tableb.patient_id + tran_id and table_b.key_id is the min key_id for that patient_id + tran_id.
I have a sp which requires a somewhat(at least for me) where clause. It needs a standard clause but then needs to differentiate the where based on whether a certain field is null or not. I didn't think an if would work but neither is my case. Below is the where clause. Thanks
where(OTHER_ORDER_DATES_.DF_RESTRICTION1 in ('Dental hold', 'Medical hold', 'Mental health hold') AND case when OTHER_ORDER_DATES_.DF_RES1TO is not null then OTHER_ORDER_DATES_.DF_RES1FROM <=@sdate AND OTHER_ORDER_DATES_.DF_RES1TO >= @edate when OTHER_ORDER_DATES_.DF_RES1TO is null OTHER_ORDER_DATES_.DF_RES1FROM <=@edate
ID GroupID User 1 101 Tom 2 101 Mark 3 101 Clark 4 102 Tom 5 102 Mark 6 103 Tom 7 103 Clark 8 104 Tom 9 104 Clark 10 105 Tom 11 105 Bred
the users of Group 101 are Tom,Mark,Clark the users of Group 102 are Tom,Mark the users of Group 103 are Tom,Clark the users of Group 104 are Tom,Clark the users of Group 105 are Tom,Bred
I want to show Tom that
Both You and Clark are together in 3 groups Both You and Mark are together in 2 groups Both You and Bred are together in 1 group
I might have to redesign the tables for this, but I'll ask anyway. I have a table with the following fields:
Email - VarChar Seminar - Int PeckingOrder - Int.
As I add addresses to the table, each one has a Seminar, and then each Seminar has a Pecking Order Value. If an email address shows up for more than one seminar, it can have multiple records. IE:
for address, seminar, and pecking order would be sample entries into the table. Give or Take 1000 records in the table at any given time. What I want to pull out is:
Distinct Email Addresses For each Email Address - The Max(PeckingOrder) And the Seminar that's associated with Max(PeckingOrder)
For the sample data set above, I'd want to see these two records returned by the query:
email1 - 1523 - 424 email2 - 1524 - 235
I can't seem to get the Having / Where clause right to pull those two records properly. Anyone have any suggestions?
Thanks, --Daniel
Edited - Didn't realize the BB removed email addresses.
i want to write a query that takes any one specific person's schedule for an entire specific day, adds an arbitrary number of days to datStartTime, and finally inserts the PKSlotINDEX corresponding to the calcultated StartTime and fkPersonINDEX.
wow complicated isnt it...
the goal is to take the schedule of one day for one person and copy it to another day; i can scrap my current layout if necessary.
SELECT goto_last_name as 'Manager', advisor_name, advisor_ao_number as 'AO', COUNT (CASE advisor_termination_date WHEN '<Null>' THEN 1 END) as '#Advisors', SUM(Weekly_TOS_GDC) as 'TOS GDC' FROM weekly_condensed_tb WHERE advisor_platform_number = 1 AND report_date BETWEEN CAST(CONVERT(VARCHAR,'9/11/2007',112) AS DATETIME) -28 AND CAST(CONVERT(VARCHAR,'9/11/2007',112) AS DATETIME) GROUP BY goto_last_name,advisor_ao_number,advisor_name HAVING COUNT (CASE advisor_termination_date WHEN '<Null>' THEN 1 END) > 1
In the query above i select a range from the date - 28 days and do a sum on the TOS GDC column. What i am trying to do is have another query where the range will be - 56 and maybe - 86 so i get the SUMS for that as well. I need to display this in SQL Reporting Services and i can only have one DataSet returned otherwise i will not be able to bind it to one table.
How can i go about this so i return one set of data for 3 different date ranges.
Hey all, I have 3 tables: 1 table with Module information (1 quiz-like part of an Exam), 1 table with the relationships between Modules and Exams (ratios) and 1 table with User information, that contains the Exam number, Module number, and score. I am trying to get a full list of modules' names for 1 exam, and the user's score for those modules they taken. All this would pertain to 1 single Exam. I am only getting records returned for those modules that the user has taken; I want the full list with NULL values for the scores of modules the user has not taken. Hope that made sense, here is my attempt:
Code:
SELECT ExamModules.Name, ExamUsers.Score FROM ExamToModule LEFT OUTER JOIN ExamModules ON ExamToModule.ModuleID = ExamModules.ID LEFT OUTER JOIN ExamUsers ON ExamToModule.ModuleID = ExamUsers.MID AND ExamToModule.ExamID = ExamUsers.EID WHERE (ExamToModule.ExamID = 1) AND (ExamUsers.UserName = 'dizzle')
In this case the user is Dizzle and the Exam's ID (primary key) is 1. I've changed LEFT to RIGHT and FULL, they all return the same few records. Help?
I'm struggling with a syntax error in my sp. I'm hoping someone can give it a fresh look and straighten me out. Thanks. Background: 1. The innermost query (using the dynamic statements) will run fine on its own. 2. I can wrap that in another query (with the innermost as a DERIVEDTBL) and it will run fine UNLESS I apply the GROUP BY. Then I get syntax errors. 3. Also, the entire query runs fine as a view (with hard coded vals for column and value). 4. I don't know what kind of formatting this message will apply, so just know that I've checked my line truncations and they all include a trailing '+. Also, I know that Occured is misspelled. That is the correct object name. Not all our DB admins are lit majors.
I have a sql table with corpcode, EmpName, rate, reportdate and Amount fields. I need to write a query that can return corpcode, EmpName, rate and sum of Amount for January, Sum of Amount for Feburary, Sum of Amount for March, Sum of Amount for April, ..........., Sum of Amount for December, Total for All months, Average for all months.
I tried few option, it didn't work for me, Is it possible to do? Have some tried like this earlier?