I have lets say two tables:
Student(id, name);
Class (id, name, student_id);
how I can select all students, but ordered by classes count?
Students:
1, "John"
2, "Andrew"
Classes:
1, french, 1
2, french, 2
3, Spanish, 1
4, English, 1
It should order:
John
Andrew
Right now I get students:
return entites.students.Include(w=>w.classes).ToList();
Order part is missing...
EDIT
Great, it works, but how it should looks, when classes table is in schools table and I want to get students ordered by schools count?
Students (id, name);
Classes (id, name, students_id);
Schools (id, name, classes_id);
Students:
1, "John"
2, "Andrew"
Classes:
1, french, 1
2, french, 2
3, Spanish, 1
4, English, 1
5, English, 2
Schools:
1, "Primary school", 1
2, "Secondary school", 2
3, "Another school", 5
It should give me:
Andrew
John
Assuming your file has a
using System.Linq;
above the namespace directive, you can do:
entities.students.Include(s => s.classes).OrderBy(s => s.classes.Count());
Related
I have 2 SQLite databases, Salesmen and Sales. Here's what the original CSV files looked like, but I've already put them in SQLite (just so you can see how the tables are layed out):
Salesmen Table
id, name
1, john
2, luther
3, bob
Sales Table
id, salesmen_id, sales_amount
1, 1, 100
2, 3, 20
3, 2, 35
4, 3, 25
5, 1, 55
6, 2, 200
7, 2, 150
My question is how do I write a function in ruby that will return all the Salesmen names, sorted by their total sales amount? I know this requires using a join, but I'm not entirely sure how the query should look like.
I want the new table to look like this:
New Table
name, total_sales
luther, 385
john, 155
bob, 45
The new sqlite query should be in this format:
$db.execute %q{
SELECT account_name, units, unit_price
FROM accounts, positions
...
}
Thanks in advance
I think this is what you want
SELECT name, sum(sales_amount)
FROM salesmen INNER JOIN sales on sales.salesmen_id = salesmen.id
GROUP BY salesmen_id
I am looking to migrate ZenCart "customers" to an existing Magento website. I tried two extensions from MagentoConnect; however it doesn't works.
osCommerce Migration Tool osCommerce Import
There are premium 3rd party migration services available in market, however I would like to do this my own. Please help me out by providing some steps to this through code.
Currently the zen cart DB is having prefix "zen_". Will this be causing inconvenience ? please point me out a starting point on this. Thanks
I have sorted this my own. What surprised me is, there are no valid doc on how to import customers from ZenCart/OsCommerce to Magento any where around web unless some forum with premium Magento extensions. Hence I am posting my solution here which would be a help for any one who is looking for this same solution.
Fetching ZenCart customer details
Obviously ZenCart uses MySQL DB. Get into MySQL prompt and look into the customers table. Use the below command respectively.
use ZenCart_DB
mysql> select * from customers\G
You can see the details of your ZenCart customers there. Sample output will be like this per customer.
customers_id: 1298
customers_gender: m
customers_firstname: firstname
customers_lastname: Lastname
customers_dob:
customers_email_address: customer#email.com
customers_nick:
customers_default_address_id:
customers_telephone: 12345678
customers_fax:
customers_password: dd2df54a57a4d35ffd2985b3584f0831:2c
customers_newsletter: 0
customers_group_pricing: 0
customers_email_format: TEXT
customers_authorization: 0
customers_referral:
customers_paypal_payerid:
customers_paypal_ec: 0
COWOA_account: 0
This is a sample and we have to take all these customer details to a unix file.
select * from customers into outfile 'customer.txt'\G
Now come out of MySQL prompt. To create a Magento user, we only need firstname,lastname,email and password. These four details are mandatory. Hence grep those details from the customer.txt file.
The location of customer.txt file will be /var/lib/mysql/ZenCart_DB/customer.txt
Grep the required customer details to different individual files which will help us to put them into for loop later.
awk {'print $3,$4,$7,$10'} customer.txt > details.txt
awk {'print$1'} details.txt > zen_firstname
awk {'print$2'} details.txt > zen_secondname
awk {'print$3'} details.txt > zen_email
awk {'print$4'} details.txt > zen_password
Creating a Magento user
No we have collected all the details. Now to create a test Magento user from backend, we have to run 5 MySQL queries in magento database. They are,
INSERT INTO customer_entity ( entity_id, entity_type_id, attribute_set_id, website_id, email, group_id, increment_id, store_id, created_at, updated_at, is_active, disable_auto_group_change) VALUES ( 1, 1, 0, 1, $email, 1, NULL, 4, 2014-11-24 11:50:33, 2014-11-24 12:05:53, 1, 0)
INSERT INTO customer_entity_varchar (value_id, entity_type_id, attribute_id, entity_id, value) VALUES (1, 1, 5, 1, $firstname);
INSERT INTO customer_entity_varchar (value_id, entity_type_id, attribute_id, entity_id, value) VALUES (2, 1, 7, 1, '$lastname' );
INSERT INTO customer_entity_varchar (value_id, entity_type_id, attribute_id, entity_id, value) VALUES (3, 1, 12, 1, '$password' );
INSERT INTO customer_entity_varchar (value_id, entity_type_id, attribute_id, entity_id, value) VALUES (5, 1, 3, 1, 'English' );
If you have too many customers to add, then better put them in for loop. This is optional for each person. we can create for loop in bash or similar in Perl or what ever language you are good at.
Few things to note,
entity_id value is important and it should be the same in all the 5 queries. It determines the customer's ID number.
value_id is like serial number to the rows in table customer_entity_varchar. Follow the sequence as it is.
attribute_id should be kept as it is in the sequence 5,7,12,3 as it represents customers firstname, lastname, password, language respectively.
That's all I think. Thanks. :)
In my Activities table I have a duration column. The duration can be in months like 1m, 4m, 8m.. etc or it can be in years like 2y, 5y, 9y etc. In my query i want to write an orderby in the ascending order based on the duration. Following is my query.
SELECT ACTIVITY,DURATION FROM ACTIVITIES
ORDER BY 2
The traditional order by is returning the result as 1m,2y,4m,5y,8m,9y. Instead I want the result as 1m,4m,8m,2y,5y,9y.
How can I achieve this?
general and specific to your case:
select blablabla from activities
order by decode(substr(col, len(col)-1, 1), 'm', 1, 'y', 12, 0)*
to_number(substr(col, 1, len(col)-1)
or (direct)
select blablabla from activities
order by decode(col, '1m', 1, '4m', 2, '8m', 3, '2y', 4, '5y', 5, '9y', 6, 7)
or (positional)
select blablabla from activities
order by instr(col, '1m,4m,8m,2y,5y,9y')
or (general, but simple)
select blablabla from activities
order by substr(col, len(col)-1, 1), substr(col, 1, len(col)-1)
You could use SUBSTR to check the unit, and just cast the rest to an integer (adjusting for the unit) to do the sorting;
SELECT ACTIVITY,DURATION FROM ACTIVITIES
ORDER BY
CASE WHEN SUBSTR(DURATION, -1, 1) = 'y'
THEN CAST(SUBSTR(DURATION, 1, LENGTH(DURATION)-1) AS INT) * 12
ELSE CAST(SUBSTR(DURATION, 1, LENGTH(DURATION)-1) AS INT)
END;
An SQLfiddle to test with.
SELECT activity, duration
FROM (SELECT activity,
duration,
substr(duration, length(duration)) unit,
to_number(substr(duration, 1, length(duration) - 1)) VALUE
FROM activities)
ORDER BY unit, VALUE;
I would recommend converting your values to months or days most likely so you are comparing "apples to apples". You should be able to do this fairly easily in the Select statement by multiplying your months or years out (unfortunately you would have to settle on a month equal to 30/31/x days in this scenario as well as a year of 365 days since leap would not be handled) since you do not have a specific date available).
I'm having a brain fart and I can't seem to push past it. I've done this before but I can't remember how I did it. Here's the gist: I have a table I created (test) and in this table I'm storing some random information. (It's a test table and so the information in the table is not applicable to anything). My test table looks like this:
Name varchar2 Primary key
age varchar2
sex char
social varchar2 Primary Key
I'm using this table as an example and that is why I have the two primary keys. I was showing a student how to do a composite key.
My question is this: how do I create a query that has a total count at the end of the rows?
I'm looking for something like this:
Name Male Female
xxxxx m
xxxxx m
xxxxx m
xxxxx m
xxxxx F
xxxxx F
Total: 4 2
I created the following query (which doesn't quite work) just as a test.
set feedback on
compute sum(COUNT(SEX)) LABEL "# of males" of sex_count on report
select count(sex) as sex_count
from test
where sex = upper('m')
Would this resolve your problem?
select name, male, female from (
select name,
decode( sex, 'm', 'm', null) Male,
decode( sex, 'f', 'f', null) Female, 0 n
from test
union
select 'Total',
to_char(sum(decode(sex, 'm', 1, 0))),
to_char(sum(decode(sex, 'f', 1, 0))),
1
from test
group by 'Total'
) order by n
Link to Fiddle:
http://sqlfiddle.com/#!4/0e501/3/0
Based on the "compute...on report" statement, it looks like you're using SQL*Plus. Try this:
compute count label total of male female on report
break on report
select name, decode(sex, 'm', sex) male, decode(sex, 'f', sex) female
from test
order by name;
I have a scenairo where I need to bind a RadPanelBar to a SQL table similar to the below structure:
ID, Name, Category
1, Fred, Male
2, Sam, Male
3, Fred, Male
4, Sam, Female
5, Louise, Female
6, Tom, Male
I need the panelbar to be in a Category > Name structure (i.e. each name to be a child item of their gender) but can't see an easy way to do this from Telerik's examples.
Any help/suggestions would be greatly appreciated.
Thanks in advance.
You should look at the Data Bindings example and the Hierarchical Data Binding example.
Each item in the RadPanelBar has an item id. For child items you have to define a parent id also. The problem is that the database table does not have root items (male, female) stored as rows, so you have to add them first before binding to a RadPanelBar.
You can read the database table into a list or dataset, like in the example, and add the missing root items there.
Or, if you are using declarative binding (in ASPX), you can use UNION statements in a SQL query:
SELECT
id
,CASE WHEN Category = 'Male' THEN -1 ELSE -2 END AS ParentID
,name
FROM table
UNION
SELECT
-1 AS id
NULL As ParentID
'Male' AS name
UNION
SELECT
-2 AS id
NULL As ParentID
'Female' AS name