MSSQL Query omitting specific row value? - c#

I have the following query to retrieve the customer sales report based on customer ID. It works, but how can I omit the customer name being duplicated?
select Cu.CustomerName, Cu.City, pd.pname,
Cs.qty, Cs.totalAmount, Cs.payed, Cs.credit, Cs.CreditEndDate
from Customer Cu
inner join CreditSales Cs on Cu.ID=Cs.CustomerID
left join Product pd on pd.pid=Cs.pid
where Cu.ID=6
order by Cu.CustomerName
The result is:
+--------+-------+---------------+----+----------+----------+----------+------------+
| Halima | Jimma | Mouse | 1 | 345.00 | 345.00 | 0.00 | 2015-08-29 |
| Halima | Jimma | Mobile | 10 | 92000.00 | 40000.00 | 52000.00 | 2015-08-23 |
| Halima | Jimma | Iphone | 2 | 13800.00 | 6500.00 | 7300.00 | 2015-08-28 |
| Halima | Jimma | Tape Recorder | 10 | 5175.00 | 4000.00 | 1175.00 | 2015-10-30 |
+--------+-------+---------------+----+----------+----------+----------+------------+
But I need like this:
+--------+-------+---------------+----+----------+----------+----------+------------+
| Halima | Jimma | Mouse | 1 | 345.00 | 345.00 | 0.00 | 2015-08-29 |
| | Jimma | Mobile | 10 | 92000.00 | 40000.00 | 52000.00 | 2015-08-23 |
| | Jimma | Iphone | 2 | 13800.00 | 6500.00 | 7300.00 | 2015-08-28 |
| | Jimma | Tape Recorder | 10 | 5175.00 | 4000.00 | 1175.00 | 2015-10-30 |
+--------+-------+---------------+----+----------+----------+----------+------------+
That means the customer name should displayed only one instance.
Any help? Thanks in Advance.

Based on Milen Pavlov response, you can try this:
SELECT CASE WHEN ROW_NUMBER() over (partition by [CustomerName] order by [CustomerName]) = 1 THEN [CustomerName] ELSE '' END
FROM Customer
ORDER BY [CustomerName]

This could do the job (I need you have to add an ordering in the over() clause):
select case when a.rn > 1 then '' else a.CustomerName end as CustomerName, a.City, a.pname, a.qty, a.totalAmount, a.payed, a.credit, a.CreditEndDate
from
(
select Cu.CustomerName, Cu.City, pd.pname,
Cs.qty, Cs.totalAmount, Cs.payed, Cs.credit, Cs.CreditEndDate, ROW_NUMBER() over (partition by Cu.CustomerName order by Cs.CreditEndDate) as rn
from Customer Cu
inner join CreditSales Cs on Cu.ID=Cs.CustomerID
left join Product pd on pd.pid=Cs.pid
where Cu.ID=6
) a
order by a.CustomerName

don't think it's a job for SQL but if you insists you can create two tables the first one with all the rows and columns and than delete the first row, and the other table with the first row and just make a left join and make the join on all the fields.

Related

Join multiple tables with Select and Where clause by Lamda expression

