please i need a quick help right now. Here is my question
i have a table with several columns. Am using yii framework to display my data what i want to do is to group my record in this way
column 1 column2 column3 column4
1 2 3 4
80 3 1 100
30 3 1 60
50 3 0 10
90 2 3 40
100 2 1 80
SO what i want to do is to query my table display column2 and column3, group by my column2. But where am having issues is that, my column2 returns 2,2 - 3,3 and group record from column3 under each of the duplicated column2
My result should be display in this way:
........................................................................
column2 -- 2
.1
.3
.........................................................................
column2 -- 3
.0
.1
controller::
protected function displaybyCategory()
{
//$model = new myModel;
$criteria= new CDbCriteria();
$criteria->distinct = true;
$criteria->group = 'column2,column3';
$criteria->order = 'column2';
//$dataProvider=new CActiveDataProvider('myModel' );
$dataProvider=new CActiveDataProvider('myModel', array(
'criteria'=>$criteria,
'pagination'=>false,
));
$this->renderpartial('application.views.competency.ctype',array(
'dataProvider'=>$dataProvider,
));
}
My View:
<ul id="example1" class="accordion">
<li>
<h3><?php echo CHtml::link(CHtml::encode($data->column2)); ?></h3>
<div class="panel loading">
<h4><?php echo CHtml::link(CHtml::encode($data->column3)); ?></h4>
</div>
</li>
</ul>
Related
I have a table like this:
Table1
Column1
Column2
1
["1","2","8"]
2
["2","3","7"]
What I would like to do is to break that table out into a list like:
Column1
column2
1
1
1
2
1
8
2
2
2
3
2
7
I haven't been able to get the right query to split the column 2 and pair this with the original column1 values.
How would I do that? Ta Steve
datatable(Column1:int, Column2:dynamic)
[
,1, dynamic(["1","2","8"])
,2, dynamic(["2","3","7"])
]
| mv-expand Column2
Column1
Column2
1
1
1
2
1
8
2
2
2
3
2
7
Fiddle
I have two columns like the following and I want to delete the duplicates.
Column1 Column2
1 10
2 9
3 8
4 7
5 6
6 5
7 4
8 3
9 2
10 1
I want to delete half of these entries so that there are only 5 rows like this:
Column1 Column2
1 10
2 9
3 8
4 7
5 6
Any ideas? I know how I could do it in C# and remove if there are duplicates then delete but I want to do it in SQL. The values represent ID's and a relationship between the ID's. Order doesnt matter in the relationship so 1-10 is the same as 10-1. So in that way there are duplicate relationships.
One way would be as follows:
DELETE t FROM MyTable t
WHERE t.Column1 > t.Column2 AND EXISTS (
SELECT * FROM MyTable tt
WHERE t.Column1=tt.Column2 AND t.Column2=tt.Column1
)
The t.Column1 > t.Column2 says that if there is a pair of matching rows, delete the one where Column1 is greater than Column2.
Demo.
There is a table in SQl database which shows the result the way below:
ID Name value
1 Chips 100
2 Chocolate 50
3 Ice Cream 200
4 Burger 40
but I want to display it like in this way:
Name value
Chocolate 50
Chips 100
Ice Cream 200
Burger 40
how can I do this in sql server?
You can use ID to do the sorting like below
ORDER BY CASE ID
WHEN 2 THEN 1
WHEN 1 THEN 2
WHEN 3 THEN 3
WHEN 4 THEN 4
ELSE 5
END
If there is dynamic rows (lot of rows), you can use same like below
ORDER BY CASE ID
WHEN 2 THEN 1
ELSE 2
END
The else part will take care of the rest of the rows.
You can use CASE in ORDER BY to make sure the record with name = 'Chocolate' comes first and the rest is sorted by ID.
select name, value
from table1
order by case name
when 'Chocolate' then -1
else ID
end
Try this
SELECT * FROM `test` WHERE 1 ORDER BY name = 'Chocolate' DESC
There's a table named sample, and it has two columns id, and id2. Some records on id2 are filled with numbers, but some are null. So I need to fill them up with the same number as their closest record. That is, if each record on id2 is not null, move on to the next one, and if each record on id2 is null, fill it in with the previous one. How can I do this with vba?
sample
id id2
1 100
2
3 500
4 600
5
6 800
sample_result
id id2
1 100
2 100
3 500
4 600
5 600
6 800
In Access I'm not sure it can be done in pure SQL, but I think this should get you close to what you want:
UPDATE sample AS s
SET s.id2 = Dmax("id2", "sample", "id <" & [s].[id])
WHERE (( ( s.id2 ) IS NULL ));
Scenario
I need to update a SQL 2008 database daily via a spreadsheet (the only option available). The format is pretty basic, however there are potentially millions of records. Column1 and Column3 will have many predefined duplicate values, already pulled out into separate tables.
Spreadsheet Sample
Column1 Column2 Column3
Apple 10 Red
Apple 20 Red
Apple 15 Blue
Apple 21 Green
Orange 10 Orange
Orange 7 Orange
Orange 9 Red
Orange 70 Blue
Orange 10 Blue
DB Setup
My database is set up with three separate tables:
//Lookup_Column1
id type
1 Apple
2 Orange
//Lookup_Column3
id type
1 Red
2 Blue
3 Green
4 Orange
//Main - this is what should be inserted, after Column1
//and Column2 are matched to their respective ID's
key Column1 Column2 Column3
1 1 10 1
2 1 20 1
3 1 15 2
4 1 21 3
5 2 10 4
6 2 7 4
7 2 9 1
8 2 70 2
9 2 10 2
Question
How can I write the SQL to insert records that match the information from the lookup tables? How can I go from this:
INSERT INTO Main(Column1, Column2) VALUES ('Apple', 10, 'Red');
To this:
INSERT INTO Main(Column1, Column2) VALUES (1, 10, 1);
//pulled from lookup tables, where Apple = 1 and Red = 1
You could try something like this:
INSERT INTO Main(Column1, Column2, Column3) VALUES
(
(SELECT id FROM Lookup_Column1 WHERE type = 'Apple'),
10,
(SELECT id FROM Lookup_Column3 WHERE type = 'Red')
);
There isn't any fault-tolerance, but it would work as long as you could parse your spreadsheet values into SELECT statements.
The source of data that inserted into a table is defined as
VALUES ( { DEFAULT | NULL | expression } [ ,...n ] ) [ ,...n ]
| derived_table
| execute_statement
| <dml_table_source>
| DEFAULT VALUES
So we can use a derived_table which is defined as
derived_table
Is any valid SELECT statement that returns rows of data
to be loaded into the table. The SELECT statement cannot contain a
common table expression (CTE).
INSERT INTO
MAIN
(Column1, Column2, column3)
SELECT
lc1.id,
10,
lc2.id
FROM
Lookup_Column1 lc1,
Lookup_Column2 lc2
WHERE
lc1.type = 'Apple'
and
lc2.type = 'Red'
For more information see INSERT (Transact-SQL)
If you could get your spreadsheet values into a staging table (or linked to the spreadsheet directly using a linked server or OPENDATASOURCE ) you could change your INSERT clause to
INSERT INTO
MAIN
( Column1, Column2, column3)
SELECT
lc1.id,
s.column2,
lc2.id
FROM
OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\YourdataSource.xls;Extended Properties=EXCEL 5.0')...[Sheet1$] s
INNER JOIN Lookup_Column1 lc1
ON s.column1 = lc1.type
INNER JOIN Lookup_Column2 lc2
ON s.column3 = lc2.type
This would allow you to remove the looping that you're currently thinking of doing.
Have you tried using a SELECT INTO statement? This allows you to select data from one table and insert it into another.
http://www.w3schools.com/sql/sql_select_into.asp