How to select product with multiple categories - asp.net

According my title of question so i have structure for Access database like this:
Category
categoryid categoryname
1 one
2 two
3 three
Product table:
productid productname categories
1 one 1,2,3
2 two 3
3 three 1,2
When i have categoryid is 1 I dont know the way to select product have multiple categorise. Because when i use In operator,i am getting some error..
Select * from product where categories In (categodyid) because cannot compare a collection with one value.
i'm stuck at here! Please help me! Thanks.

First of all, your tables are not normalized. Look at the Categories column in Product Table. Each cell should have only one value. By allowing multiple values, you risk various problems including update/insert anomalies and what you are seeing now. You also make it very difficult to do selects and other operations. Instead, think about normalizing your tables with this example:
Category
categoryid categoryname
1 one
2 two
3 three
Product
ProdductId ProductName
4 prod1
5 prod2
6 prod 3
Category_Prod
CategoryId ProductId
1 3
1 4
2 3
The third table acts as a way to remedy the many to many pattern. If you have any questions on how to do this or how to use it, let me know

This is a classic parent-child one-to-many relationship. You need a [ProductCategory] table to associate a given Product with multiple Categories:
productid categoryid
1 1
1 2
1 3
2 3
3 1
3 2

Related

How to combine multiple table results into one using FetchXml

I am using CRM 2013 Online. I want to generate a report on Opportunity entity like below:
CampaignCode Open Opportunity Count Won Opportunity Count Lost Opportunity Count
CC1 3 5 1
CC2 0 2 3
Here the results are group by on "CampaignCode" column which is a lookup. Using FetchXml I am able to get 3 tables like below:
CampaignCode Open Opportunity Count
CC1 3
CC2 0
CampaignCode Won Opportunity Count
CC1 5
CC2 2
CampaignCode Lost Opportunity Count
CC1 1
CC2 3
But I am not able to combine these 3 tables into 1. How can I do this?
For what i know it's not possible to merge multiple datasets in a single table. I will give your question a plus because I'm interested in this too.

Neo4j: Merge Duplicate Nodes

I made some wrong moves in Neo4j, and now we have a graph with duplicate nodes. Among the duplicate pairs, the full property set belongs to the first of the pair, and the relationships all belong to the second in the pair. The index is the node_auto_index.
Nodes:
Id Name Age From Profession
1 Bob 23 Canada Doctor
2 Amy 45 Switzerland Lawyer
3 Sam 09 US
4 Bob
5 Amy
6 Sam
Relationships:
Id Start End Type
1 4 6 Family
2 5 6 Family
3 4 5 Divorced
I am trying to avoid redoing the whole batch import. Is there a way to merge the nodes in cypher based on the "name" string property, while keeping all of the properties and the relationships?
Thank you!
Okay, I think I figured it out:
START first=node(*), second=node(*)
WHERE has(first.Name) and has(second.Name) and has(second.Age) and NOT(has(first.Age))
WITH first, second
WHERE first.Name= second.Name
SET first=second
The query is still processing, but is there a more efficient way of doing this?
You create a cross product here between the two sets, so that will be expensive. Better is to do an index lookup for name.
START first=node(*), second=node(*)
WHERE has(first.Name) and has(second.Name) and has(second.Age) and NOT(has(first.Age))
WITH first, second
SKIP 20000 LIMIT 20000
WHERE first.Name= second.Name
SET first=second
And you probably have to paginate the processing as well.
START n=node:node_auto_index("Name:*")
WITH n.Name, collect(n) nodes
SKIP 20000 LIMIT 20000
WHERE length(nodes) == 2
WITH head(filter(x in nodes : not(has(x.Age)))) as first, head(filter(x in nodes : has(x.Age))) as second
SET first=second

Using two datasets in a single report using SQL server reporting service

