Query Table Based on Variable Number of Selection Criteria - sql-server

I need to query a table in SQL Server based on a number of criteria, any of which could be null. Criteria are:
SaleId
City
ZipCode
County
JudgementAmountMin
JudgementAmountMax
AssessedValueMin
AssessedValueMax
Saledatestart
Saledateend
All of these can be used to filter a selection or just come of them or none of them. I came up with a solution, which broke, and after posting a question asking for help, the responses I received all told me my solution was crap. OK, great, my solution was crap, even though I used a book on stored procedures to guide me and copied most of my solution from the example printed in the text. So how do I build a solution that isn't crap?
Keep in mind that I am should be considered a fairly new SQL Server practitioner and that I would need more than a two line terse response referring to some general process that can be narrowed down to provide a solution by those who already experienced. I am willing to learn and work at finding a solution, but ask that the response be more newbie friendly than being told that my solution is not any good and questioning why anyone would do it that way. Maybe because they followed an example in a book and didn't know any better. Examples are very helpful!
Remember everyone has to start somewhere. I am deaf, so going to classes is not an option that has worked for me. I really am trying, I just need some help.
Regards
Tom

Related

Is it possible to create a table on react-table using two or more datasets/constants?

Good day everyone, this is my first time posting here but I'd like some help with a recent issue.
So, I'm working on a small React app just for fun and to keep practising, in it I made a few constants for different datasets (each with varying data fields), that is, I have various kinds of records categorized in said constants, since some records use 2+ rows and some need only one row.
Initially I was going to share the app's code, but the datasets are a tad... large, so reading the tips I thought I'd instead create an online sandbox to illustrate with a much simpler and smaller scenario of what I managed to do: [link to the sandbox].
However, looking around and trying different stuff I found out about react-table, which is what I needed due to its useful features and how lightweight it is. I mainly need it for filtering records but I want to try some other features as well.
All this brings me to my problem: I want to populate a table in react-table with the multiple datasets together and their own ways their data is placed on the JSX code, however, I can't figure out what to do and my app's code is getting messy in the process, so I thought I'd ask here to see what I can do, using the code in the sandbox as base, then I can edit my app accordingly if there's a solution for this, otherwise I guess I can maybe make one table for each dataset or just use good ol' HTML+JS+CSS? But neither are the results I'm aiming for.
I'm in no rush for answers since this is just a project for fun and to practise, however, any help is appreciated, thanks in advance.

SQLite database questions, problems with design (indexing/multiple fields)

I use stackoverflow a lot, but this is my first question here, so if i'm doing anything wrong just let me know. I'm not a programmer (I just do programming for my own needs) so I'm open to tutorial suggestions etc. I won't be offended if you just give me something to read and find the answers myself.
OK, to the point - I'm trying to write simple application to track my personal expenses and I have a problem with database design. I'm using VStudio to create the database (SQLite). I attached a diagram with my design and I have some questions.
My SQLite diagram
I don't know exactly how to design "Transactions" table. Fields like Date, Payment Type etc. seems to be easy enough but the idea was to store in this table information about transactions so I need to store multiple products there. I've read about it and created table "Transactions_Products" that will help with that. My problem is : where do I put quantity of products in the transaction? I can't think of a place to put it. I tried to find similar databases but couldn't find anything.
Second thing. I've read about indexing a lot, but I still can't grasp the idea. I don't know when to use it. Should I use it only on fields that I will be "querying" a lot?
Last one - is it better for such a small application just for myself to store my account balance in a separate table or should I just calculate it every time?
As I said, I don't need answers like: "do this, do that". If you just give me some good tutorials/articles I think I can find answers on my own, but I couldn't find it. Maybe I'm searching for it wrong.
Thank you in advance for any information.
where do I put quantity of products in the transaction?
Transactions is a bad table name as it's vague and has multiple meanings. Consider "payments", "purchase invoices", etc. See https://dba.stackexchange.com/questions/12991/ready-to-use-database-models-example/23831#23831 for some existing patterns.
Should I use [indexes] only on fields that I will be "querying" a lot?
There's no free lunch. Indexes take space, and can slow down inserts. Start with indexes on your primary keys (which is the default for SQLite), measure what is slow (looking at query plans) and add indexes if they help and if you have room.
is it better for such a small application just for myself to store my account balance in a separate table or should I just calculate it every time?
For an operational/transactional database like you describe, avoid storing calculated values. SQLite can count numbers quickly :)
Premature optimization is premature. Make it work first with full normalization. If you have performance problems, analyze what is really causing the slow-down and go from there.

