i need to get multiple query values to a one data set - c#

i want to create a report from an audit table this is the audit table, i need to get the count of address, tele, and AssName(AssociationName) of each and every person specificly
select count(tele),count(AssName),count(Address) from Audit where name='ben'
select count(tele),count(AssName),count(Address) from Audit where name='nik'
select count(tele),count(AssName),count(Address) from Audit where name='josh'
this is the query i used but i need these individual tables into one table and the count should be calculated if only "1" is in these cells. but my table has "0"s but it consider them as values and counts "0" cells toothis is the tabel now

SELECT
name,
sum(case when tele is not null then 1 else 0 end) tele,
sum(case when Assname is not null then 1 else 0 end) Assname,
sum(case when Address is not null then 1 else 0 end) Address
FROM Audit
GROUP BY name

try a group by instead
SELECT
name,
count(tele),
count(AssName),
count(Address)
FROM Audit
GROUP BY name

Select name , count (Assname), count (Address), count(tele)
from Audit
GROUP by Name
This would return your desired result.
Edit: I misunderstood your question earlier. You need to add group by the name if you want to count the number of each name a person is in the table.

just use GROUP BY
select name, count(tele),count(AssName),count(Address) from Audit group by name

Related

TSQL Transpose a column of a table into a row

I have a temporary table with one column containing 3 pieces of data: Code, Date, and Quantity.
Example:
-------
FR123456
24/02/1988
500
I need to extract the data in this column into separate columns.
Example:
Code | Date | Quantity
--------- ----------- ----
FR123456 | 24/02/1988 | 500
I used this code:
SELECT [1], [2], [3]
FROM
(
SELECT row_number() OVER (ORDER BY splitdata DESC) AS Id, splitdata
FROM splitdata
) AS SourceTable
PIVOT
(
MIN (splitdata)
FOR id IN ([1], [2], [3])
) AS PivotTable;
The problem with it is that once the content of data changes, I may get the quantity content into the date column due to the aggregate function (MIN).
I assume this is SQL-Server related...
Your problem is: A SQL-Server table does not have any kind of implicit sort order. A simple SELECT * FROM SomeWhere can return in the sort order you've inserted your data, but can return completely different as well. The only chance to ensure a sort order is an ORDER BY at the outer-most query against a (set of) unique column(s).
You can create a sort order by kind of analysing your data:
This is a mockup-table with your test data:
DECLARE #mockup TABLE(YourColumn VARCHAR(100));
INSERT INTO #mockup VALUES
('FR123456')
,('24/02/1988')
,('500');
--The query will check the values if they can be casted to a number, to a date or not.
--This will be used to place a sort order to the values.
--I use 105 in CONVERT to enforce the dateformat dd-MM-yyyy
SELECT CASE WHEN TRY_CONVERT(DATE,YourColumn,105) IS NOT NULL THEN 2
ELSE CASE WHEN TRY_CAST(YourColumn AS INT) IS NOT NULL THEN 3 ELSE 1
END
END AS SortOrder
,YourColumn
FROM #mockup
ORDER BY SortOrder;
But if there are several triplets in your table, not just one as in your sample, I'm afraid you're lost...
Btw: Your own approach tries to do exactly the same:
SELECT row_number() OVER (order by splitdata desc)as Id
This will create kind of a sort order number, but it will be random (a quantity will appeare before or after the date depending on alphanumerical rules).
Hint
Add an IDENTITY column to your table. This will use an increasing number for any row the moment it is created. These values can be used to enforce the order as inserted (by using ORDER BY with this column).
UPDATE: Your query
SELECT [1], [2] , [3]
FROM
(SELECT CASE WHEN TRY_CONVERT(DATE,splitdata,105) IS NOT NULL THEN 2
ELSE CASE WHEN TRY_CAST(splitdata AS INT) IS NOT NULL THEN 3 ELSE 1
END
END AS Id , splitdata
from #mockup ) AS SourceTable
PIVOT
(
MIN (splitdata)
FOR id IN ([1], [2], [3])
) AS PivotTable;

Query database to display the total count of data based on current date and month from multiple tables

If I try to select 2 columns, i get an error message that says, Ambiguous column name 'signed_in'. Name of the tables are UserName and Meeting. Please help
select count(*)
FROM UserName, Meeting
where YEAR(signed_in ) = datepart(YEAR, getdate());
It means that the signed_in column is present in both UserName and Meeting tables. You need to prefix the column with a table name like this:
UserName.signed_in or Meeting.signed_in
Thank you all for your answers, I really appreciate.
I have sorted it out using the following code:
select
( select count() from UserName where YEAR(signed_in ) = datepart(YEAR, getdate()) )
+
( select count() from Meeting where YEAR(signed_in ) = datepart(YEAR, getdate()))
and the total count for the both tables display in one column, one row. totaling 7 which represents 5 from UserName table and 2 from Meeting table.

Calculating the number of occurrences of specific string values in a column

I have a sperate table for each person where I store information about its presence and absence.
I want to calculate the value of 'present' and 'absent' in a column 'attendance'
table name is "teacher name".
Database looked like this.
Time Attendance
'2:30:30' 'Present'
'1:20:30' 'Present'
'4:10:30' 'Absent'[view of database in xampp][1]
'3:30:30' 'Present'
'2:32:30' 'Absent'
I want to calculate the number of occurrence of 'present' and 'absent'
so I can generate the salary according to it.
Is it possible?
Or I am doing something wrong?
SELECT Attendence, COUNT(*) FROM your_table
GROUP BY Attendence
Using LINQ, along the lines of:
.GroupBy (k => k.Attendence, (k, m) => new { Attendence = k, Count = m.Count() }
Per comment: If you need results horizontally rather than vertically, technically the proper way is to use PIVOT, but a quick and easy way is:
SELECT
SUM(CASE Attendence WHEN 'Present' THEN 1 ELSE 0 END) AS Present,
SUM(CASE Attendence WHEN 'Absent' THEN 1 ELSE 0 END) AS Absent
FROM your_table

How to remove the duplicate rows by summing up the required columns in a datatable

Hi all I am having my data table which is from my database is as follows
Name Total
XYZ 20
XYZ 20
ABC 20
Now I would like to have my data table as follows
Name Total
XYZ 40
ABC 20
I tried this linq from here Find duplicate and merge record into single datatable c# which works fine but as I am having my values from database I don't know the type of the variable so can some one help me and give me the solution in non-linq way
If you have two tables and you want to combine them all then the below is what you are after
SELECT bothTables.Name, SUM(total) FROM
(
SELECT Name, SUM(total) as total FROM Table_1 GROUP BY Name
UNION ALL
SELECT Name, SUM(total) as total FROM Table_2 GROUP BY Name
) AS bothTables
GROUP BY bothTables.Name
ORDER BY bothTables.Name desc
or if you want to do it using your Data Table (dt in this example)
var summedValues = from table in dt.AsEnumerable()
group table by table.Field<string>("Name")
into groupedTable
select new
{
Name = groupedTable.Key,
Total = groupedTable.Sum(x => x.Field<int>("Total"))
};
SQL version of the solution would be:
select Name, sum(Total) group by Name

selecting last record from table

I am deveoping a c# app in which oledb connection is used.
My table is following
payment
Id RemFee
1 2000
2 2500
1 1500
3 8000
3 5000
2 500
3 0
I want to select and check only last record of each Id and compare Remfee to 0. If it is greater than 0 then print the record. i.e. My expected result is:
Id
1
2
In this check is made for Id 1 with RemFee 1500 (as it is last record with Id1). It is grater than 0 hence record is printed.
Guessing you are using MSQ SQL Server and no row to know wich is your last inserted row. Try this code:
SELECT Id, Min(RemFee) AS RemFee
FROM payment
WHERE RemFee > 0
GROUP BY ID
Here is an SQL Fiddle working code sample
EDIT
If you only want the ID field here is one option, together with the SQL Fiddle code
SELECT myTable.Id
FROM (SELECT Id as ID, Min(RemFee) AS Remfee
FROM payment
WHERE RemFee > 0
GROUP BY ID
) myTable
SELECT ID,MIN(REMFEE) FROM payment WHERE ID NOT IN (SELECT ID FROM payment WHERE REMFEE = 0) GROUP BY ID
Select
id, Min(RemFee)
from payment
where RemFee > 0
group by id

Categories