I need to show a report of same set of data with different condition.
I need to show count of users registered by grouping region, country and userType, I have used drill down feature for showing this and is working fine. Also the reported data is the count of users registered between two dates. Along with that I have to show the total users in the system using the same drill down that is total users by region, country and usertype in a separate column along with each count (count of users between two date)
so that my result will be as follwsinitialy it will be like
Region - Country - New Reg - Total Reg - User Type 1 - UserType2
+ Region1 2 10 1 5 1 5
+ Region2 3 7 2 4 1 3
and upon expanding the region it will be like
Region - Country - New Reg - Total Reg - User Type 1 - UserType2
+ Region1 2 10 1 5 1 5
country1 1 2 1 2 - -
country2 1 8 1 8 - -
+ Region2 3 7 2 4 1 3
Is there a way I can show my report like this, I have tried with two data sets one with conditional datas and other with non conditional but it didn't work, its always bing total number of regiostered users for all the total reg columns
Unless I'm mistaken, you're trying to create an expandable table, with different grouping levels? Fortunately, this can be easily done in SSRS if you know where to look. The totals on your example don't seem to match up in the user columns, so I may have misunderstood the problem.
For starters, set up your query to produce a single dataset like this:
Region Country New Reg - Total Reg - User Type 1 - User Type 2
Region1 country1 1 2 1
Region1 country2 1 8 1
Region2 country3 2 4 1 1
Region2 country4 1 3 1
Now that you've got that, you want to set up a new table with the fields "NewReg", "TotalReg", "UserType1" and "UserType2". Then right-click the table row, and go to "Add Group > Row Group > Parent Group". Select "Country" in the Group by and click okay. Then, repeat this process and select "Region". This time however, tick the "Add group header" box. This will insert another row above the original.
Now, for each of your fields ("NewReg", "TotalReg" etc), click in the new row above and select the field again. this will automaticaly add a Sum(FieldName) value into the cell. This will add together all the individual row totals and present a new, grouped by region row when you run the report.
That should give you the table you require with the data aggregated correctly, so all you need to do is manage the show/hide the detail rows on demand.
To do this, select your detail row (the original row) and right-click "> Row visibility". Set this to "Hide". Now, select the cell that contains the "Region" and take note of its ID using Properties (for now, let's assume it's called "Region"). Click back onto your detail row and look at the properties window. At the bottom you'll see a "Visibility" setting. In there, set "InitialToggleState" to False and "ToggleItem" to the name of your region group's cell (i.e. "Region").
Now all that should be left is to do the formatting etc and tidy up.
I have solved this problem by taking all the records from DB and filtering the records to collect new reg count by using an expression as following
=Sum(IIF(Fields!RegisteredOn.Value >Parameters!FromDate.Value and Fields!RegisteredOn.Value < Parameters!EndDate.Value , 1,0))

SQL Server - Group by, having and count in a mix

I have a database with a long list of records. Most of the columns have foreign keys to other tables.
Example:
ID SectorId BranchId
-- -------- --------
5 3 5
And then I will have a table with sectors, branches ect.
My issue:
I want to know how many records which has sector 1, 2, 3 ... n. So what I want is a group by Sector and then some count(*) which will tell me how many there is of each.
Expected output
So for instance, if I have 20 records the result might look like this:
SectorId Count
-------- -----
1 3
2 10
3 4
4 6
My attempts so far
I do not normally work a lot with databases and I have been trying to solve this for 1.5 hours. I have tried something like this:
SELECT COUNT(*)
FROM Records r
GROUP BY r.Sector
WHERE r.Date BETWEEN '2011-01-01' AND '2011-12-31'
But... errors and problems all over!
I would really appreciate some help. I do know this is probably very simple.
Thanks!
The sequence of your query is not correct; it should be like this: -
SELECT COUNT(*)
FROM Records r
WHERE r.Date BETWEEN '2011-01-01' AND '2011-12-31'
GROUP BY r.Sector
The output will be only counts i.e.
count
-----
3
10
4
6
If you want to fetch both sector and count then you need to modify the query a little
SELECT r.Sector, COUNT(*) as Count
FROM Records r
WHERE r.Date BETWEEN '2011-01-01' AND '2011-12-31'
GROUP BY r.Sector
The output will be like this: -
Sector Count
------ -----
1 3
2 10
3 4
3 6
Your query was partially right,But it needs some modification.
If I write this way:-
SELECT r.SectorID,COUNT(*) AS count
FROM Records r
WHERE r.Date BETWEEN '2011-01-01' AND '2011-12-31'
GROUP BY r.SectorID
Then output will be:-
SectorID Count
1 3
2 10
3 4
4 6

ASP.NET - Data Grid and Drop Down list

I am working on a web page that has a data grid and have a need to do
the following:
Have a column that is a drop down when the grid loads (no need to click edit)
This column is bound to a column from the data query (everything up
to this point works fine)
I know need to add more values to this drop down from another
dataset (so the user can change the value if required)
These values come from a query to another table in the database. The
values will be same for all the rows in the table, these values are
based on a master key for the complete webpage.
As an example:
table 1 has:
Mangoes $12
Apricots $13
Peaches $14
This is on the grid.
The other table has:
Prices
12
13
14
15
16
I want these values from the prices table to appear in the drop down
for table 1 in the data grid, with the current values as the selected
item.
Any ideas will help. Thanks for the help.
As far as I know,
U should better change ur table design.
If ur fruit table is concern with price id, then u can easily select the item from dropdown with current value in fruit table.
Fruit
PriceId
PriceId
Price
------------------------ --------------------
Mangoes
1
<--------------------->
1
12
Apricots
2
<--------------------->
2
13
Peaches
3
<--------------------->
3
14
then it is easier to list all price in dropdown list
and also easier to choose selected price based on PriceId from Fruit table.
Sound like little confuse? let me know, if anything u want?
Hope it works!

Resources