I want data in gridview like this:
CategoryName Subcategory Name
--------------------------------
Abc Abc1,abc2,abc3
Bcs Bcs1,bcs2
def Null / No Record
How can I do that?
okay i want data from database by using single table. i have one table categories in which i have field like categryid, parentid,name. when parentid is 0 then its known by Categories else all other is subCategories.
I Am using asp.net with c# and i want to do this in gridview with using boundfield. for categories i have done but for sub categories i dont have any idea how to do.
subcategories is idedntify by its parentid. in sub categories parentid =categryid
Assuming data in your table is something like this
CategoryId| ParentID| Name |
1 | 0 | A |,
2 | 1 | B |,
3 | 0 | C |,
4 | 3 | D |
You can do something like
Create Table #ReportTable(Id identity int,CategoryId int,Category varchar(10),SubCategory Varchar(10))
Declare #CountOfRecords int = Select count(categoryid) from categories
Declare #TableIterator int = 1
While #TableIterator <= #CountOfRecords
Begin
Declare #ParentId int = (Select ParentId From Categories Where CategoryId=#TableIterator)
If #ParentId = 0
Begin
Insert Into #ReportTable(CategoryId,Category)
Select CategoryId,Category
From Categories
Where CategoryId = #TableIterator
End
Else
Begin
Update #ReportTable
Set SubCategory =
(Select SubCategory From Categories Where CategoryId = #TableIterator)
And Id = #ParentId
End
Set #TableIterator = #TableIterator + 1
End
Related
I have a table of ratings
|EMPLOYEE|Rating|
1 B
2 B
3 C
4 NULL
5 NULL
6 NULL
and i want to retrieve the count of the grades by each grading like so
Result set
|Rating|Count|
A 0
B 2
C 1
D 0
E 0
I used this query but the grades that isnt in the table will jsut appear as null
select rating,count(rating) from table group by rating
I also used this query which is basically a pivot of the above result set but for some reason it shows 3 rows of repeating data instead of just 1
select (select count(rating) from table where rating = 'E'),(select count(rating) from table where rating = 'D'),(select count(rating) from table where rating = 'C'),(select count(rating) from table where rating = 'B'),(select count(rating) from table where rating = 'A') from table group by rating
If you had a table for the assignable ratings then it would be quite simple (and flexible)
e.g. consider :-
Your existing table.
DROP TABLE IF EXISTS mytable;
CREATE TABLE IF NOT EXISTS mytable (employee TEXT, rating text);
INSERT INTO mytable VALUES (1,'B'),(2,'B'),(3,'C'),(4,null),(5,null),(6,null);
The rating table.
DROP TABLE IF EXISTS rating;
CREATE TABLE IF NOT EXISTS rating (rating);
INSERT INTO rating VALUES('A'),('B'),('C'),('D'),('E');
Then :-
SELECT rating.rating, (SELECT count(*) FROM mytable WHERE rating.rating = mytable.rating) FROM rating;
Results in :-
Flexibility
Add some new ratings e.g. as per :-
INSERT INTO rating VALUES('X'),('Y'),('Z');
And then run:--
SELECT rating.rating, (SELECT count(*) FROM mytable WHERE rating.rating = mytable.rating) FROM rating;
results in :-
I have table like this:
id | name | type
-----------------
0 | firs | 2
1 | secs | 3
2 | this | 9
1 | thus | 3
I know id (it is not unique id) and type and I want to select records only if there is specified number of records with that id and type.
For one record I tried for example this:
select * from myTable
where
(select count(*) from myTable where myTable.id = 0 and myTable.type = 2) = 1;
This returns me all rows, not just the one row I want. Can anyone please tell me, what is the right way how to get the right result?
Your where statement looks like WHERE 1 = 1 and it's always true so SELECT gets every column from your table.
If you want select for example data where id = 1 and type = 3 and number of occurrences = 2 you can do something like that:
select * from myTable
where
(select count(*) from myTable where myTable.id = 1 and myTable.type = 3) = 2 and myTable.id = 1 and myTable.type = 3;
I have two tables, A & B:
TABLE A
id | name
TABLE B
id | name | fk_idA
I want to create trigger AFTER INSERT in TABLE B which updates fk_idA appropriate with the newest id from table A.
An example:
TABLE A
id | name
1 | Andrew
2 | David
TABLE B
id | name | fk_idA
1 | Photo1 | 2
If the ID column is autoincrementing, the latest is the largest one, i.e., the one returned by MAX:
CREATE TRIGGER DefaultAIsLatest
AFTER INSERT ON TableB
FOR EACH ROW
WHEN NEW.fk_idA IS NULL
BEGIN
UPDATE TableB
SET fk_idA = (SELECT MAX(id)
FROM TableA)
WHERE id = NEW.id;
END;
I am creating a database for my Psych class and I am scoring a personality profile. I need to compare two test items and, if they match a condition, then copy into a separate table.
Example (pseudocode is between \)Sqlite3
INSERT INTO Scale
SELECT* FROM Questions
WHERE \\if Question 1 IS 'TRUE' AND Question 3 IS 'FALSE' THEN Copy this Question
and its response into the Scale table\\;
I have about 100 other questions that work like this. Sample format goes like this:
IF FirstQuestion IS value AND SecondQuestion IS value THEN
Copy both questions into the Scale TABLE.
---------- EDITED AFTER FIRST RESPONSE! EDITS FOLLOW-------------
Here is my TestItems table:
ItemID | ItemQuestion | ItemResponse
```````````````````````````````````````````````````
1 | Is the sky blue? | TRUE
2 | Are you a person? | TRUE
3 | 2 Plus 2 Equals Five | FALSE
What I want to do: If Question 1 is TRUE AND Question 3 is FALSE, then insert BOTH questions into the table 'Scale' (which is setup like TestItems). I tried this:
INSERT INTO Scale
SELECT * FROM TestItems
WHERE ((ItemID=1) AND (ItemResponse='TRUE'))
AND ((ItemID=3) AND (ItemResponse='FALSE'));
HOWEVER: The above INSERT copies neither.
The Resulting 'Scale' table should look like this:
ItemID | ItemQuestion | ItemResponse
```````````````````````````````````````````````````
1 | Is the sky blue? | TRUE
3 | 2 Plus 2 Equals Five | FALSE
There is nothing wrong with your query. You're just there:
INSERT INTO Scale
SELECT * FROM Questions
WHERE `Question 1` = 1 AND `Question 3` = 0;
Here 1 and 0 are values (in your first case, true and false). First of all you should ensure there are fields Question 1 and Question 3 in your Questions table. Secondly the column count as well as data types of Scale table should match Questions table. Otherwise you will have to do selectively choose the fields in your SELECT query.
Edit: To respond to your edit, I am not seeing an elegant solution. You could do this:
INSERT INTO Scale
SELECT * FROM TestItems WHERE ItemID = 1 AND ItemResponse = 'TRUE'
UNION
SELECT * FROM TestItems WHERE ItemID = 3 AND ItemResponse = 'FALSE'
WHERE (SELECT COUNT(*) FROM (
SELECT 1 FROM TestItems WHERE ItemID = 1 AND ItemResponse = 'TRUE'
UNION
SELECT * FROM TestItems WHERE ItemID = 3 AND ItemResponse = 'FALSE'
) AS t) >= 2
Your insert did not work because ItemID cant be both 1 and 3 at the same time. My solution gets the required records to be inserted into Scale table, but verifies both the record exists by checking the count. Additionally you could (should) do as below since this can be marginally more efficient (the above SQL was to clearly show the logic being used):
INSERT INTO Scale
SELECT * FROM TestItems WHERE ItemID = 1 AND ItemResponse = 'TRUE'
UNION
SELECT * FROM TestItems WHERE ItemID = 3 AND ItemResponse = 'FALSE'
WHERE (
SELECT COUNT(*)
FROM TestItems
WHERE ItemID = 1 AND ItemResponse = 'TRUE'
OR ItemID = 3 AND ItemResponse = 'FALSE'
) >= 2
[ASP .Net - Microsoft Visual Web Developer 2010]
Hi all,
I've problem with this code:
With MenuNavCatDataSource
Dim xReader As Data.IDataReader = .Select(DataSourceSelectArguments.Empty)
If xReader.Read Then
MenuNavCat1.Text = xReader.Item("MenuCategoryName")
MenuNavCat2.Text = xReader.Item("MenuCategoryName")
MenuNavCat3.Text = xReader.Item("MenuCategoryName")
MenuNavCat4.Text = xReader.Item("MenuCategoryName")
MenuNavCat5.Text = xReader.Item("MenuCategoryName")
End If
End With
I've 5 label and I want to parse the content of the label from the database. The database contains menus ordered from 1 to 5. And I want to show it on:
- 'MenuNavCat1' label, menu with order number 1 on database,
- 'MenuNavCat2' label, menu with order number 2 on database, and so on...
How to add where statement to the code, just like 'WHERE OrderNo = 1', and so on..?
Need your help guys..
Thank you so much.
EDIT
Here is the database:
Table MenuNavCategory
| Column Name | Data Type |
|-----------------------------|
| MenuNavCatID | int |
| CategoryName | varchar(20) |
| OrderNumber | int |
|-----------------------------|
And there is some value inserted to the table.
I want to show the 'CategoryName' onto some label.
For example:
lblCat1 | lblCat2 | lblCat3 | lblCat4
Then, using that xReader.Read, store the value on the database onto that label based on the OrderNumber...
The DataReader only reads one row of data at a time. If you have 5 labels then you should have 5 rows in your database. Then you iterate through each row using the DataReader and assign the labels:
With MenuNavCatDataSource
Dim xReader As Data.IDataReader = .Select(DataSourceSelectArguments.Empty)
'i is just a counter to keep track of what row we are on.
Dim i as integer = 1
If xReader.Read Then
Select Case i
Case 1 'First Row from DB.
MenuNavCat1.Text = xReader.Item("MenuCategoryName")
Case 2 'Second Row from DB... etc.
MenuNavCat2.Text = xReader.Item("MenuCategoryName")
Case 3
MenuNavCat3.Text = xReader.Item("MenuCategoryName")
Case 4
MenuNavCat4.Text = xReader.Item("MenuCategoryName")
Case 5
MenuNavCat5.Text = xReader.Item("MenuCategoryName")
x+=1
End If
End With
The above is just an example to try and explain how the DataReader works. I'd find another way to actually code it as the above relies on the rows in the database being in the correct order and is pretty ugly.