SQL Server or C# transpose - c#

I know this seems like a very basic question but it isn't; I can't use SQL Server pivot because I don't want SUM, I just want to transpose.
How can I transpose SQL data either in SQL query on in C# LINQ? Which is the fastest?
General data
| Company | Product | Count |
|---------|---------|-------|
| Cloud | Nokia | 10 |
| Cloud | Samsung | 30 |
| Cloud | Sony | 42 |
| Coms | Apple | 34 |
| Coms | Nokia | 39 |
| Coms | Samsung | 11 |
| Coms | Sony | 22 |
| RP | Nokia | 22 |
| RP | Samsung | 29 |
| RP | Sony | 22 |
Into this:
| Company | Nokia | Samsung | Sony | Apple |
|---------|-------|---------|------|--------|
| Cloud | 10 | 30 | 42 | 0 |
| Coms | 39 | 11 | 22 | 34 |
| RP | 22 | 29 | 22 | 0 |

This query:
SELECT
Company
, [Nokia]
, [Samsung]
, [Sony]
, [Apple]
FROM
YourTable
PIVOT (
MAX(Count) FOR Product IN ([Nokia], [Samsung], [Sony], [Apple])
)
AS p
Will produce:
| COMPANY | NOKIA | SAMSUNG | SONY | APPLE |
|---------|-------|---------|------|--------|
| Cloud | 10 | 30 | 42 | (null) |
| Coms | 39 | 11 | 22 | 34 |
| RP | 22 | 29 | 22 | (null) |
SQLFiddle Demo

Related

How to get all ids in LINQ which two columns dont have predefined values