I have 3 table like below
Table 1: Brand
+-------+---------+-------+
|Code(k)| Name | Sale | *Code - set Primary Key
+-------+---------+-------+
| AP | Apple | False |
+-------+---------+-------+
| SS | SamSung | False |
+-------+---------+-------+
| N | Nokia | False |
+-------+---------+-------+
Table 2: Category
(Code in Table 1: Brand and Brand in table Table 2: Category is same)
+-------+-------------+-------+---------+
| ID(k) | Category | Brand | Manager | *ID - set Primary Key
+-------+-------------+-------+---------+
| 1 | BasicPhone | SS | M1 |
+-------+-------------+-------+---------+
| 2 | SmartPhone | AP | M1 |
+-------+-------------+-------+---------+
| 3 | CameraPhone | SS | M1 |
+-------+-------------+-------+---------+
| 4 | Tablet | SS | M1 |
+-------+-------------+-------+---------+
| 5 | Iphone | AP | M1 |
+-------+-------------+-------+---------+
| 6 | Ipad | AP | M1 |
+-------+-------------+-------+---------+
Table 3: Product
+-------------+-------------+-------------+
| Category | CategoryDes | Description | * no Primary Key
+-------------+-------------+-------------+
| BasicPhone | NoCamera | Contact |
+-------------+-------------+-------------+
| SmartPhone | Camera | TakePhoto |
+-------------+-------------+-------------+
| CameraPhone | GoodCamera | Selfie |
+-------------+-------------+-------------+
| Tablet | BigScreen | ReadNews |
+-------------+-------------+-------------+
| Iphone | Iphone7 | Something.. |
+-------------+-------------+-------------+
| Ipad | Ipad2 | Ipad... |
+-------------+-------------+-------------+
How can I Join Table 1: Brand and Table 3: Product into Table 2: Category by Lambda expression
The result I want like this
+-----+------------+-------+---------+---------+-------+-------------+-------------+
| ID | Category | Brand | Manager | Name | Sale | CategoryDes | Description |
+-----+------------+-------+---------+---------+-------+-------------+-------------+
| 1 | BasicPhone | SS | M1 | SamSung | False | NoCamera | Contact |
+-----+------------+-------+---------+---------+-------+-------------+-------------+
| 2 | SmartPhone | AP | M1 | Apple | False | Camera | TakePhoto |
+-----+------------+-------+---------+---------+-------+-------------+-------------+
| ........... |
+-----+------------+-------+---------+---------+-------+-------------+-------------+
| ... | null | null | N | Nokia | False | null | null | *null (allowed)
+-----+------------+-------+---------+---------+-------+-------------+-------------+
Update:
I tried to Join Table 1 to Table 2
var test = db.Brand.Join(db.Category , u => u.Code, y => y.Brand,(u, y) => new { u, y });
The comment from #Holger is right, you're on the right track, just need another join. Something like this:
test = db.Brand
.Join(db.Category, b => b.Code, c => c.Brand, (b, c) => new { b, c })
.Join(db.Product, x => x.c.Category, p => p.Category, (x, p) => new { x.b, x.c, p })
Disclaimer, I didn't test this.

How to delete some row in table1 and its change should reflect in table2 in C# using sql query?

Let us suppose I have two table namely Table1 and Table2.
Table1 contains:
+------+--------+
| Name | Value |
+------+--------+
| a | 5 |
| b | 10 |
| c | 5 |
| a | 20 |
| b | 15 |
+------+--------+
Table2 contains:
+------+--------+
| Name | Value |
+------+--------+
| a | 25 |
| b | 25 |
| c | 5 |
+------+--------+
My question is if i delete row 4 and row 5 in Table1 . The content of Table 2 should contain, somewhat like:
Table2:
+------+--------+
| Name | Value |
+------+--------+
| a | 5 |
| b | 10 |
| c | 5 |
+------+--------+
If you really want to store table2 as a table, then you would need a trigger. I would instead suggest that you use a view:
create view table1_summary as
select t1.name, sum(t1.value) as value
from table1 t1
group by t1.name;
Then the values are calculated when you access the view. Note: This will be slower if table1 is big.
Here is a trigger exemple which can do the work for you :
CREATE TRIGGER [dbo].[up_update_my_second_table]
ON [dbo].[Table1] FOR DELETE
AS
DECLARE #Name VARCHAR(50)
SELECT #Name = [Name] FROM deleted --deleted represents the deleted line.
UPDATE Table2 SET [Value] = 'YourValue' WHERE [Name] = #Name
You can use Triggers in SQL to delete from Second Table when you are trying to delete from First Table ..

Join Tables MySQL

