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
Related
I have a Clients table already populated by thousands of records and now I need to search for a non-existing number in the card number column starting from the number x.
Example: I would like to search for the first available card number starting from number 2000.
Unfortunately I cannot select MAX() as there are records with 9999999 (which is the limit).
Is it possible to do this search through a single SELECT?
It's possible with a few nested SELECTs:
SELECT MIN(`card_number`) + 1 as next_available_number
FROM( SELECT (2000-1) as `card_number`
UNION
SELECT `card_number`
FROM clients
WHERE `card_number` >= 2000
) tmp
WHERE NOT EXISTS ( SELECT NULL
FROM clients
WHERE `card_number` = tmp.`card_number` + 1 )
It can be done with a self-join on your clients table, where you search for the lowest cardnumber for which the cardnumber + 1 does not exist.
In case x is 12, the query would be:
SELECT MIN(cardnumber) + 1
FROM clients
WHERE cardnumber + 1 NOT IN (SELECT cardnumber FROM clients)
AND cardnumber + 1 > 12
Eg. with a dataset of
INSERT INTO clients (cardnumber) VALUES
(1),(2),(3),(4),(5),(6),(7),(8),(9),(11),(12),(13),(14),(15),(17),(18)
this returns 16, but not 10.
Example on SQL Fiddle.
I think this is very similar to this question, but the minimum criteria is new.
If the credit card is represented as integer in your table and your starting number is 2000 you could do something like:
SELECT top 1 (card_id + 1)
FROM CreditCards t
WHERE card_id IN (
SELECT card_id
FROM CreditCards
WHERE card_id LIKE '%[2][0][0][0]%'
)
AND NOT EXISTS (SELECT 1 FROM CreditCards t2 WHERE t2.card_id = t.card_id + 1)
ORDER BY card_id
Example data (Table: CreditCards):
card_id
2000002
2000103
2000000
2000108
2000106
3000201
1000101
Result is: 2000001
Note that %[2][0][0][0]% is fixed here. You could also introduce a parameter.
It is not an optimal solution, but it does the trick.
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;
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
I want to get alternate series of records using SQL Server.
For example :
I want to skip first 10 records (1 to 10) in sequence and get other 10 records (11 to 20) after that I want to skip next 10 records (21 to 30) and get another next 10 records (31 to 40)
I have done for alternate rows as below...
SELECT ROW, EmployeeID
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY EmployeeID) AS ROW, *
FROM Employee) A
WHERE
ROW % 2 = 0
But in case of my requirement above logic will not work. Please help me to make above thing works..
Linq will also accepted
Thanks
SELECT ROW, EmployeeID
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY EmployeeID) AS ROW, *
FROM Employee) A
WHERE
((ROW - 1)/10) % 2 = 1
I have done above of your requirement like below (Example).
Generated 1200 number sequentially from 1 to 1200 like below
;WITH CTE
AS
(
SELECT 1 PERIOD_SID
UNION ALL
SELECT PERIOD_SID+1 FROM CTE WHERE PERIOD_SID<1200
)
SELECT * INTO #PERIOD1 FROM CTE
OPTION(MAXRECURSION 0)
After this i have find alternative numbers like you have mentioned in your query
SELECT PERIOD_SID FROM
(
SELECT CASE
WHEN PERIOD_SID%10 <> 0 THEN PERIOD_SID / 10
WHEN PERIOD_SID%10 = 0 THEN ( PERIOD_SID / 10 ) - 1
END RNO,
PERIOD_SID
FROM #PERIOD1 )A
WHERE RNO%2=0
so if you have sequential numbers in your query (If you dont have generate using rownumber) then apply above logic.
Your query need to convert like below.
SELECT EMPLOYEEID
FROM (SELECT Row_number()
OVER (
ORDER BY EMPLOYEEID)AS ROW,
*
FROM EMPLOYEE) A
WHERE ( CASE
WHEN RNO%10 <> 0 THEN RNO / 10
WHEN RNO%10 = 0 THEN ( RNO / 10 ) - 1
END )%2 = 0
may be you can try this
SELECT ROW, EmployeeID FROM(
SELECT ROW_NUMBER()OVER (ORDER BY EmployeeID)AS ROW,* FROM Employee)
A WHERE ((ROW - (ROW%10))/10) % 2 = 1
Don't if this works or not coz I dont have sql server to run. Please show the error/result after running this query.
Ask if any doubt.
i am working on a c# project in that i have made this query which is
SELECT COUNT(customer_column)
FROM table2
WHERE Datepart(mm, record_column) = #myMonthSelected
AND Datepart(yy, record_column) = #myYearSelected
it counts the total number of rows after sorting and now i want it to modify that a column which is having a "Bit" data type, i want to count the total number of true rows by sorting month and year same as the above query. can it be possible to narrow my search?
Try this
SELECT COUNT(customer_column)
FROM table2
WHERE Datepart(mm, record_column) = #myMonthSelected
AND Datepart(yy, record_column) = #myYearSelected
AND BitColumn = 1
I use the old "sum of bits" trick. Like this:
SELECT SUM(someBitColumn) AS TotalCount
FROM someTable
Since I'm at it, here's a variation for counting types of things:
SELECT
SUM(
CASE WHEN animalKind = 'Dog' THEN 1
ELSE 0
END CASE
) AS DogCount,
SUM(
CASE WHEN animalKind = 'Cat' THEN 1
ELSE 0
END CASE
) AS CatCount,
...
FROM animals