I need to retrieve all Ids from two columns, which doesnt contain some other list.
For example:
If I have a variable:
I need to select List of rows in All notifications by DeliveryTypeId and NotificationGroupId where those IDs not contained in pair combination in selected notifications.
var selectedNotifications = _dbContext.UserNotificationTypeDeliveryChoice.Include(m => m.NotificationGroup)
.Include(m => m.DeliveryType)
.Where(m => m.UserDefId == userDefId && m.UserTypeId == (int)userType)
.Select(m => new NotificationGroup()
{
NotificationGroupId = m.NotificationGroup.NotificationGroupId,
Name = m.NotificationGroup.Name,
DefaultDeliveryType = new DeliveryType()
{
DeliveryTypeId = m.DeliveryType.DeliveryTypeId,
Name = m.DeliveryType.Name,
HasChoosen = true
}
}).ToList();
var selectedNotificationsMatchingIds = selectedNotifications.Select(n => new { n.NotificationGroupId, n.DefaultDeliveryType.DeliveryTypeId }).ToList();
and I need a condition witch will take all rows from db where is !contains.
example:
//NotificationGroup Codelist
+---------------------+-------------+-----------------------+
| NotificationGroupId | Name | DefaultDeliveryTypeId |
+=====================+=============+=======================+
| 1 | Comments | 1 |
+---------------------+-------------+-----------------------+
| 2 | QA | 1 |
+---------------------+-------------+-----------------------+
| 3 | Services | 1 |
+---------------------+-------------+-----------------------+
| 4 | eScheduling | 1 |
+---------------------+-------------+-----------------------+
| 5 | eDelivery | 2 |
+---------------------+-------------+-----------------------+
//Delivery Type Codelist
+----------------+-----------------+
| DeliveryTypeId | Name |
+================+=================+
| 1 | SMS |
+----------------+-----------------+
| 2 | Email |
+----------------+-----------------+
| 3 | PortalMessaging |
+----------------+-----------------+
//SELECTED NOTIFICATIONS
+--------------------------------------+-----------+----------------------------+---------------------+----------------+------------+
| UserNotificationTypeDeliveryChoiceId | UserDefID | UserCompanyOrInstitutionId | NotificationGroupId | DeliveryTypeId | UserTypeId |
+======================================+===========+============================+=====================+================+============+
| 269 | 2933 | NULL | 1 | 2 | 1 |
+--------------------------------------+-----------+----------------------------+---------------------+----------------+------------+
| 270 | 2933 | NULL | 2 | 2 | 1 |
+--------------------------------------+-----------+----------------------------+---------------------+----------------+------------+
| 271 | 2933 | NULL | 3 | 2 | 1 |
+--------------------------------------+-----------+----------------------------+---------------------+----------------+------------+
| 272 | 2933 | NULL | 4 | 2 | 1 |
+--------------------------------------+-----------+----------------------------+---------------------+----------------+------------+
| 273 | 2933 | NULL | 4 | 1 | 1 |
+--------------------------------------+-----------+----------------------------+---------------------+----------------+------------+
//ALL NOTIIFICATIONS
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| NotificationGroupUserTypeId | NotificationGroupChangeTypeId | UserTypeId | NotificationGroupId | NotificationTemplateId | DateCreated | DeliveryTypeId |
+=============================+===============================+============+=====================+========================+=========================+================+
| 1 | NULL | 1 | 1 | 14 | NULL | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 6 | 11 | 1 | 4 | 21 | 2011-07-18 11:13:23.543 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 17 | NULL | 1 | 4 | 6 | NULL | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 26 | NULL | 1 | 3 | 8 | NULL | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 30 | 11 | 1 | 4 | 15 | 2011-07-17 13:16:36.897 | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 31 | 14 | 1 | 4 | 16 | 2011-07-17 13:17:00.880 | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 32 | 15 | 1 | 4 | 17 | 2011-07-17 13:17:18.220 | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 33 | 16 | 1 | 4 | 18 | 2011-07-17 13:17:34.833 | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 34 | 17 | 1 | 4 | 19 | 2011-07-17 13:17:58.337 | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 35 | 18 | 1 | 4 | 20 | 2011-07-17 13:18:35.960 | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 36 | 14 | 1 | 4 | 22 | 2011-07-18 16:00:27.320 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 37 | 15 | 1 | 4 | 23 | 2011-07-18 16:01:17.843 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 38 | 16 | 1 | 4 | 24 | 2011-07-18 16:01:36.300 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 39 | 17 | 1 | 4 | 25 | 2011-07-18 16:01:55.923 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 40 | 18 | 1 | 4 | 26 | 2011-07-18 16:02:12.607 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 41 | 8 | 1 | 3 | 27 | 2011-11-09 12:59:51.307 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 44 | 19 | 1 | 3 | 28 | 2011-12-21 14:57:31.780 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 48 | 20 | 1 | 5 | 29 | 2011-12-23 10:19:16.840 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 49 | 1 | 1 | 3 | 6 | 2011-12-22 16:09:15.047 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
So I am getting in selectedNotifications correct pairs, but then when I try to filter my new query, I m getting only two pairs but I should get it (with these example):
var notSelectedNotifications = _dbContext.NotificationGroupUserType.Include(m => m.DeliveryType)
.Include(m => m.NotificationGroup)
.Where(m => m.UserTypeId == (int)userType)
.Where(m => !selectedNotificationsMatchingIds.Contains(new { m.NotificationGroup.NotificationGroupId, m.DeliveryType.DeliveryTypeId }))
.Select(m => new NotificationGroup()
{
NotificationGroupId = m.NotificationGroupId,
Name = m.NotificationGroup.Name,
DefaultDeliveryType = new DeliveryType()
{
DeliveryTypeId = m.DeliveryType.DeliveryTypeId,
Name = m.DeliveryType.Name,
HasChoosen = false
}
}).ToList();
Any advise is welcomed. If someone can tell me, how to get all of these which is not selected in selectedNotificationsMatchingIds
The Contains() method usage default equality comparer when implementation of IEqualityComparer<T> is not provided for element stored in collection. Obviously the default equality comparer will not work for element of Anonymous class stored in selectedNotificationsMatchingIds.
You have two options:
Option 1: Re-write Where condition to use Any as:
//.Where(m => !selectedNotificationsMatchingIds
// .Contains(new { m.NotificationGroup.NotificationGroupId,
// m.DeliveryType.DeliveryTypeId }))
.Where(m => !selectedNotificationsMatchingIds
.Any(c => c.NotificationGroup.NotificationGroupId == m.NotificationGroupId
&& c.DeliveryTypeId == m.DeliveryType.DeliveryTypeId))
Option 2: Create a class having properties for NotificationGroupId and DeliveryTypeId and implement IEqualityComparer<T> interface for it. Then only you can use object of that class as part of .Contains() method.

How to query the sum of each person's availability base on their skill

I'm using sqlite, I've setup three tables.
Table for list of skills this can be expand.
+----+----------+
| ID | Skills |
+----+----------+
| 1 | Swimming |
| 2 | Running |
| 3 | Boxing |
| 4 | Dancing |
| 5 | Singing |
+----+----------+
Table for availability of each person. 0 represents the person is not available.
+-------+-----+-----+-----+-----+-----+-----+-----+
| Names | SUN | MON | TUE | WED | THU | FRI | SAT |
+-------+-----+-----+-----+-----+-----+-----+-----+
| Mark | 0 | 1 | 1 | 1 | 1 | 1 | 0 | (MON-FRI)
| Robin | 0 | 1 | 1 | 1 | 1 | 1 | 0 | (MON-FRI)
| James | 0 | 0 | 1 | 1 | 1 | 1 | 1 | (TUE-SAT)
+-------+-----+-----+-----+-----+-----+-----+-----+
Table for each person skill possess. 1/0 is a boolean value.
+-------+----------+---------+--------+---------+---------+
| Names | Swimming | Running | Boxing | Dancing | Singing |
+-------+----------+---------+--------+---------+---------+
| Mark | 1 | 1 | 1 | 1 | 1 |
| Robin | 1 | 1 | 1 | 1 | 1 |
| James | 1 | 1 | 1 | 1 | 0 |
+-------+----------+---------+--------+---------+---------+
By using the tables above how could I achieved this result to sum each person's availability base on their skill.
Expected Results:
+----------+-----+-----+-----+-----+-----+-----+-----+
| Skills | SUN | MON | TUE | WED | THU | FRI | SAT |
+----------+-----+-----+-----+-----+-----+-----+-----+
| Swimming | 0 | 2 | 3 | 3 | 3 | 3 | 1 |
| Running | 0 | 2 | 3 | 3 | 3 | 3 | 1 |
| Boxing | 0 | 2 | 3 | 3 | 3 | 3 | 1 |
| Dancing | 0 | 2 | 3 | 3 | 3 | 3 | 1 |
| Singing | 0 | 2 | 3 | 3 | 3 | 3 | 0 |
+----------+-----+-----+-----+-----+-----+-----+-----+