I have multiple tables and made 2 sub-selects (UserRecord,CustomerRecord) that i would like to merge into 1 table
UserRecord
========================
| RecordID | UserName |
========================
| 1 | Sara |
| 1 | Tom |
| 2 | Sara |
| 2 | Kurt |
| 3 | Fre |
========================
Table: CustomerRecord
============================
| RecordID | CustomerName |
============================
| 1 | Jef |
| 2 | Alex |
| 2 | Peter |
============================
Table: This should be the result
=======================================
| RecordID | UserName | CustomerName |
=======================================
| 1 | Sara | - |
| 1 | Tom | - |
| 1 | - | Jef |
| 2 | Sara | - |
| 2 | Kurt | - |
| 2 | - | Alex |
| 2 | - | Peter |
| 3 | Fre | - |
=======================================
- = null
I tried with left, right, left outer, right outer ... join on the 2 tables but i don't get what i would like.
SELECT *
FROM UserRecord AS ur
INNER JOIN CustomerRecord AS cr ON ur.RecordID = cr.RecordID;
What you want is not a join, but a UNION:
SELECT RecordID, UserName, NULL AS CustomerName FROM UserRecord
UNION
SELECT RecordID, NULL AS UserName, CustomerName FROM CustomerRecord
... which just appends records from the two tables.
I'd just add that the order will not be the one you have shown in your expected result. If order is important then you should SELECT from this UNION and add an explicit ORDER BY clause on this outer select. Something like:
SELECT * FROM (
SELECT RecordID, UserName, NULL AS CustomerName FROM UserRecord
UNION
SELECT RecordID, NULL AS UserName, CustomerName FROM CustomerRecord
) ORDER BY RecordID, UserName, CustomerName
You can use a simple union
select recordid, username, null as customername from userrecord
union
select recordid, null, customername from customerrecord

MySQL how many columns table have [duplicate]

This question already has answers here:
Find the number of columns in a table
(21 answers)
Closed 9 years ago.
I have table:
SHOW COLUMNS FROM `darbuotojai`;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| vardas | char(20) | YES | | NULL | |
| pavarde | char(30) | YES | | NULL | |
| email | char(100) | YES | | NULL | |
| pareigos | char(50) | YES | | NULL | |
| ilg_tel_nr | decimal(8,0) | NO | PRI | 0 | |
| trump_tel_nr | decimal(3,0) | NO | PRI | 0 | |
| inv_nr | char(10) | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
How I can count columns in table with sql command?
And how I can identify them?
SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'darbuotojai'
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'database_name'
AND table_name = 'tbl_name'
SELECT count(*) FROM information_schema.`COLUMNS` C
WHERE table_name = 'your_table_name'
AND TABLE_SCHEMA = "your_db_name"
TABLE_SCHEMA is required only if table name exists in more than one db

Gridview and Select query

I am working with gridview and tables, I am using c# and asp.net. Assuming I have 2 tables, lets just name it Profiles and Info.
In table profiles let's say I have fields with p_Id, FirstName, LastName and in Transaction I have I_Id, childsID, fathersID, mothersID.
This is what my tables looks like:
Profile
| p_Id | FirstName | LastName |
| 1 | Jack | Cole |
| 2 | Cynthia | Cole |
| 3 | Robert | Cole |
Info
| I_Id | childsID | fathersID | mothersID |
| 1 | 1 | 3 | 2 |
Now in my gridview I have to display FirstName, LastName, Father and Mother. But I need to display their names not their ID's.
For now my gridview looks like this:
First Name | Last Name | Father| Mother |
Jack | Cole | 3 | 2 |
what I want is like this:
First Name | Last Name | Father | Mother |
Jack | Cole | Robert Cole | Cynthia Cole |
try this
select p.firstName, p.LastName
, (select sp.FirstName+' '+sp.Lastname FROM profile sp WHERE sp.p_id=i.fathersid) as father
, (select sp.FirstName+' '+sp.Lastname FROM profile sp WHERE sp.p_id=i.mothersid) as mother
from info i
inner join profile p ON (p.p_id=i.childsID)

Categories

Resources