sql, product inventory module

I have been tasked with creating a product inventory module. After reading all the posts I can find on Stack Overflow, I have decided the best way is to not keep a separate, running ‘balance’, but to create one on the fly. I have attached a representation of the tables involved.
Actually, it seems like I don't have enough reputation points to include a picture, so here is a link to a dropbox file:
So I have two questions, which are somewhat related, so it seem like I should include them in the same question posting, though I am not a frequent poster and a sql noob. So please excuse me if I am displaying my ignorance with posting or sql.
First, does this look correct (I named all the columns as non-opaque as possible)? I have to create reports that show the current inventory balance for all the products and for products individually as well as a ‘Transaction Register’ with running balance.
Second, provided the first answer is yes, is this a good candidate for creating a view?
Complex question. Difficult to answer without understanding the full scope of the project. One point - I see there is no Current On-hand table. I agree that the running balance at any point in time is best to use a calculated table. It is however common practice to keep a current on-hand table. This gives you the on hand inventory and values with-out having to sum up the transaction. This is the approach in Microsoft Axapta, and other products I have worked with.

Logic for recommender application

I am developing an application - which would have users answer maybe 10 questions - which would have 3-4 options for each question. At the end of the 10th question, based on the responses, it would need to suggest a certain solution. Since there are 100's of permutation and combinations - what's the logic that would be required to use and the database design,
thanks
EDIT some more detailed explanation
if my application is used to recommend a data plan from various mobile operators - based on the user answering questions like the time spent on the internet, the type of files being downloaded and so on. So, if the response to question 1 was a and question 2 was c, etc - then it would be a certain plan. If the response to question 1 was b and for question 2 it was c, then it would recommend a certain plan. So, if there were 10 questions - then the combinations can be quite large. So is there a certain algorithm that can handle this?
I. what would be the logic?
If I understand correctly, you would define "rules" such as
If the answer to question 5. is either A or B then the suggested plan would be planB, otherwise execute the rest of the rules.
So you would use a rule engine e.g.: http://www.jboss.org/drools/
II. what would be the database design?
This is quite simple:
USERS table,
QUESTIONS table and
ANSWERS table which would refer to the two others
Possibly there would be a QUESTIONNAIRE table as well, and the QUESTIONS table would refer to it.
Just a 'quick' comment, consider letting the user see changes in what company they could be recommended as they answer every question.
For example, if I am most interested in price that would be the question I would answer first and immediately see the 3 cheapest plans/products recommended to me.
The second question could be coverage and if I then could see the 3 plans with best coverage (in my area) that would be interesting too.
When I answer the third question about smart phone features and I say I want internet, then the first question should spit out the 3 cheapest plans/products that include internet, obviously they could change.
And so on...
Maybe it also could be a good idea to let the user "dive into" each question and see the full range of options for that answer. As a user I would appreciate that.
Above comments is just how I would appreciate if a form was made for me, I don't want to answer 10 questions about stuff I'm not really putting any value on, each user is different and will prefer to make their choice on their questions.
So, based on above it would be like a check list where the top answers would be the plans/products with the most fitting check marks. And to give immediate responses (as the user answer/alter each question), here AJAX would probably be your choice.

Election 2008 data files - how to know which candidate received the financial contribution?

I was trying to programmatically go through presidential campaign contributions to see which web 2.0 people contributed to which candidates. You can get the data file indiv08.zip on the site http://www.fec.gov/finance/disclosure/ftpdet.shtml#a2007_2008
I can parse out who contributed and how much they contributed, but I cannot figure out who the contribution went to. There seems to be some ID, but it does not match the candidate IDs in any other file I can find. If you can help with this, I think I could make a pretty nice page showing who contributed to which candidate.
update -- I wonder if I just misunderstand this contribution system. Perhaps candidates cannot receive contributions at all, only committees? For example I see "C00431445OBAMA FOR AMERICA" received a lot of contributions. That makes it a bit more complicated then to associate those committees to candidates. Basically I want to know who supported Obama and who supported McCain.
On page 3 of the tutorial that is linked at the top of the page you liked to contains the column names including "Candidate Identification".

Resources