How to join Join two DataRows into a single DataRow through Column Name?

I am trying to figure out how to join two respective datarows into single datarow in dataset through Department column Name.
In provided dataset output i want to join Gastroenterology and Medical Gastroen(two datrows) through column name to single datarow (similar to Required Final dataset Output with Merged Rows).
Need Your ideas/help how it can be accomplished in asp.net and/or C#.
DataSet Output
Department Male Visit Female Visit Total Count
---------- ---------- ------------ -----------
Endocrinology 10 20 30
Gastroenterology 15 25 40
General Medicine 25 05 30
Medical Gastroen 30 20 50
Required Final Dataset Output with Merged Rows
Department Male Visit Female Visit Total Count
---------- ---------- ------------ -----------
Endocrinology 10 20 30
Gastroenterology 45 45 90
General Medicine 25 05 30
I think you must use joining for this in your database query. that would be better.
Table A
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Table B.
+-----+---------------------+-------------+--------+
| OID | DATE | ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
SQL QUERY
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
INNER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Resulted Table:
+----+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+---------------------+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
+----+----------+--------+---------------------+
I hope it will help you.
You can Do something like this.
DataTable _dataTable = new DataTable();
DataRow _dataRow1 = null;
_dataTable.TableName = "Products";
_dataTable.Columns.Add("ID",typeof(int));
_dataTable.Columns[0].AutoIncrementSeed = 1;
_dataTable.Columns[0].AutoIncrement = true;
_dataTable.Columns.Add("ProductsName");
_dataTable.Columns.Add("Price");
_dataRow1 = _dataTable.NewRow();
_dataRow1["ProductsName"] = "Sony Laptop";
_dataRow1["Price"] = "15000";
_dataTable.Rows.Add(_dataRow1);
DataRow _dataRow2 = null;
_dataRow2 = _dataTable.NewRow();
_dataTable.Rows.Add(_dataRow2);
_dataRow2["ProductsName"] = "LG Laptop";
_dataRow2["Price"] = "15000";
DataSet _dataSet = new DataSet();
_dataSet.Tables.Add(_dataTable);

Show data from two model with same column name same value in ASP.NET

I'm new to ASP.NET, I have only experiences in C# Windows Form and SQL Server. Now I have starting my new project in ASP.NET MVC and already have database from my customer. It has two model included some same column name like this:
Table 1
---------------------------------
| Location | Item | Model | Tag |
---------------------------------
| 1 | 10 | A5 | 221 |
| 2 | 10 | A6 | 233 |
| 3 | 12 | A8 | 332 |
| 4 | 15 | C1 | 223 |
Table 2
-------------------------------------------------
| Location | Item | Model | Tag | DWeek | DYear |
-------------------------------------------------
| 1 | 10 | A5 | 221 | 01 | 15 |
| 2 | 10 | A6 | 233 | 01 | 15 |
| 3 | 12 | A8 | 332 | 02 | 15 |
| 4 | 15 | C1 | 223 | 03 | 15 |
I just want to show the data which have same Location and Item in one table, I don't know how to query in Entity framework, I have only know how to code by SQL, and I think it seem to be like this:
select
r.Location
,r.Item
,d.Location
,d.Item
,d.DWeek
,d.DYear
,r.Model
,r.[Tag No]
,d.[Tag No]
from Register r, Due d
where r.Location = d.Location
and r.Item = d.Item
My question is how to query like this in Entity framework for show data in a view, and is it possible to update and create two model in the same time?
I am using Lambda Expression
var Query=Register.Join(Due,r=>r.Location,d=>d.Location,(r,d)=>new {r,d}).Where(
X=>X.r.Location = X.d.Location && X.r.Item = X.d.Item).Select(X=>new {
LOCATION_1 =X.r.Location;
ITEM_1=X.r.Item;
LOCATION_2=X.d.Location;
ITEM_2=X.d.Item;
DWEEK=X.d.DWeek;
DTEAR=X.d.DYear;
MODEL=X.r.Model;
TAGNO_1=X.r.Tag_No;
TAGNO_1=X.d.Tag_No;
}).ToList();

Solution for Dividing WorkSheet into Multiple Files with vba/excel/c#

I would like to divide a worksheet into multiple files.
I have a worksheet with about 10,000 rows. there is fancy formatting, conditional formatting, nice colors, and I want to preserve all of these attributes.
I need to divide this worksheet up.
the input would be:
+-------+----+----+----+----+
| Alex | 45 | 6 | 23 | 56 |
| Alex | 61 | 47 | 56 | 56 |
| Liza | 49 | 70 | 34 | 37 |
| Alex | 33 | 30 | 22 | 39 |
| Tommy | | 66 | 62 | 29 |
| Liza | | 38 | 49 | 80 |
| Alex | 23 | 56 | 56 | 39 |
| Liza | 32 | 46 | 40 | 43 |
| Liza | | 90 | 24 | 38 |
| Tommy | 38 | 10 | 52 | 23 |
| Nancy | 35 | 36 | 23 | 25 |
+-------+----+----+----+----+
and the output would be separate files like this (please keep in mind i want to preserve all the fancy formatting, and thus the solution has work directly with excel, and not with just CSV (because csv cannot retain formatting))
end products:
+------+----+----+----+----+
| | | | | |
| Alex | 45 | 6 | 23 | 56 |
| Alex | 61 | 47 | 56 | 56 |
| Alex | 33 | 30 | 22 | 39 |
| Alex | 23 | 56 | 56 | 39 |
+------+----+----+----+----+
and
+------+----+----+----+----+
| | | | | |
| Liza | 49 | 70 | 34 | 37 |
| Liza | | 38 | 49 | 80 |
| Liza | 32 | 46 | 40 | 43 |
| Liza | | 90 | 24 | 38 |
+------+----+----+----+----+
and
+-------+----+----+----+----+
| | | | | |
| Nancy | 35 | 36 | 23 | 25 |
+-------+----+----+----+----+
and
+-------+----+----+----+----+
| | | | | |
| Tommy | | 66 | 62 | 29 |
| Tommy | 38 | 10 | 52 | 23 |
+-------+----+----+----+----+
the solution can be a combination of VBA/.NET. please note that i need multiple files as outputs.
what is the quickest way to get this working? thanks so much for any input!
please note that this is excel 2007 and later
I done this before.
You can use this code:
Option Explicit
Sub getInformations()
Dim varName As String
Application.ScreenUpdating = False
'Replace Tabelle1 with the name of your sheet where the Informations are
Worksheets("Tabelle1").Select
Worksheets("Tabelle1").Copy After:=Sheets("Tabelle1")
Sheets("Tabelle1 (2)").Select
Sheets("Tabelle1 (2)").Name = "Temp"
Do Until Range("A1").Value = vbNullString
varName = Range("A1").Value
Workbooks.Add
'Change the Path where you want to save the File
ActiveWorkbook.SaveAs ("C:\Documents and Settings\vgellhom\Desktop\" & varName & ".xls")
'Change The Name of the Excel Workbopk to the Name of the Workbook with the Names
Workbooks("Data.xls").Activate
Sheets("Temp").Select
varName = Range("A1").Value
Do While True
Cells.Find(What:=varName).Activate
Range(ActiveCell.Row & ":" & ActiveCell.Row).Select
Selection.Copy
Workbooks(varName & ".xls").Activate
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Activate
'Change The Name of the Excel Workbopk to the Name of the Workbook with the Names
Workbooks("Data.xls").Activate
Sheets("Temp").Select
Selection.Delete Shift:=xlUp
If Not Cells.FindNext(After:=ActiveCell) Is Nothing Then
Cells.Find(What:=varName).Activate
Else
Exit Do
End If
Loop
Workbooks(varName & ".xls").Activate
'Change the Path where you want to save the File
Application.DisplayAlerts = False
ActiveWorkbook.Save
Application.DisplayAlerts = True
Workbooks(varName & ".xls").Close
Loop
Application.DisplayAlerts = False
Sheets("Temp").Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Hope that helps you...
Since Excel formatting is usually a big pain in the a**, I would recommend to try a following solution:
Calculate and store all the unique keys.
Create a copy of a file for each key (like file_Alex.xls[x], file_Liza.xls[x] and so on).
Process each file, deleting all non-key rows, thus all key entries are left. Also because you are only deleting entire rows all the formatting in file is retained.
This is very unoptimized, but also extremely simple solution. If it's a one-time job, it should do just fine.

Categories

Resources